walkor 12 년 전
부모
커밋
a9964a61bd

+ 1 - 1
conf/conf.d/GameGateway.conf

@@ -1,4 +1,4 @@
-listen = tcp://127.0.0.1:8282
+listen = tcp://115.28.44.100:8282
 persistent_connection = 1
 start_workers = 5
 user = www-data

+ 8 - 0
man/Core/AbstractWorker.php

@@ -34,6 +34,13 @@ abstract class AbstractWorker
     const MSG_TYPE_FILE_MONITOR = 2;
     
     /**
+     * worker名称
+     * @var string
+     */
+    protected $workerName = __CLASS__;
+    
+    
+    /**
      * worker监听端口的Socket
      * @var resource
      */
@@ -58,6 +65,7 @@ abstract class AbstractWorker
      */
     public function __construct()
     {
+        $this->workerName = get_class($this);
         $this->installSignal();
         $this->addShutdownHook();
     }

+ 17 - 19
man/Core/Events/Libevent.php

@@ -4,7 +4,6 @@ require_once WORKERMAN_ROOT_DIR . 'man/Core/Events/interfaces.php';
 /**
  * 
  * libevent事件轮询库的封装
- * Worker类事件的轮询库
  * 
  * @author walkor <worker-man@qq.com>
  */
@@ -43,25 +42,25 @@ class Libevent implements BaseEvent
      */
     public function add($fd, $flag, $func, $args = null)
     {
-        $event_key = (int)$fd;
+        $fd_key = (int)$fd;
         
         if ($flag == self::EV_SIGNAL)
         {
             $real_flag = EV_SIGNAL | EV_PERSIST;
             // 创建一个用于监听的event
-            $this->eventSignal[$event_key] = event_new();
+            $this->eventSignal[$fd_key] = event_new();
             // 设置监听处理函数
-            if(!event_set($this->eventSignal[$event_key], $fd, $real_flag, $func, $args))
+            if(!event_set($this->eventSignal[$fd_key], $fd, $real_flag, $func, $args))
             {
                 return false;
             }
             // 设置event base
-            if(!event_base_set($this->eventSignal[$event_key], $this->eventBase))
+            if(!event_base_set($this->eventSignal[$fd_key], $this->eventBase))
             {
                 return false;
             }
             // 添加事件
-            if(!event_add($this->eventSignal[$event_key]))
+            if(!event_add($this->eventSignal[$fd_key]))
             {
                 return false;
             }
@@ -71,22 +70,22 @@ class Libevent implements BaseEvent
         $real_flag = EV_READ | EV_PERSIST;
         
         // 创建一个用于监听的event
-        $this->allEvents[$event_key][$flag] = event_new();
+        $this->allEvents[$fd_key][$flag] = event_new();
         
         // 设置监听处理函数
-        if(!event_set($this->allEvents[$event_key][$flag], $fd, $real_flag, $func, $args))
+        if(!event_set($this->allEvents[$fd_key][$flag], $fd, $real_flag, $func, $args))
         {
             return false;
         }
         
         // 设置event base
-        if(!event_base_set($this->allEvents[$event_key][$flag], $this->eventBase))
+        if(!event_base_set($this->allEvents[$fd_key][$flag], $this->eventBase))
         {
             return false;
         }
         
         // 添加事件
-        if(!event_add($this->allEvents[$event_key][$flag]))
+        if(!event_add($this->allEvents[$fd_key][$flag]))
         {
             return false;
         }
@@ -100,35 +99,34 @@ class Libevent implements BaseEvent
      */
     public function del($fd ,$flag)
     {
-        $event_key = (int)$fd;
+        $fd_key = (int)$fd;
         switch($flag)
         {
             // 读事件
             case \Man\Core\Events\BaseEvent::EV_READ:
             case \Man\Core\Events\BaseEvent::EV_WRITE:
-                if(isset($this->allEvents[$event_key][$flag]))
+                if(isset($this->allEvents[$fd_key][$flag]))
                 {
-                    event_del($this->allEvents[$event_key][$flag]);
+                    event_del($this->allEvents[$fd_key][$flag]);
                 }
-                unset($this->allEvents[$event_key][$flag]);
+                unset($this->allEvents[$fd_key][$flag]);
             case  \Man\Core\Events\BaseEvent::EV_SIGNAL:
-                if(isset($this->eventSignal[$event_key]))
+                if(isset($this->eventSignal[$fd_key]))
                 {
-                    event_del($this->eventSignal[$event_key]);
+                    event_del($this->eventSignal[$fd_key]);
                 }
-                unset($this->eventSignal[$event_key]);
+                unset($this->eventSignal[$fd_key]);
         }
         return true;
     }
 
     /**
-     * 事件轮训主循环
+     * 轮训主循环
      * @see \Man\Core\Events\BaseEvent::loop()
      */
     public function loop()
     {
         event_base_loop($this->eventBase);
     }
-    
 }
 

+ 19 - 20
man/Core/Events/Select.php

@@ -4,7 +4,6 @@ require_once WORKERMAN_ROOT_DIR . 'man/Core/Events/interfaces.php';
 /**
  * 
  * select 轮询封装
- * 目前master进程使用这个库
  * 如果没有其它可用库worker进程也会自动使用该库
  * 
 * @author walkor <worker-man@qq.com>
@@ -88,22 +87,22 @@ class Select implements BaseEvent
     public function add($fd, $flag, $func, $args = null)
     {
         // key
-        $event_key = (int)$fd;
+        $fd_key = (int)$fd;
         switch ($flag)
         {
             // 可读事件
             case self::EV_READ:
-                $this->allEvents[$event_key][$flag] = array('args'=>$args, 'func'=>$func, 'fd'=>$fd);
-                $this->readFds[$event_key] = $fd;
+                $this->allEvents[$fd_key][$flag] = array('args'=>$args, 'func'=>$func, 'fd'=>$fd);
+                $this->readFds[$fd_key] = $fd;
                 break;
             // 写事件 目前没用到,未实现
             case self::EV_WRITE:
-                $this->allEvents[$event_key][$flag] = array('args'=>$args, 'func'=>$func, 'fd'=>$fd);
-                $this->writeFds[$event_key] = $fd;
+                $this->allEvents[$fd_key][$flag] = array('args'=>$args, 'func'=>$func, 'fd'=>$fd);
+                $this->writeFds[$fd_key] = $fd;
                 break;
             // 信号处理事件
             case self::EV_SIGNAL:
-                $this->signalEvents[$event_key][$flag] = array('args'=>$args, 'func'=>$func, 'fd'=>$fd);
+                $this->signalEvents[$fd_key][$flag] = array('args'=>$args, 'func'=>$func, 'fd'=>$fd);
                 pcntl_signal($fd, array($this, 'signalHandler'));
                 break;
         }
@@ -126,28 +125,28 @@ class Select implements BaseEvent
      */
     public function del($fd ,$flag)
     {
-        $event_key = (int)$fd;
+        $fd_key = (int)$fd;
         switch ($flag)
         {
             // 可读事件
             case self::EV_READ:
-                unset($this->allEvents[$event_key][$flag], $this->readFds[$event_key]);
-                if(empty($this->allEvents[$event_key]))
+                unset($this->allEvents[$fd_key][$flag], $this->readFds[$fd_key]);
+                if(empty($this->allEvents[$fd_key]))
                 {
-                    unset($this->allEvents[$event_key]);
+                    unset($this->allEvents[$fd_key]);
                 }
                 break;
             // 可写事件
             case self::EV_WRITE:
-                unset($this->allEvents[$event_key][$flag], $this->writeFds[$event_key]);
-                if(empty($this->allEvents[$event_key]))
+                unset($this->allEvents[$fd_key][$flag], $this->writeFds[$fd_key]);
+                if(empty($this->allEvents[$fd_key]))
                 {
-                    unset($this->allEvents[$event_key]);
+                    unset($this->allEvents[$fd_key]);
                 }
                 break;
             // 信号
             case self::EV_SIGNAL:
-                unset($this->signalEvents[$event_key]);
+                unset($this->signalEvents[$fd_key]);
                 pcntl_signal($fd, SIG_IGN);
                 break;
         }
@@ -186,18 +185,18 @@ class Select implements BaseEvent
             // 检查所有可读描述符
             foreach($read as $fd)
             {
-                $event_key = (int) $fd;
-                if(isset($this->allEvents[$event_key][self::EV_READ]))
+                $fd_key = (int) $fd;
+                if(isset($this->allEvents[$fd_key][self::EV_READ]))
                 {
-                    call_user_func_array($this->allEvents[$event_key][self::EV_READ]['func'], array($this->allEvents[$event_key][self::EV_READ]['fd'], self::EV_READ,  $this->allEvents[$event_key][self::EV_READ]['args']));
+                    call_user_func_array($this->allEvents[$fd_key][self::EV_READ]['func'], array($this->allEvents[$fd_key][self::EV_READ]['fd'], self::EV_READ,  $this->allEvents[$fd_key][self::EV_READ]['args']));
                 }
             }
             
             // 检查可写描述符,没用到,暂不实现
             foreach($write as $fd)
             {
-                $event_key = (int) $fd;
-                if(isset($this->allEvents[$event_key][self::EV_WRITE]))
+                $fd_key = (int) $fd;
+                if(isset($this->allEvents[$fd_key][self::EV_WRITE]))
                 {
                     // 留空
                 }

+ 26 - 7
man/Core/Events/interfaces.php

@@ -4,29 +4,48 @@ namespace Man\Core\Events;
  * 
  * 事件轮询库的通用接口
  * 其它事件轮询库需要实现这些接口才能在这个server框架中使用
- * 目前 Select libevent libev libuv这些事件轮询库已经封装好这些接口可以直接使用
  * 
  * @author walkor <worker-man@qq.com>
  *
  */
 interface BaseEvent
 {
-    // 数据可读事件
+    /**
+     * 数据可读事件
+     * @var integer
+     */
     const EV_READ = 1;
     
-    // 数据可写事件
+    /**
+     * 数据可写事件
+     * @var integer
+     */
     const EV_WRITE = 2;
     
-    // 信号事件
+    /**
+     * 信号事件
+     * @var integer
+     */
     const EV_SIGNAL = 4;
     
-    // 事件添加
+    /**
+     * 事件添加
+     * @param resource $fd
+     * @param int $flag
+     * @param callable $func
+     */
     public function add($fd, $flag, $func);
     
-    // 事件删除
+    /**
+     * 事件删除
+     * @param resource $fd
+     * @param int $flag
+     */
     public function del($fd, $flag);
     
-    // 轮询事件
+    /**
+     * 轮询
+     */
     public function loop();
 }
 

+ 1 - 4
man/Core/Lib/Checker.php

@@ -34,8 +34,6 @@ class Checker
                         'sysvshm'   => false,
                         'sysvmsg'   => false,
                         'libevent'  => false,
-                        'ev'        => false,
-                        'uv'        => false,
                         'proctitle' => false,
         );
     
@@ -60,7 +58,7 @@ class Checker
             else
             {
                 // ev uv inotify不是必须
-                if('ev' == $ext_name || 'uv' == $ext_name || 'proctitle' == $ext_name)
+                if('proctitle' == $ext_name)
                 {
                     continue;
                 }
@@ -226,6 +224,5 @@ class Checker
         {
             exit("\n\033[31;40mYou should start the server as root\033[0m\n\n\033[31;40mServer start failed\033[0m\n\n");
         }
-        
     }
 }

+ 46 - 1
man/Core/Lib/Config.php

@@ -1,11 +1,35 @@
 <?php
 namespace Man\Core\Lib;
+/**
+ * 
+ * 配置
+ * @author walkor
+ *
+ */
 class Config
 {
+    /**
+     * 配置文件名称
+     * @var string
+     */
     public static $filename;
+    
+    /**
+     * 配置数据
+     * @var array
+     */
     public static $config = array();
+    
+    /**
+     * 势力
+     * @var instance of Config
+     */
     protected static $instances = null;
 
+    /**
+     * 构造函数
+     * @throws \Exception
+     */
     private function __construct()
      {
         $config_file = WORKERMAN_ROOT_DIR . 'conf/workerman.conf';
@@ -22,6 +46,11 @@ class Config
         }
     }
     
+    /**
+     * 解析配置文件
+     * @param string $config_file
+     * @throws \Exception
+     */
     protected static function parseFile($config_file)
     {
         $config = parse_ini_file($config_file, true);
@@ -32,6 +61,10 @@ class Config
         return $config;
     }
 
+   /**
+    * 获取实例
+    * @return \Man\Core\Lib\instance
+    */
     public static function instance()
     {
         if (!self::$instances) {
@@ -40,6 +73,11 @@ class Config
         return self::$instances;
     }
 
+    /**
+     * 获取配置
+     * @param string $uri
+     * @return mixed
+     */
     public static function get($uri)
     {
         $node = self::$config;
@@ -51,10 +89,13 @@ class Config
             }
             $node = $node[$path];
         }
-
         return $node;
     }
     
