function server_loop() { while ($this->client_sock[0] = stream_socket_accept($this->server_sock, -1)) { stream_set_blocking($this->client_sock[0], 1); stream_set_timeout($this->client_sock[0], 0, $this->timeout_micro); if (feof($this->client_sock[0])) { $this->close(0); } //堵塞Server必须读完全部数据 $data = sw_fread_stream($this->client_sock[0], $this->buffer_size); $this->protocol->onRecive(0, $data); } }
function server_loop() { while (true) { $read_fds = $this->fds; if (stream_select($read_fds, $write = null, $exp = null, 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($client_socket_id); } } else { $data = sw_fread_stream($socket, $this->buffer_size); if ($data !== false) { $this->protocol->onRecive($socket_id, $data); } else { $this->close($socket_id); } } } } } }
/** * 接收到数据后进行处理 * @param $client_socket * @param $events * @param $arg * @return unknown_type */ function sw_server_handle_receive($client_socket, $events, $arg) { $server = $arg[0]; $client_id = $arg[1]; $data = sw_fread_stream($client_socket, $server->buffer_size); if ($data !== false) { $server->protocol->onRecive($client_id, $data); } else { $server->close($client_id); } }