Browse Source

分离 应用

walkor 12 years ago
parent
commit
cbc017c580

+ 0 - 81
applications/Game/Event.php

@@ -1,81 +0,0 @@
-<?php
-/**
- * 
- * 
- * @author walkor <worker-man@qq.com>
- * 
- */
-
-require_once WORKERMAN_ROOT_DIR . 'applications/Game/Store.php';
-
-class Event
-{
-   public static function onConnect($address, $socket_id, $sid)
-   {
-       // 检查sid是否合法
-       $uid = self::getUidBySid($sid);
-       // 不合法踢掉
-       if(!$uid)
-       {
-           self::kickAddress($address, $socket_id);
-           return;
-       }
-       
-       // 合法记录uid到address的映射
-       self::storeUidAddress($uid, $address);
-       
-       // 发送数据包到address,确认connection成功
-       self::notifyConnectionSuccess($address, $socket_id, $uid);
-   }
-   
-   public static function onClose($address, $uid)
-   {
-       $buf = new Gamebuffer();
-       $buf->header['cmd'] = GameBuffer::CMD_GATEWAY;
-       $buf->header['sub_cmd'] = GameBuffer::SCMD_BROADCAST;
-       $buf->header['from_uid'] = $uid;
-       $buf->body = "logout bye!!!";
-       GameBuffer::sendToAll($buf->getBuffer());
-       self::deleteUidAddress($uid);
-   }
-   
-   public static function kickUid($uid)
-   {
-       
-   }
-   
-   public static function kickAddress($address, $socket_id)
-   {
-     
-   }
-   
-   public static function storeUidAddress($uid, $address)
-   {
-       Store::set($uid, $address);
-   }
-   
-   public static function getAddressByUid($uid)
-   {
-       return Store::get($uid);
-   }
-   
-   public static function deleteUidAddress($uid)
-   {
-       return Store::delete($uid);
-   }
-   
-   protected static function notifyConnectionSuccess($address, $socket_id, $uid)
-   {
-       $buf = new GameBuffer();
-       $buf->header['cmd'] = GameBuffer::CMD_GATEWAY;
-       $buf->header['sub_cmd'] = GameBuffer::SCMD_CONNECT_SUCCESS;
-       $buf->header['from_uid'] = $socket_id;
-       $buf->header['to_uid'] = $uid;
-       GameBuffer::sendToGateway($address, $buf->getBuffer());
-   }
-   
-   protected static function getUidBySid($sid)
-   {
-       return $sid;
-   }
-}

+ 0 - 157
applications/Game/Protocols/Buffer.php

