2017 © Pedro PelĂĄez
 

library coroutine-io

Fast socket server implementation using *Generator*

image

kzykhys/coroutine-io

Fast socket server implementation using *Generator*

  • Wednesday, November 27, 2013
  • by kzykhys
  • Repository
  • 2 Watchers
  • 5 Stars
  • 70 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 2 Versions
  • 1 % Grown

The README.md

CoroutineIO

Latest Stable Version Build Status, (*1)

Fast socket server implementation using Generator. This project is heavily inspired by @nikic's great post., (*2)

Following project shows the possibility of CoroutineIO., (*3)

  • [kzykhys/coupe][2] - CoupĂ© - A Handy HTTP/HTTPS Server written in PURE PHP

Requirements

  • PHP5.5+

Installation

Create or update your composer.json and run composer update, (*4)

``` json { "require": { "kzykhys/coroutine-io": "~0.1.0" } }, (*5)


Example (HTTP Server) --------------------- 1. Run `php example.php` 2. Open `http://localhost:8000` in browser and hold down the F5 key 3. Watch the console output

php example.php, (*6)


::1:50531 GET / HTTP/1.1 Host: localhost:8000 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 Accept-Language: ja,en-us;q=0.7,en;q=0.3 Accept-Encoding: gzip, deflate DNT: 1 Connection: keep-alive, (*7)


### example.php ``` php <?php use CoroutineIO\Example\HttpHandler; use CoroutineIO\Example\HttpServer; require __DIR__ . '/vendor/autoload.php'; $server = new HttpServer(new HttpHandler()); $server->run();

Class HttpServer

``` php <?php, (*8)

namespace CoroutineIO\Example;, (*9)

use CoroutineIO\Exception\Exception; use CoroutineIO\Server\Server;, (*10)

/** * Simple HTTP Server Implementation */ class HttpServer extends Server {, (*11)

/**
 * {@inheritdoc}
 */
public function createSocket($address = 'localhost:8000')
{
    $socket = @stream_socket_server('tcp://' . $address, $no, $str);

    if (!$socket) {
        throw new Exception("$str ($no)");
    }

    return $socket;
}

}, (*12)


### Class HttpHandler ``` php <?php namespace CoroutineIO\Example; use CoroutineIO\Server\HandlerInterface; use CoroutineIO\Socket\ProtectedStreamSocket; use CoroutineIO\Socket\StreamSocket; /** * Simple HTTP Server Implementation */ class HttpHandler implements HandlerInterface { /** * {@inheritdoc} */ public function handleClient(StreamSocket $socket) { $socket->block(false); $data = (yield $socket->read(8048)); $response = $this->handleRequest($data, new ProtectedStreamSocket($socket)); yield $socket->write($response); yield $socket->close(); } /** * {@inheritdoc} */ public function handleRequest($input, ProtectedStreamSocket $socket) { // Displays request information echo $socket->getRemoteName() . "\n"; echo $input; return "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 5\n\nHello"; } }

License

The MIT License, (*13)

Author

Kazuyuki Hayashi (@kzykhys), (*14)

The Versions

27/11 2013

dev-master

9999999-dev

Fast socket server implementation using *Generator*

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

by Kazuyuki Hayashi

server tcp socket multitasking udp coroutine

27/11 2013

v0.1.0

0.1.0.0

Fast socket server implementation using *Generator*

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

by Kazuyuki Hayashi

server tcp socket multitasking udp coroutine