Skip to content

Commit

Permalink
Merge pull request #13 from exodus4d/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
exodus4d authored Jun 2, 2019
2 parents 96d0037 + a25cf73 commit cd2ca80
Show file tree
Hide file tree
Showing 3 changed files with 953 additions and 5 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@
out
gen

composer.lock
vendor

19 changes: 15 additions & 4 deletions app/Socket/TcpSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ class TcpSocket {
*/
const DEFAULT_DEBUG = false;

/**
* max length for JSON data string
* -> throw OverflowException on exceed
*/
const JSON_DECODE_MAX_LENGTH = 65536 * 4;

/**
* global server loop
* @var EventLoop\LoopInterface
Expand Down Expand Up @@ -218,7 +224,7 @@ protected function initRead(Socket\ConnectionInterface $connection) : Promise\Pr
if('json' == $this->acceptType){
// new empty stream for processing JSON
$stream = new Stream\ThroughStream();
$streamDecoded = new NDJson\Decoder($stream, true);
$streamDecoded = new NDJson\Decoder($stream, true, 512, 0, self::JSON_DECODE_MAX_LENGTH);

// promise get resolved on first emit('data')
$promise = Promise\Stream\first($streamDecoded);
Expand Down Expand Up @@ -388,7 +394,7 @@ protected function cancelTimer(Socket\ConnectionInterface $connection, string $t
$this->hasConnection($connection) &&
($data = (array)$this->connections->offsetGet($connection)) &&
isset($data['timers']) && isset($data['timers'][$timerName]) &&
($data['timers'][$timerName] instanceof EventLoop\Timer\TimerInterface)
($data['timers'][$timerName] instanceof EventLoop\TimerInterface)
){
$this->loop->cancelTimer($data['timers'][$timerName]);

Expand Down Expand Up @@ -535,11 +541,16 @@ protected function getStats() : array {
*/
protected function debug(Socket\ConnectionInterface $connection, string $method, string $message = ''){
if(self::DEFAULT_DEBUG){
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
$caller = array_shift($backtrace);
$callerOrig = array_shift($backtrace);

$data = [
date('Y-m-d H:i:s'),
__CLASS__ . '()',
'DEBUG',
$connection->getRemoteAddress(),
$method . '()',
$caller['file'] . ' line ' . $caller['line'],
$callerOrig['function'] . '()' . (($callerOrig['function'] !== $method) ? ' [' . $method . '()]' : ''),
$message
];

Expand Down
Loading

0 comments on commit cd2ca80

Please sign in to comment.