@@ -1,157 +0,0 @@
-<?php 
-/**
- * 二进制协议
- * 
- * struct BufferProtocol
- * {
- *     unsigned char     version,//版本
- *     unsigned short    series_id,//序列号 udp协议使用
- *     unsigned short    cmd,//主命令字
- *     unsigned short    sub_cmd,//子命令字
- *     int                         code,//返回码
- *     unsigned int        from_uid,//来自用户uid
- *     unsigned int        to_uid,//发往的uid
- *     unsigned int       pack_len,//包长
- *     char[pack_length-HEAD_LEN] body//包体
- * }
- * 
- * @author walkor <worker-man@qq.com>
- */
-
-class Buffer
-{
-    /**
-     * 版本
-     * @var integer
-     */
-    const VERSION = 0x01;
-    
-    /**
-     * 包头长度
-     * @var integer
-     */
-    const HEAD_LEN = 23;
-     
-    /**
-     * 序列号,防止串包
-     * @var integer
-     */
-    protected static $seriesId = 0;
-    
-    /**
-     * 协议头
-     * @var array
-     */
-    public $header = array(
-        'version'        => self::VERSION,
-        'series_id'      => 0,
-        'cmd'            => 0,
-        'sub_cmd'     => 0,
-        'code'           => 0,
-        'from_uid'    => 0,
-        'to_uid'         => 0,
-        'pack_len'    => self::HEAD_LEN
-    );
-    
-    /**
-     * 包体
-     * @var string
-     */
-    public $body = '';
-    
-    /**
-     * 初始化
-     * @return void
-     */
-    public function __construct($buffer = null)
-    {
-        if($buffer)
-        {
-            $data = self::bufferToData($buffer);
-            $this->body = $data['body'];
-            unset($data['body']);
-            $this->header = $data;
-        }
-        else
-        {
-            if(self::$seriesId>=65535)
-            {
-                self::$seriesId = 0;
-            }
-            else
-            {
-                $this->header['series_id'] = self::$seriesId++;
-            }
-        }
-    }
-    
-    /**
-     * 判断数据包是否都到了
-     * @param string $buffer
-     * @return int int=0数据是完整的 int>0数据不完整,还要继续接收int字节
-     */
-    public static function input($buffer, &$data = null)
-    {
-        $len = strlen($buffer);
-        if($len < self::HEAD_LEN)
-        {
-            return self::HEAD_LEN - $len;
-        }
-        
-        $data = unpack("Cversion/Sseries_id/Scmd/Ssub_cmd/icode/Ifrom_uid/Ito_uid/Ipack_len", $buffer);
-        if($data['pack_len'] > $len)
-        {
-            return $data['pack_len'] - $len;
-        }
-        $data['body'] = '';
-        $body_len = $data['pack_len'] - self::HEAD_LEN;
-        if($body_len > 0)
-        {
-            $data['body'] = substr($buffer, self::HEAD_LEN, $body_len);
-        }
-        return 0;
-    }
-    
-    
-    /**
-     * 设置包体
-     * @param string $body_str
-     * @return void
-     */
-    public function setBody($body_str)
-    {
-        $this->body = (string) $body_str;
-    }
-    
-    /**
-     * 获取整个包的buffer
-     * @param string $data
-     * @return string
-     */
-    public function getBuffer()
-    {
-        $this->header['pack_len'] = self::HEAD_LEN + strlen($this->body);
-        return pack("CSSSiIII", $this->header['version'],  $this->header['series_id'], $this->header['cmd'], $this->header['sub_cmd'], $this->header['code'], $this->header['from_uid'], $this->header['to_uid'], $this->header['pack_len']).$this->body;
-    }
-    
-    /**
-     * 从二进制数据转换为数组
-     * @param string $buffer
-     * @return array
-     */    
-    public static function decode($buffer)
-    {
-        $data = unpack("Cversion/Sseries_id/Scmd/Ssub_cmd/icode/Ifrom_uid/Ito_uid/Ipack_len", $buffer);
-        $data['body'] = '';
-        $body_len = $data['pack_len'] - self::HEAD_LEN;
-        if($body_len > 0)
-        {
-            $data['body'] = substr($buffer, self::HEAD_LEN, $body_len);
-        }
-        return $data;
-    }
-    
-}
-
-
-

+ 0 - 80
applications/Game/Protocols/GameBuffer.php

