access(); } public function testAsyncALl() { global $config; $host = $config['room_factory']['host']; $port = $config['room_factory']['port']; $fac_socket = $this->open_socket($host,$port); $host = $config['room_srv']['host']; $port = $config['room_srv']['port']; $room_socket = $this->open_socket($host,$port); $processor = new empty_processor(); $looper = new event\event_looper(); $looper->init($processor); $looper->add_listen($fac_socket); $looper->add_listen($room_socket); $looper->run_loop(); } public function testAsyncFactroy() { global $config; $host = $config['room_factory']['host']; $port = $config['room_factory']['port']; $socket = $this->open_socket($host,$port); $processor = new empty_processor(); $looper = new event\buffer_looper(); $looper->init($processor); $looper->add_listen($socket); $looper->run_loop(); } public function testSyncFactroy() { global $config; $host = $config['room_factory']['host']; $port = $config['room_factory']['port']; $socket = $this->open_socket($host,$port,false); while (true) { $stream = socket_accept($socket); while (true) { $read = @socket_read($stream,1024); if(strlen($read) == 0) break; else { Log::record("read={$read}",Log::DEBUG); } } } } public function testAsyncOrgFactroy() { global $config; $host = $config['room_factory']['host']; $port = $config['room_factory']['port']; $contents = []; $socket = $this->open_socket($host,$port,false); while (true) { $stream = socket_accept($socket); $fd = intval($stream); $contents[$fd] = ''; socket_set_nonblock($stream); while (true) { $rfds = [$stream]; $wfds = []; $efds = []; if(socket_select($rfds,$wfds,$efds,0) < 1) continue; if (in_array($stream, $rfds)) { $rfd = intval($stream); $read = @socket_read($stream, 1024); if($read === false) { continue; } else { if (strlen($read) == 0) break; else { $contents[$rfd] .= $read; Log::record("read={$read}", Log::DEBUG); $ret = $this->proc($contents[$fd]); if($ret === false) { $x = 0; } else { $contents[$rfd] = $ret; } } } } } } } public function testAsyncRoom() { global $config; $host = $config['room_srv']['host']; $port = $config['room_srv']['port']; $socket = $this->open_socket($host,$port); $processor = new empty_processor(); $looper = new event_looper(); $looper->init($processor); $looper->add_listen($socket); $looper->run_loop(); } private function open_socket($host,$port,$nonbolck = true) { $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if(!socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) { echo "socket_set_option 地址重用失败.\n"; return false; } if($nonbolck && !socket_set_nonblock($sock)) { $err = socket_last_error(); Log::record("socket_set_blocking error : {$err}",Log::DEBUG); return false; } if(!socket_bind($sock, $host, $port)) { echo "无法绑定socket {$host} : {$port},请退出之前进程.\n"; return false; } if(!socket_listen($sock)) { echo "无法监听socket,请退出之前进程.\n"; return false; } return $sock; } const body_header_len = 10; public function testProc() { $content = $this->get_content(); $this->proc($content); } private function proc($content) { $start = 0; $left = strlen($content); $i = 0; while($left > self::body_header_len) { $header = substr($content,$start,self::body_header_len); if(!is_numeric($header)) { return false; } $body_len = intval($header); if($body_len == 0) { //这是一个心跳包 $start += self::body_header_len; $left -= self::body_header_len; $i++; } elseif($body_len < 0) { return false; } else { if($left >= self::body_header_len + $body_len) { $i++; $body = substr($content,$start + self::body_header_len,$body_len); $start += self::body_header_len + $body_len; $left -= self::body_header_len + $body_len; } else { break; } } } if($start > 0) { $str = substr($content,$start); if($str === false) { return ''; } else { return $str; } } return $content; } private function get_content() { return '0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 88 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 89 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 90 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 91 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 92 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 93 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 94 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 95 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 96 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 97 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 98 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 99 }0000000090{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 100 }0000000090{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 101 }0000000090{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 102 }0000000090{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 103 }0000000090{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 104 }0000000090{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 105 }0000000090{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 106 }0000000090{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 107 }0000000090{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 108 }0000000090{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 109 }0000000090{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 110 }"room" : 37, "set" : 69 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 70 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 71 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 72 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 73 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 74 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 75 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 76 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 77 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 78 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 79 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 80 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 81 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 82 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 83 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 84 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 85 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 86 }0000000089{ "act" : "access", "msgtype" : "message", "op" : "build", "room" : 37, "set" : 87 }0000000089{ "act" : "access", "msgt'; } }