Browse Source

workerman主程序 判断客户端是否断开

walkor 11 năm trước cách đây
mục cha
commit
543b631e62

+ 60 - 3
applications/Common/Protocols/Http.php → applications/Common/Protocols/Http/Http.php

@@ -1,5 +1,5 @@
 <?php 
-namespace  App\Common\Protocols;
+namespace  App\Common\Protocols\Http;
 
 /**
  * 判断http协议的数据包是否完整
@@ -145,6 +145,12 @@ function http_start($http_string, $SERVER = array())
             case 'referer':
                 $_SERVER['HTTP_REFERER'] = $value;
                 break;
+            case 'if-modified-since':
+                $_SERVER['HTTP_IF_MODIFIED_SINCE'] = $value;
+                break;
+            case 'if-none-match':
+                $_SERVER['HTTP_IF_NONE_MATCH'] = $value;
+                break;
         }
     }
     
@@ -252,6 +258,12 @@ function header($content, $replace = true, $http_response_code = 0)
             return false;
         }
     }
+    
+    if(isset(HttpCache::$codes[$http_response_code]))
+    {
+        HttpCache::$header['Http-Code'] = 'HTTP/1.1 ' .  HttpCache::$codes[$http_response_code];
+    }
+    
     if($key == 'Set-Cookie')
     {
         HttpCache::$header[$key][] = $content;
@@ -262,7 +274,7 @@ function header($content, $replace = true, $http_response_code = 0)
     }
     if('location' == strtolower($key))
     {
-        header("HTTP/1.1 302 Moved Temporarily");
+        header('HTTP/1.1 302 Moved Temporarily');
     }
     return true;
 }
@@ -365,7 +377,7 @@ function session_write_close()
     {
         return \session_write_close();
     }
-    if(HttpCache::$instance->sessionStarted && !empty($_SESSION))
+    if(!empty(HttpCache::$instance->sessionStarted) && !empty($_SESSION))
     {
        $session_str = session_encode();
        if($session_str && HttpCache::$instance->sessionFile)
@@ -400,6 +412,51 @@ function jump_exit($msg = '')
  */
 class HttpCache
 {
+    public static $codes = array(
+            100 => 'Continue',
+            101 => 'Switching Protocols',
+            200 => 'OK',
+            201 => 'Created',
+            202 => 'Accepted',
+            203 => 'Non-Authoritative Information',
+            204 => 'No Content',
+            205 => 'Reset Content',
+            206 => 'Partial Content',
+            300 => 'Multiple Choices',
+            301 => 'Moved Permanently',
+            302 => 'Found',
+            303 => 'See Other',
+            304 => 'Not Modified',
+            305 => 'Use Proxy',
+            306 => '(Unused)',
+            307 => 'Temporary Redirect',
+            400 => 'Bad Request',
+            401 => 'Unauthorized',
+            402 => 'Payment Required',
+            403 => 'Forbidden',
+            404 => 'Not Found',
+            405 => 'Method Not Allowed',
+            406 => 'Not Acceptable',
+            407 => 'Proxy Authentication Required',
+            408 => 'Request Timeout',
+            409 => 'Conflict',
+            410 => 'Gone',
+            411 => 'Length Required',
+            412 => 'Precondition Failed',
+            413 => 'Request Entity Too Large',
+            414 => 'Request-URI Too Long',
+            415 => 'Unsupported Media Type',
+            416 => 'Requested Range Not Satisfiable',
+            417 => 'Expectation Failed',
+            422 => 'Unprocessable Entity',
+            423 => 'Locked',
+            500 => 'Internal Server Error',
+            501 => 'Not Implemented',
+            502 => 'Bad Gateway',
+            503 => 'Service Unavailable',
+            504 => 'Gateway Timeout',
+            505 => 'HTTP Version Not Supported',
+      );
     public static $instance = null;
     public static $header = array();
     public static $sessionPath = '';

+ 80 - 0
applications/Common/Protocols/Http/mime.types

@@ -0,0 +1,80 @@
+
+types {
+    text/html                             html htm shtml;
+    text/css                              css;
+    text/xml                              xml;
+    image/gif                             gif;
+    image/jpeg                            jpeg jpg;
+    application/x-javascript              js;
+    application/atom+xml                  atom;
+    application/rss+xml                   rss;
+
+    text/mathml                           mml;
+    text/plain                            txt;
+    text/vnd.sun.j2me.app-descriptor      jad;
+    text/vnd.wap.wml                      wml;
+    text/x-component                      htc;
+
+    image/png                             png;
+    image/tiff                            tif tiff;
+    image/vnd.wap.wbmp                    wbmp;
+    image/x-icon                          ico;
+    image/x-jng                           jng;
+    image/x-ms-bmp                        bmp;
+    image/svg+xml                         svg svgz;
+    image/webp                            webp;
+
+    application/java-archive              jar war ear;
+    application/mac-binhex40              hqx;
+    application/msword                    doc;
+    application/pdf                       pdf;
+    application/postscript                ps eps ai;
+    application/rtf                       rtf;
+    application/vnd.ms-excel              xls;
+    application/vnd.ms-powerpoint         ppt;
+    application/vnd.wap.wmlc              wmlc;
+    application/vnd.google-earth.kml+xml  kml;
+    application/vnd.google-earth.kmz      kmz;
+    application/x-7z-compressed           7z;
+    application/x-cocoa                   cco;
+    application/x-java-archive-diff       jardiff;
+    application/x-java-jnlp-file          jnlp;
+    application/x-makeself                run;
+    application/x-perl                    pl pm;
+    application/x-pilot                   prc pdb;
+    application/x-rar-compressed          rar;
+    application/x-redhat-package-manager  rpm;
+    application/x-sea                     sea;
+    application/x-shockwave-flash         swf;
+    application/x-stuffit                 sit;
+    application/x-tcl                     tcl tk;
+    application/x-x509-ca-cert            der pem crt;
+    application/x-xpinstall               xpi;
+    application/xhtml+xml                 xhtml;
+    application/zip                       zip;
+
+    application/octet-stream              bin exe dll;
+    application/octet-stream              deb;
+    application/octet-stream              dmg;
+    application/octet-stream              eot;
+    application/octet-stream              iso img;
+    application/octet-stream              msi msp msm;
+
+    audio/midi                            mid midi kar;
+    audio/mpeg                            mp3;
+    audio/ogg                             ogg;
+    audio/x-m4a                           m4a;
+    audio/x-realaudio                     ra;
+
+    video/3gpp                            3gpp 3gp;
+    video/mp4                             mp4;
+    video/mpeg                            mpeg mpg;
+    video/quicktime                       mov;
+    video/webm                            webm;
+    video/x-flv                           flv;
+    video/x-m4v                           m4v;
+    video/x-mng                           mng;
+    video/x-ms-asf                        asx asf;
+    video/x-ms-wmv                        wmv;
+    video/x-msvideo                       avi;
+}

+ 2 - 2
man/Core/SocketWorker.php

@@ -356,11 +356,11 @@ abstract class SocketWorker extends AbstractWorker
         $this->currentDealFd = $fd;
         $buffer = stream_socket_recvfrom($connection, $this->recvBuffers[$fd]['remain_len']);
         // 出错了
-        if('' == $buffer)
+        if('' == $buffer && '' == ($buffer = fread($connection, $this->recvBuffers[$fd]['remain_len'])))
         {
             if(!feof($connection))
             {
-                continue;
+                return;
             }
             
             // 客户端提前断开链接

+ 88 - 0
workers/StatisticWorker.php

@@ -0,0 +1,88 @@
+<?php 
+require_once WORKERMAN_ROOT_DIR . 'man/Core/SocketWorker.php';
+/**
+ * 
+* @author walkor <worker-man@qq.com>
+ */
+class StatisticWorker extends Man\Core\SocketWorker
+{
+    public function dealInput($recv_str)
+    {
+        
+    }
+    
+    
+    public function dealProcess($recv_str)
+    {
+        
+    }
+} 
+
+/**
+ * 
+ * struct statisticPortocol
+ * {
+ *     unsigned char module_name_len;
+ *     unsigned char interface_name_len;
+ *     float cost_time_ms;
+ *     unsigned char $success;
+ *     int code;
+ *     unsigned short msg_len;
+ *     unsigned int time;
+ *     unsigned int ip;
+ *     char[module_name_len] module_name;
+ *     char[interface_name_len] interface_name;
+ *     char[msg_len] msg;
+ * }
+ *  
+ * @author valkor
+ */
+class statisticProtocol
+{
+    
+    public static function encode($module, $interface , $cost_time_ms, $success, $code = 0,$msg = '')
+    {
+        $module_len = strlen($module);
+        $interface_len = strlen($interface);
+        $bin_data = pack("CC", $module_len, $interface_len);
+        $bin_data .= self::FToN($cost_time_ms);
+        $bin_data .= pack("C", $success);
+    }
+    
+    public static function decode($bin_data)
+    {
+        
+    }
+    
+    public static function IToN($val)
+    {
+        
+    }
+    
+    public static function NToI($val)
+    {
+        $foo = unpack("N", $val);
+        if($foo[1] > 0x7fffffff)
+        {
+            $foo[1] = 0 - (($foo[1] - 1) ^ 0xffffffff); 
+        }
+        return $foo[1];
+    }
+    
+    /* Convert float from HostOrder to Network Order */
+    public static function FToN( $val )
+    {
+        $a = unpack("I",pack( "f",$val ));
+        return pack("N",$a[1] );
+    }
+    
+    /* Convert float from Network Order to HostOrder */
+    public static function NToF($val )
+    {
+        $a = unpack("N",$val);
+        $b = unpack("f",pack( "I",$a[1]));
+        return $b[1];
+    }
+    
+    
+}