So verwenden Sie Protobuf-PHP und Socket

小云云
Freigeben: 2023-03-22 14:28:01
Original
2216 Leute haben es durchsucht

In diesem Artikel erfahren Sie hauptsächlich, wie Sie Protobuf-PHP und Socket verwenden. Ich hoffe, dass er Ihnen helfen kann.

vi login.proto

package login;
message ReqCheckVerifyVerLoginClient
{
 required int32 game = 1; ///< 游戏类型编号 required bytes version = 2; ///< 游戏版本号}
message AnsCheckVerifyVerLoginClient
{
     required uint32 ret_code = 1;  //返回码     optional uint32 forbid_flag = 2; //冻结时间     optional uint32 time_diff = 3;  //剩余解封时间}
Nach dem Login kopieren

protoc --plugin=/usr/local/php/bin/protoc-gen-php --php_out= -I 🎜>

login.php abrufen

namespace login {  class ReqCheckVerifyVerLoginClient extends \DrSlump\Protobuf\Message {    /**  @var int */    public $game = null;    
    /**  @var string */    public $version = null;    
    /** @var \Closure[] */    protected static $__extensions = array();.........
Nach dem Login kopieren
login.php verwenden

function.php

<?php/** * Created by PhpStorm. * User: Administrator * Date: 2018/3/23 * Time: 19:26 */require &#39;socket.php&#39;;require &#39;structNum.php&#39;;function socket($ip,$port,$packed){    try{
        $socket = Socket::singleton();
        $socket->connect($ip,$port);        if(count($packed) >1){            foreach ($packed as $key){
                $sockResult = $socket->sendRequest($key);// 将包发送给服务器                sleep(3);
            }
        }
        $getrepost = $socket->getResponse();
        $socket->disconnect (); //关闭链接    } catch (Exception $e) {
        var_dump($e);
        $this->log_error(" error send to server".$e->getMessage());
    }    return $getrepost;
}function unPackData($data){
    $rev_len = unpack("L*",substr($data,0,4));
    $rev_num = unpack("S*",substr($data,4,2));
    $rev_data = substr($data,6,2);
    $data_array = array(        &#39;num&#39; =>$rev_num,        &#39;data&#39;=>$rev_data
    );    return $data_array;
}
$structNum;function StructNum($num){
    $class = $GLOBALS[&#39;structNum&#39;][$num];    return $class;
}
Nach dem Login kopieren
socket.php

<?phpdefine("CONNECTED", true);
define("DISCONNECTED", false); Class Socket {     private static $instance;     private $connection = null;     private $connectionState = DISCONNECTED;     private $defaultHost = "192.168.128.198";     private $defaultPort = 9301;     private $defaultTimeOut = 60;     public $debug = false;     public function __construct()
     {
     }     /**      * Singleton pattern. Returns the same instance to all callers      *      * @return Socket      */     public static function singleton()
     {         if (self::$instance == null || ! self::$instance instanceof Socket)
         {             self::$instance = new Socket();
         }         return self::$instance;
     }     public function connect($serverHost=false,$serverPort=false,$timeOut=false)
     {         if($serverHost == false)
         {             $serverHost = $this->defaultHost;
         }         if($serverPort == false)
         {             $serverPort = $this->defaultPort;
         }
         $this->defaultHost = $serverHost;
         $this->defaultPort = $serverPort;         if($timeOut == false)
         {             $timeOut = $this->defaultTimeOut;
         }
         $this->connection = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);         if(socket_connect($this->connection,$serverHost,$serverPort) == false)
         {
             $errorString = socket_strerror(socket_last_error($this->connection));
             $this->_throwError("Connecting to {$serverHost}:{$serverPort} failed.<br>Reason: {$errorString}");
         }else{
             $this->_throwMsg("Socket connected!");
         }
         $this->connectionState = CONNECTED;
     }     /**      * Disconnects from the server      *      * @return True on succes, false if the connection was already closed      */     public function disconnect()
     {         if($this->validateConnection())
         {
             socket_close($this->connection);
             $this->connectionState = DISCONNECTED;
             $this->_throwMsg("Socket disconnected!");             return true;
         }         return false;
     }     public function sendRequest($data)
     {         if($this->validateConnection())
         {
              $result = socket_write($this->connection,$data,strlen($data));              return $result;
         }else{
             $this->_throwError("Sending command \"{$data}\" failed.<br>Reason: Not connected");
         }
     }     public function getResponse()
     {         if($this->validateConnection())
         {             if( ($ret = socket_read($this->connection,8192)) == false){
                 $this->_throwError("Receiving response from server failed.<br>Reason: Not bytes to read");                 return false;
             } else{                 return $ret;
            }
         }
     }     public function isConn()
     {         return $this->connectionState;
     }     public function getUnreadBytes()
     {
         $info = socket_get_status($this->connection);         return $info[&#39;unread_bytes&#39;];
     }     public function getConnName(&$addr, &$port)
     {         if ($this->validateConnection())
         {
             socket_getsockname($this->connection,$addr,$port);
         }
     }     public function waitForResponse()
     {         if($this->validateConnection())
         {             return socket_read($this->connection, 2048);
         }
         $this->_throwError("Receiving response from server failed.<br>Reason: Not connected");         return false;
     }     /**      * Validates the connection state      *      * @return bool      */     private function validateConnection()
     {         return (is_resource($this->connection) && ($this->connectionState != DISCONNECTED));
     }     /**      * Throws an error      *      * @return void      */     private function _throwError($errorMessage)
     {         echo "Socket error: " . $errorMessage;
     }     /**      * Throws an message      *      * @return void      */     private function _throwMsg($msg)
     {         if ($this->debug)
         {             echo "Socket message: " . $msg . "\n\n";
         }
     }     /**      * If there still was a connection alive, disconnect it      */     public function __destruct()
     {
         $this->disconnect();
     }
 }
Nach dem Login kopieren
structNum.php // Klassenwörterbuch

<?php/** * Created by PhpStorm. * User: Administrator * Date: 2018/3/26 * Time: 17:54 */$structNum = array(    46=>new \login\ReqCheckVerifyVerLoginClient(),    47=>new \login\AnsCheckVerifyVerLoginClient(),
);
Nach dem Login kopieren
vi use_login.php

<?php/** * Created by PhpStorm. * User: Administrator * Date: 2018/3/26 * Time: 15:20 */require_once &#39;DrSlump/Protobuf.php&#39;;
\DrSlump\Protobuf::autoload();require &#39;login.php&#39;;require &#39;function.php&#39;;//实例化 class 46$login = StructNum(46);
$login->setGame(3);
$login->setVersion(&#39;0.0.1&#39;);
$first_data = pack("I*",24063);//一个可识别的ID
$first_len = pack("I*",4);
$second_data = $login->serialize();//序列化
$second_len = pack("I*",strlen($second_data) + 2);
$second_num = substr(pack("I*",46),0,2);
$first_pack = $first_len.$first_data; //字节 包体
$second_pack = $second_len.$second_num.$second_data;//长度 协议 内容(protobuf)
$port = 9301;
$ip = "192.168.128.198";
$pack = array($first_pack,$second_pack);
$result = socket($ip,$port,$pack);//连接 发送 接受数据  数据为长度 协议 内容(protobuf)
$unPackDatas = unPackData($result);//拆分数据(按需拆分) 按 4 2 2 拆分//实例化 class 46$reLogin = StructNum($unPackDatas[&#39;num&#39;][1]);
$reLogin->parse($unPackDatas[&#39;data&#39;]);//拆分后的内容 再反序列化
$reLogin->getRetCode();
$reLogin->getForbidFlag();
$reLogin->clearTimeDiff();
var_dump($reLogin);
Nach dem Login kopieren
Verwandte Empfehlungen:

Detaillierte Erläuterung der Socket-Kommunikation in php

PHP-Socket-Server-Konstruktion und Testbeispiel-Sharing

Einfache Methode zur Verwendung von Socket in PHP

Das obige ist der detaillierte Inhalt vonSo verwenden Sie Protobuf-PHP und Socket. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage