@@ -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