Just for fun покажу минимальный тест сокета.class Test extends \ATL\Task
{
public function main()
{
Log::msg("Main task started");
yield true;
try {
Log::msg("Creating socket");
$this->taskAddChildTask($socket = new \ATL\Socket\TCP('192.168.77.10', 80, 15));
yield true;
Log::msg("Waiting for socket to connect");
if (($result = yield ($wait = new \ATL\Socket\WaitForConnect($socket))) !== true) {
if (is_array($result)) throw new \Exception("Socket error {$result[0]} while connecting: {$result[1]}");
throw new \Exception("Socket timed out while connecting");
}
Log::msg("Socket connected");
Log::msg("Local name: ".$socket->socketLocalName);
Log::msg("Remote name: ".$socket->socketRemoteName);
Log::msg("Writing HTTP request to the socket");
$socket->writeBulk([
"GET /test.php HTTP/1.1\r\n",
"Host: alex-at.net\r\n",
"Connection: close\r\n",
"\r\n",
]);
Log::msg("Waiting for write data to be flushed down");
if (($result = yield ($wait = new \ATL\Socket\WaitForWriteFlush($socket, 5))) !== true) {
if (is_array($result)) throw new \Exception("Socket error {$result[0]} while writing: {$result[1]}");
throw new \Exception("Socket timed out while waiting for write");
}
Log::msg("Reading from socket");
$wait = new \ATL\Socket\Read($socket, 5);
$fout = fopen(__DIR__.'/socket_test.out', 'wb');
do {
if (($result = yield $wait) !== true) {
if (is_array($result)) throw new \Exception("Socket error {$result[0]} while reading: {$result[1]}");
throw new \Exception("Socket timed out while reading data");
}
if ($wait->readData !== null)
fwrite($fout, implode('', $wait->readData));
} while ($wait->readData !== null);
fclose($fout);
Log::msg("Waiting for socket to disconnect");
if (($result = yield ($wait = new \ATL\Socket\Disconnect($socket, 5, true))) !== true) {
if (is_array($result)) throw new \Exception("Socket error {$result[0]} while disconnecting: {$result[1]}");
throw new \Exception("Socket timed out while waiting for disconnect");
}
} catch (\Exception $e) {
Log::msg("ERROR: ".$e->getMessage());
}
if (!$socket->isDisconnected()) {
Log::msg("Aborting socket");
$socket->abort();
yield true;
}
Log::msg("Main task ended");
}
}