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; //剩余解封时间}
protoc --plugin=/usr/local/php/bin/protoc-gen-php --php_out=. -i. login.proto
得到login.php
namespace login { class reqcheckverifyverloginclient extends \drslump\protobuf\message { /** @var int */ public $game = null; /** @var string */ public $version = null; /** @var \closure[] */ protected static $__extensions = array();.........
使用login.php
function.php
<?php/** * created by phpstorm. * user: administrator * date: 2018/3/23 * time: 19:26 */require 'socket.php';require 'structnum.php';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( 'num' =>$rev_num, 'data'=>$rev_data ); return $data_array; } $structnum;function structnum($num){ $class = $globals['structnum'][$num]; return $class; }
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['unread_bytes']; } 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(); } }
structnum.php // 类的字典
<?php/** * created by phpstorm. * user: administrator * date: 2018/3/26 * time: 17:54 */$structnum = array( 46=>new \login\reqcheckverifyverloginclient(), 47=>new \login\anscheckverifyverloginclient(), );
vi use_login.php
<?php/** * created by phpstorm. * user: administrator * date: 2018/3/26 * time: 15:20 */require_once 'drslump/protobuf.php'; \drslump\protobuf::autoload();require 'login.php';require 'function.php';//实例化 class 46$login = structnum(46); $login->setgame(3); $login->setversion('0.0.1'); $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['num'][1]); $relogin->parse($unpackdatas['data']);//拆分后的内容 再反序列化 $relogin->getretcode(); $relogin->getforbidflag(); $relogin->cleartimediff(); var_dump($relogin);
相关推荐:
php中socket通讯详解
php之socket服务器搭建和测试实例分享
php中socket简单使用方法
以上就是protobuf-php和socket的使用方法的详细内容。