public function listen($options = array(), $listeningListener = '') { $default_options = array('port' => 1337, 'host' => '127.0.0.1', 'backlog' => 100); $options = array_merge($default_options, $options); $this->setPort($options['port']); $this->setHost($options['host']); $this->setBacklog($options['backlog']); $this->setListeningListener($listeningListener); $this->server = uv_tcp_init(); //very tricky, must use $this->server, using local variable will lead to segmentation fault uv_tcp_bind($this->server, uv_ip4_addr($options['host'], $options['port'])); $self = $this; uv_listen($this->server, $options['backlog'], function ($server) use($self) { $client = uv_tcp_init(); uv_accept($server, $client); $socket = new \Phastlight\Module\NET\Socket(); uv_read_start($client, function ($uvSocket, $nread, $buffer) use($self, $socket) { if ($nread > 0) { $socket->setSocket($uvSocket); call_user_func_array($self->getConnectionListener(), array($socket)); $socket->emit('data', $buffer); } }); }); }
public function listen($port, $host = '127.0.0.1', $backlog = 100) { $this->port = $port; $this->host = $host; $self = $this; $system = $this->getSystem(); $this->server = uv_tcp_init(); $request = $self->getServerRequest(); $response = $self->getServerResponse(); $requestListener = $self->getRequestListener(); uv_tcp_bind($this->server, uv_ip4_addr($host, $port)); uv_listen($this->server, $backlog, function ($server) use($self, &$request, &$response, &$requestListener) { $client = uv_tcp_init(); uv_accept($server, $client); uv_read_start($client, function ($socket, $nread, $buffer) use($self, &$request, &$response, &$requestListener) { $self->generateServerVariables($buffer, $socket); call_user_func_array($requestListener, array($request, $response)); $status_code = $response->getStatusCode(); $statusMessage = $response->getReasonPhrase(); $headers = $response->getHeaders(); $header = ""; if (count($headers) > 0) { foreach ($headers as $key => $val) { $header .= $key . ": " . $val . "\r\n"; } } $output = $response->getData(); $buffer = "HTTP/1.1 {$status_code} {$statusMessage}\r\n{$header}\r\n{$output}"; uv_write($socket, $buffer); uv_close($socket); }); }); }
public function onConnect($server, $status) { $client = uv_tcp_init(); uv_tcp_nodelay($client, 1); uv_accept($server, $client); $this->clients[(int) $client] = $client; $this->parsers[(int) $client] = uv_http_parser_init(); uv_read_start($client, array($this, "onRead")); }
/** * Accepts a client connection from the server instance. * * @param ServerInterface $server * @return ClientInterface */ public function accept(ServerInterface $server) { $this->server = $server; \uv_accept($server->getBackend(), $this->connection); $this->running = true; return $this; }
<?php $tcp = uv_tcp_init(); uv_tcp_bind($tcp, uv_ip4_addr('0.0.0.0', 9999)); uv_listen($tcp, 100, function ($server) { $client = uv_tcp_init(); uv_accept($server, $client); var_dump(uv_tcp_getsockname($server)); uv_read_start($client, function ($socket, $nread, $buffer) use($server) { var_dump($buffer); uv_close($socket); uv_close($server); }); }); $c = uv_tcp_init(); uv_tcp_connect($c, uv_ip4_addr('0.0.0.0', 9999), function ($client, $stat) { if ($stat == 0) { uv_write($client, "Hello", function ($socket, $stat) { uv_close($socket); }); } }); uv_run();
###### # # # # ##### ##### ##### ##### # # ##### ##### # # # # ##### # # # # # # # # # # # # # # # # # # ## # # # # ## # # # # # ##### ##### # # # ##### # # http server started on port {$port} EOF; echo $banner; $server = uv_tcp_init(); uv_tcp_bind6($server, uv_ip6_addr($address, $port)); $clients = array(); $parsers = array(); uv_listen($server, 511, function ($server_stream) use(&$parsers, &$clients) { $client = uv_tcp_init(); uv_accept($server_stream, $client); $clients[(int) $client] = $client; $parsers[(int) $client] = uv_http_parser_init(); uv_read_start($client, function ($client, $nread, $buffer) use(&$parsers, &$clients) { if ($nread < 0) { uv_shutdown($client, function ($client) use(&$parsers, &$clients) { uv_close($client, function ($client) use(&$parsers, &$clients) { unset($parsers[(int) $client]); unset($clients[(int) $client]); }); }); return; } else { if ($nread == 0) { if (uv_last_error() == UV::EOF) { uv_shutdown($client, function ($client) use(&$parsers, &$clients) {
<?php define("PIPE_PATH", dirname(__FILE__) . "/pipe_test.sock"); @unlink(PIPE_PATH); $a = uv_pipe_init(uv_default_loop(), 0); $ret = uv_pipe_bind($a, PIPE_PATH); uv_listen($a, 8192, function ($stream) { $pipe = uv_pipe_init(uv_default_loop(), 0); uv_accept($stream, $pipe); uv_read_start($pipe, function ($pipe, $nread, $buffer) use($stream) { echo $buffer; uv_read_stop($pipe); uv_close($stream, function () { @unlink(PIPE_PATH); }); }); }); $b = uv_pipe_init(uv_default_loop(), 0); uv_pipe_connect($b, PIPE_PATH, function ($a, $b) { uv_write($b, "Hello", function ($stream, $stat) { uv_close($stream); }); }); uv_run(); exit;