php thrift TServerSocket achieve port multiplexes

<?php
namespace Message\Controller;
use Think\Controller;
use Thrift\Exception\TException;
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TBufferedTransport;
use Thrift\Transport\THttpClient;
use Thrift\Transport\TPhpStream;
use Thrift\TMultiplexedProcessor;
use Thrift\Protocol\TMultiplexedProtocol;
use Message\Services\MessageServie;
use Rpc\Msg\MessageClient;
use Rpc\Msg\MessageProcessor;
use Thrift\Factory\TBinaryProtocolFactory;
use Thrift\Factory\TTransportFactory;
use Thrift\Server\TServerSocket;
use Thrift\Server\TSimpleServer;
use Thrift\Server\TForkingServer;
use Thrift\Transport\TSocket;

 

server

public function message_rpc()
    {
        the try {
             // initialize multiple service providers handle 
            $ MessageProcessor = new new \ Rpc \ Msg \ MessageProcessor ( new new MessageServie ());
             // create multiple service Processor 
            $ sendProcessor = new new \ Rpc \ Msg \ SendMsgProcessor ( new new \ the Message \ Services \ SendMsgServie ());

            // The service is registered to TMultiplexedProcessor in 
            $ tFactory = new new TTransportFactory ();
            $pFactory = new TBinaryProtocolFactory(true, true);
            Processor $ = new new TMultiplexedProcessor ();
             // register the service TMultiplexedProcessor
             // queue consumer rpc request 
            $ processor-> registerProcessor ( " MessageApiAction " , $ MessageProcessor);
             // message rpc request 
            $ processor-> registerProcessor ( " SendMsg " , $ sendProcessor);


            // initialize data transmission Transport
             // by using the transmission format initialization data transmission protocol




            // monitoring starting 
            $ = Transport new new TServerSocket ( ' 0.0.0.0 ' , ' 9988 ' );


//            $processor->process($pFactory, $pFactory);


            $server = new TForkingServer($processor, $transport, $tFactory, $tFactory, $pFactory, $pFactory);
            $server->serve();
        } catch (TException $tx) {
            \Think\Log::write($tx->getMessage());
        } catch(\Exception $e){
            \Think\Log::write($e->getMessage());
        }
    }

 

client

public function local()
    {
        try {
            ini_set('memory_limit', '1024M');
            $socket = new TSocket('192.168.1.188', '9988');
            $socket->setRecvTimeout(50000);
            $socket->setDebug(true);
            $transport = new TBufferedTransport($socket, 1024, 1024);
            $protocol = new TBinaryProtocol($transport);

            $client = new  MessageClient(new TMultiplexedProtocol($protocol, "MessageApiAction"));
//            $client = new MessageClient($protocol);
            $transport->open();
            $result = $client->MessageApiAction('message api');
            print_r($result);
            $transport->close();
        } catch (TException $tx) {
            print_r($tx->getMessage());
        }

    }

    public function send_msg()
    {
        try {
            ini_set('memory_limit', '1024M');
            $socket = new TSocket('192.168.1.188', '9988');
            $socket->setRecvTimeout(50000);
            $socket->setDebug(true);
            $transport = new TBufferedTransport($socket, 1024, 1024);
            $protocol = new TBinaryProtocol($transport);


            $client = new  \Rpc\Msg\SendMsgClient(new TMultiplexedProtocol($protocol, "sendMsg"));

//            $client = new \Rpc\Msg\SendMsgClient($protocol);
            $transport->open();
            $result = $client->sendMsg('send msg');
            print_r($result);
            $transport->close();
        } catch (TException $tx) {
            print_r($tx->getMessage());
        }

    }

 

Guess you like

Origin www.cnblogs.com/sunlong88/p/11016277.html