Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Jan 9, 2020
2 parents 6b504d1 + f43a3db commit cb269f2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
2 changes: 0 additions & 2 deletions src/Components/WscMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ protected function connect()

// Set the stream context options if they're already set in the config
$context = $this->getStreamContext();


if ($this->config->hasProxy()) {
$this->socket = $this->proxy();
} else {
Expand Down
7 changes: 6 additions & 1 deletion src/Components/WssMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

/**
* Class WssMain
*
* @package WSSC\Components
*
* @property ServerConfig config
*/
class WssMain implements CommonsContract
{

private $isPcntlLoaded = false;

/**
Expand All @@ -26,7 +28,7 @@ class WssMain implements CommonsContract
protected function decode(string $data)
{
if (empty($data)) {
return NULL; // close has been sent
return null; // close has been sent
}

$unmaskedPayload = '';
Expand Down Expand Up @@ -93,6 +95,7 @@ protected function decode(string $data)

/**
* Returns true if pcntl ext loaded and false otherwise
*
* @return bool
*/
protected function isPcntlLoaded(): bool
Expand All @@ -102,6 +105,7 @@ protected function isPcntlLoaded(): bool

/**
* Sets pre-loaded pcntl state
*
* @param bool $isPcntlLoaded
*/
protected function setIsPcntlLoaded(bool $isPcntlLoaded): void
Expand All @@ -111,6 +115,7 @@ protected function setIsPcntlLoaded(bool $isPcntlLoaded): void

/**
* Detects decode data type
*
* @param string $firstByteBinary
* @param array $decodedData
*/
Expand Down
62 changes: 34 additions & 28 deletions src/WebSocketServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class WebSocketServer extends WssMain implements WebSocketServerContract
private const HEADER_BYTES_READ = 1024;

// stream non-blocking
public const NON_BLOCK = 0;
public const NON_BLOCK = 0;

/**
* WebSocketServer constructor.
Expand All @@ -65,13 +65,15 @@ public function __construct(
*/
public function run()
{
$errno = NULL;
$errno = null;
$errorMessage = '';

$server = stream_socket_server("tcp://{$this->config->getHost()}:{$this->config->getPort()}", $errno, $errorMessage);
$server = stream_socket_server("tcp://{$this->config->getHost()}:{$this->config->getPort()}", $errno,
$errorMessage);

if ($server === false) {
throw new WebSocketException('Could not bind to socket: ' . $errno . ' - ' . $errorMessage . PHP_EOL, CommonsContract::SERVER_COULD_NOT_BIND_TO_SOCKET);
throw new WebSocketException('Could not bind to socket: ' . $errno . ' - ' . $errorMessage . PHP_EOL,
CommonsContract::SERVER_COULD_NOT_BIND_TO_SOCKET);
}

@cli_set_process_title($this->config->getProcessName());
Expand Down Expand Up @@ -144,7 +146,8 @@ private function looping($server)

//start reading and use a large timeout
if (!stream_select($readSocks, $write, $except, $this->config->getStreamSelectTimeout())) {
throw new WebSocketException('something went wrong while selecting', CommonsContract::SERVER_SELECT_ERROR);
throw new WebSocketException('something went wrong while selecting',
CommonsContract::SERVER_SELECT_ERROR);
}

//new client
Expand Down Expand Up @@ -195,30 +198,32 @@ private function messagesWorker(array $readSocks)
{
foreach ($readSocks as $kSock => $sock) {
$data = $this->decode(fread($sock, self::MAX_BYTES_READ));
$dataType = $data['type'];
$dataPayload = $data['payload'];

// to manipulate connection through send/close methods via handler, specified in IConnection
$this->cureentConn = new Connection($sock, $this->clients);
if (empty($data) || $dataType === self::EVENT_TYPE_CLOSE) { // close event triggered from client - browser tab or close socket event
// trigger CLOSE event
try {
$this->handler->onClose($this->cureentConn);
} catch (WebSocketException $e) {
$e->printStack();
if ($data !== null) {
$dataType = $data['type'];
$dataPayload = $data['payload'];

// to manipulate connection through send/close methods via handler, specified in IConnection
$this->cureentConn = new Connection($sock, $this->clients);
if (empty($data) || $dataType === self::EVENT_TYPE_CLOSE) { // close event triggered from client - browser tab or close socket event
// trigger CLOSE event
try {
$this->handler->onClose($this->cureentConn);
} catch (WebSocketException $e) {
$e->printStack();
}

// to avoid event leaks
unset($this->clients[array_search($sock, $this->clients)], $readSocks[$kSock]);
continue;
}

// to avoid event leaks
unset($this->clients[array_search($sock, $this->clients)], $readSocks[$kSock]);
continue;
}

if (method_exists($this->handler, self::MAP_EVENT_TYPE_TO_METHODS[$dataType])) {
try {
// dynamic call: onMessage, onPing, onPong
$this->handler->{self::MAP_EVENT_TYPE_TO_METHODS[$dataType]}($this->cureentConn, $dataPayload);
} catch (WebSocketException $e) {
$e->printStack();
if (method_exists($this->handler, self::MAP_EVENT_TYPE_TO_METHODS[$dataType])) {
try {
// dynamic call: onMessage, onPing, onPong
$this->handler->{self::MAP_EVENT_TYPE_TO_METHODS[$dataType]}($this->cureentConn, $dataPayload);
} catch (WebSocketException $e) {
$e->printStack();
}
}
}
}
Expand Down Expand Up @@ -278,7 +283,8 @@ private function getHeadersUpgrade(): string
{
$handShakeHeaders = self::HEADER_HTTP1_1 . self::HEADERS_EOL;
if (empty($this->headersUpgrade)) {
throw new ConnectionException('Headers for upgrade handshake are not set' . PHP_EOL, CommonsContract::SERVER_HEADERS_NOT_SET);
throw new ConnectionException('Headers for upgrade handshake are not set' . PHP_EOL,
CommonsContract::SERVER_HEADERS_NOT_SET);
}

foreach ($this->headersUpgrade as $key => $header) {
Expand Down

0 comments on commit cb269f2

Please sign in to comment.