Refactoring
Separated socket server actions into separate methods. Removed master socket from list of clients
This commit is contained in:
parent
ac8644125c
commit
b08fcd3105
@ -33,7 +33,6 @@ class Server implements ServerInterface {
|
|||||||
$socket = $host->getResource();
|
$socket = $host->getResource();
|
||||||
|
|
||||||
$this->_resources[] = $socket;
|
$this->_resources[] = $socket;
|
||||||
$this->_connections[$socket] = $host;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -84,25 +83,17 @@ class Server implements ServerInterface {
|
|||||||
$num_changed = @socket_select($changed, $write = NULL, $except = NULL, NULL);
|
$num_changed = @socket_select($changed, $write = NULL, $except = NULL, NULL);
|
||||||
foreach($changed as $resource) {
|
foreach($changed as $resource) {
|
||||||
if ($this->_master->getResource() == $resource) {
|
if ($this->_master->getResource() == $resource) {
|
||||||
$new_connection = clone $this->_master;
|
$this->onConnect($this->_master);
|
||||||
$this->_resources[] = $new_connection->getResource();
|
|
||||||
$this->_connections[$new_connection->getResource()] = $new_connection;
|
|
||||||
|
|
||||||
// /here $this->_receivers->handleConnection($new_connection);
|
|
||||||
$this->tmpRIterator('handleConnect', $new_connection);
|
|
||||||
} else {
|
} else {
|
||||||
$conn = $this->_connections[$resource];
|
$conn = $this->_connections[$resource];
|
||||||
$data = null;
|
$data = null;
|
||||||
$bytes = $conn->recv($data, 4096, 0);
|
$bytes = $conn->recv($data, 4096, 0);
|
||||||
|
|
||||||
if ($bytes == 0) {
|
if ($bytes == 0) {
|
||||||
$this->tmpRIterator('handleClose', $conn);
|
$this->onClose($conn);
|
||||||
// $this->_receivers->handleDisconnect($conn);
|
|
||||||
|
|
||||||
unset($this->_connections[$resource]);
|
|
||||||
unset($this->_resources[array_search($resource, $this->_resources)]);
|
|
||||||
} else {
|
} else {
|
||||||
$this->tmpRIterator('handleMessage', $data, $conn);
|
$this->onMessage($data, $conn);
|
||||||
|
|
||||||
// new Message
|
// new Message
|
||||||
// $this->_receivers->handleMessage($msg, $conn);
|
// $this->_receivers->handleMessage($msg, $conn);
|
||||||
}
|
}
|
||||||
@ -114,6 +105,28 @@ class Server implements ServerInterface {
|
|||||||
// declare(ticks = 1);
|
// declare(ticks = 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function onConnect(Socket $master) {
|
||||||
|
$new_connection = clone $master;
|
||||||
|
$this->_resources[] = $new_connection->getResource();
|
||||||
|
$this->_connections[$new_connection->getResource()] = $new_connection;
|
||||||
|
|
||||||
|
// /here $this->_receivers->handleConnection($new_connection);
|
||||||
|
$this->tmpRIterator('handleConnect', $new_connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function onMessage($msg, Socket $from) {
|
||||||
|
$this->tmpRIterator('handleMessage', $data, $conn);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function onClose(Socket $conn) {
|
||||||
|
$resource = $conn->getResource();
|
||||||
|
$this->tmpRIterator('handleClose', $conn);
|
||||||
|
// $this->_receivers->handleDisconnect($conn);
|
||||||
|
|
||||||
|
unset($this->_connections[$resource]);
|
||||||
|
unset($this->_resources[array_search($resource, $this->_resources)]);
|
||||||
|
}
|
||||||
|
|
||||||
protected function tmpRIterator() {
|
protected function tmpRIterator() {
|
||||||
$args = func_get_args();
|
$args = func_get_args();
|
||||||
$fn = array_shift($args);
|
$fn = array_shift($args);
|
||||||
|
Loading…
Reference in New Issue
Block a user