public function assertConnection(array $options, $message = null) { $settings = array_merge(["ip" => "0.0.0.0", "port" => 0, "startServer" => false, "match" => true], $options); // optionally starting server if ($settings["startServer"]) { $serverLoop = EventLoopFactory::create(); $server = new SocketServer($serverLoop); $server->listen($settings["port"]); } // client setup $clientLoop = EventLoopFactory::create(); $dnsResolverFactory = new DnsResolverFactory(); $dns = $dnsResolverFactory->createCached("8.8.8.8", $clientLoop); // dunno why dns is required for this shit $connector = new SocketConnector($clientLoop, $dns); $promise = $connector->create($settings["ip"], $settings["port"])->then(function (SocketStream $stream) { $stream->close(); return true; }, function (SocketConnectionException $e) { return false; }); $clientLoop->run(); // catching the output $out = null; $promise->done(function ($v) use(&$out) { $out = $v; }); // optionally cleaning up the server if ($settings["startServer"]) { $server->shutdown(); } $this->assertEquals($out, $settings["match"], $message); }
/** * @param NetworkAddressInterface $remotePeer * @return \React\Promise\PromiseInterface */ public function rawConnect(NetworkAddressInterface $remotePeer) { return $this->socketConnector->create($remotePeer->getIp()->getHost(), $remotePeer->getPort())->then(function (Stream $stream) { $peer = new Peer($this->msgs, $this->eventLoop); $peer->setupStream($stream); return $peer; }); }
/** * Start transport provider * * @param \Thruway\Peer\ClientInterface $client * @param \React\EventLoop\LoopInterface $loop */ public function startTransportProvider(ClientInterface $client, LoopInterface $loop) { $this->client = $client; $this->loop = $loop; $dnsResolverFactory = new \React\Dns\Resolver\Factory(); $dns = $dnsResolverFactory->createCached('8.8.8.8', $loop); $connector = new Connector($loop, $dns); $connector->create($this->address, $this->port)->then(function (Stream $stream) { $stream->on('data', [$this, "handleData"]); $stream->on('close', [$this, "handleClose"]); $this->handleConnection($stream); }); }
/** @test */ public function connectionToIp6TcpServerShouldSucceed() { $capturedStream = null; $loop = new StreamSelectLoop(); $server = new Server($loop); $server->on('connection', $this->expectCallableOnce()); $server->on('connection', array($server, 'shutdown')); $server->listen(9999, '::1'); $dns = $this->createResolverMock(); $connector = new Connector($loop, $dns); $connector->create('::1', 9999)->then(function ($stream) use(&$capturedStream) { $capturedStream = $stream; $stream->end(); }); $loop->run(); $this->assertInstanceOf('React\\Stream\\Stream', $capturedStream); }
/** @test */ public function gettingStuffFromGoogleShouldWork() { $loop = new StreamSelectLoop(); $factory = new Factory(); $dns = $factory->create('8.8.8.8', $loop); $connector = new Connector($loop, $dns); $connected = false; $response = null; $connector->create('google.com', 80)->then(function ($conn) use(&$connected) { $connected = true; $conn->write("GET / HTTP/1.0\r\n\r\n"); return BufferedSink::createPromise($conn); })->then(function ($data) use(&$response) { $response = $data; }); $loop->run(); $this->assertTrue($connected); $this->assertRegExp('#^HTTP/1\\.0#', $response); }
/** * @param Connector $connector * @param NetworkAddressInterface $remoteAddr * @return \React\Promise\Promise|\React\Promise\PromiseInterface */ public function connect(Connector $connector, NetworkAddressInterface $remoteAddr) { $deferred = new Deferred(); $this->remoteAddr = $remoteAddr; $this->inbound = false; $connector->create($this->remoteAddr->getIp(), $this->remoteAddr->getPort())->then(function (Stream $stream) use($deferred) { $this->stream = $stream; $this->setupConnection(); $this->on('version', function () { $this->verack(); }); $this->on('verack', function () use($deferred) { if (false === $this->exchangedVersion) { $this->exchangedVersion = true; $this->emit('ready', [$this]); $deferred->resolve($this); } }); $this->version(); }, function ($error) use($deferred) { $deferred->reject($error); }); return $deferred->promise(); }
public function __construct(LoopInterface $loop, Resolver $resolver) { $this->loop = $loop; $this->resolver = $resolver; parent::__construct($loop, $resolver); }
public function __construct(LoopInterface $loop, Resolver $resolver, array $contextOptions = null) { parent::__construct($loop, $resolver); $contextOptions = null === $contextOptions ? array() : $contextOptions; $this->contextOptions = $contextOptions; }
private function createImapstream(&$imapaccount) { echo 'createImapstream'; //$loop = \React\EventLoop\Factory::create(); $dnsResolverFactory = new Factory(); $dns = $dnsResolverFactory->createCached('8.8.8.8', $this->loop); $imapconnector = new Connector($this->loop, $dns); /** @var Mailaccount $mailaccount */ $mailaccount = $imapaccount['mailaccount']; $imapport = $mailaccount->getImapport() ? $mailaccount->getImapport() : 993; //$imapport=143; if ($imapport == 993) { //secure $secureConnector = new SecureConnector($imapconnector, $this->loop); $conn = $secureConnector->create($mailaccount->getImapserver(), $imapport); } else { $conn = $imapconnector->create($mailaccount->getImapserver(), $imapport); } echo $mailaccount->getImapserver() . $imapport; $conn->then(function (\React\Stream\Stream $imapstream) use(&$imapaccount) { echo 'Jojo'; $uid = uniqid(); echo $imapaccount['mailaccount']->getImapserver() . ': ' . $uid; $imapaccount['imapstream'] = $imapstream; $login = $imapaccount['mailaccount']->getImapusername(); $password = $imapaccount['mailaccount']->getImappassword(); $imapstream->write($uid . " LOGIN {$login} {$password}\r\n"); $status = 'LOGIN'; $imapstream->on('data', function ($data) use($uid, &$status, &$imapstream, &$imapaccount) { echo $imapaccount['mailaccount']->getImapserver() . ': ' . $data; $dataexpl = explode("\r\n", $data); foreach ($dataexpl as $dexpl) { if (preg_match("/^" . $uid . " OK/", $dexpl)) { //login OK: if ($status == 'LOGIN') { $imapstream->write($uid . " SELECT " . $imapaccount['mailaccount']->getImappathprefix() . "\r\n"); $status = 'SELECT'; echo "SEND: SELECT {$status}\r\n"; } else { if ($status == 'SELECT') { $imapstream->write($uid . " IDLE\r\n"); $status = 'IDLE'; echo "SEND: IDLE {$status}\r\n"; } } } if ($status == 'IDLE') { if (preg_match("/^\\* (\\d+) RECENT/", $dexpl, $countrecent)) { //login OK: $countrecent = $countrecent[1]; echo 'RECENT:' . $countrecent; $this->notifychanges($imapaccount, ['mailaccount_id' => $imapaccount['mailaccount']->getId(), 'recent' => $countrecent]); } if (preg_match("/^\\* (\\d+) EXISTS/", $dexpl, $countexists)) { //login OK: $countexists = $countexists[1]; echo 'EXISTS' . $countexists; $this->notifychanges($imapaccount, ['mailaccount_id' => $imapaccount['mailaccount']->getId(), 'exists' => $countexists]); } } } }); $imapstream->on('end', function () use($uid, &$status, &$imapstream, &$imapaccount) { echo 'END!!!!!'; $this->createImapstream($imapaccount); }); }, function (\Exception $error) use(&$imapaccount) { echo "Call Error: \n"; dump($error->getMessage()); echo "trying again...\n"; $this->createImapstream($imapaccount); }); return $imapaccount; }
public function masterServer($bindMasterTo) { cli_set_process_title("mpcmf/console server:run/master -b {$bindMasterTo['host']} -p {$bindMasterTo['port']}"); $output = $this->output; //MPCMF_DEBUG && $output->writeln('<error>[MASTER]</error> Preparing server'); $loop = Factory::create(); $dnsResolverFactory = new reactResolver(); $dns = $dnsResolverFactory->createCached('8.8.8.8', $loop); $connector = new Connector($loop, $dns); $output->writeln('<error>[MASTER]</error> Binding callables and building socketServer'); $socketServer = new reactSocketServer($loop); $clientId = null; $socketServer->on('connection', function (Connection $clientConnection) use($connector, $output, $clientId, $loop) { $clientConnection->pause(); MPCMF_DEBUG && ($clientId = spl_object_hash($clientConnection)); do { $threadKey = array_rand($this->threads); if ($this->threads[$threadKey]->isAlive()) { break; } $loop->tick(); } while (true); $childPort = json_decode($threadKey, true)['port']; //MPCMF_DEBUG && $output->writeln("<error>[MASTER:{$clientId}]</error> Client connected, using port {$childPort}"); $clientConnection->on('end', function () use($clientConnection, $clientId, $output) { //MPCMF_DEBUG && $output->writeln("<error>[MASTER:{$clientId}]</error> Client connection ending"); }); $clientConnection->on('close', function () use($clientConnection, $clientId, $output) { //MPCMF_DEBUG && $output->writeln("<error>[MASTER:{$clientId}]</error> Client connection closed"); }); /** @var \React\Promise\FulfilledPromise|\React\Promise\Promise|\React\Promise\RejectedPromise $childConnection */ $childConnection = $connector->create($this->childHost, $childPort); $childConnection->then(function (reactStream $childStream) use($clientConnection, $childConnection, $output, $clientId) { $childStream->pause(); //MPCMF_DEBUG && $output->writeln('<error>=================== ' . spl_object_hash($childStream) . ' CHILD STREAM OPEN </error>'); $childStream->on('end', function () use($clientConnection, $childConnection, $childStream, $output, $clientId) { //MPCMF_DEBUG && $output->writeln("<error>[MASTER:{$clientId}]</error> Child closed connection"); //MPCMF_DEBUG && $output->writeln('<error>=================== ' . spl_object_hash($childStream) . ' CHILD STREAM CLOSE</error>'); $childStream->close(); $clientConnection->getBuffer()->on('full-drain', function () use($clientConnection, $output, $clientId) { //MPCMF_DEBUG && $output->writeln("<error>[MASTER:{$clientId}]</error> Buffer is empty, closing client connection"); $clientConnection->close(); }); }); $childStream->on('data', function ($data) use($clientConnection, $childConnection, $childStream, $output, $clientId) { //MPCMF_DEBUG && $output->writeln("<error>[MASTER:{$clientId}]</error> Response from child received"); //MPCMF_DEBUG && $output->writeln("<error>[MASTER:{$clientId}]</error> Sending response to client"); $clientConnection->write($data); }); $childStream->resume(); $clientConnection->on('data', function ($data) use($clientConnection, $childConnection, $output, $clientId, $childStream) { //MPCMF_DEBUG && $output->writeln("<error>[MASTER:{$clientId}]</error> Client data received"); //MPCMF_DEBUG && $output->writeln("<error>[MASTER:{$clientId}]</error> Sending request to child"); $childStream->write($data); }); $clientConnection->resume(); }); }); $output->writeln("<error>[MASTER]</error> Starting server on {$bindMasterTo['host']}:{$bindMasterTo['port']}"); $socketServer->listen($bindMasterTo['port'], $bindMasterTo['host']); $loop->addPeriodicTimer(1.0, [$this, 'checkThreads']); $loop->run(); }
public function __construct(LoopInterface $loop, Resolver $resolver = null) { parent::__construct($loop, $resolver); }