Giter Site home page Giter Site logo

Comments (3)

walkor avatar walkor commented on June 6, 2024

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.

xenion54 avatar xenion54 commented on June 6, 2024

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.

walkor avatar walkor commented on June 6, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.