+    /**
+     * 获取所有的workers
+     * @return array
+     */
     public static function getAllWorkers()
     {
          $copy = self::$config;
@@ -62,6 +103,10 @@ class Config
          return $copy;
     }
     
+    /**
+     * 重新载入配置
+     * @return void
+     */
     public static function reload()
     {
         self::$instances = null;

+ 4 - 1
man/Core/Master.php

@@ -145,6 +145,7 @@ class Master
      */
     public static function run()
     {
+        // 输出信息
         self::notice("Server is starting ...", true);
         // 初始化
         self::init();
@@ -160,7 +161,7 @@ class Master
         self::createSocketsAndListen();
         // 创建worker进程
         self::createWorkers();
-        
+        // 输出信息
         self::notice("Server start success ...", true);
         // 标记sever状态为运行中...
         self::$serverStatus = self::STATUS_RUNNING;
@@ -198,6 +199,7 @@ class Master
      */
     public static function checkEnv()
     {
+        // 检查PID文件
         Lib\Checker::checkPidFile();
         
         // 检查扩展支持情况
@@ -262,6 +264,7 @@ class Master
     
     /**
      * 保存主进程pid
+     * @return void
      */
     public static function savePid()
     {

+ 6 - 34
man/Core/SocketWorker.php

@@ -70,12 +70,6 @@ abstract class SocketWorker extends AbstractWorker
     protected $currentClientAddress = '';
     
     /**
-     * worker的服务状态
-     * @var integer
-     */
-    protected $workerStatus = self::STATUS_RUNNING;
-    
-    /**
      * 是否是长链接,(短连接每次请求后服务器主动断开,长连接一般是客户端主动断开)
      * @var bool
      */
@@ -94,12 +88,6 @@ abstract class SocketWorker extends AbstractWorker
     protected $event = null;
     
     /**
-     * worker名称
-     * @var string
-     */
-    protected $workerName = __CLASS__;
-    
-    /**
      * 该worker进程处理多少请求后退出,0表示不自动退出
      * @var integer
      */
@@ -158,25 +146,18 @@ abstract class SocketWorker extends AbstractWorker
      * @param string $protocol
      * @return void
      */
-    public function __construct($worker_name = '')
+    public function __construct()
     {
         // worker name
-        if(!empty($worker_name))
-        {
-            $this->workerName = $worker_name;
-        }
-        else
-        {
-            $this->workerName = get_class($this);
-        }
+        $this->workerName = get_class($this);
         
         // 是否开启长连接
-        $this->isPersistentConnection = (bool)Lib\Config::get( $worker_name . '.persistent_connection');
+        $this->isPersistentConnection = (bool)Lib\Config::get( $this->workerName . '.persistent_connection');
         // 最大请求数,如果没有配置则使用PHP_INT_MAX
-        $this->maxRequests = (int)Lib\Config::get( $worker_name . '.max_requests');
+        $this->maxRequests = (int)Lib\Config::get( $this->workerName . '.max_requests');
         $this->maxRequests = $this->maxRequests <= 0 ? PHP_INT_MAX : $this->maxRequests;
 
-        $preread_length = (int)Lib\Config::get( $worker_name . '.preread_length');
+        $preread_length = (int)Lib\Config::get( $this->workerName . '.preread_length');
         if($preread_length > 0)
         {
             $this->prereadLength = $preread_length;
@@ -414,8 +395,6 @@ abstract class SocketWorker extends AbstractWorker
             }
             catch(\Exception $e)
             {
-                // 关闭闹钟
-                //pcntl_alarm(0);
                 $this->notice('CODE:' . $e->getCode() . ' MESSAGE:' . $e->getMessage()."\n".$e->getTraceAsString()."\nCLIENT_IP:".$this->getRemoteIp()."\nBUFFER:[".var_export($this->recvBuffers[$fd]['buf'],true)."]\n");
                 $this->statusInfo['throw_exception'] ++;
                 $this->sendToClient($e->getMessage());
@@ -484,12 +463,6 @@ abstract class SocketWorker extends AbstractWorker
      */
     protected function installSignal()
     {
-        // 如果是由worker脚本启动则不安装信号
-        if(!defined('WORKERMAN_PID_FILE'))
-        {
-            return;
-        }
-        
         // 闹钟信号
         $this->event->add(SIGALRM, Events\BaseEvent::EV_SIGNAL, array($this, 'signalHandler'), SIGALRM);
         // 终止进程信号
@@ -564,8 +537,7 @@ abstract class SocketWorker extends AbstractWorker
             return true;
         }
         // udp 直接发送,要求数据包不能超过65515
-        $len = stream_socket_sendto($this->mainSocket, $str_to_send, 0, $this->currentClientAddress);
-        return $len == strlen($str_to_send);
+       return strlen($str_to_send) == stream_socket_sendto($this->mainSocket, $str_to_send, 0, $this->currentClientAddress);
     }
     
     /**

+ 2 - 2
man/Protocols/Buffer.php

@@ -1,7 +1,7 @@
 <?php 
 namespace Man\Protocols;
 /**
- * 通用的server协议,二进制协议
+ * 二进制协议
  * 
  * struct BufferProtocol
  * {
@@ -13,7 +13,7 @@ namespace Man\Protocols;
  *     unsigned int        from_uid,//来自用户uid
  *     unsigned int        to_uid,//发往的uid
  *     unsigned int       pack_len,//包长
- *     char[pack_length-15] body//包体
+ *     char[pack_length-HEAD_LEN] body//包体
  * }
  * 
  * @author walkor <worker-man@qq.com>

+ 1 - 3
workers/GameGateway.php

@@ -169,9 +169,7 @@ class GameGateway extends Man\Core\SocketWorker
                 }
                 $this->uidConnMap[$uid] = $socket_id;
                 $this->connUidMap[$socket_id] = $uid;
-                $buf = new GameBuffer();
-                $buf->body = 'connect success';
-                $this->sendToUid($uid, $buf->getBuffer());
+                $this->sendToUid($uid, $recv_str);
                 return;
             default :
                 $this->notice('gateway inner pack sub_cmd err data:' .$recv_str . ' serialize:' . serialize($data) );

+ 1 - 1
workers/Monitor.php

@@ -341,7 +341,7 @@ class Monitor extends Man\Core\SocketWorker
         {
             $pid = $message['pid'];
             $worker_name = $message['worker_name'];
-            $address = \Man\Core\Lib\Config::get($this->workerName . '.listen');
+            $address = \Man\Core\Lib\Config::get($worker_name . '.listen');
             if(!$address)
             {
                 $address = '';