[TASK] Moved the project to namespaces.

[TASK] Removed all the protocol handlers.
[TASK] Clients are no longer part of the SocketFramework itself.
[TASK] The Framework provides an "interface" to external clients - the ClientDispatcher.
[TASK] Added the Core package and added a better ClassLoader.
[TASK] Created a bootstrap module to include in other projects.
[TASK] Cleaned up comments, reformatted them.
This commit is contained in:
Jan Philipp Timme
2011-12-03 12:43:43 +01:00
parent 8de6bb29b6
commit 16ff75eb1d
32 changed files with 573 additions and 541 deletions
@@ -0,0 +1,447 @@
<?php
namespace JPT\SocketFramework\Connection;
/**
* This class is a decorator for the SocketHandler class.
* It provides a buffer for the SocketHandler, so reading and writing
* a full line won't be that much pain.
*
* @author jpt
* @package Connection
* @depends Socket
* @depends Misc
*/
class ConnectionHandler {
/**
* Buffer that contains incoming data.
* Contents were received from the SocketHandler.
* This buffer does not use a linebreak, it's a temporary store for data.
*
* @var \JPT\SocketFramework\Misc\Buffer
*/
protected $bufferIncoming;
/**
* Buffer that contains outgoing data.
* Contents will be sent to the SocketHandler.
* This buffer does not use a linebreak, it's a temporary store for data.
*
* @var \JPT\SocketFramework\Misc\Buffer
*/
protected $bufferOutgoing;
/**
* Contains the instance of the SocketHandler class.
* According to the Liskov substitution principle, decoration pattern must be used in this case.
*
* @var \JPT\SocketFramework\Socket\SocketHandler
*/
protected $socketHandler;
/**
* @var \JPT\SocketFramework\Connection\ConnectionPool
*/
protected $connectionPool;
/**
* A boolean that indicates whether this Connection is a listening server socket or a usual client socket.
*
* @var boolean
*/
protected $isServer;
/**
* Unique Connection ID.
*
* @var int
*/
protected $id;
/**
* Connection Group.
*
* @var string
*/
protected $group;
/**
* @var string
*/
protected $protocol;
/**
* @var boolean
*/
protected $IPv6;
/**
* @var string
*/
protected $host;
/**
* @var int
*/
protected $port;
/**
* @var boolean
*/
protected $reconnectOnDisconnect;
/**
* Calls parent constructor.
*
* @param $socket
* @param $linebreak
* @return void
*/
public function __construct($socket, $id, $group = "", $protocol = "") {
$this->bufferIncoming = new \JPT\SocketFramework\Misc\Buffer();
$this->bufferOutgoing = new \JPT\SocketFramework\Misc\Buffer();
$this->socketHandler = new \JPT\SocketFramework\Socket\SocketHandler($socket);
$this->id = $id;
$this->group = $group;
$this->protocol = $protocol;
$this->isServer = FALSE;
$this->host = "";
$this->port = 0;
$this->reconnectOnDisconnect = FALSE;
$this->IPv6 = FALSE;
}
/**
* Calls parent destructor.
*
* @return void
*/
public function __destruct() {
unset($this->socketHandler);
}
/**
* Injector for the internal ConnectionPool access.
*
* @param \JPT\SocketFramework\Connection\ConnectionPool $connectionPool
* @return void
*/
public function injectConnectionPool($connectionPool) {
$this->connectionPool = $connectionPool;
}
/**
* @return string
*/
public function getProtocol() {
return $this->protocol;
}
/**
* @return string Connection Group
*/
public function getGroup() {
return $this->group;
}
/**
* @return int Connection ID
*/
public function getID() {
return $this->id;
}
/**
* Returns whether this connection is a listening socket or a general client connection socket.
*
* @return boolean
*/
public function isServer() {
return $this->isServer;
}
/**
* Sets the IPv6-flag.
*
* @param boolean $IPv6
* @return void
*/
public function setIPv6($IPv6) {
$this->IPv6 = $IPv6;
}
/**
* Sets reconnectOnDisconnect flag.
*
* @param boolean $reconnect
* @return void
*/
public function setReconnect($reconnect) {
$this->reconnectOnDisconnect = $reconnect;
}
/**
* Gets reconnectOnDisconnect flag.
*
* @return boolean
*/
public function getReconnect() {
return $this->reconnectOnDisconnect;
}
/**
* 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 bufferIncoming.
* Returns a boolean that will indicate whether the socket is still okay.
*
* @throws \JPT\SocketFramework\Exception\SocketException
* @return boolean
*/
public function readToBuffer() {
$data = $this->socketHandler->read();
//set connection status flag properly.
if($data === "") {
$this->shutdown();
return FALSE;
}
$this->bufferIncoming->addData($data);
return TRUE;
}
/**
* Writes the bufferOutgoing to the SocketHandler.
* Returns a boolean that will indicate whether the socket is still okay.
*
* @throws \JPT\SocketFramework\Exception\SocketException
* @return boolean
*/
public function writeFromBuffer() {
$bufferContent = $this->bufferOutgoing->getAllBufferContents();
//this might not be cool, but it should do.
if($bufferContent === "") return TRUE;
$result = $this->socketHandler->write($bufferContent);
if($result === FALSE) {
$this->shutdown();
return FALSE;
}
return TRUE;
}
/**
* Calls error() on \JPT\SocketFramework\Socket\SocketHandler.
*
* @throws \JPT\SocketFramework\Exception\SocketException
* @return void
*/
public function handleSocketError() {
$this->socketHandler->error();
}
/**
* Determines whether this ConnectionHandler has data to read.
*
* @return boolean
*/
public function canRead() {
return $this->bufferIncoming->hasData();
}
/**
* Determines whether this ConnectionHandler has data to write.
*
* @return boolean
*/
public function canWrite() {
return $this->bufferOutgoing->hasData();
}
/**
* Reads new data into bufferIncoming.
* Returns a full line from bufferIncoming.
*
* @return string
*/
public function read() {
return $this->bufferIncoming->getAllBufferContents();
}
/**
* Writes data into bufferOutgoing.
* Sends data from bufferOutgoing to the SocketHandler.
*
* @param $data
* @return void
*/
public function write($data) {
$this->bufferOutgoing->addData($data);
}
/**
* Calls SocketHandler.
*
* @see \JPT\SocketFramework\Socket\SocketHandler
* @throws \JPT\SocketFramework\Exception\SocketException
* @return ressource
*/
public function accept() {
return $this->socketHandler->accept();
}
/**
* Calls SocketHandler.
*
* @see \JPT\SocketFramework\Socket\SocketHandler
* @throws \JPT\SocketFramework\Exception\SocketException
* @return string
*/
public function getRemoteName() {
return $this->socketHandler->getRemoteName();
}
/**
* Calls SocketHandler.
*
* @see \JPT\SocketFramework\Socket\SocketHandler
* @throws \JPT\SocketFramework\Exception\SocketException
* @return string
*/
public function getLocalName() {
return $this->socketHandler->getLocalName();
}
/**
* Calls SocketHandler.
*
* @see \JPT\SocketFramework\Socket\SocketHandler
* @throws \JPT\SocketFramework\Exception\SocketException
* @return void
*/
public function close() {
return $this->socketHandler->close();
}
/**
* Calls SocketHandler, stores connection data.
*
* @see \JPT\SocketFramework\Socket\SocketHandler
* @throws \JPT\SocketFramework\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 \JPT\SocketFramework\Socket\SocketHandler
* @throws \JPT\SocketFramework\Exception\SocketException
* @throws \JPT\SocketFramework\Exception\GeneralException
* @return \JPT\SocketFramework\Connection\ConnectionHandler
*/
public function reconnect() {
if($this->reconnectOnDisconnect === FALSE) throw new \JPT\SocketFramework\Exception\GeneralException("Cannot reconnect: Reconnect-Flag not set!", 1290951385);
if(empty($this->host) === TRUE) throw new \JPT\SocketFramework\Exception\GeneralException("Cannot reconnect: No host specified.", 1290950818);
if(empty($this->port) === TRUE) throw new \JPT\SocketFramework\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 \JPT\SocketFramework\Socket\SocketHandler
* @throws \JPT\SocketFramework\Exception\SocketException
* @return void
*/
public function bind($address, $port) {
$this->host = $address;
$this->port = $port;
return $this->socketHandler->bind($address, $port);
}
/**
* Calls SocketHandler.
*
* @see \JPT\SocketFramework\Socket\SocketHandler
* @throws \JPT\SocketFramework\Exception\SocketException
* @return void
*/
public function listen() {
$this->isServer = TRUE;
return $this->socketHandler->listen();
}
/**
* @see \JPT\SocketFramework\Socket\SocketHandler
* @return boolean
*/
public function isConnected() {
return $this->socketHandler->isConnected();
}
/**
* Sets the is_connected-flag in the socket handler.
*
* @see \JPT\SocketFramework\Socket\SocketHandler
* @param boolean $connected
* @return void
*/
protected function setConnected($connected) {
return $this->socketHandler->setConnected($connected);
}
/**
* @see \JPT\SocketFramework\Socket\SocketHandler
* @return boolean
*/
public function isListening() {
return $this->socketHandler->isListening();
}
/**
* Calls SocketHandler.
*
* @see \JPT\SocketFramework\Socket\SocketHandler
* @throws \JPT\SocketFramework\Exception\SocketException
* @return ressource
*/
public function getSocket() {
return $this->socketHandler->getSocket();
}
/**
* @return \JPT\SocketFramework\Socket\SocketHandler
*/
public function getSocketHandler() {
return $this->socketHandler;
}
/**
* Calls SocketHandler.
*
* @see \JPT\SocketFramework\Socket\SocketHandler
* @return void
*/
public function hasBeenAccepted() {
return $this->socketHandler->hasBeenAccepted();
}
}
?>
@@ -0,0 +1,230 @@
<?php
namespace JPT\SocketFramework\Connection;
/**
* Connection pool class. Contains the SocketPool.
*
* @author jpt
* @package Connection
* @depends Socket
*/
class ConnectionPool {
/**
* SocketPool instance.
*
* @var \JPT\SocketFramework\Socket\SocketPool
*/
protected $socketPool;
/**
* Contains all ConnectionHandler instances.
*
* @var array
*/
protected $connectionHandlers;
/**
* @var int Next ID for a new ConnectionHandler
*/
protected $nextID;
/**
* Creates an Instance of SocketPool.
*
* @return void
*/
public function __construct() {
$this->connectionHandlers = array();
$this->socketPool = new \JPT\SocketFramework\Socket\SocketPool();
$this->nextID = 1;
}
/**
* Destroys the SocketPool.
*
* @return void
*/
public function __destruct() {
unset($this->socketPool);
}
/**
* Creates a new TcpConnection.
*
* @param boolean $IPv6 will determine whether the socket uses IPv4 or IPv6.
* @return \JPT\SocketFramework\Connection\ConnectionHandler
*/
public function createTcpConnection($group = "", $protocol = "RAW", $IPv6 = FALSE) {
$socket = $this->socketPool->createTcpSocket($IPv6);
$connectionHandler = new \JPT\SocketFramework\Connection\ConnectionHandler($socket, $this->nextID, $group, $protocol);
$connectionHandler->setIPv6($IPv6);
$connectionHandler->injectConnectionPool($this);
$this->addConnectionHandler($connectionHandler);
return $connectionHandler;
}
/**
* Returns the array with the current connectionHandlers.
*
* @return array
*/
public function getConnectionHandlers() {
return $this->connectionHandlers;
}
/**
* Adds a ConnectionHandler to the pool.
*
* @param \JPT\SocketFramework\Connection\ConnectionHandler $addConnectionHandler
* @return void
*/
public function addConnectionHandler($addConnectionHandler) {
array_push($this->connectionHandlers, $addConnectionHandler);
$this->nextID++;
}
/**
* Removes a ConnectionHandler from the pool.
*
* @param \JPT\SocketFramework\Connection\ConnectionHandler $removeConnectionHandler
* @return void
*/
public function removeConnectionHandler($removeConnectionHandler) {
foreach($this->connectionHandlers AS $key=>$connectionHandler) {
if($connectionHandler === $removeConnectionHandler) {
$this->socketPool->removeSocket($removeConnectionHandler->getSocket());
$removeConnectionHandler->close();
unset($this->connectionHandlers[$key]);
}
}
}
/**
* Returns ConnectionHandler for the given socket ressource.
*
* @param ressource $socketRessource
* @return \JPT\SocketFramework\Connection\ConnectionHandler
*/
protected function getConnectionHandlerForSocketRessource($socketRessource) {
foreach($this->connectionHandlers AS $connectionHandler) {
if($connectionHandler->getSocket() === $socketRessource) {
return $connectionHandler;
}
}
}
/**
* Calls select() on SocketPool and updates the ConnectionHandler.
* Will also accept incoming connections and add them to the pool.
*
* @return array An array containing the ConnectionHandler for each Socket with new data.
* @throws \JPT\SocketFramework\Exception\GeneralException
* @throws \JPT\SocketFramework\Exception\SocketException
*/
public function select() {
$read = array();
$write = array();
$except = array();
foreach($this->connectionHandlers AS $connectionHandler) {
$connectionSocket = $connectionHandler->getSocket();
$read[] = $connectionSocket;
if($connectionHandler->canWrite() === TRUE && $connectionHandler->isServer() === FALSE) {
$write[] = $connectionSocket;
//the line above does not work for freshly connected stuff.
//this is the fallback - just write the stuff - no matter what happens.
if($connectionHandler->writeFromBuffer() === FALSE) $this->removeConnectionHandler($connectionHandler);
}
}
$except = $read;
//Arrays are prepared, let's have socket_select() take a look and process its results.
$tempArray = array();
$selectedSockets = $this->socketPool->select($read, $write, $except);
foreach($selectedSockets AS $selectedType=>$selectedArray) { //read, write, except, this loop won't kill performance
foreach($selectedArray AS $socket) {
$connectionHandler = $this->getConnectionHandlerForSocketRessource($socket);
switch($selectedType) {
case "read":
if($connectionHandler->isServer() === TRUE) {
$acceptedSocket = $connectionHandler->accept();
$acceptedSocketHandler = new \JPT\SocketFramework\Connection\ConnectionHandler($acceptedSocket, $this->nextID, $connectionHandler->getGroup(), $connectionHandler->getProtocol());
$acceptedSocketHandler->hasBeenAccepted();
$this->addConnectionHandler($acceptedSocketHandler);
} else {
$connectionHandler->readToBuffer();
}
break;
case "write":
//this might still work on active connections that are "in use" and already received data.
//however, it does not for freshly connected ones.
if($connectionHandler->writeFromBuffer() === FALSE) $this->removeConnectionHandler($connectionHandler);
break;
case "except":
$connectionHandler->handleSocketError();
break;
default:
throw new Exception_GeneralException("Unknown select type: '" . $selectedType . "'", 1289737080);
break;
}
//Put the ConnectionHandler into the array. We'll return this for further operations.
$tempArray[$selectedType][] = $connectionHandler;
}
}
return $tempArray;
}
/**
* Writes the given data to all sockets in $group.
*
* @param string $group
* @param string $data
* @throws \JPT\SocketFramework\Exception\SocketException
* @return void
*/
public function writeToGroup($group, $data) {
foreach($this->connectionHandlers AS $connectionHandler) {
if($connectionHandler->getGroup() === $group && $connectionHandler->isServer() === FALSE) {
$connectionHandler->write($data);
}
}
}
/**
* Writes the given data to the socket with $id.
*
* @param int $id
* @param string $data
* @return void
*/
public function writeToID($id, $data) {
foreach($this->connectionHandlers AS $connectionHandler) {
if($connectionHandler->getID() === $id && $connectionHandler->isServer() === FALSE) {
$connectionHandler->write($data);
}
}
}
/**
* @see \JPT\SocketFramework\Socket\SocketPool
* @return int
*/
public function countSockets() {
return $this->socketPool->countSockets();
}
/**
* Returns the amount of active connections.
* A connection is active when the handler is connected.
*
* @return int
*/
public function countActiveConnections() {
$count = 0;
foreach($this->connectionHandlers AS $connectionHandler) {
if($connectionHandler->isConnected() === TRUE) $count++;
}
return $count;
}
}
?>