Example #1
0
 /**
  * @param ConnectionInterface $conn
  */
 public function onConnection(ConnectionInterface $conn)
 {
     $labels = ['remote' => $conn->getRemoteAddress(), 'request_id' => Uuid::uuid4()->toString()];
     $request = new HttpdRequest($conn->getRemoteAddress(), $labels);
     $response = new HttpdResponse($conn, $labels);
     // Wire request and response event to global observer
     $request->subscribe($this->observable);
     $response->subscribe($this->observable);
     // Remote connection closed, notify everything's done
     $conn->on("end", [$request, 'notifyCompleted']);
     $conn->on("end", function () use($response, $labels) {
         $response->notifyNext(new Event("/httpd/connection/closed", $response, $labels));
         $response->notifyCompleted();
     });
     // No observers we can't do anything
     if (!$this->observers) {
         $response->sendError("No route defined", 404);
         return;
     }
     $parser = new RequestParser($request);
     $conn->on('data', array($parser, 'parse'));
     // Head is received we can dispatch route
     $request->take(1)->subscribeCallback(function () use($request, $response, $labels) {
         $this->dispatch($request, $response, $labels);
     });
 }
Example #2
0
 /**
  * Triggered when a new connection is received from React
  * @param \React\Socket\ConnectionInterface $conn
  */
 public function handleConnect($conn)
 {
     $conn->decor = new IoConnection($conn);
     $conn->decor->resourceId = (int) $conn->stream;
     $conn->decor->remoteAddress = $conn->getRemoteAddress();
     $this->app->onOpen($conn->decor);
     $conn->on('data', $this->handlers[0]);
     $conn->on('end', $this->handlers[1]);
     $conn->on('error', $this->handlers[2]);
 }
Example #3
0
 public function connect(ConnectionInterface $conn)
 {
     $conn->write(sprintf("Hello %s!\n", $conn->id));
     $conn->on('wait', function () use($conn) {
         $conn->write("Please wait until a partner connects.\n");
     });
     $conn->on('pipe', function ($source) use($conn) {
         $message = "You are now talking to %s.\n";
         $conn->write(sprintf($message, $source->id));
     });
     $this->app->connect($conn);
 }
Example #4
0
 public function connect(ConnectionInterface $conn)
 {
     $this->logger->info(sprintf("New connection %s", $conn->id));
     $conn->on('end', function () use($conn) {
         $this->logger->info(sprintf("Connection %s disconnected", $conn->id));
     });
     $conn->on('pipe', function ($source) use($conn) {
         if (!empty($conn->pipeLogged) || !empty($source->pipeLogged)) {
             return;
         }
         $this->logger->info(sprintf("Pairing up connection %s with waiting connection %s", $source->id, $conn->id));
         $conn->pipeLogged = $conn->pipeLogged = true;
     });
     $this->app->connect($conn);
 }
Example #5
0
 public function handleConnection(ConnectionInterface $connection)
 {
     $parser = $this->protocol->createResponseParser();
     $parser = new RequestParser();
     $that = $this;
     $business = $this->business;
     if ($this->config->get('requirepass') !== '') {
         $business = new AuthInvoker($business);
     }
     $client = new Client($connection, $business, reset($this->databases));
     $this->clients->attach($client);
     $connection->on('data', function ($data) use($parser, $that, $client) {
         try {
             $messages = $parser->pushIncoming($data);
         } catch (ParserException $e) {
             $that->emit('error', array($e, $client));
             return;
         }
         foreach ($messages as $message) {
             $that->handleRequest($message, $client);
         }
     });
     $connection->on('close', function () use($that, $client) {
         $that->handleDisconnection($client);
     });
     $this->emit('connection', array($client, $this));
 }
Example #6
0
 public function __construct(ConnectionInterface $src)
 {
     $this->src = $src;
     $src->on('data', function ($data) {
         $this->buffer .= $data;
         if ($this->dst) {
             $this->dst->write($this->buffer);
             $this->buffer = '';
         }
     });
     $src->on('end', function () {
         $this->end = true;
         if ($this->dst) {
             $this->dst->end();
         }
     });
 }
Example #7
0
 /**
  * @param ConnectionInterface $connection
  * @param MiddlewareQueueInterface $queue
  */
 public function __construct(ConnectionInterface $connection, MiddlewareQueueInterface $queue)
 {
     $this->connection = $connection;
     $this->queue = $queue;
     $this->connection->on('end', function () {
         $this->close();
     });
 }
 /**
  * On every new connection add event handler to receive commands from clients.
  *
  * @param ConnectionInterface $connection
  */
 public function onConnection(ConnectionInterface $connection)
 {
     $this->log('New connection from ' . $connection->getRemoteAddress());
     $buffer = '';
     $connection->on('data', function ($data) use(&$buffer, &$connection) {
         $buffer .= $data;
         if (strpos($buffer, Client::END_OF_COMMAND)) {
             $chunks = explode(Client::END_OF_COMMAND, $buffer);
             $count = count($chunks);
             $buffer = $chunks[$count - 1];
             for ($i = 0; $i < $count - 1; $i++) {
                 $command = json_decode($chunks[$i], true);
                 $this->runCommand($command, $connection);
             }
         }
     });
 }
Example #9
0
 public function connection(ConnectionInterface $connection)
 {
     $requestEmitter = new RequestProcessor($connection, $this->queue);
     $connection->on('data', [$requestEmitter, 'parseRequest']);
     $requestEmitter->on('request', [$requestEmitter, 'runRequest']);
 }