Skip to content
Permalink
d49dc0d793
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
105 lines (92 sloc) 2.95 KB
<?php
namespace Amp\Socket;
use Amp\CancellationToken;
use Amp\CancelledException;
use Amp\Loop;
use Amp\Promise;
const LOOP_CONNECTOR_IDENTIFIER = Connector::class;
/**
* @deprecated Use Server::listen() instead.
*
* Listen for client connections on the specified server address.
*
* If you want to accept TLS connections, you have to use `yield $socket->setupTls()` after accepting new clients.
*
* @see Server::listen()
*
* @param string $uri URI in scheme://host:port format. TCP is assumed if no scheme is present.
* @param BindContext|null $context Context options for listening.
*
* @return Server
*
* @throws SocketException If binding to the specified URI failed.
* @throws \Error If an invalid scheme is given.
*/
function listen(string $uri, ?BindContext $context = null): Server
{
return Server::listen($uri, $context);
}
/**
* Set or access the global socket Connector instance.
*
* @param Connector|null $connector
*
* @return Connector
*/
function connector(Connector $connector = null): Connector
{
if ($connector === null) {
if ($connector = Loop::getState(LOOP_CONNECTOR_IDENTIFIER)) {
return $connector;
}
$connector = new DnsConnector;
}
Loop::setState(LOOP_CONNECTOR_IDENTIFIER, $connector);
return $connector;
}
/**
* Asynchronously establish a socket connection to the specified URI.
*
* @param string $uri URI in scheme://host:port format. TCP is assumed if no scheme is present.
* @param ConnectContext $context Socket connect context to use when connecting.
* @param CancellationToken|null $token
*
* @return Promise<EncryptableSocket>
*
* @throws ConnectException
* @throws CancelledException
*/
function connect(string $uri, ConnectContext $context = null, CancellationToken $token = null): Promise
{
return connector()->connect($uri, $context, $token);
}
/**
* Returns a pair of connected stream socket resources.
*
* @return ResourceSocket[] Pair of socket resources.
*
* @throws SocketException If creating the sockets fails.
*/
function createPair(): array
{
if (($sockets = @\stream_socket_pair(\stripos(PHP_OS, 'win') === 0 ? STREAM_PF_INET : STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP)) === false) {
$message = 'Failed to create socket pair.';
if ($error = \error_get_last()) {
$message .= \sprintf(' Errno: %d; %s', $error['type'], $error['message']);
}
throw new SocketException($message);
}
return [ResourceSocket::fromClientSocket($sockets[0]), ResourceSocket::fromClientSocket($sockets[1])];
}
/**
* @see https://wiki.openssl.org/index.php/Manual:OPENSSL_VERSION_NUMBER(3)
* @return bool
*/
function hasTlsAlpnSupport(): bool
{
return \defined('OPENSSL_VERSION_NUMBER') && \OPENSSL_VERSION_NUMBER >= 0x10002000;
}
function hasTlsSecurityLevelSupport(): bool
{
return \defined('OPENSSL_VERSION_NUMBER') && \OPENSSL_VERSION_NUMBER >= 0x10100000;
}