-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'next-major' into stable
- Loading branch information
Showing
37 changed files
with
846 additions
and
493 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
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of RakLib. | ||
* Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/RakLib> | ||
* | ||
* RakLib is not affiliated with Jenkins Software LLC nor RakNet. | ||
* | ||
* RakLib is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace raklib\client; | ||
|
||
use raklib\generic\Socket; | ||
use raklib\generic\SocketException; | ||
use raklib\utils\InternetAddress; | ||
use function socket_connect; | ||
use function socket_last_error; | ||
use function socket_recv; | ||
use function socket_send; | ||
use function socket_strerror; | ||
use function strlen; | ||
use function trim; | ||
|
||
class ClientSocket extends Socket{ | ||
|
||
public function __construct( | ||
private InternetAddress $connectAddress | ||
){ | ||
parent::__construct($this->connectAddress->getVersion() === 6); | ||
|
||
if(!@socket_connect($this->socket, $this->connectAddress->getIp(), $this->connectAddress->getPort())){ | ||
$error = socket_last_error($this->socket); | ||
throw new SocketException("Failed to connect to " . $this->connectAddress . ": " . trim(socket_strerror($error)), $error); | ||
} | ||
//TODO: is an 8 MB buffer really appropriate for a client?? | ||
$this->setSendBuffer(1024 * 1024 * 8)->setRecvBuffer(1024 * 1024 * 8); | ||
} | ||
|
||
public function getConnectAddress() : InternetAddress{ | ||
return $this->connectAddress; | ||
} | ||
|
||
/** | ||
* @throws SocketException | ||
*/ | ||
public function readPacket() : ?string{ | ||
$buffer = ""; | ||
if(@socket_recv($this->socket, $buffer, 65535, 0) === false){ | ||
$errno = socket_last_error($this->socket); | ||
if($errno === SOCKET_EWOULDBLOCK){ | ||
return null; | ||
} | ||
throw new SocketException("Failed to recv (errno $errno): " . trim(socket_strerror($errno)), $errno); | ||
} | ||
return $buffer; | ||
} | ||
|
||
/** | ||
* @throws SocketException | ||
*/ | ||
public function writePacket(string $buffer) : int{ | ||
$result = @socket_send($this->socket, $buffer, strlen($buffer), 0); | ||
if($result === false){ | ||
$errno = socket_last_error($this->socket); | ||
throw new SocketException("Failed to send packet (errno $errno): " . trim(socket_strerror($errno)), $errno); | ||
} | ||
return $result; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of RakLib. | ||
* Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/RakLib> | ||
* | ||
* RakLib is not affiliated with Jenkins Software LLC nor RakNet. | ||
* | ||
* RakLib is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace raklib\generic; | ||
|
||
final class DisconnectReason{ | ||
public const CLIENT_DISCONNECT = 0; | ||
public const SERVER_DISCONNECT = 1; | ||
public const PEER_TIMEOUT = 2; | ||
public const CLIENT_RECONNECT = 3; | ||
public const SERVER_SHUTDOWN = 4; //TODO: do we really need a separate reason for this in addition to SERVER_DISCONNECT? | ||
public static function toString(int $reason) : string{ | ||
return match($reason){ | ||
self::CLIENT_DISCONNECT => "client disconnect", | ||
self::SERVER_DISCONNECT => "server disconnect", | ||
self::PEER_TIMEOUT => "timeout", | ||
self::CLIENT_RECONNECT => "new session established on same address and port", | ||
self::SERVER_SHUTDOWN => "server shutdown", | ||
default => "Unknown reason $reason" | ||
}; | ||
} | ||
} |
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
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
Oops, something went wrong.