stanley-king 7 vuotta sitten
vanhempi
commit
dad784dd24

+ 1 - 0
centra_srv.php

@@ -3,6 +3,7 @@
 define('BASE_ROOT_PATH',str_replace('\\','/',dirname(__FILE__)));
 
 require_once (BASE_ROOT_PATH . '/fooder.php');
+require_once (BASE_ROOT_PATH . '/helper/search/srv_base.php');
 require_once (BASE_ROOT_PATH . '/helper/search/server.php');
 require_once (BASE_ROOT_PATH . '/helper/search/processor.php');
 require_once (BASE_ROOT_PATH . '/helper/search/event_handler.php');

+ 62 - 0
helper/room/client.php

@@ -0,0 +1,62 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 2017/12/14
+ * Time: 上午11:43
+ */
+
+
+namespace room;
+
+use search\tcp_client;
+
+class _client extends tcp_client
+{
+    protected static $stInstance;
+    public function __construct()
+    {
+        parent::__construct();
+        $this->mBodyType = tcp_client::JsonType;
+    }
+
+    public static function instance()
+    {
+        if(self::$stInstance == null) {
+            self::$stInstance = new _client();
+        }
+        return self::$stInstance;
+    }
+    public function __destruct()
+    {
+        parent::__destruct();
+    }
+
+    public function remote_addr()
+    {
+        global $config;
+
+        $host = $config['room']['host'];
+        $port = $config['room']['port'];
+        return "{$host}:{$port}";
+    }
+
+    public function create()
+    {
+        $param = ["act" => 'creater','op' => '', "params" => ''];
+        $result = $this->request($param);
+        if(empty($result)) return false;
+
+        $code = intval($result['code']);
+        if($code != 200) {
+            return false;
+        }
+        else {
+            return true;
+        }
+    }
+
+    public function invite($roomid,$user) {
+
+    }
+}

+ 24 - 0
helper/room/processor.php

@@ -0,0 +1,24 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 2017/12/14
+ * Time: 下午12:07
+ */
+
+namespace room;
+
+use search\IProcessor;
+
+class processor implements IProcessor
+{
+    private $mAccess;
+    private $mFcgis;
+
+    public function
+
+    public function handle_input($bufid,$body)
+    {
+
+    }
+}

+ 29 - 0
helper/room/server.php

@@ -0,0 +1,29 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 2017/12/14
+ * Time: 上午11:58
+ */
+
+namespace room;
+use search\srv_base;
+
+class server extends srv_base
+{
+    private static $stInstance;
+
+    public static function instance()
+    {
+        if(self::$stInstance == null) {
+            self::$stInstance = new server();
+        }
+        return self::$stInstance;
+    }
+
+    protected function __construct()
+    {
+        parent::__construct();
+    }
+
+}

+ 1 - 1
helper/search/processor.php

@@ -25,7 +25,7 @@ class processor implements IProcessor
 
     const ValidateArea   = 20;
 