@@ -1,80 +0,0 @@
-<?php
-/**
- * 
- * 命令字相关
-* @author walkor <worker-man@qq.com>
-* 
- */
-require_once WORKERMAN_ROOT_DIR . 'applications/Game/Protocols/Buffer.php';
-require_once WORKERMAN_ROOT_DIR . 'applications/Game/Event.php';
-
-class GameBuffer extends Buffer
-{
-    // 系统命令
-    const CMD_SYSTEM = 128;
-    // 连接事件 
-    const SCMD_ON_CONNECT = 1;
-    // 关闭事件
-    const SCMD_ON_CLOSE = 2;
-    
-    // 发送给网关的命令
-    const CMD_GATEWAY = 129;
-    // 给用户发送数据包
-    const SCMD_SEND_DATA = 3;
-    // 根据uid踢人
-    const SCMD_KICK_UID = 4;
-    // 根据地址和socket编号踢人
-    const SCMD_KICK_ADDRESS = 5;
-    // 广播内容
-    const SCMD_BROADCAST = 6;
-    // 通知连接成功
-    const SCMD_CONNECT_SUCCESS = 7;
-    
-    // 用户中心
-    const CMD_USER = 1;
-    // 登录
-    const SCMD_LOGIN = 8;
-    // 发言
-    const SCMD_SAY = 9;
- 
-    public static $cmdMap = array(
-            self::CMD_USER  => 'User',
-            self::CMD_GATEWAY => 'GateWay',
-            self::CMD_SYSTEM => 'System',
-     );
-    
-    public static $scmdMap = array(
-            self::SCMD_BROADCAST     => 'broadcast',
-            self::SCMD_LOGIN                => 'login',
-            self::SCMD_ON_CONNECT   =>'onConnect',
-            self::SCMD_ON_CLOSE         => 'onClose',
-            self::SCMD_SAY          => 'say',
-     );
-    
-    public static function sendToGateway($address, $bin_data, $to_uid = 0, $from_uid = 0)
-    {
-        $client = stream_socket_client($address);
-        $len = stream_socket_sendto($client, $bin_data);
-        return $len == strlen($bin_data);
-    }
-    
-    public static function sendToUid($uid, $buffer)
-    {
-        $address = Event::getAddressByUid($uid);
-        if($address)
-        {
-            return self::sendToGateway($address, $buffer);
-        }
-        return false;
-    }
-    
-    public static function sendToAll($buffer)
-    {
-        $data = GameBuffer::decode($buffer);
-        $all_addresses = Store::get('GLOBAL_GATEWAY_ADDRESS');
-        foreach($all_addresses as $address)
-        {
-            self::sendToGateway($address, $buffer);
-        }
-    }
-}

+ 0 - 28
applications/Game/Store.php

@@ -1,28 +0,0 @@
-<?php
-/**
- * 
- * 
- * @author walkor <worker-man@qq.com>
- * 
- */
-
-class Store
-{
-    public static function set($key, $value, $ttl = 0)
-    {
-        $key = strval($key);
-        return apc_store($key, $value);
-    }
-    
-   public static function get($key)
-   {
-       return apc_fetch($key);
-   }
-   
-   public static function delete($key)
-   {
-       $key = strval($key);
-       return apc_delete($key);
-   }
-   
-}

+ 0 - 268
applications/Game/Tests/gameChat.php

