Skip to content

Commit 6c83e0f

Browse files
committed
InternetAddress is no longer mutable
closes #45
1 parent 9d6f564 commit 6c83e0f

6 files changed

Lines changed: 31 additions & 36 deletions

File tree

src/generic/Socket.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ class Socket{
5656
*/
5757
public function __construct(InternetAddress $bindAddress){
5858
$this->bindAddress = $bindAddress;
59-
$socket = @socket_create($bindAddress->version === 4 ? AF_INET : AF_INET6, SOCK_DGRAM, SOL_UDP);
59+
$socket = @socket_create($bindAddress->getVersion() === 4 ? AF_INET : AF_INET6, SOCK_DGRAM, SOL_UDP);
6060
if($socket === false){
6161
throw new \RuntimeException("Failed to create socket: " . trim(socket_strerror(socket_last_error())));
6262
}
6363
$this->socket = $socket;
6464

65-
if($bindAddress->version === 6){
65+
if($bindAddress->getVersion() === 6){
6666
socket_set_option($this->socket, IPPROTO_IPV6, IPV6_V6ONLY, 1); //Don't map IPv4 to IPv6, the implementation can create another RakLib instance to handle IPv4
6767
}
6868

69-
if(@socket_bind($this->socket, $bindAddress->ip, $bindAddress->port) === true){
69+
if(@socket_bind($this->socket, $bindAddress->getIp(), $bindAddress->getPort()) === true){
7070
$this->setSendBuffer(1024 * 1024 * 8)->setRecvBuffer(1024 * 1024 * 8);
7171
}else{
7272
$error = socket_last_error($this->socket);

src/protocol/PacketSerializer.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,27 @@ public function putString(string $v) : void{
6868
}
6969

7070
public function putAddress(InternetAddress $address) : void{
71-
$this->putByte($address->version);
72-
if($address->version === 4){
73-
$parts = explode(".", $address->ip);
71+
$version = $address->getVersion();
72+
$this->putByte($version);
73+
if($version === 4){
74+
$parts = explode(".", $address->getIp());
7475
assert(count($parts) === 4, "Wrong number of parts in IPv4 IP, expected 4, got " . count($parts));
7576
foreach($parts as $b){
7677
$this->putByte((~((int) $b)) & 0xff);
7778
}
78-
$this->putShort($address->port);
79-
}elseif($address->version === 6){
79+
$this->putShort($address->getPort());
80+
}elseif($version === 6){
8081
$this->putLShort(AF_INET6);
81-
$this->putShort($address->port);
82+
$this->putShort($address->getPort());
8283
$this->putInt(0);
83-
$rawIp = inet_pton($address->ip);
84+
$rawIp = inet_pton($address->getIp());
8485
if($rawIp === false){
8586
throw new \InvalidArgumentException("Invalid IPv6 address could not be encoded");
8687
}
8788
$this->put($rawIp);
8889
$this->putInt(0);
8990
}else{
90-
throw new \InvalidArgumentException("IP version $address->version is not supported");
91+
throw new \InvalidArgumentException("IP version $version is not supported");
9192
}
9293
}
9394
}

src/server/Server.php

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@ class Server implements ServerInterface{
9696
/** @var int */
9797
protected $maxMtuSize;
9898

99-
/** @var InternetAddress */
100-
protected $reusableAddress;
101-
10299
/** @var int */
103100
protected $nextSessionId = 0;
104101

@@ -122,8 +119,6 @@ public function __construct(int $serverId, \Logger $logger, Socket $socket, int
122119
$this->startTimeMS = (int) (microtime(true) * 1000);
123120

124121
$this->unconnectedMessageHandler = new UnconnectedMessageHandler($this, $protocolAcceptor);
125-
126-
$this->reusableAddress = clone $this->socket->getBindAddress();
127122
}
128123

129124
/**
@@ -134,7 +129,7 @@ public function getRakNetTimeMS() : int{
134129
}
135130

136131
public function getPort() : int{
137-
return $this->socket->getBindAddress()->port;
132+
return $this->socket->getBindAddress()->getPort();
138133
}
139134

140135
public function getMaxMtuSize() : int{
@@ -225,10 +220,8 @@ private function tick() : void{
225220

226221
/** @phpstan-impure */
227222
private function receivePacket() : bool{
228-
$address = $this->reusableAddress;
229-
230223
try{
231-
$buffer = $this->socket->readPacket($address->ip, $address->port);
224+
$buffer = $this->socket->readPacket($addressIp, $addressPort);
232225
}catch(SocketException $e){
233226
$error = $e->getCode();
234227
if($error === SOCKET_ECONNRESET){ //client disconnected improperly, maybe crash or lost connection
@@ -244,23 +237,24 @@ private function receivePacket() : bool{
244237
$len = strlen($buffer);
245238

246239
$this->receiveBytes += $len;
247-
if(isset($this->block[$address->ip])){
240+
if(isset($this->block[$addressIp])){
248241
return true;
249242
}
250243

251-
if(isset($this->ipSec[$address->ip])){
252-
if(++$this->ipSec[$address->ip] >= $this->packetLimit){
253-
$this->blockAddress($address->ip);
244+
if(isset($this->ipSec[$addressIp])){
245+
if(++$this->ipSec[$addressIp] >= $this->packetLimit){
246+
$this->blockAddress($addressIp);
254247
return true;
255248
}
256249
}else{
257-
$this->ipSec[$address->ip] = 1;
250+
$this->ipSec[$addressIp] = 1;
258251
}
259252

260253
if($len < 1){
261254
return true;
262255
}
263256

257+
$address = new InternetAddress($addressIp, $addressPort, $this->socket->getBindAddress()->getVersion());
264258
try{
265259
$session = $this->getSessionByAddress($address);
266260
if($session !== null){
@@ -283,7 +277,7 @@ private function receivePacket() : bool{
283277
foreach($this->rawPacketFilters as $pattern){
284278
if(preg_match($pattern, $buffer) > 0){
285279
$handled = true;
286-
$this->eventListener->onRawPacketReceive($address->ip, $address->port, $buffer);
280+
$this->eventListener->onRawPacketReceive($address->getIp(), $address->getPort(), $buffer);
287281
break;
288282
}
289283
}
@@ -307,7 +301,7 @@ private function receivePacket() : bool{
307301
}else{
308302
$logFn();
309303
}
310-
$this->blockAddress($address->ip, 5);
304+
$this->blockAddress($address->getIp(), 5);
311305
}
312306

313307
return true;
@@ -317,7 +311,7 @@ public function sendPacket(Packet $packet, InternetAddress $address) : void{
317311
$out = new PacketSerializer(); //TODO: reusable streams to reduce allocations
318312
$packet->encode($out);
319313
try{
320-
$this->sendBytes += $this->socket->writePacket($out->getBuffer(), $address->ip, $address->port);
314+
$this->sendBytes += $this->socket->writePacket($out->getBuffer(), $address->getIp(), $address->getPort());
321315
}catch(SocketException $e){
322316
$this->logger->debug($e->getMessage());
323317
}
@@ -413,7 +407,7 @@ private function removeSessionInternal(Session $session) : void{
413407

414408
public function openSession(Session $session) : void{
415409
$address = $session->getAddress();
416-
$this->eventListener->onClientConnect($session->getInternalId(), $address->ip, $address->port, $session->getID());
410+
$this->eventListener->onClientConnect($session->getInternalId(), $address->getIp(), $address->getPort(), $session->getID());
417411
}
418412

419413
private function checkSessions() : void{

src/server/Session.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ private function handleEncapsulatedPacketRoute(EncapsulatedPacket $packet) : voi
228228
$dataPacket = new NewIncomingConnection();
229229
$dataPacket->decode(new PacketSerializer($packet->buffer));
230230

231-
if($dataPacket->address->port === $this->server->getPort() or !$this->server->portChecking){
231+
if($dataPacket->address->getPort() === $this->server->getPort() or !$this->server->portChecking){
232232
$this->state = self::STATE_CONNECTED; //FINALLY!
233233
$this->isTemporal = false;
234234
$this->server->openSession($this);

src/server/UnconnectedMessageHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool
8484
$this->server->sendPacket(OpenConnectionReply1::create($this->server->getID(), false, $packet->mtuSize + 28), $address);
8585
}
8686
}elseif($packet instanceof OpenConnectionRequest2){
87-
if($packet->serverAddress->port === $this->server->getPort() or !$this->server->portChecking){
87+
if($packet->serverAddress->getPort() === $this->server->getPort() or !$this->server->portChecking){
8888
if($packet->mtuSize < Session::MIN_MTU_SIZE){
8989
$this->server->getLogger()->debug("Not creating session for $address due to bad MTU size $packet->mtuSize");
9090
return true;
@@ -93,7 +93,7 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool
9393
$this->server->sendPacket(OpenConnectionReply2::create($this->server->getID(), $address, $mtuSize, false), $address);
9494
$this->server->createSession($address, $packet->clientID, $mtuSize);
9595
}else{
96-
$this->server->getLogger()->debug("Not creating session for $address due to mismatched port, expected " . $this->server->getPort() . ", got " . $packet->serverAddress->port);
96+
$this->server->getLogger()->debug("Not creating session for $address due to mismatched port, expected " . $this->server->getPort() . ", got " . $packet->serverAddress->getPort());
9797
}
9898
}else{
9999
return false;

src/utils/InternetAddress.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@
1717

1818
namespace raklib\utils;
1919

20-
class InternetAddress{
20+
final class InternetAddress{
2121

2222
/**
2323
* @var string
2424
*/
25-
public $ip;
25+
private $ip;
2626
/**
2727
* @var int
2828
*/
29-
public $port;
29+
private $port;
3030
/**
3131
* @var int
3232
*/
33-
public $version;
33+
private $version;
3434

3535
public function __construct(string $address, int $port, int $version){
3636
$this->ip = $address;

0 commit comments

Comments
 (0)