[TASK] Restructured the ClientManagers work()-function.
This commit is contained in:
parent
f9beef4c81
commit
77be9470a0
|
@ -83,7 +83,7 @@ class Client_ClientManager {
|
|||
* @return void
|
||||
*/
|
||||
public function work() {
|
||||
//firstly, create clients for connections without one.
|
||||
//first, create clients for connections that do NOT have one already.
|
||||
foreach($this->connectionPool->getConnectionHandlers() AS $connectionHandler) {
|
||||
if(isset($this->clientPool[$connectionHandler->getID()])) continue;
|
||||
//new connections might need a client, so we'll create one here.
|
||||
|
@ -94,34 +94,35 @@ class Client_ClientManager {
|
|||
|
||||
//then, process all connections that have stuff to read.
|
||||
$connectionHandlers = $this->connectionPool->select();
|
||||
//nothing to do? return right now. :)
|
||||
if(isset($connectionHandlers["read"]) === FALSE || count($connectionHandlers["read"]) === 0) return;
|
||||
foreach($connectionHandlers["read"] AS $connectionHandler) {
|
||||
//handle disconnects
|
||||
if($connectionHandler->isConnected() === FALSE && $connectionHandler->isListening() === FALSE) {
|
||||
//check whether the reconnect-flag is set
|
||||
if($connectionHandler->getReconnect() === TRUE) {
|
||||
//do the reconnect stuff...
|
||||
$this->handleReconnectForConnectionHandler($connectionHandler);
|
||||
}
|
||||
//this connectionHandler won't do much anymore - kill it.
|
||||
//do we need to reconnect? do it.
|
||||
if($connectionHandler->getReconnect() === TRUE) $this->handleReconnectForConnectionHandler($connectionHandler);
|
||||
//this old connectionHandler won't do much anymore - kill it.
|
||||
$this->removeClientForConnectionHandler($connectionHandler);
|
||||
//since the old connectionHandler does not exist anymore, continue.
|
||||
continue;
|
||||
}
|
||||
//handle accepted sockets, adopt them and treat them with care :-)
|
||||
if($connectionHandler->isListening() === TRUE) {
|
||||
$this->addClientForConnectionHandler($connectionHandler);
|
||||
//TODO: maybe we want to trigger the "client" here, too? -- YES
|
||||
$this->initializeClientForConnectionHandler($connectionHandler);
|
||||
}
|
||||
|
||||
//prepare and get input
|
||||
$incomingData = "";
|
||||
$outgoingData = "";
|
||||
while($data = $connectionHandler->read()) $incomingData .= $data;
|
||||
unset($data);
|
||||
|
||||
//call the registered client
|
||||
if(isset($this->clientPool[$connectionHandler->getID()])) {
|
||||
//let the client process the data, send the results back.
|
||||
while($data = $connectionHandler->read()) {
|
||||
$result = $this->clientPool[$connectionHandler->getID()]->processRawData($data);
|
||||
if($result !== "") $connectionHandler->write($result);
|
||||
}
|
||||
}
|
||||
if(isset($this->clientPool[$connectionHandler->getID()])) $outgoingData .= $this->clientPool[$connectionHandler->getID()]->processRawData($incomingData);
|
||||
|
||||
//output will be sent.
|
||||
if($outgoingData !== "") $connectionHandler->write($result);
|
||||
}
|
||||
//after that, we're done here.
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue