Пример #1
0
 /**
  * 服务主体循环方法
  *
  * 接收请求及数据等相关处理
  * 触发对应的回调事件
  *
  * @return void
  */
 private function loop()
 {
     $arrRead = array($this->socket);
     $arrWrite = null;
     $arrExcept = null;
     foreach ($this->connections as $key => $session) {
         $arrRead[] = $session->socket;
     }
     $changes = socket_select($arrRead, $arrWrite, $arrExcept, $this->options['timeout']);
     if ($changes === false) {
         Exception::newThrow($this->socket);
     }
     if ($changes == 0) {
         return;
     }
     foreach ($arrRead as $socket) {
         if ($socket == $this->socket) {
             $this->doAccept();
             continue;
         }
         $this->doRead($socket);
     }
 }
Пример #2
0
 /**
  * 构造函数,初始化socket
  *
  * 不能直接new创建连接,只能通过factory()方法创建 
  *
  * @param  string $host    服务器ip地址
  * @param  number $port    服务器端口
  */
 private function __construct($host, $port)
 {
     $this->host = $host;
     $this->port = $port;
     $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
     if ($this->socket === false) {
         throw new Exception('Socket create failure');
     }
     if (socket_set_option($this->socket, SOL_SOCKET, SO_REUSEADDR, 1) === false) {
         Exception::newThrow($this->socket);
     }
     $this->running = true;
 }