[TASK] Added the reconnect-flag that works now.
This commit is contained in:
@@ -31,6 +31,11 @@ class Connection_ConnectionHandler {
|
||||
*/
|
||||
protected $socketHandler;
|
||||
|
||||
/**
|
||||
* @var Connection_ConnectionPool
|
||||
*/
|
||||
protected $connectionPool;
|
||||
|
||||
/**
|
||||
* A boolean that indicates whether this Connection is a listening server socket or a usual client socket.
|
||||
* @var boolean
|
||||
@@ -54,6 +59,26 @@ class Connection_ConnectionHandler {
|
||||
*/
|
||||
protected $protocol;
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $IPv6;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $host;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $port;
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $reconnect_on_disconnect;
|
||||
|
||||
/**
|
||||
* Calls parent constructor.
|
||||
* @param $socket
|
||||
@@ -68,6 +93,10 @@ class Connection_ConnectionHandler {
|
||||
$this->group = $group;
|
||||
$this->protocol = $protocol;
|
||||
$this->is_server = FALSE;
|
||||
$this->host = "";
|
||||
$this->port = 0;
|
||||
$this->reconnect_on_disconnect = FALSE;
|
||||
$this->IPv6 = FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,6 +107,14 @@ class Connection_ConnectionHandler {
|
||||
unset($this->socketHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Injector for the internal ConnectionPool access.
|
||||
* @param Connection_ConnectionPool $connectionPool
|
||||
*/
|
||||
public function injectConnectionPool($connectionPool) {
|
||||
$this->connectionPool = $connectionPool;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
@@ -107,6 +144,40 @@ class Connection_ConnectionHandler {
|
||||
return $this->is_server;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the IPv6-flag.
|
||||
* @param boolean $IPv6
|
||||
*/
|
||||
public function setIPv6($IPv6) {
|
||||
$this->IPv6 = $IPv6;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets reconnect_on_disconnect flag.
|
||||
* @param boolean $reconnect
|
||||
*/
|
||||
public function setReconnect($reconnect) {
|
||||
$this->reconnect_on_disconnect = $reconnect;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets reconnect_on_disconnect flag.
|
||||
* @return boolean
|
||||
*/
|
||||
public function getReconnect() {
|
||||
return $this->reconnect_on_disconnect;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is called when socket_read() or socket_write() fail.
|
||||
* It creates a new ConnectionHandler that will reconnect.
|
||||
* @return void
|
||||
*/
|
||||
protected function shutdown() {
|
||||
$this->setConnected(FALSE);
|
||||
$this->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads from SocketHandler, writes into buffer_incoming.
|
||||
* Returns a boolean that will indicate whether the socket is still okay.
|
||||
@@ -115,7 +186,11 @@ class Connection_ConnectionHandler {
|
||||
*/
|
||||
public function readToBuffer() {
|
||||
$data = $this->socketHandler->read();
|
||||
if($data === "") return FALSE;
|
||||
//set connection status flag properly.
|
||||
if($data === "") {
|
||||
$this->shutdown();
|
||||
return FALSE;
|
||||
}
|
||||
$this->buffer_incoming->addData($data);
|
||||
return TRUE;
|
||||
}
|
||||
@@ -129,7 +204,10 @@ class Connection_ConnectionHandler {
|
||||
public function writeFromBuffer() {
|
||||
while($this->buffer_outgoing->hasLines()) {
|
||||
$result = $this->socketHandler->write($this->buffer_outgoing->getNextLine());
|
||||
if($result === FALSE) return FALSE;
|
||||
if($result === FALSE) {
|
||||
$this->shutdown();
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
@@ -219,15 +297,36 @@ class Connection_ConnectionHandler {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls SocketHandler
|
||||
* Calls SocketHandler, stores connection data.
|
||||
* @see Socket_SocketHandler
|
||||
* @throws Exception_SocketException
|
||||
* @param string $address
|
||||
* @param int $port
|
||||
* @return void
|
||||
*/
|
||||
public function connect($address, $port) {
|
||||
$this->host = $address;
|
||||
$this->port = $port;
|
||||
return $this->socketHandler->connect($address, $port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls SocketHandler, uses stored connection data to fork a new instance of itself.
|
||||
* @see Socket_SocketHandler
|
||||
* @throws Exception_SocketException
|
||||
* @throws Exception_GeneralException
|
||||
* @return Connection_ConnectionHandler
|
||||
*/
|
||||
public function reconnect() {
|
||||
if($this->reconnect_on_disconnect === FALSE) throw new Exception_GeneralException("Cannot reconnect: Reconnect-Flag not set!", 1290951385);
|
||||
if(empty($this->host) === TRUE) throw new Exception_GeneralException("Cannot reconnect: No host specified.", 1290950818);
|
||||
if(empty($this->port) === TRUE) throw new Exception_GeneralException("Cannot reconnect: No port specified.", 1290950844);
|
||||
$newConnectionHandler = $this->connectionPool->createTcpConnection($this->group, $this->protocol, $this->IPv6);
|
||||
$newConnectionHandler->setReconnect($this->getReconnect());
|
||||
$newConnectionHandler->connect($this->host, $this->port);
|
||||
return $newConnectionHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls SocketHandler
|
||||
* @see Socket_SocketHandler
|
||||
@@ -235,6 +334,8 @@ class Connection_ConnectionHandler {
|
||||
* @return void
|
||||
*/
|
||||
public function bind($address, $port) {
|
||||
$this->host = $address;
|
||||
$this->port = $port;
|
||||
return $this->socketHandler->bind($address, $port);
|
||||
}
|
||||
|
||||
@@ -257,6 +358,16 @@ class Connection_ConnectionHandler {
|
||||
return $this->socketHandler->isConnected();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the is_connected-flag in the socket handler.
|
||||
* @see Socket_SocketHandler
|
||||
* @param boolean $connected
|
||||
* @return void
|
||||
*/
|
||||
private function setConnected($connected) {
|
||||
return $this->socketHandler->setConnected($connected);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Socket_SocketHandler
|
||||
* @return boolean
|
||||
|
||||
Reference in New Issue
Block a user