Comments (3)
In Linux systems, it is possible to pass descriptors between processes. In Workerman, the code looks something like this.
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Connection\TcpConnection;
use Workerman\Events\EventInterface;
use Workerman\Worker;
$worker = new Worker('websocket://0.0.0.0:1234');
$count = 4;
$worker->count = $count;
$fd_array = [];
for ($i = 0; $i < $count; $i++) {
socket_create_pair(AF_UNIX, SOCK_DGRAM, 0, $fds);
$fd_array[] = $fds;
}
$worker->onWorkerStart = function ($worker) use ($fd_array) {
$id = $worker->id;
$stream = socket_export_stream($fd_array[$id][0]);
Worker::$globalEvent->add($stream, EventInterface::EV_READ, function () use ($stream, $worker) {
echo "Receive connection, current_pid=" . posix_getpid() . " current_worker->id=$worker->id\n";
$socket = socket_import_stream($stream);
$data = [
'controllen' => socket_cmsg_space(SOL_SOCKET, SCM_RIGHTS, 1)
];
socket_recvmsg($socket, $data, 0);
$socket = socket_export_stream($data['control'][0]['data'][0]);
createConnectionForWorker($worker, $socket);
});
};
$worker->onConnect = function (TcpConnection $connection) use ($fd_array, $worker, $count){
$id = $worker->id;
$toId = ($id + 1) % $count;
echo "Send connection From pid=" . posix_getpid() . " worker->id=$id To worker->id=$toId\n";
socket_sendmsg($fd_array[$toId][1], [
'control' => [[
'level' => SOL_SOCKET,
'type' => SCM_RIGHTS,
'data' => [$connection->getSocket()]
]]
], 0);
$connection->close();
};
$worker->onMessage = function (TcpConnection $connection, $request) use ($worker) {
echo "onMessage current_pid=" . posix_getpid() . " current_worker->id=$worker->id\n";
$connection->send('ok');
};
function createConnectionForWorker($worker, $socket)
{
$connection = new TcpConnection($socket);
$worker->connections[$connection->id] = $connection;
$connection->worker = $worker;
$connection->protocol = $worker->protocol;
$connection->transport = $worker->transport;
$connection->onMessage = $worker->onMessage;
$connection->onClose = $worker->onClose;
$connection->onError = $worker->onError;
}
Worker::runAll();
from workerman.
Hi @walkor. Thank you so much. It looks hard, but its rly work. I m doing selfmade proxy, and when 2 connections with needed identifiers are connected, i need to put to one process, cause i think that sending message between each other will be faster in same process, than use Channel Class evry time when message received. If i wrong way, pls say what you think about it. Anyway, again, thank so much for answer and for nice library.
In Linux systems, it is possible to pass descriptors between processes. In Workerman, the code looks something like this.
<?php require_once __DIR__ . '/vendor/autoload.php'; use Workerman\Connection\TcpConnection; use Workerman\Events\EventInterface; use Workerman\Worker; $worker = new Worker('websocket://0.0.0.0:1234'); $count = 4; $worker->count = $count; $fd_array = []; for ($i = 0; $i < $count; $i++) { socket_create_pair(AF_UNIX, SOCK_DGRAM, 0, $fds); $fd_array[] = $fds; } $worker->onWorkerStart = function ($worker) use ($fd_array) { $id = $worker->id; $stream = socket_export_stream($fd_array[$id][0]); Worker::$globalEvent->add($stream, EventInterface::EV_READ, function () use ($stream, $worker) { echo "Receive connection, current_pid=" . posix_getpid() . " current_worker->id=$worker->id\n"; $socket = socket_import_stream($stream); $data = [ 'controllen' => socket_cmsg_space(SOL_SOCKET, SCM_RIGHTS, 1) ]; socket_recvmsg($socket, $data, 0); $socket = socket_export_stream($data['control'][0]['data'][0]); createConnectionForWorker($worker, $socket); }); }; $worker->onConnect = function (TcpConnection $connection) use ($fd_array, $worker, $count){ $id = $worker->id; $toId = ($id + 1) % $count; echo "Send connection From pid=" . posix_getpid() . " worker->id=$id To worker->id=$toId\n"; socket_sendmsg($fd_array[$toId][1], [ 'control' => [[ 'level' => SOL_SOCKET, 'type' => SCM_RIGHTS, 'data' => [$connection->getSocket()] ]] ], 0); $connection->close(); }; $worker->onMessage = function (TcpConnection $connection, $request) use ($worker) { echo "onMessage current_pid=" . posix_getpid() . " current_worker->id=$worker->id\n"; $connection->send('ok'); }; function createConnectionForWorker($worker, $socket) { $connection = new TcpConnection($socket); $worker->connections[$connection->id] = $connection; $connection->worker = $worker; $connection->protocol = $worker->protocol; $connection->transport = $worker->transport; $connection->onMessage = $worker->onMessage; $connection->onClose = $worker->onClose; $connection->onError = $worker->onError; } Worker::runAll();
Hi @walkor. Thank you so much. It looks hard, but its rly work. I m doing selfmade proxy, and when 2 connections with needed identifiers are connected, i need to put to one process, cause i think that sending message between each other will be faster in same process, than use Channel Class evry time when message received. If i wrong way, pls say what you think about it. Anyway, again, thank so much for answer and for nice library.
from workerman.
i think that sending message between each other will be faster in same process, than use Channel Class evry time when message received
Yes
from workerman.
Related Issues (20)
- How to start script many times at same time HOT 2
- Kill worker in linux HOT 4
- From Ratchet to Workerman HOT 1
- Error package because of receiving data before handshake HOT 3
- Restrict access by origin domain and limit the number of connections HOT 1
- Implementation of the onError() handler HOT 1
- Param sameSite in the $response->cookie() should be a string HOT 1
- v5版本使用swow发生文件不存在异常 HOT 3
- [BUG] 监听[::]22348地址,REMOTE_ADDR获取不到客户端ip HOT 1
- Run Workerman in daemon mode with Windows HOT 2
- Estrange behavior in composer require HOT 1
- runtime test HOT 2
- add a watch command
- ssl secure connection HOT 1
- Transferring data between workers HOT 5
- What different between Protocols\Ws vs Protocols\Websocket ? HOT 2
- TCP connection->send always returns null HOT 1
- Connection close or set NULL using Request connection HOT 3
- $request->path() 有问题 HOT 1
- Output a grouped routing list HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from workerman.