$address = "::1";
$port = 8888;
$banner = <<<EOF
#    # ##### ##### #####         ##### ##### #####  #     # ##### #####
#    #   #     #   #    #       #      #     #    # #     # #     #    #
######   #     #   #    # ##### #####  ##### #####   #   #  ##### #####
#    #   #     #   #####             # #     #  #    #   #  #     #  #
#    #   #     #   #            #    # #     #   ##   # #   #     #   ##
#    #   #     #   #            #####  ##### #    #    #    ##### #    #

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]);
                });
            });
Exemple #2
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());
 }
function getRequestType($type)
{
    $key = strtolower($type);
    switch ($key) {
        case "set":
            return ProtocolCached_Request_RequestType::SET;
            break;
        case "get":
            return ProtocolCached_Request_RequestType::GET;
            break;
        default:
            throw new Exception("GET or SET only");
    }
}
$c = uv_tcp_init();
uv_tcp_connect6($c, uv_ip6_addr('::1', 9999), function ($client, $stat) {
    if ($stat == 0) {
        $request = new ProtocolCached_Request();
        $type = getRequestType($_SERVER['argv'][1]);
        $request->setType($type);
        if ($type == ProtocolCached_Request_RequestType::SET) {
            $set = new ProtocolCached_SetRequest();
            $set->setKey($_SERVER['argv'][2]);
            $set->setValue($_SERVER['argv'][3]);
            $request->setSet($set);
        } else {
            if ($type == ProtocolCached_Request_RequestType::GET) {
                $set = new ProtocolCached_GetRequest();
                $set->setKey($_SERVER['argv'][2]);
                $request->setGet($set);
            }
    public function set($key, $value)
    {
        $this->store[$key] = $value;
    }
    public function get($key)
    {
        $result = NULL;
        if (isset($this->store[$key])) {
            $result = $this->store[$key];
        }
        return $result;
    }
}
$store = new KeyValueStore();
$tcp = uv_tcp_init();
uv_tcp_bind6($tcp, uv_ip6_addr('::1', 9999));
echo "# Server listening to ::1 9999\n";
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);
 /**
  * Constructs the socket, based on its IP and its port
  *
  * @param string $address
  * @param int $port
  */
 public function __construct($address, $port)
 {
     $this->socket = \uv_ip6_addr($address, $port);
     $this->port = $port;
 }