Skip to content

Commit

Permalink
Replaced all old style callables to new first class callable syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
luzrain committed Oct 30, 2023
1 parent 1c24fe4 commit c445ca7
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 84 deletions.
16 changes: 7 additions & 9 deletions src/Connection/AsyncTcpConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class AsyncTcpConnection extends TcpConnection
/**
* PHP built-in protocols.
*
* @var array<string,string>
* @var array<string, string>
*/
public const BUILD_IN_TRANSPORTS = [
'tcp' => 'tcp',
Expand Down Expand Up @@ -230,7 +230,7 @@ public function reconnect(int $after = 0): void
Timer::del($this->reconnectTimer);
}
if ($after > 0) {
$this->reconnectTimer = Timer::add($after, [$this, 'connect'], null, false);
$this->reconnectTimer = Timer::add($after, $this->connect(...), null, false);
return;
}
$this->connect();
Expand Down Expand Up @@ -302,10 +302,10 @@ public function connect(): void
return;
}
// Add socket to global event loop waiting connection is successfully established or failed.
$this->eventLoop->onWritable($this->socket, [$this, 'checkConnection']);
$this->eventLoop->onWritable($this->socket, $this->checkConnection(...));
// For windows.
if (DIRECTORY_SEPARATOR === '\\' && method_exists($this->eventLoop, 'onExcept')) {
$this->eventLoop->onExcept($this->socket, [$this, 'checkConnection']);
$this->eventLoop->onExcept($this->socket, $this->checkConnection(...));
}
}

Expand Down Expand Up @@ -402,11 +402,11 @@ public function checkConnection(): void
} else {
// There are some data waiting to send.
if ($this->sendBuffer) {
$this->eventLoop->onWritable($this->socket, [$this, 'baseWrite']);
$this->eventLoop->onWritable($this->socket, $this->baseWrite(...));
}
}
// Register a listener waiting read event.
$this->eventLoop->onReadable($this->socket, [$this, 'baseRead']);
$this->eventLoop->onReadable($this->socket, $this->baseRead(...));

$this->status = self::STATUS_ESTABLISHED;
$this->remoteAddress = $address;
Expand All @@ -422,13 +422,12 @@ public function checkConnection(): void
// Try to emit protocol::onConnect
if ($this->protocol && method_exists($this->protocol, 'onConnect')) {
try {
[$this->protocol, 'onConnect']($this);
$this->protocol->onConnect($this);
} catch (Throwable $e) {
$this->error($e);
}
}
} else {

// Connection failed.
$this->emitError(static::CONNECT_FAIL, 'connect ' . $this->remoteAddress . ' fail after ' . round(microtime(true) - $this->connectStartTime, 4) . ' seconds');
if ($this->status === self::STATUS_CLOSING) {
Expand All @@ -438,6 +437,5 @@ public function checkConnection(): void
$this->onConnect = null;
}
}

}
}
3 changes: 1 addition & 2 deletions src/Connection/AsyncUdpConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public function connect(): void

