Skip to content

Commit

Permalink
Merge branch 'next-major' into stable
Browse files Browse the repository at this point in the history
  • Loading branch information
dktapps committed Feb 13, 2023
2 parents 939abd0 + 0742715 commit 1f490cf
Show file tree
Hide file tree
Showing 37 changed files with 846 additions and 493 deletions.
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ parameters:
level: 9
paths:
- src
- tools
reportUnmatchedIgnoredErrors: false #enabling this makes build results too volatile when phpstan bugs get fixed
ignoreErrors:
-
Expand Down
7 changes: 2 additions & 5 deletions src/RakLib.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ abstract class RakLib{
*/
public const DEFAULT_PROTOCOL_VERSION = 6;

/**
* Regular RakNet uses 10 by default. MCPE uses 20. Configure this value as appropriate.
* @var int
*/
public static $SYSTEM_ADDRESS_COUNT = 20;
/** Regular RakNet uses 10 by default. MCPE uses 20. Configure this value as appropriate. */
public static int $SYSTEM_ADDRESS_COUNT = 20;
}
75 changes: 75 additions & 0 deletions src/client/ClientSocket.php
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;
}
}
36 changes: 36 additions & 0 deletions src/generic/DisconnectReason.php
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"
};
}
}
70 changes: 20 additions & 50 deletions src/generic/ReceiveReliabilityLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,71 +28,44 @@

final class ReceiveReliabilityLayer{

/** @var int */
public static $WINDOW_SIZE = 2048;
public static int $WINDOW_SIZE = 2048;

/** @var \Logger */
private $logger;

/**
* @var \Closure
* @phpstan-var \Closure(EncapsulatedPacket) : void
*/
private $onRecv;

/**
* @var \Closure
* @phpstan-var \Closure(AcknowledgePacket) : void
*/
private $sendPacket;

/** @var int */
private $windowStart;
/** @var int */
private $windowEnd;
/** @var int */
private $highestSeqNumber = -1;
private int $windowStart;
private int $windowEnd;
private int $highestSeqNumber = -1;

/** @var int[] */
private $ACKQueue = [];
private array $ACKQueue = [];
/** @var int[] */
private $NACKQueue = [];
private array $NACKQueue = [];

/** @var int */
private $reliableWindowStart;
/** @var int */
private $reliableWindowEnd;
private int $reliableWindowStart;
private int $reliableWindowEnd;
/** @var bool[] */
private $reliableWindow = [];
private array $reliableWindow = [];

/** @var int[] */
private $receiveOrderedIndex;
private array $receiveOrderedIndex;
/** @var int[] */
private $receiveSequencedHighestIndex;
private array $receiveSequencedHighestIndex;
/** @var EncapsulatedPacket[][] */
private $receiveOrderedPackets;
private array $receiveOrderedPackets;

/** @var (EncapsulatedPacket|null)[][] */
private $splitPackets = [];

/**
* @var int
* @phpstan-var positive-int
*/
private $maxSplitPacketPartCount;
/** @var int */
private $maxConcurrentSplitPackets;
private array $splitPackets = [];

/**
* @phpstan-param positive-int $maxSplitPacketPartCount
* @phpstan-param \Closure(EncapsulatedPacket) : void $onRecv
* @phpstan-param \Closure(AcknowledgePacket) : void $sendPacket
*/
public function __construct(\Logger $logger, \Closure $onRecv, \Closure $sendPacket, int $maxSplitPacketPartCount = PHP_INT_MAX, int $maxConcurrentSplitPackets = PHP_INT_MAX){
$this->logger = $logger;
$this->onRecv = $onRecv;
$this->sendPacket = $sendPacket;

public function __construct(
private \Logger $logger,
private \Closure $onRecv,
private \Closure $sendPacket,
private int $maxSplitPacketPartCount = PHP_INT_MAX,
private int $maxConcurrentSplitPackets = PHP_INT_MAX
){
$this->windowStart = 0;
$this->windowEnd = self::$WINDOW_SIZE;

Expand All @@ -103,9 +76,6 @@ public function __construct(\Logger $logger, \Closure $onRecv, \Closure $sendPac
$this->receiveSequencedHighestIndex = array_fill(0, PacketReliability::MAX_ORDER_CHANNELS, 0);

$this->receiveOrderedPackets = array_fill(0, PacketReliability::MAX_ORDER_CHANNELS, []);

$this->maxSplitPacketPartCount = $maxSplitPacketPartCount;
$this->maxConcurrentSplitPackets = $maxConcurrentSplitPackets;
}

private function handleEncapsulatedPacketRoute(EncapsulatedPacket $pk) : void{
Expand Down
10 changes: 4 additions & 6 deletions src/generic/ReliableCacheEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@

final class ReliableCacheEntry{

/** @var EncapsulatedPacket[] */
private $packets;
/** @var float */
private $timestamp;
private float $timestamp;

/**
* @param EncapsulatedPacket[] $packets
*/
public function __construct(array $packets){
$this->packets = $packets;
public function __construct(
private array $packets
){
$this->timestamp = microtime(true);
}

Expand Down
52 changes: 15 additions & 37 deletions src/generic/SendReliabilityLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,68 +22,46 @@
use raklib\protocol\NACK;
use raklib\protocol\PacketReliability;
use raklib\protocol\SplitPacketInfo;
use raklib\server\Session;
use function array_fill;
use function count;
use function str_split;
use function strlen;
use function time;

final class SendReliabilityLayer{

/**
* @var \Closure
* @phpstan-var \Closure(Datagram) : void
*/
private $sendDatagramCallback;
/**
* @var \Closure
* @phpstan-var \Closure(int) : void
*/
private $onACK;

/**
* @var int
* @phpstan-var int<Session::MIN_MTU_SIZE, max>
*/
private $mtuSize;

/** @var EncapsulatedPacket[] */
private $sendQueue = [];
private array $sendQueue = [];

/** @var int */
private $splitID = 0;
private int $splitID = 0;

/** @var int */
private $sendSeqNumber = 0;
private int $sendSeqNumber = 0;

/** @var int */
private $messageIndex = 0;
private int $messageIndex = 0;

/** @var int[] */
private $sendOrderedIndex;
private array $sendOrderedIndex;
/** @var int[] */
private $sendSequencedIndex;
private array $sendSequencedIndex;

/** @var ReliableCacheEntry[] */
private $resendQueue = [];
private array $resendQueue = [];

/** @var ReliableCacheEntry[] */
private $reliableCache = [];
private array $reliableCache = [];

/** @var int[][] */
private $needACK = [];
private array $needACK = [];

/**
* @phpstan-param int<Session::MIN_MTU_SIZE, max> $mtuSize
* @phpstan-param \Closure(Datagram) : void $sendDatagram
* @phpstan-param \Closure(Datagram) : void $sendDatagramCallback
* @phpstan-param \Closure(int) : void $onACK
*/
public function __construct(int $mtuSize, \Closure $sendDatagram, \Closure $onACK){
$this->mtuSize = $mtuSize;
$this->sendDatagramCallback = $sendDatagram;
$this->onACK = $onACK;

public function __construct(
private int $mtuSize,
private \Closure $sendDatagramCallback,
private \Closure $onACK
){
$this->sendOrderedIndex = array_fill(0, PacketReliability::MAX_ORDER_CHANNELS, 0);
$this->sendSequencedIndex = array_fill(0, PacketReliability::MAX_ORDER_CHANNELS, 0);
}
Expand Down
Loading

0 comments on commit 1f490cf

Please sign in to comment.