Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
5 changed files
with
89 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,86 @@ | ||
<?php | ||
define('PID_FILE_NAME', '/tmp/swoole_server.pid'); | ||
|
||
$serv = new FixedHeaderServer(); | ||
$serv = new SocketServer(); | ||
$serv->run('0.0.0.0', 9504); | ||
|
||
class FixedHeaderServer | ||
class SocketServer | ||
{ | ||
protected $buffer = array(); | ||
protected $length = array(); | ||
|
||
/** | ||
* @var swoole_server | ||
*/ | ||
protected $serv; | ||
protected $serv; //swoole server | ||
|
||
const MAX_PACKAGE_LEN = 8000000; | ||
const MAX_PACKAGE_LEN = 8000000; //max data accept | ||
|
||
function onPackage($fd, $pkg) | ||
function run($host, $port) | ||
{ | ||
$this->current_fd = $fd; | ||
var_dump($pkg); | ||
$resp = "hello world"; | ||
$this->serv->send($fd, $resp); | ||
$this->current_fd = ''; | ||
register_shutdown_function(array($this, 'errorHandler')); | ||
$this->serv = new swoole_server($host, $port); | ||
|
||
$this->serv->set(array( | ||
//'daemonize' => true, | ||
'max_request' => 2000, //reload worker by run xx times | ||
'dispatch_mode' => 3, //who come first who is | ||
'worker_num' => 5, //how much worker will start | ||
'reactor_num' => 2, // depend cpu how much cpu you have | ||
'backlog' => 128, //accept queue | ||
'open_cpu_affinity' => 1, //get cpu more time | ||
'open_tcp_nodelay' => 1, // for small packet to open | ||
'tcp_defer_accept' => 5, //client will accept when not have data | ||
'max_conn' => 10000, | ||
'task_worker_num' => 10, | ||
'task_ipc_mode' => 2, //use queue with "who come first who is" | ||
'message_queue_key' => 0x72000100, | ||
'open_length_check' => true, | ||
'package_max_length' => 999999999, | ||
'package_length_type' => 'N', //see php pack() | ||
'package_length_offset' => 0, | ||
'package_body_offset' => 4, | ||
|
||
)); | ||
|
||
$this->serv->on('receive', array($this, 'onReceive')); | ||
$this->serv->on('close', array($this, 'onClose')); | ||
$this->serv->on('task', array($this, 'onTask')); | ||
$this->serv->on('finish', array($this, 'onFinish')); | ||
$this->serv->start(); | ||
} | ||
|
||
|
||
function onReceive($serv, $fd, $from_id, $data) | ||
{ | ||
echo "package".substr($data, -4, 4)." length=". (strlen($data) - 2)."\n"; | ||
$packet = json_decode(substr($data,4), true); | ||
|
||
//todo::包可能解析失败 | ||
$packet["socketfd"] = $fd; | ||
$task_id = $serv->task(json_encode($packet)); | ||
//todo::任务可能下发失败 | ||
} | ||
|
||
function onReceive_unpack_php($serv, $fd, $from_id, $data) | ||
function onTask($serv, $task_id, $from_id, $data) | ||
{ | ||
if (empty($this->buffer[$fd])) | ||
{ | ||
$this->buffer[$fd] = ''; | ||
$this->length[$fd] = 0; | ||
} | ||
|
||
$this->buffer[$fd] .= $data; | ||
$buffer = &$this->buffer[$fd]; | ||
|
||
do | ||
{ | ||
if ($this->length[$fd] === 0) | ||
{ | ||
$n = unpack('Nlen', substr($buffer, 0, 4)); | ||
$this->length[$fd] = $n['len']; | ||
if ($n['len'] > self::MAX_PACKAGE_LEN) | ||
{ | ||
$this->serv->close($fd); | ||
return; | ||
} | ||
} | ||
|
||
if (strlen($buffer) >= $this->length[$fd]) | ||
{ | ||
$this->onPackage($fd, substr($buffer, 0, $this->length[$fd])); | ||
$buffer = substr($buffer, $this->length[$fd]); | ||
$this->length[$fd] = 0; | ||
} | ||
else | ||
{ | ||
break; | ||
} | ||
} while(strlen($buffer) > 0); | ||
$data = json_decode($data, true); | ||
$fd = $data["socketfd"]; | ||
|
||
$result = array( | ||
"code" => "0", | ||
"msg" => "ok", | ||
"data" => $data, | ||
); | ||
$serv->send($fd, json_encode($result)); | ||
} | ||
|
||
function onClose($serv, $fd) | ||
function onFinish($serv, $task_id, $data) | ||
{ | ||
unset($this->buffer[$fd], $this->length[$fd]); | ||
|
||
} | ||
|
||
function run($host, $port) | ||
function onClose($serv, $fd) | ||
{ | ||
register_shutdown_function(array($this, 'errorHandler')); | ||
$this->serv = new swoole_server($host, $port); | ||
file_put_contents(PID_FILE_NAME, posix_getpid()); | ||
|
||
$this->serv->set(array( | ||
'max_request' => 0, | ||
// 'dispatch_mode' => 3, | ||
'open_length_check' => true, | ||
'package_max_length' => 81920, | ||
'package_length_type' => 'n', //see php pack() | ||
'package_length_offset' => 0, | ||
'package_body_offset' => 2, | ||
'worker_num' => 2, | ||
)); | ||
|
||
$this->serv->on('receive', array($this, 'onReceive')); | ||
$this->serv->on('close', array($this, 'onClose')); | ||
$this->serv->start(); | ||
} | ||
|
||
function errorHandler() | ||
{ | ||
if(!empty($this->current_fd)) | ||
{ | ||
$rsp = Proxy::shutdown_handler(); | ||
$rsp && $this->serv->send($this->current_fd, $rsp); | ||
} | ||
//if (!empty($this->current_fd)) { | ||
// $rsp = Proxy::shutdown_handler(); | ||
// $rsp && $this->serv->send($this->current_fd, $rsp); | ||
//} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters