Bläddra i källkod

fix socket reuse

stanley-king 7 år sedan
förälder
incheckning
226087e468
11 ändrade filer med 53 tillägg och 48 borttagningar
  1. 9 18
      centra_srv.php
  2. 1 1
      helper/room/access_client.php
  3. 1 1
      helper/room/factory_client.php
  4. 4 8
      helper/search/srv_base.php
  5. 1 1
      helper/search/tcp_client.php
  6. BIN
      mac_ugcman
  7. BIN
      mac_webacc
  8. 18 8
      room_factory.php
  9. 19 11
      room_srv.php
  10. BIN
      ugcman
  11. BIN
      webacc

+ 9 - 18
centra_srv.php

@@ -59,7 +59,6 @@ function fork_subprocess($count,$listen_fd)
         fclose(STDIN);  // Close all of the standard
         fclose(STDOUT); // file descriptors as we
         fclose(STDERR); // are running as a daemon.
-//        fclose($listen_fd);
 
         Log::record("pid = {$pid} count = {$count} ",Log::DEBUG);
         search_work($listen_fd);
@@ -72,6 +71,7 @@ function fork_subprocess($count,$listen_fd)
     }
     else
     {
+        socket_close($listen_fd);
         Log::record("pid = {$pid} count = {$count} ",Log::DEBUG);
         $ret = pcntl_waitpid($pid,$status,WNOHANG);
         if($ret == 0) {
@@ -86,35 +86,26 @@ function fork_subprocess($count,$listen_fd)
     }
 }
 
-function remote_addr()
-{
-    global $config;
-
-    $host = $config['searcher']['host'];
-    $port = $config['searcher']['port'];
-
-    return "tcp://{$host}:{$port}";
-}
-
 global $config;
-
 $host = $config['searcher']['host'];
-$port = intval($config['searcher']['port']);
+$port = $config['searcher']['port'];
 
 $listen_fd = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
 if(!socket_set_option($listen_fd, SOL_SOCKET, SO_REUSEADDR, 1)) {
     echo "socket_set_option 地址重用失败.\n";
 }
+if(!socket_set_nonblock($listen_fd)) {
+    $err = socket_last_error();
+    Log::record("socket_set_blocking error : {$err}",Log::DEBUG);
+}
+
 if(!socket_bind($listen_fd, $host, $port)) {
     echo "无法创建socket,请退出之前进程.\n";
     return;
 }
-
-
-
-$listen_fd = stream_socket_server (remote_addr(), $errno, $errstr);
-if($listen_fd == false) {
+if(!socket_listen($listen_fd)) {
     echo "无法创建socket,请退出之前进程.\n";
+    return;
 }
 
 $count = 1;

+ 1 - 1
helper/room/access_client.php

@@ -38,7 +38,7 @@ class access_client extends tcp_client
 
         $host = $config['room_srv']['host'];
         $port = $config['room_srv']['port'];
-        return "{$host}:{$port}";
+        return "tcp://{$host}:{$port}";
     }
 
     public function join($room,$room_key)

+ 1 - 1
helper/room/factory_client.php

@@ -40,7 +40,7 @@ class factory_client extends tcp_client
 
         $host = $config['room_factory']['host'];
         $port = $config['room_factory']['port'];
-        return "{$host}:{$port}";
+        return "tcp://{$host}:{$port}";
     }
 
     public function create_bargain($goods_id,$user,$lowest_price,$usable_days,$random = true,$total_num = 10)

+ 4 - 8
helper/search/srv_base.php

@@ -47,10 +47,6 @@ abstract class srv_base
     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();
 
@@ -80,14 +76,14 @@ abstract class srv_base
 
         static $bufid = 1;
 
