[TASK] Restructured the ClientManagers work()-function.

This commit is contained in:
Jan Philipp Timme 2011-06-29 18:25:43 +00:00
parent f9beef4c81
commit 77be9470a0
1 changed files with 16 additions and 15 deletions

View File

@ -83,7 +83,7 @@ class Client_ClientManager {
* @return void * @return void
*/ */
public function work() { 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) { foreach($this->connectionPool->getConnectionHandlers() AS $connectionHandler) {
if(isset($this->clientPool[$connectionHandler->getID()])) continue; 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.
@ -94,34 +94,35 @@ class Client_ClientManager {
//then, process all connections that have stuff to read. //then, process all connections that have stuff to read.
$connectionHandlers = $this->connectionPool->select(); $connectionHandlers = $this->connectionPool->select();
//nothing to do? return right now. :)
if(isset($connectionHandlers["read"]) === FALSE || count($connectionHandlers["read"]) === 0) return; if(isset($connectionHandlers["read"]) === FALSE || count($connectionHandlers["read"]) === 0) return;
foreach($connectionHandlers["read"] AS $connectionHandler) { foreach($connectionHandlers["read"] AS $connectionHandler) {
//handle disconnects //handle disconnects
if($connectionHandler->isConnected() === FALSE && $connectionHandler->isListening() === FALSE) { if($connectionHandler->isConnected() === FALSE && $connectionHandler->isListening() === FALSE) {
//check whether the reconnect-flag is set //do we need to reconnect? do it.
if($connectionHandler->getReconnect() === TRUE) { if($connectionHandler->getReconnect() === TRUE) $this->handleReconnectForConnectionHandler($connectionHandler);
//do the reconnect stuff... //this old connectionHandler won't do much anymore - kill it.
$this->handleReconnectForConnectionHandler($connectionHandler);
}
//this connectionHandler won't do much anymore - kill it.
$this->removeClientForConnectionHandler($connectionHandler); $this->removeClientForConnectionHandler($connectionHandler);
//since the old connectionHandler does not exist anymore, continue.
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
$this->initializeClientForConnectionHandler($connectionHandler); $this->initializeClientForConnectionHandler($connectionHandler);
} }
//prepare and get input
$incomingData = "";
$outgoingData = "";
while($data = $connectionHandler->read()) $incomingData .= $data;
unset($data);
//call the registered client //call the registered client
if(isset($this->clientPool[$connectionHandler->getID()])) { if(isset($this->clientPool[$connectionHandler->getID()])) $outgoingData .= $this->clientPool[$connectionHandler->getID()]->processRawData($incomingData);
//let the client process the data, send the results back.
while($data = $connectionHandler->read()) { //output will be sent.
$result = $this->clientPool[$connectionHandler->getID()]->processRawData($data); if($outgoingData !== "") $connectionHandler->write($result);
if($result !== "") $connectionHandler->write($result);
}
}
} }
//after that, we're done here. //after that, we're done here.
} }