[TOC]
* * * * *
## 1 网络连接文件
~~~
Workerman/Connection/
ConnectionInterface.php ;网络连接接口
TcpConnection.php ;Tcp网络连接
UpdConnection.php ;Udp网络连接
AsyncTcpConnection.php ;异步Tcp网络连接
~~~
## 2 网络连接接口(ConnectionInterface.php)
~~~
;连接统计:连接总数,请求总数,异常总数,发送失败总数
public static $statistics = array(
'connection_count' => 0,
'total_request' => 0,
'throw_exception' => 0,
'send_fail' => 0,
);
;接受数据回调,连接关闭回调,发生错误回调
public $onMessage = null;
public $onClose = null;
public $onError = null;
~~~
~~~
;连接发送数据接口
abstract public function send($send_buffer);
;获取客户端Ip接口
abstract public function getRemoteIp();
;获取客户端端口接口
abstract public function getRemotePort();
;关闭连接接口
abstract public function close($data = null);
~~~
## 3 Tcp网络连接(TcpConnection.php)
>[info] 成员变量
~~~
;数据读取缓存区上限
const READ_BUFFER_SIZE = 65535;
;连接状态:建立连接,完成建立,正在关闭,关闭完成
const STATUS_CONNECTING = 1;
const STATUS_ESTABLISH = 2;
const STATUS_CLOSING = 4;
const STATUS_CLOSED = 8;
;连接回调接口:接受数据,连接关闭,发生错误,数据满,数据发送
public $onMessage = null;
public $onClose = null;
public $onError = null;
public $onBufferFull = null;
public $onBufferDrain = null;
;应用层协议,所属worker进程,连接id,所属worker的id
public $protocol = null;
public $worker = null;
public $id = 0;
protected $_id = 0;
;数据包发送缓存最大上限,数据包发送缓存默认上限,
public $maxSendBufferSize = 1048576;
public static $defaultMaxSendBufferSize = 1048576;
;最大可发送数据包
public static $maxPackageSize = 10485760;
protected static $_idRecorder = 1;
;socket套接符,发送缓存,接受缓存,当前数据包大小
protected $_socket = null;
protected $_sendBuffer = '';
protected $_recvBuffer = '';
protected $_currentPackageLength = 0;
;当前链接状态,客户端地址,是否阻塞
protected $_status = self::STATUS_ESTABLISH;
protected $_remoteAddress = '';
protected $_isPaused = false;
~~~
>[info] 成员方法
### __construct($socket, $remote_address = ''):Tcp连接构造函数
~~~
public function __construct($socket, $remote_address = '')
~~~
> $socket: 监听套接字
> $remote_address:客户端地址
* * * * *
### send($send_buffer, $raw = false):tcp数据发送接口
~~~
public function send($send_buffer, $raw = false)
~~~
> $send_buffer:发送内容
> $raw:??
* * * * *
### getRemoteIp():获取客户端ip
~~~
public function getRemoteIp()
~~~
### getRemotePort():获取客户端端口
~~~
public function getRemotePort()
~~~
### pauseRecv():阻塞读数据
~~~
public function pauseRecv()
~~~
### resumeRecv():恢复读数据
~~~
public function resumeRecv()
~~~
### baseRead($socket, $check_eof = true):tcp读数据
~~~
public function baseRead($socket, $check_eof = true)
~~~
> $socket:socket描述符
> $check_eof:??
* * * * *
### baseWrite():tcp写数据
~~~
public function baseWrite()
~~~
### pipe($dest):数据重定向
~~~
public function pipe($dest)
~~~
> $dest:重定向目的
* * * * *
### consumeRecvBuffer($length):截取数据接收区
~~~
public function consumeRecvBuffer($length)
~~~
> $length:截取长度
* * * * *
### close($data = null):tcp连接关闭接口
~~~
public function close($data = null)
~~~
> $data:关闭时发送数据
* * * * *
### destroy():tcp链接注销接口
~~~
public function destroy()
~~~
### getSocket():获取连接socket描述符
~~~
public function getSocket()
~~~
### checkBufferIsFull():检测发送缓冲区是否达到上限
~~~
protected function checkBufferIsFull()
~~~
## 4 Udp网络连接(UpdConnection.php)
>[info] 成员变量
~~~
;udp应用层协议
public $protocol = null;
;udp连接socket描述
protected $_socket = null;
;客户端ip
protected $_remoteIp = '';
;客户端端口
protected $_remotePort = 0;
;客户端地址
protected $_remoteAddress = '';
~~~
>[info] 成员方法
### __construct($socket, $remote_address):Udp构造函数
`public function __construct($socket, $remote_address)`
### send($send_buffer, $raw = false):Udp连接数据发送接口
`public function send($send_buffer, $raw = false)`
### getRemoteIp():获取客户端ip
`public function getRemoteIp()`
### getRemotePort():获取客户端端口
`public function getRemotePort()`
### close($data = null):连接关闭
`public function close($data = null)`
## 5 异步Tcp网络连接(AsyncTcpConnection.php)
>[info] 成员变量
~~~
;连接回调
public $onConnect = null;
;连接默认状态
protected $_status = self::STATUS_CONNECTING;
;客户端主机
protected $_remoteHost = '';
~~~
>[info] 成员方法
### __construct($remote_address):异步Tcp构造函数
public function __construct($remote_address)
### connect():建立 异步Tcp连接
public function connect()
### getRemoteHost():获取客户端主机
public function getRemoteHost()
### emitError($code, $msg):手动触发错误回调
protected function emitError($code, $msg)
### checkConnection($socket):检查连接是否建立
public function checkConnection($socket)
## 4 网络连接总结
网络连接实现服务器与客户端的对应关系。
~~~
ConnectionInterface.php 网络连接抽象
Tcp 连接网络连接
Udp 数据包网络连接
异步Tcp 异步连接
~~~