@@ -1,268 +0,0 @@
-<?php
-ini_set('display_errors', 'on');
-error_reporting(E_ALL);
-
-$sock = stream_socket_client("tcp://115.28.44.100:8282");
-if(!$sock)exit("can not create sock\n");
-
-$buf = new GameBuffer();
-$buf->body = rand(1,100000000); 
-
-fwrite($sock, $buf->getBuffer());
-$ret = fread($sock, 1024);
-$ret = GameBuffer::decode($ret);
-if(isset($ret['to_uid']))
-{
-    echo "chart room login success , your uid is [{$ret['to_uid']}]\n";
-    echo "use uid:words send message to one user\n";
-    echo "use words send message to all\n";
-}
-
-stream_set_blocking($sock, 0);
-stream_set_blocking(STDIN, 0);
-
-$read = array(STDIN, $sock);
-
-$write = $ex = array();
-while(1)
-{
-    $read_copy = $read;
-    if($ret = stream_select($read_copy, $write, $ex, 1000))
-    {
-       foreach($read as $fd)
-       {
-          // 接收消息
-          if((int)$fd === (int)$sock)
-          {
-              $ret = fread($fd, 102400);
-              if(!$ret){continue;exit("connection closed\n ");}
-              $ret = GameBuffer::decode($ret);
-              echo $ret['from_uid'] , ':', $ret['body'], "\n";
-              continue;
-          }
-          // 向某个uid发送消息 格式为 uid:xxxxxxxx
-          $ret = fgets(STDIN, 10240);
-          if(!$ret)continue;
-          if(preg_match("/(\d+):(.*)/", $ret, $match))
-          {
-             $uid = $match[1];
-             $words = $match[2];
-             $buf = new GameBuffer();
-             $buf->header['cmd'] = GameBuffer::CMD_USER;
-             $buf->header['sub_cmd'] = GameBuffer::SCMD_SAY;
-             $buf->header['to_uid'] = $uid;
-             $buf->body = $words;
-             fwrite($sock, $buf->getBuffer());
-             continue;
-          }
-          // 向所有用户发消息
-          $buf = new GameBuffer();
-          $buf->header['cmd'] = GameBuffer::CMD_USER;
-          $buf->header['sub_cmd'] = GameBuffer::SCMD_BROADCAST;
-          $buf->body = trim($ret);
-          fwrite($sock, $buf->getBuffer());
-          continue;
-
-       }
-    }
-  
-}
-
-
-/**
- * 二进制协议
- *
- * struct BufferProtocol
- * {
- *     unsigned char     version,//版本
- *     unsigned short    series_id,//序列号 udp协议使用
- *     unsigned short    cmd,//主命令字
- *     unsigned short    sub_cmd,//子命令字
- *     int                         code,//返回码
- *     unsigned int        from_uid,//来自用户uid
- *     unsigned int        to_uid,//发往的uid
- *     unsigned int       pack_len,//包长
- *     char[pack_length-HEAD_LEN] body//包体
- * }
- *
- * @author walkor <worker-man@qq.com>
- */
-
-class Buffer
-{
-    /**
-     * 版本
-     * @var integer
-     */
-    const VERSION = 0x01;
-
-    /**
-     * 包头长度
-     * @var integer
-     */
-    const HEAD_LEN = 23;
-
-    /**
-     * 序列号,防止串包
-     * @var integer
-     */
-    protected static $seriesId = 0;
-
-    /**
-     * 协议头
-     * @var array
-     */
-    public $header = array(
-        'version'        => self::VERSION,
-        'series_id'      => 0,
-        'cmd'            => 0,
-        'sub_cmd'     => 0,
-        'code'           => 0,
-        'from_uid'    => 0,
-        'to_uid'         => 0,
-        'pack_len'    => self::HEAD_LEN
-    );
-/**
-     * 包体
-     * @var string
-     */
-    public $body = '';
-
-    /**
-     * 初始化
-     * @return void
-     */
-    public function __construct($buffer = null)
-    {
-        if($buffer)
-        {
-            $data = self::bufferToData($buffer);
-            $this->body = $data['body'];
-            unset($data['body']);
-            $this->header = $data;
-        }
-        else
-        {
-            if(self::$seriesId>=65535)
-            {
-                self::$seriesId = 0;
-            }
-            else
-            {
-                $this->header['series_id'] = self::$seriesId++;
-            }
-        }
-    }
-
-    /**
-     * 判断数据包是否都到了
-     * @param string $buffer
-     * @return int int=0数据是完整的 int>0数据不完整,还要继续接收int字节
-     */
-    public static function input($buffer, &$data = null)
-    {
-        $len = strlen($buffer);
-        if($len < self::HEAD_LEN)
-        {
-            return self::HEAD_LEN - $len;
-        }
-
-        $data = unpack("Cversion/Sseries_id/Scmd/Ssub_cmd/icode/Ifrom_uid/Ito_uid/Ipack_len", $buffer);
-        if($data['pack_len'] > $len)
-        {
-            return $data['pack_len'] - $len;
-        }
-        $data['body'] = '';
-        $body_len = $data['pack_len'] - self::HEAD_LEN;
-        if($body_len > 0)
-        {
-            $data['body'] = substr($buffer, self::HEAD_LEN, $body_len);
- }
-        return 0;
-    }
-
-
-    /**
-     * 设置包体
-     * @param string $body_str
-     * @return void
-     */
-    public function setBody($body_str)
-    {
-        $this->body = (string) $body_str;
-    }
-
-    /**
-     * 获取整个包的buffer
-     * @param string $data
-     * @return string
-     */
-    public function getBuffer()
-    {
-        $this->header['pack_len'] = self::HEAD_LEN + strlen($this->body);
-        return pack("CSSSiIII", $this->header['version'],  $this->header['series_id'], $this->header['cmd'], $this->header['sub_cmd'], $this->header['code'], $this->header['from_uid'], $this->header['to_uid'], $this->header['pack_len']).$this->body;
-    }
-
-    /**
-     * 从二进制数据转换为数组
-     * @param string $buffer
-     * @return array
-     */
-    public static function decode($buffer)
-    {
-        $data = unpack("Cversion/Sseries_id/Scmd/Ssub_cmd/icode/Ifrom_uid/Ito_uid/Ipack_len", $buffer);
-        $data['body'] = '';
-        $body_len = $data['pack_len'] - self::HEAD_LEN;
-        if($body_len > 0)
-        {
-            $data['body'] = substr($buffer, self::HEAD_LEN, $body_len);
-        }
-        return $data;
-    }
-
-}
-
-
-
-
-
-/**
- *
- * 命令字相关
-* @author walkor <worker-man@qq.com>
-*
- */
-
-class GameBuffer extends Buffer
-{
-    // 系统命令
-    const CMD_SYSTEM = 128;
-    // 连接事件
-    const SCMD_ON_CONNECT = 1;
-    // 关闭事件
-    const SCMD_ON_CLOSE = 2;
-
-    // 发送给网关的命令
-    const CMD_GATEWAY = 129;
-    // 给用户发送数据包
-    const SCMD_SEND_DATA = 3;
-    // 根据uid踢人
-    const SCMD_KICK_UID = 4;
-    // 根据地址和socket编号踢人
-    const SCMD_KICK_ADDRESS = 5;
-    // 广播内容
-    const SCMD_BROADCAST = 6;
-    // 通知连接成功
-    const SCMD_CONNECT_SUCCESS = 7;
-
-    // 用户中心
-    const CMD_USER = 1;
-    // 登录
-    const SCMD_LOGIN = 8;
-    // 发言
-    const SCMD_SAY = 9;
-
-}
-
-
-