stream_set_blocking($this->socket, false);
if ($this->onMessage) {
$this->eventLoop->onReadable($this->socket, [$this, 'baseRead']);
$this->eventLoop->onReadable($this->socket, $this->baseRead(...));
}
$this->connected = true;
// Try to emit onConnect callback.
Expand All @@ -214,5 +214,4 @@ public function connect(): void
}
}
}

}
1 change: 0 additions & 1 deletion src/Connection/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,4 @@ public function error(Throwable $exception): void
throw $exception;
}
}

}
13 changes: 7 additions & 6 deletions src/Connection/TcpConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public function __construct(EventInterface $eventLoop, $socket, string $remoteAd
stream_set_read_buffer($this->socket, 0);
}
$this->eventLoop = $eventLoop;
$this->eventLoop->onReadable($this->socket, [$this, 'baseRead']);
$this->eventLoop->onReadable($this->socket, $this->baseRead(...));
$this->maxSendBufferSize = self::$defaultMaxSendBufferSize;
$this->maxPackageSize = self::$defaultMaxPackageSize;
$this->remoteAddress = $remoteAddress;
Expand Down Expand Up @@ -415,7 +415,7 @@ public function send(mixed $sendBuffer, bool $raw = false)
// Attempt to send data directly.
if ($this->sendBuffer === '') {
if ($this->transport === 'ssl') {
$this->eventLoop->onWritable($this->socket, [$this, 'baseWrite']);
$this->eventLoop->onWritable($this->socket, $this->baseWrite(...));
$this->sendBuffer = $sendBuffer;
$this->checkBufferWillFull();
return;
Expand Down Expand Up @@ -451,7 +451,7 @@ public function send(mixed $sendBuffer, bool $raw = false)
}
$this->sendBuffer = $sendBuffer;
}
$this->eventLoop->onWritable($this->socket, [$this, 'baseWrite']);
$this->eventLoop->onWritable($this->socket, $this->baseWrite(...));
// Check if send buffer will be full.
$this->checkBufferWillFull();
return;
Expand Down Expand Up @@ -587,7 +587,7 @@ public function pauseRecv(): void
public function resumeRecv(): void
{
if ($this->isPaused === true) {
$this->eventLoop->onReadable($this->socket, [$this, 'baseRead']);
$this->eventLoop->onReadable($this->socket, $this->baseRead(...));
$this->isPaused = false;
$this->baseRead($this->socket, false);
}
Expand All @@ -610,7 +610,7 @@ public function baseRead($socket, bool $checkEof = true): void
if ($this->doSslHandshake($socket)) {
$this->sslHandshakeCompleted = true;
if ($this->sendBuffer) {
$this->eventLoop->onWritable($socket, [$this, 'baseWrite']);
$this->eventLoop->onWritable($socket, $this->baseWrite(...));
}
} else {
return;
Expand All @@ -621,6 +621,7 @@ public function baseRead($socket, bool $checkEof = true): void
try {
$buffer = @fread($socket, self::READ_BUFFER_SIZE);
} catch (Throwable) {
// do nothing
}

// Check connection closed.
Expand Down Expand Up @@ -1018,7 +1019,7 @@ public function destroy(): void
// Try to emit protocol::onClose
if ($this->protocol && method_exists($this->protocol, 'onClose')) {
try {
([$this->protocol, 'onClose'])($this);
$this->protocol->onClose($this);
} catch (Throwable $e) {
$this->error($e);
}
Expand Down
1 change: 0 additions & 1 deletion src/Events/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,5 +469,4 @@ public function error(Throwable $e): void
}
($this->errorHandler)($e);
}

}
1 change: 0 additions & 1 deletion src/Events/Swoole.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,5 +263,4 @@ public function error(Throwable $e): void
}
($this->errorHandler)($e);
}

}
5 changes: 2 additions & 3 deletions src/Protocols/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,8 @@ public static function input(string $buffer, TcpConnection $connection): int
}

$header = substr($buffer, 0, $crlfPos);
$hostHeaderPosition = stripos($header, "\r\nHost: ");

if (false === $hostHeaderPosition && $firstLine[2] === "HTTP/1.1") {
if (!str_contains($header, "\r\nHost: ") && $firstLine[2] === "HTTP/1.1") {
$connection->close("HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n", true);
return 0;
}
Expand All @@ -138,7 +137,7 @@ public static function input(string $buffer, TcpConnection $connection): int
$hasContentLength = true;
} else {
$hasContentLength = false;
if (false !== stripos($header, "\r\nTransfer-Encoding:")) {
if (str_contains($header, "\r\nTransfer-Encoding:")) {
$connection->close("HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n", true);
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Protocols/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class Response implements Stringable
/**
* Phrases.
*
* @var array<int,string>
* @var array<int, string>
*
* @link https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
*/
Expand Down
1 change: 0 additions & 1 deletion src/Protocols/Http/Session/RedisClusterSessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,4 @@ public function read(string $sessionId): string
{
return $this->redis->get($sessionId);
}

}
1 change: 0 additions & 1 deletion src/Protocols/Websocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,5 +432,4 @@ public static function dealHandshake(string $buffer, TcpConnection $connection):
"HTTP/1.0 200 OK\r\nServer: workerman\r\n\r\n<div style=\"text-align:center\"><h1>WebSocket</h1><hr>workerman</div>", true);
return 0;
}

}
2 changes: 1 addition & 1 deletion src/Timer.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static function init(EventInterface $event = null): void
return;
}
if (function_exists('pcntl_signal')) {
pcntl_signal(SIGALRM, ['\Workerman\Timer', 'signalHandle'], false);
pcntl_signal(SIGALRM, self::signalHandle(...), false);
}
}

Expand Down
Loading

0 comments on commit c445ca7

Please sign in to comment.