[TASK] Added function for clients, so they're able to send initial data right after connecting to a server.
[TASK] Restructured parts of the core to support sending data right after connecting. [TASK] Adapted core part for automatic reconnect to support sending initial data, too.
This commit is contained in:
@@ -16,17 +16,17 @@ class Socket_SocketHandler {
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $is_connected;
|
||||
protected $isConnected;
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $is_bound;
|
||||
protected $isBound;
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $is_listening;
|
||||
protected $isListening;
|
||||
|
||||
/**
|
||||
* Default constructor. Sets the socket.
|
||||
@@ -37,9 +37,9 @@ class Socket_SocketHandler {
|
||||
public function __construct($socket) {
|
||||
if(is_resource($socket) === FALSE) throw new Exception_SocketException("Invalid socket ressource!", 1289663108);
|
||||
$this->socket = $socket;
|
||||
$this->is_bound = FALSE;
|
||||
$this->is_connected = FALSE;
|
||||
$this->is_listening = FALSE;
|
||||
$this->isBound = FALSE;
|
||||
$this->isConnected = FALSE;
|
||||
$this->isListening = FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,23 +62,23 @@ class Socket_SocketHandler {
|
||||
* @return boolean
|
||||
*/
|
||||
public function isConnected() {
|
||||
return $this->is_connected;
|
||||
return $this->isConnected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets is_connected-flag.
|
||||
* Sets isConnected-flag.
|
||||
* @param boolean $connected
|
||||
* @return void
|
||||
*/
|
||||
public function setConnected($connected) {
|
||||
$this->is_connected = $connected;
|
||||
$this->isConnected = $connected;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function isListening() {
|
||||
return $this->is_listening;
|
||||
return $this->isListening;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,10 +89,10 @@ class Socket_SocketHandler {
|
||||
* @return void
|
||||
*/
|
||||
public function connect($address, $port) {
|
||||
if($this->is_connected === TRUE) throw new Exception_SocketException("Socket is already connected!", 1289663170);
|
||||
if($this->isConnected === TRUE) throw new Exception_SocketException("Socket is already connected!", 1289663170);
|
||||
$result = socket_connect($this->socket, $address, $port);
|
||||
if($result === FALSE) $this->error();
|
||||
$this->is_connected = TRUE;
|
||||
$this->isConnected = TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,12 +103,12 @@ class Socket_SocketHandler {
|
||||
* @return void
|
||||
*/
|
||||
public function bind($address, $port) {
|
||||
if($this->is_bound === TRUE) throw new Exception_SocketException("Socket is already bound!", 1289663212);
|
||||
if($this->isBound === TRUE) throw new Exception_SocketException("Socket is already bound!", 1289663212);
|
||||
$result = socket_set_option($this->socket, SOL_SOCKET, SO_REUSEADDR, 1);
|
||||
if($result === FALSE) $this->error();
|
||||
$result = socket_bind($this->socket, $address, $port);
|
||||
if($result === FALSE) $this->error();
|
||||
$this->is_bound = TRUE;
|
||||
$this->isBound = TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -117,10 +117,10 @@ class Socket_SocketHandler {
|
||||
* @return void
|
||||
*/
|
||||
public function listen() {
|
||||
if($this->is_bound === FALSE) throw new Exception_SocketException("Cannot listen on unbound socket!", 1289663220);
|
||||
if($this->isBound === FALSE) throw new Exception_SocketException("Cannot listen on unbound socket!", 1289663220);
|
||||
$result = socket_listen($this->socket);
|
||||
if($result === FALSE) $this->error();
|
||||
$this->is_listening = TRUE;
|
||||
$this->isListening = TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -128,7 +128,7 @@ class Socket_SocketHandler {
|
||||
* @return void
|
||||
*/
|
||||
public function hasBeenAccepted() {
|
||||
$this->is_connected = TRUE;
|
||||
$this->isConnected = TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -138,8 +138,8 @@ class Socket_SocketHandler {
|
||||
* @return ressource
|
||||
*/
|
||||
public function accept() {
|
||||
if($this->is_bound === FALSE) throw new Exception_SocketException("Cannot accept connections from unbound socket!", 1289663239);
|
||||
if($this->is_listening === FALSE) throw new Exception_SocketException("Cannot accept connections from socket that is not listening!", 1289663241);
|
||||
if($this->isBound === FALSE) throw new Exception_SocketException("Cannot accept connections from unbound socket!", 1289663239);
|
||||
if($this->isListening === FALSE) throw new Exception_SocketException("Cannot accept connections from socket that is not listening!", 1289663241);
|
||||
$accept = socket_accept($this->socket);
|
||||
if($accept === FALSE) $this->error();
|
||||
return $accept;
|
||||
@@ -151,7 +151,7 @@ class Socket_SocketHandler {
|
||||
* @return string
|
||||
*/
|
||||
public function getRemoteName() {
|
||||
if($this->is_connected === FALSE) throw new Exception_SocketException("Socket not connected, cannot retrieve remote name!", 1289928192);
|
||||
if($this->isConnected === FALSE) throw new Exception_SocketException("Socket not connected, cannot retrieve remote name!", 1289928192);
|
||||
$result = socket_getpeername($this->socket, $address, $port);
|
||||
if($result === FALSE) $this->error();
|
||||
return $address . ":" . $port;
|
||||
@@ -163,7 +163,7 @@ class Socket_SocketHandler {
|
||||
* @return string
|
||||
*/
|
||||
public function getLocalName() {
|
||||
if($this->is_bound === FALSE ^ $this->is_connected === FALSE) throw new Exception_SocketException("Socket is not bound or connected, no local name available!", 1289928256);
|
||||
if($this->isBound === FALSE ^ $this->isConnected === FALSE) throw new Exception_SocketException("Socket is not bound or connected, no local name available!", 1289928256);
|
||||
$result = socket_getsockname($this->socket, $address, $port);
|
||||
if($result === FALSE) $this->error();
|
||||
return $address . ":" . $port;
|
||||
@@ -199,7 +199,7 @@ class Socket_SocketHandler {
|
||||
*/
|
||||
public function close() {
|
||||
usleep(100000);
|
||||
if($this->is_connected === TRUE) {
|
||||
if($this->isConnected === TRUE) {
|
||||
$result = socket_shutdown($this->socket);
|
||||
if($result === FALSE) $this->error();
|
||||
}
|
||||
@@ -207,7 +207,7 @@ class Socket_SocketHandler {
|
||||
$result = socket_close($this->socket);
|
||||
if($result === FALSE) $this->error();
|
||||
}
|
||||
$this->is_connected = FALSE;
|
||||
$this->isConnected = FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -71,8 +71,8 @@ class Socket_SocketPool {
|
||||
* @return ressource
|
||||
*/
|
||||
public function createTcpSocket($IPv6 = FALSE) {
|
||||
$Domain = ($IPv6) ? AF_INET6 : AF_INET;
|
||||
$socket = socket_create($Domain, SOCK_STREAM, SOL_TCP);
|
||||
$domain = ($IPv6) ? AF_INET6 : AF_INET;
|
||||
$socket = socket_create($domain, SOCK_STREAM, SOL_TCP);
|
||||
socket_set_block($socket);
|
||||
if($socket === FALSE) throw new Exception_SocketException("socket_create() failed!", 1290273709);
|
||||
$this->addSocket($socket);
|
||||
@@ -90,7 +90,7 @@ class Socket_SocketPool {
|
||||
*/
|
||||
public function select($read, $write, $except, $timeout = NULL) {
|
||||
$n = socket_select($read, $write, $except, $timeout);
|
||||
if($n === FALSE) throw new Exception_SocketException("socket_select() failed!", 1290273693);
|
||||
if($n === FALSE) throw new Exception_SocketException("socket_select() failed! - Error: " . socket_strerror(socket_last_error()), 1290273693);
|
||||
if($n === 0) return array("read" => array(), "write" => array(), "except" => array());
|
||||
return array("read" => $read, "write" => $write, "except" => $except);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user