Skip to content

Commit 540461b

Browse files
committed
feat: support custom listening hosts
1 parent 3a9cf75 commit 540461b

File tree

7 files changed

+36
-14
lines changed

7 files changed

+36
-14
lines changed

src/Application.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function __construct(
5757
$feConfig = $this->container->get(FrontendConfig::class);
5858
$feSeparated = !\in_array(
5959
$feConfig->port,
60-
\array_map(static fn(SocketServer $item): ?int => $item->type === 'tcp' ? $item->port : null, $map, ),
60+
\array_map(static fn(SocketServer $item): ?int => $item->protocol === 'tcp' ? $item->port : null, $map),
6161
true,
6262
);
6363
$withFrontend and $this->configureFrontend($feSeparated);
@@ -104,7 +104,7 @@ public function __construct(
104104
$this->configureFileObserver();
105105

106106
foreach ($map as $config) {
107-
$withFrontend && !$feSeparated && $config->type === 'tcp' && $config->port === $feConfig->port
107+
$withFrontend && !$feSeparated && $config->protocol === 'tcp' && $config->port === $feConfig->port
108108
? $this->prepareServerFiber($config, $inspectorWithFrontend, $this->logger)
109109
: $this->prepareServerFiber($config, $inspector, $this->logger);
110110
}
@@ -225,7 +225,7 @@ private function configureFrontend(bool $separated): void
225225
$tcpConfig = $this->container->get(TcpPorts::class);
226226
$config = $this->container->get(FrontendConfig::class);
227227
$this->prepareServerFiber(
228-
new SocketServer(port: $config->port, pollingInterval: $tcpConfig->pollingInterval),
228+
new SocketServer(port: $config->port, host: $config->host, pollingInterval: $tcpConfig->pollingInterval),
229229
$inspector,
230230
$this->logger,
231231
);
@@ -255,6 +255,8 @@ private function createServer(SocketServer $config, Inspector $inspector): Serve
255255
};
256256

257257
return Server::init(
258+
$config->protocol,
259+
$config->host,
258260
$config->port,
259261
payloadSize: 524_288,
260262
acceptPeriod: \max(50, $config->pollingInterval) / 1e6,

src/Command/Run.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function getServers(Container $container): array
7272
$port > 0 && $port < 65536 or throw new \InvalidArgumentException(
7373
\sprintf('Invalid port `%s`. It must be in range 1-65535.', $port),
7474
);
75-
$servers[] = new SocketServer($port, $config->host, $config->type, $config->pollingInterval);
75+
$servers[] = new SocketServer($port, $config->host, $config->protocol, $config->pollingInterval);
7676
}
7777
return $servers;
7878
}

src/Config/Server/Frontend.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ final class Frontend
1818
#[InputOption('ui')]
1919
#[XPath('/trap/frontend/@port')]
2020
public int $port = 8000;
21+
22+
/** @var non-empty-string */
23+
#[Env('TRAP_UI_HOST')]
24+
#[XPath('/trap/frontend/@host')]
25+
public string $host = '127.0.0.1';
2126
}

src/Config/Server/SocketServer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class SocketServer
1818
public function __construct(
1919
public readonly int $port,
2020
public readonly string $host = '127.0.0.1',
21-
public readonly string $type = 'tcp',
21+
public readonly string $protocol = 'tcp',
2222
public int $pollingInterval = 1_000,
2323
) {}
2424
}

src/Config/Server/TcpPorts.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ final class TcpPorts
3232
#[Env('TRAP_TCP_HOST')]
3333
public string $host = '127.0.0.1';
3434

35-
public string $type = 'tcp';
35+
public string $protocol = 'tcp';
3636

3737
/**
3838
* Time to wait between socket_accept() and socket_select() calls in microseconds.

src/Service/Container.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct()
3737
}
3838

3939
/**
40-
* @template T of object
40+
* @template T
4141
* @param class-string<T> $id
4242
* @param array $arguments Will be used if the object is created for the first time.
4343
* @return T

src/Socket/Server.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,41 +30,56 @@ final class Server implements Processable, Cancellable, Destroyable
3030
private float $lastAccept = 0;
3131

3232
/**
33-
* @param null|\Closure(Client, int $id): void $clientInflector
33+
* @param non-empty-string $protocol
34+
* @param non-empty-string $address
35+
* @param int<1, 65535> $port
36+
* @param null|\Closure(Client, int $id): mixed $clientInflector
3437
* @param positive-int $payloadSize Max payload size.
3538
* @param float $acceptPeriod Time to wait between socket_accept() calls in seconds.
3639
*/
3740
private function __construct(
41+
string $protocol,
42+
string $address,
3843
int $port,
3944
private readonly int $payloadSize,
4045
private readonly float $acceptPeriod,
4146
private readonly ?\Closure $clientInflector,
4247
private readonly Logger $logger,
4348
) {
44-
$this->socket = @\socket_create_listen($port) ?: throw new \RuntimeException('Socket create failed.');
49+
$this->socket = \socket_create(\AF_INET, \SOCK_STREAM, \getprotobyname($protocol))
50+
?: throw new \RuntimeException('Socket create failed.');
51+
@\socket_set_nonblock($this->socket)
52+
?: throw new \RuntimeException('Socket set nonblock failed.');
53+
@\socket_bind($this->socket, $address, $port)
54+
?: throw new \RuntimeException('Socket bind failed.');
4555

4656
/** @link https://github.com/buggregator/trap/pull/14 */
4757
// \socket_set_option($this->socket, \SOL_SOCKET, \SO_LINGER, ['l_linger' => 0, 'l_onoff' => 1]);
4858

49-
\socket_set_nonblock($this->socket);
59+
@\socket_listen($this->socket, \SOMAXCONN)
60+
?: throw new \RuntimeException('Socket listen failed.');
5061

51-
$logger->status('App', 'Server started on 127.0.0.1:%s', $port);
62+
$logger->status('App', 'Server started on %s:%s', $address, $port);
5263
}
5364

5465
/**
66+
* @param non-empty-string $protocol
67+
* @param non-empty-string $address
5568
* @param int<1, 65535> $port
5669
* @param positive-int $payloadSize Max payload size.
5770
* @param float $acceptPeriod Time to wait between socket_accept() calls in seconds.
58-
* @param null|\Closure(Client, int $id): void $clientInflector
71+
* @param null|\Closure(Client, int $id): mixed $clientInflector
5972
*/
6073
public static function init(
61-
int $port = 9912,
74+
string $protocol,
75+
string $address,
76+
int $port,
6277
int $payloadSize = 10485760,
6378
float $acceptPeriod = .001,
6479
?\Closure $clientInflector = null,
6580
Logger $logger = new Logger(),
6681
): self {
67-
return new self($port, $payloadSize, $acceptPeriod, $clientInflector, $logger);
82+
return new self($protocol, $address, $port, $payloadSize, $acceptPeriod, $clientInflector, $logger);
6883
}
6984

7085
public function destroy(): void

0 commit comments

Comments
 (0)