function server_loop() { while (true) { $read_fds = $this->fds; $write = $exp = null; if (stream_select($read_fds, $write, $exp, null)) { foreach ($read_fds as $socket) { $socket_id = (int) $socket; if ($socket_id == $this->server_socket_id) { if ($client_socket_id = parent::accept()) { $this->fds[$client_socket_id] = $this->client_sock[$client_socket_id]; $this->protocol->onConnect($this, $client_socket_id, 0); } } else { $data = Stream::read($socket, $this->buffer_size); if (!empty($data)) { $this->protocol->onReceive($this, $socket_id, 0, $data); } else { $this->close($socket_id); } } } } } }
/** * 关闭服务器程序 * @return unknown_type */ function shutdown() { //关闭服务器端 \Swoole\Network\Stream::close($this->server_sock); //关闭事件循环 $this->protocol->onShutdown(); }
function sendData($sock, $data) { return Network\Stream::write($sock, $data); }
/** * 接收到数据后进行处理 * @param $client_socket * @param $events * @param $arg * @return unknown_type */ function event_receive($client_socket, $events, $client_id) { $data = Stream::read($client_socket, $this->buffer_size); if ($data !== false) { $this->protocol->onReceive($this, $client_id, 0, $data); } else { $this->close($client_id); } }