-    public function handle_input($body)
+    public function handle_input($bufid,$body)
     {
         if(StatesHelper::fetch_state('init')) {
             Log::record("searcher::instance reload data.",Log::DEBUG);

+ 1 - 1
helper/search/searcher.php

@@ -59,7 +59,6 @@ class category_dict extends words
 {
     private $mGcHot;   //一个GC对应一个HotID
     private $mHoTree;  //HOT的父子关系
-
     private $mHotBrand;
     private $mHotCommon;
 
@@ -220,6 +219,7 @@ class special_dict extends words
         $this->spid_names = new one_one();
         $this->init();
     }
+
     private function init()
     {
         $mod_special = Model();

+ 3 - 178
helper/search/server.php

@@ -7,31 +7,10 @@
  */
 
 namespace search;
-use Log;
 
-interface IProcessor
+class CentraHelper extends srv_base
 {
-    public function handle_input($body);
-}
-
-class CentraHelper
-{
-    const body_header_len = 10;
-    const read_time_out  = 600;
-    const write_time_out = 5;
-
-    private $mListenSocket;
-    private $mEvbase;
-    private $mEv;
-
-    private $mStreams;
-    private $mBuffers;
-    private $mContents;
-
-    private $mProcessor;
-
     private static $stInstance;
-
     public static function instance()
     {
         if(self::$stInstance == null) {
@@ -40,162 +19,8 @@ class CentraHelper
         return self::$stInstance;
     }
 
-    private function __construct()
+    protected function __construct()
     {
-        $this->mStreams = array();
-        $this->mBuffers = array();
-        $this->mContents = [];
-    }
-    public function init(IProcessor $processor)
-    {
-        $this->mProcessor = $processor;
-    }
-
-    public function run_loop($sockfd)
-    {
-        $this->mListenSocket = $sockfd;
-        Log::record("stream_set_blocking",Log::DEBUG);
-        if(stream_set_blocking($this->mListenSocket, 0) == false) {
-            Log::record("stream_set_blocking error",Log::DEBUG);
-        }
-        $this->mEvbase = event_base_new();
-        $this->mEv = event_new();
-
-        Log::record("event_set",Log::DEBUG);
-        if(event_set($this->mEv, $this->mListenSocket, EV_READ | EV_PERSIST, 'ev_accept', $this->mEvbase) == false) {
-            Log::record("event_set error EV_READ | EV_PERSIST",Log::DEBUG);
-        }
-
-        Log::record("event_base_set",Log::DEBUG);
-        if(event_base_set($this->mEv, $this->mEvbase) == false) {
-            Log::record("event_set error EV_READ | EV_PERSIST",Log::DEBUG);
-        }
-
-        Log::record("event_add",Log::DEBUG);
-        if(event_add($this->mEv) == false) {
-            Log::record("event_add error EV_READ | EV_PERSIST",Log::DEBUG);
-        }
-
-        $ret = event_base_loop($this->mEvbase);
-        Log::record("event_base_loop ret={$ret}",Log::DEBUG);
-    }
-
-    public function ev_accept($socket, $flag, $base)
-    {
-        $pid = posix_getpid();
-        Log::record("ev_accept pid={$pid} socket_fd={$socket}",Log::DEBUG);
-
-        static $bufid = 1;
-
-        $stream = stream_socket_accept($socket);
-        if($stream == false) {
-            Log::record("stream_socket_accept return false pid={$pid} socket_fd={$socket}",Log::DEBUG);
-            return;
-        }
-        Log::record("stream_socket_accept pid={$pid}  stream={$stream}",Log::DEBUG);
-
-        stream_set_blocking($stream, 0);
-        $buffer = event_buffer_new($stream, 'ev_read', NULL, 'ev_error', $bufid);
-        if($buffer == false) {
-            fclose($stream);
-            Log::record("event_buffer_new return false pid={$pid} socket_fd={$socket}",Log::DEBUG);
-            return;
-        }
-
-        event_buffer_base_set($buffer, $base);
-        event_buffer_timeout_set($buffer, self::read_time_out, self::write_time_out);
-        event_buffer_watermark_set($buffer, EV_READ, 0, 0xffffff);
-        event_buffer_priority_set($buffer, 10);
-        event_buffer_enable($buffer, EV_READ | EV_PERSIST);
-
-        $this->mStreams[$bufid] = $stream;
-        $this->mBuffers[$bufid] = $buffer;
-        $this->mContents[$bufid] = "";
-
-        $bufid++;
-        if($bufid < 0) $bufid = 1;
-    }
-
-    public function ev_read($buffer, $bufid)
-    {
-        $pid = posix_getpid();
-        Log::record("ev_read begin pid={$pid}",Log::DEBUG);
-
-        $content = &$this->mContents[$bufid];
-        while (true)
-        {
-            $read = event_buffer_read($buffer, 256);
-            if(empty($read)) {
-                break;
-            } else {
-                $content .= $read;
-            }
-        }
-
-        $start = 0;
-        $left = strlen($content);
-        do
-        {
-            if($left > self::body_header_len)
-            {
-                $len = substr($content,$start,self::body_header_len);
-                $len = intval($len);
-
-                if($left >= self::body_header_len + $len)
-                {
-                    $body = substr($content,$start + self::body_header_len,$len);
-                    if($this->mProcessor != null)
-                    {
-                        $data = $this->mProcessor->handle_input($body);
-                        $header = sprintf("%010d",strlen($data));
-                        $data = $header . $data;
-                        $ret = event_buffer_write($buffer,$data,strlen($data));
-                        if($ret == false) break;
-                    }
-
-                    $start += self::body_header_len + $len;
-                    $left = $left - self::body_header_len - $len;
-                }
-                else {
-                    break;
-                }
-            }
-            else {
-                break;
-            }
-        } while ($left > 0);
-
-        if($start > 0) {
-            $content = substr($content,$start);
-        }
-        Log::record("ev_read end pid={$pid}",Log::DEBUG);
-    }
-
-    public function ev_error($buffer, $error, $bufid)
-    {
-        $error = socket_strerror($error);
-        Log::record("ev_error id={$bufid} error={$error}",Log::DEBUG);
-
-        event_buffer_disable($buffer, EV_READ | EV_WRITE);
-        event_buffer_free($buffer);
-
-        if(array_key_exists($bufid,$this->mStreams)) {
-            stream_socket_shutdown($this->mStreams[$bufid],STREAM_SHUT_RDWR);
-            fclose($this->mStreams[$bufid]);
-            unset($this->mStreams[$bufid]);
-        }
-
-        unset($this->mBuffers[$bufid]);
-        unset($this->mContents[$bufid]);
-    }
-
-    private function remote_addr()
-    {
-        global $config;
-
-        $host = $config['searcher']['host'];
-        $port = $config['searcher']['port'];
-
-        return "{$host}:{$port}";
+        parent::__construct();
     }
 }

+ 183 - 0
helper/search/srv_base.php

@@ -0,0 +1,183 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 2017/12/14
+ * Time: 下午2:28
+ */
+
+namespace search;
+
+namespace search;
+use Log;
+
+interface IProcessor
+{
+    public function handle_input($bufid,$body);
+}
+
+abstract class srv_base
+{
+    const body_header_len = 10;
+    const read_time_out  = 600;
+    const write_time_out = 5;
+
+    private $mListenSocket;
+    protected $mEvbase;
+    protected $mEv;
+
+    protected $mStreams;
+    protected $mBuffers;
+    protected $mContents;
+    protected $mProcessor;
+
+    protected function __construct()
+    {
+        $this->mStreams = array();
+        $this->mBuffers = array();
+        $this->mContents = [];
+    }
+
+    public function init(IProcessor $processor)
+    {
+        $this->mProcessor = $processor;
+    }
+
+    public function run_loop($sockfd)
+    {
+        $this->mListenSocket = $sockfd;
+        Log::record("stream_set_blocking",Log::DEBUG);
+        if(stream_set_blocking($this->mListenSocket, 0) == false) {
+            Log::record("stream_set_blocking error",Log::DEBUG);
+        }
+        $this->mEvbase = event_base_new();
+        $this->mEv = event_new();
+
+        Log::record("event_set",Log::DEBUG);
+        if(event_set($this->mEv, $this->mListenSocket, EV_READ | EV_PERSIST, 'ev_accept', $this->mEvbase) == false) {
+            Log::record("event_set error EV_READ | EV_PERSIST",Log::DEBUG);
+        }
+
+        Log::record("event_base_set",Log::DEBUG);
+        if(event_base_set($this->mEv, $this->mEvbase) == false) {
+            Log::record("event_set error EV_READ | EV_PERSIST",Log::DEBUG);
+        }
+
+        Log::record("event_add",Log::DEBUG);
+        if(event_add($this->mEv) == false) {
+            Log::record("event_add error EV_READ | EV_PERSIST",Log::DEBUG);
+        }
+
+        $ret = event_base_loop($this->mEvbase);
+        Log::record("event_base_loop ret={$ret}",Log::DEBUG);
+    }
+
+    public function ev_accept($socket, $flag, $base)
+    {
+        $pid = posix_getpid();
+        Log::record("ev_accept pid={$pid} socket_fd={$socket}",Log::DEBUG);
+
+        static $bufid = 1;
+
+        $stream = stream_socket_accept($socket);
+        if($stream == false) {
+            Log::record("stream_socket_accept return false pid={$pid} socket_fd={$socket}",Log::DEBUG);
+            return;
+        }
+        Log::record("stream_socket_accept pid={$pid}  stream={$stream}",Log::DEBUG);
+
+        stream_set_blocking($stream, 0);
+        $buffer = event_buffer_new($stream, 'ev_read', NULL, 'ev_error', $bufid);
+        if($buffer == false) {
+            fclose($stream);
+            Log::record("event_buffer_new return false pid={$pid} socket_fd={$socket}",Log::DEBUG);
+            return;
+        }
+
+        event_buffer_base_set($buffer, $base);
+        event_buffer_timeout_set($buffer, self::read_time_out, self::write_time_out);
+        event_buffer_watermark_set($buffer, EV_READ, 0, 0xffffff);
+        event_buffer_priority_set($buffer, 10);
+        event_buffer_enable($buffer, EV_READ | EV_PERSIST);
+
+        $this->mStreams[$bufid] = $stream;
+        $this->mBuffers[$bufid] = $buffer;
+        $this->mContents[$bufid] = "";
+
+        $bufid++;
+        if($bufid < 0) $bufid = 1;
+    }
+
+    public function ev_read($buffer, $bufid)
+    {
+        $pid = posix_getpid();
+        Log::record("ev_read begin pid={$pid}",Log::DEBUG);
+
+        $content = &$this->mContents[$bufid];
+        while (true)
+        {
+            $read = event_buffer_read($buffer, 256);
+            if(empty($read)) {
+                break;
+            } else {
+                $content .= $read;
+            }
+        }
+
+        $start = 0;
+        $left = strlen($content);
+        do
+        {
+            if($left > self::body_header_len)
+            {
+                $len = substr($content,$start,self::body_header_len);
+                $len = intval($len);
+
+                if($left >= self::body_header_len + $len)
+                {
+                    $body = substr($content,$start + self::body_header_len,$len);
+                    if($this->mProcessor != null)
+                    {
+                        $data = $this->mProcessor->handle_input($bufid,$body);
+                        $header = sprintf("%010d",strlen($data));
+                        $data = $header . $data;
+                        $ret = event_buffer_write($buffer,$data,strlen($data));
+                        if($ret == false) break;
+                    }
+
+                    $start += self::body_header_len + $len;
+                    $left = $left - self::body_header_len - $len;
+                }
+                else {
+                    break;
+                }
+            }
+            else {
+                break;
+            }
+        } while ($left > 0);
+
+        if($start > 0) {
+            $content = substr($content,$start);
+        }
+        Log::record("ev_read end pid={$pid}",Log::DEBUG);
+    }
+
+    public function ev_error($buffer, $error, $bufid)
+    {
+        $error = socket_strerror($error);
+        Log::record("ev_error id={$bufid} error={$error}",Log::DEBUG);
+
+        event_buffer_disable($buffer, EV_READ | EV_WRITE);
+        event_buffer_free($buffer);
+
+        if(array_key_exists($bufid,$this->mStreams)) {
+            stream_socket_shutdown($this->mStreams[$bufid],STREAM_SHUT_RDWR);
+            fclose($this->mStreams[$bufid]);
+            unset($this->mStreams[$bufid]);
+        }
+
+        unset($this->mBuffers[$bufid]);
+        unset($this->mContents[$bufid]);
+    }
+}

+ 22 - 0
mobile/control/room.php

@@ -0,0 +1,22 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 2017/12/14
+ * Time: 上午11:36
+ */
+
+require_once(BASE_ROOT_PATH . '/helper/room/room_client.php');
+
+class roomControl extends mobileControl
+{
+    public function __construct() {
+        parent::__construct();
+    }
+
+    public function create_bargainOp()
+    {
+        $result = room\_client::instance()->create();
+
+    }
+}

+ 77 - 0
room_srv.php

@@ -0,0 +1,77 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 2017/12/14
+ * Time: 上午11:53
+ */
+
+define('BASE_ROOT_PATH',str_replace('\\','/',dirname(__FILE__)));
+
+require_once (BASE_ROOT_PATH . '/fooder.php');
+require_once(BASE_ROOT_PATH . '/helper/room/room_server.php');
+
+
+function search_work($sockfd)
+{
+    Base::run_util();
+    room\server::instance()->init(new search\processor());
+    room\server::instance()->run_loop($sockfd);
+}
+
+function fork_subprocess($count,$listen_fd)
+{
+    if (($pid = pcntl_fork()) === 0)
+    {
+        //ob_end_clean(); // Discard the output buffer and close
+        fclose(STDIN);  // Close all of the standard
+        fclose(STDOUT); // file descriptors as we
+        fclose(STDERR); // are running as a daemon.
+
+        Log::record("pid = {$pid} count = {$count} ",Log::DEBUG);
+        search_work($listen_fd);
+        exit();
+    }
+    elseif($pid === -1)
+    {
+        Log::record("pid = {$pid} count = {$count} ",Log::DEBUG);
+        die('could not fork');
+    }
+    else
+    {
+        Log::record("pid = {$pid} count = {$count} ",Log::DEBUG);
+        $ret = pcntl_waitpid($pid,$status,WNOHANG);
+        if($ret == 0) {
+            Log::record("spawn-fcgi: successful ret == 0 PID: {$pid}",Log::DEBUG);
+        }
+        elseif($ret == -1) {
+            Log::record("spawn-fcgi: ret == 0  PID: {$pid}",Log::DEBUG);
+        }
+        else {
+            Log::record("spawn-fcgi: ret == 0 child exited PID: {$pid}.",Log::DEBUG);
+        }
+    }
+}
+
+function remote_addr()
+{
+    global $config;
+    $host = $config['room']['host'];
+    $port = $config['room']['port'];
+    return "{$host}:{$port}";
+}
+
+$listen_fd = stream_socket_server (remote_addr(), $errno, $errstr);
+if($listen_fd == false) {
+    echo "无法创建socket,请退出之前进程.\n";
+}
+
+//$count = 1;
+//while ($count-- > 0) {
+//    fork_subprocess($count,$listen_fd);
+//}
+
+search_work($listen_fd);
+
+
+