Пример #1
0
 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);
             }
         });
     });
 }
Пример #2
0
 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);
         });
     });
 }
Пример #3
0
 public function listen($port)
 {
     printf("# server listened at {$port}\n");
     uv_tcp_nodelay($this->server, 1);
     uv_tcp_bind($this->server, uv_ip4_addr("0.0.0.0", $port));
     uv_listen($this->server, 511, array($this, "onConnect"));
     uv_run(uv_default_loop());
 }
Пример #4
0
<?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();
Пример #5
0
 /**
  * @param int $timeout
  * @param callable $callback
  * @return $this|ServerInterface
  */
 public function listen($timeout, callable $callback)
 {
     $this->on(['connection'], $callback);
     if ($this->registeredListener !== true) {
         $server = $this;
         \uv_listen($this->connection, $timeout, function () use($server) {
             $client = new Client($this->getServiceManager(), $this->getLoop());
             $client->accept($server);
             $server->emit(new Event('connection'), [$client, $server]);
             $client->poll();
         });
     }
     return $this;
 }
Пример #6
0
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) {
                        uv_close($client, function ($client) use(&$parsers, &$clients) {
                            unset($parsers[(int) $client]);
                            unset($clients[(int) $client]);
                        });
                    });
                    return;
                }
            } else {
                $result = array();
                if (uv_http_parser_execute($parsers[(int) $client], $buffer, $result)) {
                    $response = "HTTP/1.1 200 OK\r\n\r\nHello World";
                    uv_write($client, $response, function ($client) use(&$parsers, &$clients) {
                        uv_close($client, function ($client) use(&$parsers, &$clients) {
                            unset($parsers[(int) $client]);
                            unset($clients[(int) $client]);
                        });
                    });
                }
            }
        }
    });
});
Пример #7
0
 public function listen($port)
 {
     uv_tcp_nodelay($this->server, 1);
     uv_tcp_bind6($this->server, uv_ip6_addr("::1", $port));
     uv_listen($this->server, 511, array($this, "onConnect"));
     uv_run(uv_default_loop());
 }
Пример #8
0
uv_listen($tcp, 100, function ($server) use($store) {
    $client = uv_tcp_init();
    uv_accept($server, $client);
    echo "# Accepted\n";
    uv_read_start($client, function ($socket, $nread, $buffer) use($server, $store) {
        try {
            $result = null;
            $request = ProtocolCached_Request::parseFromString($buffer);
            switch ($request->getType()) {
                case ProtocolCached_Request_RequestType::SET:
                    echo sprintf("# set operation: %d, %d\n", $request->getSet()->getKey(), strlen($request->getSet()->getValue()));
                    $store->set($request->getSet()->getKey(), $request->getSet()->getValue());
                    var_dump($store);
                    var_dump($request);
                    break;
                case ProtocolCached_Request_RequestType::GET:
                    $result = $store->get($request->getGet()->getKey());
                    break;
            }
        } catch (ProtocolBuffers\InvalidProtocoBufferException $e) {
            echo $e->getMessage();
        }
        if ($result) {
            $response = new ProtocolCached_Response();
            $response->setType(ProtocolCached_Response_ResponseType::GET);
            $payload = new ProtocolCached_GetResponse();
            $payload->setValue($result);
            $response->setGet($payload);
            uv_write($socket, $response->serializeToString(), function ($socket, $stat) {
                echo "# Connection closed\n";
                uv_close($socket);
            });
        } else {
            echo "# Connection closed\n";
            uv_close($socket);
        }
    });
});
Пример #9
0
<?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;
Пример #10
0
<?php

$tcp = uv_tcp_init();
uv_tcp_bind($tcp, uv_ip4_addr('0.0.0.0', 79));
$users = array(array("username" => "chobie", "name" => "Shuhei Tanuma", "twitter" => "chobi_e"));
function pad($str)
{
    return str_pad($str, 20, ' ', STR_PAD_RIGHT);
}
uv_listen($tcp, 100, function ($server) use($users) {
    $client = uv_tcp_init();
    uv_accept($server, $client);
    uv_read_start($client, function ($socket, $nread, $buffer) use($users) {
        $buffer = str_replace("/W", "", $buffer);
        if ($buffer == "\r\n") {
            $data = "";
            $keys = array("Login", "Name", "Twitter");
            $data .= join("", array_map("pad", $keys)) . "\r\n";
            foreach ($users as $user) {
                $data .= join("", array_map("pad", array_values($user))) . "\r\n";
            }
            uv_write($socket, $data, function ($client, $stat) {
                uv_close($client);
            });
        } else {
            var_dump($buffer);
            uv_close($socket);
        }
    });
});
uv_run();