+ 0 - 27
applications/Game/User.php

@@ -1,27 +0,0 @@
-<?php
-require_once WORKERMAN_ROOT_DIR . 'applications/Game/Protocols/GameBuffer.php';
-class User
-{
-    public static function broadcast($data)
-    {
-        $buf = new GameBuffer();
-        $buf->header['cmd'] = GameBuffer::CMD_GATEWAY;
-        $buf->header['sub_cmd'] = GameBuffer::SCMD_BROADCAST;
-        $buf->header['from_uid'] = $data['from_uid'];
-        $buf->body = $data['body'];
-        GameBuffer::sendToAll($buf->getBuffer());
-    }
-
-
-    public static function say($data)
-    {
-        $buf = new GameBuffer();
-        $buf->header['cmd'] = GameBuffer::CMD_GATEWAY;
-        $buf->header['sub_cmd'] = GameBuffer::SCMD_SEND_DATA;
-        $buf->header['from_uid'] = $data['from_uid'];
-        $buf->header['to_uid'] = $data['to_uid'];
-        $buf->body = $data['body'];
-        GameBuffer::sendToUid($data['to_uid'], $buf->getBuffer());
-    }
-
-}

+ 0 - 9
conf/conf.d/GameGateway.conf

@@ -1,9 +0,0 @@
-listen = tcp://115.28.44.100:8282
-persistent_connection = 1
-start_workers = 5
-user = www-data
-preread_length = 23
-lan_ip = 127.0.0.1
-lan_port_start = 10000
-game_worker[] = udp://127.0.0.1:8383
-game_worker[] = udp://127.0.0.1:8383

+ 0 - 4
conf/conf.d/GameWorker.conf

@@ -1,4 +0,0 @@
-listen = udp://127.0.0.1:8383
-start_workers = 5
-user = www-data
-preread_length = 23

+ 0 - 294
workers/GameGateway.php

