ソースを参照

add workerman admin

walkor 12 年 前
コミット
23264c65ce

+ 29 - 17
applications/Common/Protocols/SimpleHttp.php → applications/Common/Protocols/Http.php

@@ -1,11 +1,11 @@
 <?php 
-
+namespace  App\Common\Protocols;
 /**
  * http 协议解析 相关
- * 简单的实现 不支持header cookie
  * @author walkor <worker-man@qq.com>
  * */
-class SimpleHttp{
+class Http
+{
     
     /**
      * 构造函数
@@ -85,9 +85,7 @@ class SimpleHttp{
                 'HTTP_COOKIE'    => '',
                 );
         
-        $_POST = array();
-        $_GET = array();
-        $GLOBALS['HTTP_RAW_POST_DATA'] = array();
+        $_POST = $_GET = $_COOKIE = $REQUEST = $GLOBALS['HTTP_RAW_POST_DATA'] = array();
         
         // 将header分割成数组
         $header_data = explode("\r\n", $data);
@@ -134,6 +132,10 @@ class SimpleHttp{
                 {
                     $_SERVER['HTTP_COOKIE'] = $tmp[1];
                 }
+                if($_SERVER['HTTP_COOKIE'])
+                {
+                    parse_str(str_replace('; ', '&', $_SERVER['HTTP_COOKIE']), $_COOKIE);
+                }
             }
         }
         
@@ -170,15 +172,25 @@ class SimpleHttp{
     }
     
     /**
-     * 
-     * @param string $name
-     * @param string/int $value
-     * @param int $expire
-     */
-    public static function setcookie($name, $value='', $expire=0)
-    {
-        // 待完善
-    }
+	 * Set the cookie
+	 * @param string $name         Name of cookie
+	 * @param string $value        Value
+	 * @param integer $maxage      . Optional. Max-Age. Default is 0.
+	 * @param string $path         . Optional. Path. Default is empty string.
+	 * @param bool|string $domain  . Optional. Secure. Default is false.
+	 * @param boolean $secure      . Optional. HTTPOnly. Default is false.
+	 * @param bool $HTTPOnly
+	 * @return void
+	 */
+	public static function setcookie($name, $value = '', $maxage = 0, $path = '', $domain = '', $secure = false, $HTTPOnly = false) {
+		self::header(
+			'Set-Cookie: ' . $name . '=' . rawurlencode($value)
+			. (empty($domain) ? '' : '; Domain=' . $domain)
+			. (empty($maxage) ? '' : '; Max-Age=' . $maxage)
+			. (empty($path) ? '' : '; Path=' . $path)
+			. (!$secure ? '' : '; Secure')
+			. (!$HTTPOnly ? '' : '; HttpOnly'), false);
+	}
     
     /**
      * 清除header
@@ -190,7 +202,7 @@ class SimpleHttp{
     }
     
     /**
-     * 打包http协议,用于返回数据给nginx
+     * 打包http协议,用于返回数据
      * 
      * @param string $data
      * @return string
@@ -198,7 +210,7 @@ class SimpleHttp{
     public static function encode($data)
     {
         // header
-        $header = "Server: PHPServer/1.0\r\nContent-Length: ".strlen($data)."\r\n";
+        $header = "Server: WorkerMan/1.0\r\nContent-Length: ".strlen($data)."\r\n";
         
         // 没有Content-Type默认给个
         if(!isset(self::$header['Content-Type']))

+ 24 - 0
applications/Common/Protocols/Session.php

@@ -0,0 +1,24 @@
+<?php 
+namespace  App\Common\Protocols;
+/**
+ * http session 相关
+ * @author walkor <worker-man@qq.com>
+ * */
+class Session{
+    
+    /**
+     * 构造函数
+     */
+    private  function __construct(){}
+    
+    /**
+     * http头
+     * @var array
+     */
+    public static $started = false;
+    
+   public static function start()
+   {
+       
+   }
+}

+ 13 - 0
conf/conf.d/WorkerManAdmin.conf

@@ -0,0 +1,13 @@
+;WorkerMan管理后台
+;监听的端口
+listen = tcp://0.0.0.0:3000
+;http 协议 这里设置成短连接
+persistent_connection = 0
+;启动多少worker进程
+start_workers=1
+;接收多少请求后退出
+max_requests=1000
+;以哪个用户运行该worker进程
+user=root
+;socket有数据可读的时候预读长度,一般设置为应用层协议包头的长度
+preread_length=84000

+ 3 - 3
workers/Monitor.php

@@ -441,7 +441,7 @@ class Monitor extends Man\Core\SocketWorker
         
         $ip = $this->getIp();
         
-        $this->sendSms('告警消息 PHPServer框架监控 ip:'.$ip.' 主进程意外退出');
+        $this->sendSms('告警消息 WorkerMan框架监控 ip:'.$ip.' 主进程意外退出');
         
         // 记录这次告警时间
         self::$lastWarningTimeMap[self::WARNING_MASTER_DEAD] = $time_now;
@@ -456,7 +456,7 @@ class Monitor extends Man\Core\SocketWorker
         $status = $this->getMasterStatus();
         if(empty($status))
         {
-            $this->notice("can not get master status");
+            $this->notice("can not get master status" , false);
             return;
         }
         $status = $status['worker_exit_code'];
@@ -553,7 +553,7 @@ class Monitor extends Man\Core\SocketWorker
     
         $ip = $this->getIp();
     
-        $this->sendSms('告警消息 PHPServer框架监控 '.$ip.' '.$worker_name.'进程频繁退出 退出次数'.$exit_count.' 退出状态码:'.$status);
+        $this->sendSms('告警消息 WorkerMan框架监控 '.$ip.' '.$worker_name.'进程频繁退出 退出次数'.$exit_count.' 退出状态码:'.$status);
     
         // 记录这次告警时间
         self::$lastWarningTimeMap[self::WARNING_TOO_MANY_WORKERS_EXIT] = $time_now;

+ 38 - 0
workers/WorkerManAdmin.php

@@ -0,0 +1,38 @@
+<?php
+require_once WORKERMAN_ROOT_DIR . 'man/Core/SocketWorker.php';
+require_once WORKERMAN_ROOT_DIR . 'applications/Common/Protocols/Http.php';
+require_once WORKERMAN_ROOT_DIR . 'applications/Common/Protocols/Session.php';
+
+/**
+ * 
+ *  WorkerMan 管理后台
+ *  HTTP协议
+ *  
+ * @author walkor <worker-man@qq.com>
+ */
+class WorkerManAdmin extends Man\Core\SocketWorker
+{
+    /**
+     * 确定数据是否接收完整
+     * @see Man\Core.SocketWorker::dealInput()
+     */
+    public function dealInput($recv_str)
+    {
+        return App\Common\Protocols\Http::dealInput($recv_str); 
+    }
+
+    /**
+     * 数据接收完整后处理业务逻辑
+     * @see Man\Core.SocketWorker::dealProcess()
+     */
+    public function dealProcess($recv_str)
+    {
+        /**
+         * 解析http协议,生成$_POST $_GET $_COOKIE
+         */
+        App\Common\Protocols\Http::decode($recv_str);
+        
+        var_dump($_GET,$_POST,$_COOKIE);
+        $this->sendToClient(App\Common\Protocols\Http::encode(var_export($_COOKIE, true)));
+    }
+}