Skip to content

Commit

Permalink
try out spl_object_id
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-grahn committed Sep 22, 2024
1 parent 3d27c21 commit 1c8bb80
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion vend/socket/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function close()
@socket_shutdown($this->socket, 2);
@socket_close($this->socket);
}
$this->socket = (string)$this->socket;
$this->socket = spl_object_id($this->socket);
}

public function write($buffer, $length = 4096)
Expand Down
4 changes: 2 additions & 2 deletions vend/socket/SocketClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function doWrite()
$this->onWrite();
return true;
} catch (\Exception $e) {
$old_socket = (string)$this->socket;
$old_socket = spl_object_id($this->socket);
$this->close();
$this->socket = $old_socket;
$this->disconnected = true;
Expand All @@ -74,7 +74,7 @@ public function read($length = 4096)
$this->read_buffer .= parent::read($length);
$this->onRead();
} catch (\Exception $e) {
$old_socket = (string)$this->socket;
$old_socket = spl_object_id($this->socket);
$this->close();
$this->socket = $old_socket;
$this->disconnected = true;
Expand Down
26 changes: 13 additions & 13 deletions vend/socket/SocketDaemon.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function createServer($server_class, $client_class, $bind_address = 0, $b
if (!is_subclass_of($server, '\chabot\SocketServer')) {
throw new \Exception("Invalid server class specified! Has to be a subclass of \chabot\SocketServer");
}
$this->servers[(string)$server->socket] = $server;
$this->servers[spl_object_id($server->socket)] = $server;
return $server;
}

Expand All @@ -43,8 +43,8 @@ public function createClient($client_class, $remote_address, $remote_port, $bind
}
$client->setNonBlock(true);
$client->connect($remote_address, $remote_port);
output("Adding client " . (string)$client->socket);
$this->clients[(string)$client->socket] = $client;
output("Adding client " . spl_object_id($client->socket));
$this->clients[spl_object_id($client->socket)] = $client;
return $client;
}

Expand Down Expand Up @@ -93,21 +93,21 @@ private function cleanSockets()
# maybe could do this instead: $this->clients = array_filter($this->clients, fn($s) => !$s->disconnected && $this->socket instanceof \Socket)
foreach ($this->clients as $socket) {
if ($socket->disconnected || !$this->socket instanceof \Socket) {
if (isset($this->clients[(string)$socket->socket])) {
unset($this->clients[(string)$socket->socket]);
if (isset($this->clients[spl_object_id($socket->socket)])) {
unset($this->clients[spl_object_id($socket->socket)]);
}
}
}
}

private function getClass($socket)
{
if (isset($this->clients[(string)$socket])) {
return $this->clients[(string)$socket];
} elseif (isset($this->servers[(string)$socket])) {
return $this->servers[(string)$socket];
if (isset($this->clients[spl_object_id($socket)])) {
return $this->clients[spl_object_id($socket)];
} elseif (isset($this->servers[spl_object_id($socket)])) {
return $this->servers[spl_object_id($socket)];
} else {
throw (new \Exception("Could not locate socket class for $socket"));
throw (new \Exception("Could not locate socket class for socket"));
}
}

Expand All @@ -124,7 +124,7 @@ public function process()
$socket = $this->getClass($socket);
if (is_subclass_of($socket, '\chabot\SocketServer')) {
$client = $socket->accept();
$this->clients[(string)$client->socket] = $client;
$this->clients[spl_object_id($client->socket)] = $client;
} elseif (is_subclass_of($socket, '\chabot\SocketClient')) {
// regular onRead event
$socket->read();
Expand All @@ -144,8 +144,8 @@ public function process()
$socket = $this->getClass($socket);
if (is_subclass_of($socket, '\chabot\SocketClient')) {
$socket->onDisconnect();
if (isset($this->clients[(string)$socket->socket])) {
unset($this->clients[(string)$socket->socket]);
if (isset($this->clients[spl_object_id($socket->socket)])) {
unset($this->clients[spl_object_id($socket->socket)]);
}
}
}
Expand Down

0 comments on commit 1c8bb80

Please sign in to comment.