@@ -1,294 +0,0 @@
-<?php
-/**
- * 
- * 暴露给客户端的连接网关 只负责网络io
- * 1、监听客户端连接
- * 2、监听后端回应并转发回应给前端
- * 
- * @author walkor <worker-man@qq.com>
- * 
- */
-require_once WORKERMAN_ROOT_DIR . 'man/Core/SocketWorker.php';
-require_once WORKERMAN_ROOT_DIR . 'applications/Game/Protocols/GameBuffer.php';
-require_once WORKERMAN_ROOT_DIR . 'applications/Game/Store.php';
-
-class GameGateway extends Man\Core\SocketWorker
-{
-    // 内部通信socket
-    protected $innerMainSocket = null;
-    // 内网ip
-    protected $lanIp = '127.0.0.1';
-    // 内部通信端口
-    protected $lanPort = 0;
-    // uid到连接的映射
-    protected $uidConnMap = array();
-    // 连接到uid的映射
-    protected $connUidMap = array();
-    
-    // 到GameWorker的通信地址
-    protected $workerAddresses = array();
-    
-    // 当前处理的包数据
-    protected $data = array();
-    
-    public function start()
-    {
-        // 安装信号处理函数
-        $this->installSignal();
-        
-        // 添加accept事件
-        $ret = $this->event->add($this->mainSocket,  Man\Core\Events\BaseEvent::EV_READ, array($this, 'accept'));
-        
-        // 创建内部通信套接字
-        $start_port = Man\Core\Lib\Config::get($this->workerName.'.lan_port_start');
-        $this->lanPort = $start_port - posix_getppid() + posix_getpid();
-        $this->lanIp = Man\Core\Lib\Config::get($this->workerName.'.lan_ip');
-        if(!$this->lanIp)
-        {
-            $this->notice($this->workerName.'.lan_ip not set');
-            $this->lanIp = '127.0.0.1';
-        }
-        $error_no = 0;
-        $error_msg = '';
-        $this->innerMainSocket = stream_socket_server("udp://".$this->lanIp.':'.$this->lanPort, $error_no, $error_msg, STREAM_SERVER_BIND);
-        if(!$this->innerMainSocket)
-        {
-            $this->notice('create innerMainSocket fail and exit '.$error_no . ':'.$error_msg);
-            sleep(1);
-            exit(0);
-        }
-        else
-        {
-            stream_set_blocking($this->innerMainSocket , 0);
-        }
-        
-        $this->registerAddress("udp://".$this->lanIp.':'.$this->lanPort);
-        
-        // 添加读udp事件
-        $this->event->add($this->innerMainSocket,  Man\Core\Events\BaseEvent::EV_READ, array($this, 'recvUdp'));
-        
-        // 初始化到worker的通信地址
-        $this->initWorkerAddresses();
-        
-        // 主体循环,整个子进程会阻塞在这个函数上
-        $ret = $this->event->loop();
-        $this->notice('worker loop exit');
-        exit(0);
-    }
-    
-    /**
-     * 存储全局的通信地址
-     * @param string $address
-     * @todo 用锁机制等保证数据完整性
-     */
-    protected function registerAddress($address)
-    {
-        $key = 'GLOBAL_GATEWAY_ADDRESS';
-        $addresses = Store::get($key);
-        if(empty($addresses))
-        {
-            $addresses = array($address);
-        }
-        else
-        {
-            $addresses[] = $address;
-        }
-        Store::set($key, $addresses);
-    }
-    
-    /**
-     * 接收Udp数据
-     * 如果数据超过一个udp包长,需要业务自己解析包体,判断数据是否全部到达
-     * @param resource $socket
-     * @param $null_one $flag
-     * @param $null_two $base
-     * @return void
-     */
-    public function recvUdp($socket, $null_one = null, $null_two = null)
-    {
-        $data = stream_socket_recvfrom($socket , self::MAX_UDP_PACKEG_SIZE, 0, $address);
-        // 惊群效应
-        if(false === $data || empty($address))
-        {
-            return false;
-        }
-         
-        $this->currentClientAddress = $address;
-       
-        $this->innerDealProcess($data);
-    }
-    
-    protected function initWorkerAddresses()
-    {
-        $this->workerAddresses = Man\Core\Lib\Config::get($this->workerName.'.game_worker');
-        if(!$this->workerAddresses)
-        {
-            $this->notice($this->workerName.'game_worker not set');
-        }
-    }
-    
-    public function dealInput($recv_str)
-    {
-        return GameBuffer::input($recv_str, $this->data);
-    }
-
-    public function innerDealProcess($recv_str)
-    {
-        $data = GameBuffer::decode($recv_str);
-        if($data['cmd'] != GameBuffer::CMD_GATEWAY)
-        {
-            $this->notice('gateway inner pack err data:' .$recv_str . ' serialize:' . serialize($data) );
-            return;
-        }
-        switch($data['sub_cmd'])
-        {
-            case GameBuffer::SCMD_SEND_DATA:
-                return $this->sendToUid($data['to_uid'], $recv_str);
-               
-            case GameBuffer::SCMD_KICK_UID:
-                return $this->closeClientByUid($data['to_uid'] );
-                
-            case GameBuffer::SCMD_KICK_ADDRESS:
-                $fd = (int)trim($data['body']);
-                $uid = $this->getUidByFd($fd);
-                if($uid)
-                {
-                    return $this->closeClientByUid($uid);
-                }
-                return;
-            case GameBuffer::SCMD_BROADCAST:
-                return $this->broadCast($recv_str);
-            case GameBuffer::SCMD_CONNECT_SUCCESS:
-                $socket_id = $data['from_uid'];
-                $uid = $data['to_uid'];
-                // 查看是否已经绑定uid
-                $binded_uid = $this->getUidByFd($socket_id);
-                if($binded_uid)
-                {
-                    $this->notice('notify connection success fail ' . $socket_id . ' already binded data:'.serialize($data));
-                    return;
-                }
-                $this->uidConnMap[$uid] = $socket_id;
-                $this->connUidMap[$socket_id] = $uid;
-                $this->sendToUid($uid, $recv_str);
-                return;
-            default :
-                $this->notice('gateway inner pack sub_cmd err data:' .$recv_str . ' serialize:' . serialize($data) );
-        }
-    }
-    
-    protected function broadCast($bin_data)
-    {
-        foreach($this->uidConnMap as $uid=>$conn)
-        {
-            $this->sendToUid($uid, $bin_data);
-        }
-    }
-    
-    public function closeClientByUid($uid)
-    {
-        $fd = $this->getFdByUid($uid);
-        if($fd)
-        {
-            unset($this->uidConnMap[$uid], $this->connUidMap[$fd]);
-            parent::closeClient($fd);
-        }
-    }
-    
-    protected function getFdByUid($uid)
-    {
-        if(isset($this->uidConnMap[$uid]))
-        {
-            return $this->uidConnMap[$uid];
-        }
-        return 0;
-    }
-    
-    protected function getUidByFd($fd)
-    {
-        if(isset($this->connUidMap[$fd]))
-        {
-            return $this->connUidMap[$fd];
-        }
-        return 0;
-    }
-    
-    public function sendToUid($uid, $bin_data)
-    {
-        if(!isset($this->uidConnMap[$uid]))
-        {
-            return false;
-        }
-        $send_len = fwrite($this->connections[$this->uidConnMap[$uid]], $bin_data);
-        return $send_len == strlen($bin_data);
-    }
-
-    protected function closeClient($fd)
-    {
-        if($uid = $this->getUidByFd($fd))
-        {
-            $buf = new GameBuffer();
-            $buf->header['cmd'] = GameBuffer::CMD_SYSTEM;
-            $buf->header['sub_cmd'] = GameBuffer::SCMD_ON_CLOSE;
-            $buf->header['from_uid'] = $uid;
-            $this->sendToWorker($buf->getBuffer());
-            unset($this->uidConnMap[$uid], $this->connUidMap[$fd]);
-        }
-        parent::closeClient($fd);
-    }
-    
-    public function dealProcess($recv_str)
-    {
-        // 判断用户是否认证过
-        $from_uid = $this->getUidByFd($this->currentDealFd);
-        if(!$from_uid)
-        {
-            // 没传sid
-            if(empty($this->data['body']))
-            {
-                $this->notice("onConnect miss sid ip:".$this->getRemoteIp(). " data[".serialize($this->data)."]");
-                $this->closeClient($this->currentDealFd);
-                return;
-            }
-            // 发送onconnet事件包,包体是sid
-            $on_buffer = new GameBuffer();
-            $on_buffer->header['cmd'] = GameBuffer::CMD_SYSTEM;
-            $on_buffer->header['sub_cmd'] = GameBuffer::SCMD_ON_CONNECT;
-            // 用from_uid来临时存储socketid
-            $on_buffer->header['from_uid'] = $this->currentDealFd;
-            // 用to_uid来临时存储通信端口号
-            $on_buffer->header['to_uid'] = $this->lanPort;
-            $on_buffer->body = $this->data['body'];
-            $this->sendToWorker($on_buffer->getBuffer());
-            return;
-        }
-        
-        // 认证过
-        $this->fillFromUid($recv_str, $from_uid);
-        $this->sendToWorker($recv_str);
-    }
-    
-    // 讲协议的from_uid填充为正确的值
-    protected function fillFromUid(&$bin_data, $from_uid)
-    {
-        // from_uid在包头的12-15字节
-        $bin_data = substr_replace($bin_data, pack('I', $from_uid), 11, 4);
-    }
-    
-    protected function sendToWorker($bin_data)
-    {
-        $client = stream_socket_client($this->workerAddresses[array_rand($this->workerAddresses)]);
-        $len = stream_socket_sendto($client, $bin_data);
-        return $len == strlen($bin_data);
-    }
-    
-    protected function notice($str, $display=true)
-    {
-        $str = 'Worker['.get_class($this).']:'."$str ip:".$this->getRemoteIp();
-        Man\Core\Lib\Log::add($str);
-        if($display && Man\Core\Lib\Config::get('workerman.debug') == 1)
-        {
-            echo $str."\n";
-        }
-    }
-}

