phpsocks5/Workerman/Connection/UdpConnection.php

116 lines
2.6 KiB
PHP
Raw Normal View History

2015-04-04 21:46:31 +08:00
<?php
2016-09-20 21:27:41 +08:00
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
2015-04-04 21:46:31 +08:00
namespace Workerman\Connection;
/**
2016-09-20 21:27:41 +08:00
* UdpConnection.
2015-04-04 21:46:31 +08:00
*/
class UdpConnection extends ConnectionInterface
{
/**
2016-09-20 21:27:41 +08:00
* Application layer protocol.
* The format is like this Workerman\\Protocols\\Http.
*
* @var \Workerman\Protocols\ProtocolInterface
2015-04-04 21:46:31 +08:00
*/
2016-09-20 21:27:41 +08:00
public $protocol = null;
2015-04-04 21:46:31 +08:00
/**
2016-09-20 21:27:41 +08:00
* Udp socket.
*
2015-04-04 21:46:31 +08:00
* @var resource
*/
protected $_socket = null;
2016-09-20 21:27:41 +08:00
2015-04-04 21:46:31 +08:00
/**
2016-09-20 21:27:41 +08:00
* Remote address.
*
2015-04-04 21:46:31 +08:00
* @var string
*/
protected $_remoteAddress = '';
/**
2016-09-20 21:27:41 +08:00
* Construct.
*
2015-04-04 21:46:31 +08:00
* @param resource $socket
2016-09-20 21:27:41 +08:00
* @param string $remote_address
2015-04-04 21:46:31 +08:00
*/
public function __construct($socket, $remote_address)
{
2016-09-20 21:27:41 +08:00
$this->_socket = $socket;
2015-04-04 21:46:31 +08:00
$this->_remoteAddress = $remote_address;
}
2016-09-20 21:27:41 +08:00
2015-04-04 21:46:31 +08:00
/**
2016-09-20 21:27:41 +08:00
* Sends data on the connection.
*
2015-04-04 21:46:31 +08:00
* @param string $send_buffer
2016-09-20 21:27:41 +08:00
* @param bool $raw
2015-04-04 21:46:31 +08:00
* @return void|boolean
*/
2016-09-20 21:27:41 +08:00
public function send($send_buffer, $raw = false)
2015-04-04 21:46:31 +08:00
{
2016-09-20 21:27:41 +08:00
if (false === $raw && $this->protocol) {
$parser = $this->protocol;
$send_buffer = $parser::encode($send_buffer, $this);
if ($send_buffer === '') {
return null;
}
}
2015-04-04 21:46:31 +08:00
return strlen($send_buffer) === stream_socket_sendto($this->_socket, $send_buffer, 0, $this->_remoteAddress);
}
2016-09-20 21:27:41 +08:00
2015-04-04 21:46:31 +08:00
/**
2016-09-20 21:27:41 +08:00
* Get remote IP.
*
2015-04-04 21:46:31 +08:00
* @return string
*/
public function getRemoteIp()
{
2016-09-20 21:27:41 +08:00
$pos = strrpos($this->_remoteAddress, ':');
if ($pos) {
return trim(substr($this->_remoteAddress, 0, $pos), '[]');
2015-04-04 21:46:31 +08:00
}
2016-09-20 21:27:41 +08:00
return '';
2015-04-04 21:46:31 +08:00
}
2016-09-20 21:27:41 +08:00
2015-04-04 21:46:31 +08:00
/**
2016-09-20 21:27:41 +08:00
* Get remote port.
*
* @return int
2015-04-04 21:46:31 +08:00
*/
public function getRemotePort()
{
2016-09-20 21:27:41 +08:00
if ($this->_remoteAddress) {
return (int)substr(strrchr($this->_remoteAddress, ':'), 1);
2015-04-04 21:46:31 +08:00
}
2016-09-20 21:27:41 +08:00
return 0;
2015-04-04 21:46:31 +08:00
}
/**
2016-09-20 21:27:41 +08:00
* Close connection.
*
* @param mixed $data
* @param bool $raw
* @return bool
2015-04-04 21:46:31 +08:00
*/
2016-09-20 21:27:41 +08:00
public function close($data = null, $raw = false)
2015-04-04 21:46:31 +08:00
{
2016-09-20 21:27:41 +08:00
if ($data !== null) {
$this->send($data, $raw);
2015-04-04 21:46:31 +08:00
}
return true;
}
}