[TASK] Optimized structure for public function work()
This commit is contained in:
parent
2c5e520c9f
commit
78866ed149
|
@ -80,21 +80,16 @@ class Client_ClientManager {
|
||||||
/**
|
/**
|
||||||
* Main worker function.
|
* Main worker function.
|
||||||
* Processes incoming data, calls clients and much more.
|
* Processes incoming data, calls clients and much more.
|
||||||
* TODO: refactor this? (split it into handleFoobar() functions)
|
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function work() {
|
public function work() {
|
||||||
//firstly, create clients for connections without one.
|
//firstly, create clients for connections without one.
|
||||||
foreach($this->connectionPool->getConnectionHandlers() AS $connectionHandler) {
|
foreach($this->connectionPool->getConnectionHandlers() AS $connectionHandler) {
|
||||||
if(!isset($this->clientPool[$connectionHandler->getID()])) {
|
if(isset($this->clientPool[$connectionHandler->getID()])) continue;
|
||||||
//new connections might need a client, so we'll create one here.
|
//new connections might need a client, so we'll create one here.
|
||||||
$this->addClientForConnectionHandler($connectionHandler);
|
$this->addClientForConnectionHandler($connectionHandler);
|
||||||
//allow client to send initial stuff right after connecting to the server.
|
//allow client to send initial stuff right after connecting to the server.
|
||||||
$this->clientPool[$connectionHandler->getID()]->initializeConnection();
|
$this->initializeClientForConnectionHandler($connectionHandler);
|
||||||
//after initializing, have it process an empty string in order to get stuff to write. >.<
|
|
||||||
$result = $this->clientPool[$connectionHandler->getID()]->processRawData("");
|
|
||||||
if($result !== "") $connectionHandler->write($result);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//then, process all connections that have stuff to read.
|
//then, process all connections that have stuff to read.
|
||||||
|
@ -105,33 +100,18 @@ class Client_ClientManager {
|
||||||
if($connectionHandler->isConnected() === FALSE && $connectionHandler->isListening() === FALSE) {
|
if($connectionHandler->isConnected() === FALSE && $connectionHandler->isListening() === FALSE) {
|
||||||
//check whether the reconnect-flag is set
|
//check whether the reconnect-flag is set
|
||||||
if($connectionHandler->getReconnect() === TRUE) {
|
if($connectionHandler->getReconnect() === TRUE) {
|
||||||
//have the connectionHandler do the reconnect.
|
//do the reconnect stuff...
|
||||||
$newConnectionHandler = $connectionHandler->reconnect();
|
$this->handleReconnectForConnectionHandler($connectionHandler);
|
||||||
//get the client and reset it.
|
|
||||||
$client = $this->clientPool[$connectionHandler->getID()];
|
|
||||||
$client->resetConnectionStatus();
|
|
||||||
//assign the client to the new connectionHandler.
|
|
||||||
$this->clientPool[$newConnectionHandler->getID()] = $client;
|
|
||||||
//get the config and assign it to the new connectionHandler, too.
|
|
||||||
$config = $this->configPool[$connectionHandler->getID()];
|
|
||||||
$this->configPool[$newConnectionHandler->getID()] = $config;
|
|
||||||
//remove old connection
|
|
||||||
$this->removeConnection($connectionHandler);
|
|
||||||
//re-initialize the client
|
|
||||||
$this->clientPool[$newConnectionHandler->getID()]->initializeConnection();
|
|
||||||
//and of course, process stuff to get the buffer filled. :/
|
|
||||||
$result = $this->clientPool[$newConnectionHandler->getID()]->processRawData("");
|
|
||||||
if($result !== "") $newConnectionHandler->write($result);
|
|
||||||
}
|
}
|
||||||
$this->removeClientForConnectionHandler($connectionHandler);
|
|
||||||
//this connectionHandler won't do much anymore - kill it.
|
//this connectionHandler won't do much anymore - kill it.
|
||||||
|
$this->removeClientForConnectionHandler($connectionHandler);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
//handle accepted sockets, adopt them and treat them with care :-)
|
//handle accepted sockets, adopt them and treat them with care :-)
|
||||||
if($connectionHandler->isListening() === TRUE) {
|
if($connectionHandler->isListening() === TRUE) {
|
||||||
$this->addClientForConnectionHandler($connectionHandler);
|
$this->addClientForConnectionHandler($connectionHandler);
|
||||||
//TODO: maybe we want to trigger the "client" here, too? -- YES
|
//TODO: maybe we want to trigger the "client" here, too? -- YES
|
||||||
$this->clientPool[$connectionHandler->getID()]->initializeConnection();
|
$this->initializeClientForConnectionHandler($connectionHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
//call the registered client
|
//call the registered client
|
||||||
|
@ -139,9 +119,7 @@ class Client_ClientManager {
|
||||||
//let the client process the data, send the results back.
|
//let the client process the data, send the results back.
|
||||||
while($data = $connectionHandler->read()) {
|
while($data = $connectionHandler->read()) {
|
||||||
$result = $this->clientPool[$connectionHandler->getID()]->processRawData($data);
|
$result = $this->clientPool[$connectionHandler->getID()]->processRawData($data);
|
||||||
if($result !== "") {
|
if($result !== "") $connectionHandler->write($result);
|
||||||
$connectionHandler->write($result);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -212,6 +190,42 @@ class Client_ClientManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the client of a given connectionHandler.
|
||||||
|
* This should be done after establishing a connection.
|
||||||
|
* @param Connection_ConnectionHandler $connectionHandler
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function initializeClientForConnectionHandler($connectionHandler) {
|
||||||
|
$this->clientPool[$connectionHandler->getID()]->initializeConnection();
|
||||||
|
//after initializing, have it process an empty string in order to get stuff to write. >.<
|
||||||
|
$result = $this->clientPool[$connectionHandler->getID()]->processRawData("");
|
||||||
|
if($result !== "") $connectionHandler->write($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new connectionHandler, resets the clients connection status,
|
||||||
|
* copies the old config and assigns the new connectionHandler to the client.
|
||||||
|
* @param Connection_ConnectionHandler $connectionHandler
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function handleReconnectForConnectionHandler($connectionHandler) {
|
||||||
|
//have the connectionHandler do the reconnect.
|
||||||
|
$newConnectionHandler = $connectionHandler->reconnect();
|
||||||
|
//get the client and reset it.
|
||||||
|
$client = $this->clientPool[$connectionHandler->getID()];
|
||||||
|
$client->resetConnectionStatus();
|
||||||
|
//assign the client to the new connectionHandler.
|
||||||
|
$this->clientPool[$newConnectionHandler->getID()] = $client;
|
||||||
|
//get the config and assign it to the new connectionHandler, too.
|
||||||
|
$config = $this->configPool[$connectionHandler->getID()];
|
||||||
|
$this->configPool[$newConnectionHandler->getID()] = $config;
|
||||||
|
//re-initialize the client
|
||||||
|
$this->initializeClientForConnectionHandler($newConnectionHandler);
|
||||||
|
//remove old connection
|
||||||
|
$this->removeConnection($connectionHandler);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a client to our client pool.
|
* Adds a client to our client pool.
|
||||||
* @param Connection_ConnectionHandler $connectionHandler
|
* @param Connection_ConnectionHandler $connectionHandler
|
||||||
|
|
Loading…
Reference in New Issue