+ 0 - 50
workers/GameWorker.php

@@ -1,50 +0,0 @@
-<?php
-/**
- * 
- * 处理具体逻辑
- * 
- * @author walkor <worker-man@qq.com>
- * 
- */
-require_once WORKERMAN_ROOT_DIR . 'man/Core/SocketWorker.php';
-require_once WORKERMAN_ROOT_DIR . 'applications/Game/Protocols/GameBuffer.php';
-require_once WORKERMAN_ROOT_DIR . 'applications/Game/Event.php';
-require_once WORKERMAN_ROOT_DIR . 'applications/Game/User.php';
-
-class GameWorker extends Man\Core\SocketWorker
-{
-    protected $data = array();
-    public function dealInput($recv_str)
-    {
-        return GameBuffer::input($recv_str, $this->data); 
-    }
-
-    public function dealProcess($recv_str)
-    {
-        if(!isset(GameBuffer::$cmdMap[$this->data['cmd']]) || !isset(GameBuffer::$scmdMap[$this->data['sub_cmd']]))
-        {
-            $this->notice('cmd err ' . serialize($this->data) );
-            return;
-        }
-        $class = GameBuffer::$cmdMap[$this->data['cmd']];
-        $method = GameBuffer::$scmdMap[$this->data['sub_cmd']];
-        if(!method_exists($class, $method))
-        {
-            if($class == 'System')
-            {
-                switch($this->data['sub_cmd'])
-                {
-                    case GameBuffer::SCMD_ON_CONNECT:
-                        call_user_func_array(array('Event', 'onConnect'), array('udp://'.$this->getRemoteIp().':'.$this->data['to_uid'], $this->data['from_uid'], $this->data['body']));
-                        return;
-                    case GameBuffer::SCMD_ON_CLOSE:
-                        call_user_func_array(array('Event', 'onClose'), array('udp://'.$this->getRemoteIp().':'.$this->data['to_uid'], $this->data['from_uid']));
-                        return; 
-                }
-            }
-            $this->notice("cmd err $class::$method not exists");
-            return;
-        }
-        call_user_func_array(array($class, $method),  array($this->data));
-    }
-}