123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2018/7/21
- * Time: 下午6:06
- */
- define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
- require_once(BASE_ROOT_PATH . '/fooder.php');
- require_once(BASE_ROOT_PATH . '/helper/event_looper.php');
- require_once(BASE_ROOT_PATH . '/helper/search/tcp_client.php');
- require_once(BASE_ROOT_PATH . '/helper/room/factory_processor.php');
- require_once(BASE_ROOT_PATH . '/helper/room/proto_type.php');
- require_once(BASE_ROOT_PATH . '/helper/room/room_info.php');
- require_once(BASE_ROOT_PATH . '/helper/room/msg_builder.php');
- require_once(BASE_ROOT_PATH . '/helper/room/base_room.php');
- require_once(BASE_ROOT_PATH . '/helper/room/chatwo.php');
- require_once(BASE_ROOT_PATH . '/helper/room/chat_room.php');
- require_once(BASE_ROOT_PATH . '/helper/room/bargain_room.php');
- require_once(BASE_ROOT_PATH . '/helper/room/shake_room.php');
- require_once(BASE_ROOT_PATH . '/helper/room/factory.php');
- require_once(BASE_ROOT_PATH . '/helper/room/room_client.php');
- require_once(BASE_ROOT_PATH . '/helper/room/factory_client.php');
- class TestServer extends PHPUnit_Framework_TestCase
- {
- const admin_member_id = 36429;
- public static function setUpBeforeClass()
- {
- Base::run_util();
- }
- public function testFactory()
- {
- room\factory_client::instance()->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';
- }
- }
|