-        $stream = stream_socket_accept($socket);
+        $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);
+        socket_set_nonblock($stream);
         $buffer = event_buffer_new($stream, 'ev_read', NULL, 'ev_error', $bufid);
         if($buffer == false) {
             fclose($stream);
@@ -170,7 +166,7 @@ abstract class srv_base
         event_buffer_free($buffer);
 
         if(array_key_exists($bufid,$this->mStreams)) {
-            stream_socket_shutdown($this->mStreams[$bufid],STREAM_SHUT_RDWR);
+            socket_shutdown($this->mStreams[$bufid],STREAM_SHUT_RDWR);
             fclose($this->mStreams[$bufid]);
             unset($this->mStreams[$bufid]);
         }
@@ -207,7 +203,7 @@ abstract class srv_base
         event_buffer_free($buffer);
 
         if(array_key_exists($bufid,$this->mStreams)) {
-            stream_socket_shutdown($this->mStreams[$bufid],STREAM_SHUT_RDWR);
+            socket_shutdown($this->mStreams[$bufid],STREAM_SHUT_RDWR);
             fclose($this->mStreams[$bufid]);
             unset($this->mStreams[$bufid]);
         }

+ 1 - 1
helper/search/tcp_client.php

@@ -260,7 +260,7 @@ class relation_client extends tcp_client
         $host = $config['relation']['host'];
         $port = $config['relation']['port'];
 
-        return "{$host}:{$port}";
+        return "tcp://{$host}:{$port}";
     }
     public function add_inviter($param)
     {

BIN
mac_ugcman


BIN
mac_webacc


+ 18 - 8
room_factory.php

@@ -50,6 +50,7 @@ function fork_subprocess($count,$listen_fd)
     }
     else
     {
+        socket_close($listen_fd);
         Log::record("pid = {$pid} count = {$count} ",Log::DEBUG);
         $ret = pcntl_waitpid($pid,$status,WNOHANG);
         if($ret == 0) {
@@ -64,17 +65,26 @@ function fork_subprocess($count,$listen_fd)
     }
 }
 
-function remote_addr()
-{
-    global $config;
-    $host = $config['room_factory']['host'];
-    $port = $config['room_factory']['port'];
-    return "{$host}:{$port}";
+global $config;
+$host = $config['room_factory']['host'];
+$port = $config['room_factory']['port'];
+
+$listen_fd = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
+if(!socket_set_option($listen_fd, SOL_SOCKET, SO_REUSEADDR, 1)) {
+    echo "socket_set_option 地址重用失败.\n";
+}
+if(!socket_set_nonblock($listen_fd)) {
+    $err = socket_last_error();
+    Log::record("socket_set_blocking error : {$err}",Log::DEBUG);
 }
 
-$listen_fd = stream_socket_server (remote_addr(), $errno, $errstr);
-if($listen_fd == false) {
+if(!socket_bind($listen_fd, $host, $port)) {
+    echo "无法创建socket,请退出之前进程.\n";
+    return;
+}
+if(!socket_listen($listen_fd)) {
     echo "无法创建socket,请退出之前进程.\n";
+    return;
 }
 
 $count = 1;

+ 19 - 11
room_srv.php

@@ -63,17 +63,27 @@ function fork_subprocess($count,$listen_fd)
     }
 }
 
-function remote_addr()
-{
-    global $config;
-    $host = $config['room_srv']['host'];
-    $port = $config['room_srv']['port'];
-    return "{$host}:{$port}";
+
+global $config;
+$host = $config['room_srv']['host'];
+$port = $config['room_srv']['port'];
+
+$listen_fd = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
+if(!socket_set_option($listen_fd, SOL_SOCKET, SO_REUSEADDR, 1)) {
+    echo "socket_set_option 地址重用失败.\n";
+}
+if(!socket_set_nonblock($listen_fd)) {
+    $err = socket_last_error();
+    Log::record("socket_set_blocking error : {$err}",Log::DEBUG);
 }
 
-$listen_fd = stream_socket_server (remote_addr(), $errno, $errstr);
-if($listen_fd == false) {
+if(!socket_bind($listen_fd, $host, $port)) {
     echo "无法创建socket,请退出之前进程.\n";
+    return;
+}
+if(!socket_listen($listen_fd)) {
+    echo "无法创建socket,请退出之前进程.\n";
+    return;
 }
 
 $count = 1;
@@ -81,6 +91,4 @@ while ($count-- > 0) {
     fork_subprocess($count,$listen_fd);
 }
 
-//search_work($listen_fd);
-
-
+//search_work($sockfd);

BIN
ugcman


BIN
webacc