Create a tcp server from raw socket
slince opened this issue · 1 comments
Hello!
I'm trying to write a TCP application; I expect to create TCP sockets in the master process and complete bind and listen; Accept in the fork child process;
Can this library support passing raw socket
to the constructor:
// create raw socket
$socket = @\stream_socket_server($uri, $errno, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $streamContext);
// create server
$server = new TcpServer($socket, $loop);
Hi @slince, thanks for this excellent question!
If you create a socket file descriptor and then fork the process, both the parent and child process will have access to the same socket file descriptor and can accept connections from it. This means you don't have to "create a new server" in the child process, the whole process space will be copied to the child process. That being said, forking in PHP is anything but trivial and it's way too easy to leak resources, so I wouldn't recommend doing this unless you're sure you know what you're doing.
On top of this, we're also working on a solution to create a socket server instance from an existing file descriptor number (i.e. a socket file descriptor inherited from the the parent process). This is being discussed in #164 which also includes a prototype implementation for you to give a try.
I believe this has been answered, so I'm closing this for now. Please come back with more details if this problem persists and we can always reopen this 👍