[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:28:02 +01:00
parent 8de6bb29b6
commit 16ff75eb1d
32 changed files with 573 additions and 541 deletions

4
.gitignore vendored
View File

@ -1 +1,3 @@
/.project .project
.settings
.buildpath

View File

@ -1,17 +0,0 @@
<?php
/**
* Autoloader function.
* All classes will be in Classes/$Package$/$ClassName$.php
* @throws Exception_GeneralException
* @param string $className
* @return void
*/
function __autoload($className) {
$file = "Classes" . DIRECTORY_SEPARATOR . str_replace("_", DIRECTORY_SEPARATOR, $className) . ".php";
if(file_exists($file) === TRUE) {
require_once($file);
} else {
throw new Exception_GeneralException("Could not autoload class: '" . $className . "' - File does not exist: '" . $file . "'!", 1289659295);
}
}
?>

View File

@ -1,123 +0,0 @@
<?php
/**
* Abstract class that implements the basic api for any client.
* @author jpt
* @abstract
* @package Client
*/
abstract class Client_AbstractClient {
/**
* @var int
*/
protected $ID;
/**
* @var string
*/
protected $group;
/**
* @var mixed Instance of Protocol_AbstractProtocolHandler
*/
protected $protocolHandler;
/**
* Contains a reference to the ClientManager in order to change stuff on the fly.
* @var Client_ClientManager
*/
protected $clientManager;
/**
* Injects ProtocolHandler
* @param mixed $protocolHandler instance of Protocol_AbstractProtocolHandler
* @return void
* @throws Exception_WrongDatatypeException
*/
public function injectProtocolHandler($protocolHandler) {
if(!$protocolHandler instanceof Protocol_AbstractProtocolHandler) {
throw new Exception_WrongDatatypeException("Cannot inject ProtocolHandler! Expected an instance of Protocol_AbstractProtocolHandler, '" . get_type($protocolHandler) . "' given.", 1291156562);
}
$this->protocolHandler = $protocolHandler;
}
/**
* Forwards incoming data to the ProtocolHandler
* Let's the ProtocolHandler do all the work and forward its results to the Clients.
* Return resulting raw data.
* @param string $rawData
* @return string
*/
public function processRawData($rawData) {
$this->protocolHandler->pushRawData($rawData);
while($this->protocolHandler->canWork()) {
$this->processContentObject($this->protocolHandler->work());
}
return $this->protocolHandler->getRawData();
}
/**
* This function will be the main entry point of any client.
* Any reactions will be passed to the ProtocolHandler.
* The ProtocolHandler will then pass the data back to the socket.
* @param string $data
* @return void
*/
abstract protected function processContentObject($contentObject);
/**
* This function will load the given config.
* @param array $config
* @return void
*/
abstract public function loadConfig($config);
/**
* Will reset the connectionStatus of the client.
* Implementation is optional and depends on the client.
* Should be used to reset internal variables.
* @return void
*/
public function resetConnectionStatus() {
}
/**
* This function gets called every time, the connection is established.
* This allows the client to send initial data.
* @return void
*/
public function initializeConnection() {
}
/**
* @param int $id
* @return void
*/
public function setID($id) {
$this->ID = $id;
}
/**
* @param string $group
* @return void
*/
public function setGroup($group) {
$this->group = $group;
}
/**
* Injects ClientManager
* @param Client_ClientManager $clientManager
* @return void
* @throws Exception_WrongDatatypeException
*/
public function injectClientManager($clientManager) {
if(!$clientManager instanceof Client_ClientManager) {
throw new Exception_WrongDatatypeException("Cannot inject ClientManager! Expected an instance of Client_ClientManager, '" . get_type($clientManager) . "' given.", 1291156565);
}
$this->clientManager = $clientManager;
}
}
?>

View File

@ -1,9 +0,0 @@
<?php
/**
* ProtocolHandlerException
* @author jpt
* @package Exception
* @exception
*/
class Exception_ProtocolHandlerException extends Exception_GeneralException {}
?>

View File

@ -1,9 +0,0 @@
<?php
/**
* SocketException
* @author jpt
* @package Exception
* @exception
*/
class Exception_SocketException extends Exception_GeneralException {}
?>

View File

@ -1,9 +0,0 @@
<?php
/**
* WrongDatatypeException
* @author jpt
* @package Exception
* @exception
*/
class Exception_WrongDatatypeException extends Exception_GeneralException {}
?>

View File

@ -1,35 +0,0 @@
<?php
/**
* The abstract ContentObject that will contain
* "parsed data" from the ProtocolHandler
* @author JPT
* @abstract
* @package Protocol
*/
abstract class Protocol_AbstractProtocolContentObject {
/**
* Contains the raw data.
* Standard attribute
* @var string
*/
protected $rawData;
/**
* Default constructor.
* Sets the raw data.
* @return void
*/
public function __construct($rawData) {
$this->rawData = $rawData;
}
/**
* Returns raw data.
* @return string
*/
public function getRawData() {
return $this->rawData;
}
}
?>

View File

@ -1,75 +0,0 @@
<?php
/**
* Abstract ProtocolHandler with the basic api.
* The ProtocolHandler contains two buffers for incoming and outgoing data.
* It will store the data and provide functions to add incoming and get outgoing data.
* @author jpt
* @abstract
* @package Protocol
* TODO: finish this shit
* TODO: Move the buffers here, they will be more useful.
*/
abstract class Protocol_AbstractProtocolHandler {
/**
* Buffer for data that was received by the Client.
* @var Misc_Buffer
*/
protected $bufferIncoming;
/**
* Buffer for data that will be sent by the Client.
* @var Misc_Buffer
*/
protected $bufferOutgoing;
/**
* This function will be called by the constructor.
* It will create the necessary instances of Misc_Buffer and
* put them in $bufferIncoming and $bufferOutgoing.
* @return void
*/
abstract protected function createBuffers();
/**
* General constructor.
* Calls createBuffers()
* @return void
*/
public function __construct() {
$this->createBuffers();
}
/**
* Returns whether work() can be called or not.
* Depends on the protocol and its implementation.
* @return boolean
*/
abstract public function canWork();
/**
* Main worker function of the ProtocolHandler.
* Will fetch data from the bufferIncoming, process it,
* and create a ProtocolContentObject out of it that will be returned to the client.
* @return mixed instance of Protocol_AbstractProtocolContentObject
*/
abstract public function work();
/**
* Adds the incoming data to the internal buffer.
* @param string $rawData
* @return void
*/
public function pushRawData($rawData) {
$this->bufferIncoming->addData($rawData);
}
/**
* Returns all outgoing data of the internal buffer.
* @return string
*/
public function getRawData() {
return $this->bufferOutgoing->getAllBufferContents();
}
}
?>

View File

@ -1,8 +0,0 @@
<?php
/**
* Raw ContentObject
* @author JPT
* @package Protocol
*/
class Protocol_BotProtocolContentObject extends Protocol_AbstractProtocolContentObject {}
?>

View File

@ -1,52 +0,0 @@
<?php
/**
* A 1:1 passthrough handler
* @author JPT
* @package Protocol
*/
class Protocol_BotProtocolHandler extends Protocol_AbstractProtocolHandler {
/**
* Is called by the constructor.
* Shall create the two buffers and set them up.
* @return void
*/
protected function createBuffers() {
$linebreak = "\r\n";
$this->bufferIncoming = new Misc_Buffer($linebreak);
$this->bufferOutgoing = new Misc_Buffer($linebreak);
}
/**
* Main worker function. It will be called in a loop.
* The returned ContentObject will be passed to the client.
* @return Protocol_BotProtocolContentObject
*/
public function work() {
$data = $this->bufferIncoming->getNextLine();
return new Protocol_BotProtocolContentObject($data);
}
/**
* Returns whether there is work to be done.
* Important in order to assure that a ContentObject is created and passed to the Client.
* @return boolean
*/
public function canWork() {
return $this->bufferIncoming->hasLines();
}
/**
* Will put raw data into the outgoing buffer.
* This function will be removed soon.
* The ProtocolHandler shall take care of this directly.
* @deprecated
* @param string $data
* @return void
*/
public function sendRaw($data) {
$this->bufferOutgoing->addLine($data);
}
}
?>

View File

@ -1,8 +0,0 @@
<?php
/**
* Raw ContentObject
* @author JPT
* @package Protocol
*/
class Protocol_RawProtocolContentObject extends Protocol_AbstractProtocolContentObject {}
?>

View File

@ -1,17 +0,0 @@
<?php
/**
* A 1:1 passthrough handler
* @author JPT
* @package Protocol
*/
class Protocol_RawProtocolHandler extends Protocol_AbstractProtocolHandler {
/**
* @param string $data
* @return Protocol_RawProtocolContentObject
*/
public function parse($data) {
return new Protocol_RawProtocolContentObject($data);
}
}
?>

View File

@ -38,7 +38,7 @@ PROJECT_NUMBER =
# If a relative path is entered, it will be relative to the location # If a relative path is entered, it will be relative to the location
# where doxygen was started. If left blank the current directory will be used. # where doxygen was started. If left blank the current directory will be used.
OUTPUT_DIRECTORY = Documentation/Doxygen/ OUTPUT_DIRECTORY = SocketFramework/Documentation/Doxygen/
# If the CREATE_SUBDIRS tag is set to YES, then doxygen will create # If the CREATE_SUBDIRS tag is set to YES, then doxygen will create
# 4096 sub-directories (in 2 levels) under the output directory of each output # 4096 sub-directories (in 2 levels) under the output directory of each output
@ -60,7 +60,7 @@ CREATE_SUBDIRS = YES
# Polish, Portuguese, Romanian, Russian, Serbian, Serbian-Cyrilic, Slovak, # Polish, Portuguese, Romanian, Russian, Serbian, Serbian-Cyrilic, Slovak,
# Slovene, Spanish, Swedish, Ukrainian, and Vietnamese. # Slovene, Spanish, Swedish, Ukrainian, and Vietnamese.
OUTPUT_LANGUAGE = German OUTPUT_LANGUAGE = English
# If the BRIEF_MEMBER_DESC tag is set to YES (the default) Doxygen will # If the BRIEF_MEMBER_DESC tag is set to YES (the default) Doxygen will
# include brief member descriptions after the members that are listed in # include brief member descriptions after the members that are listed in

View File

@ -1,6 +1,6 @@
<?php <?php
error_reporting(E_ALL); error_reporting(E_ALL);
require_once('AutoLoader.php'); require_once('SocketFramework' . DIRECTORY_SEPARATOR . 'Bootstrap.php');
try { try {
require_once('Testcode' . DIRECTORY_SEPARATOR . 'Client' . DIRECTORY_SEPARATOR . 'ClientManagerTest.php'); require_once('Testcode' . DIRECTORY_SEPARATOR . 'Client' . DIRECTORY_SEPARATOR . 'ClientManagerTest.php');

View File

@ -0,0 +1,12 @@
<?php
/**
* This file bootstraps the SocketFramework by initializing the error_reporting to E_ALL
* and initializing the ClassLoader.
* @author jpt
*/
error_reporting(\E_ALL);
require_once("Classes" . \DIRECTORY_SEPARATOR . "Core" . \DIRECTORY_SEPARATOR . "ClassLoader.php");
$classLoader = new \JPT\SocketFramework\Core\ClassLoader();
$classLoader->initialize();
?>

View File

@ -0,0 +1,107 @@
<?php
namespace JPT\SocketFramework\Client;
/**
* Abstract class that implements the basic api for the client dispatcher.
* If you want to write your own client dispatcher (which should be very normal),
* you have to extend this class, so you're able to use it in this socket framework.
*
* @author jpt
* @abstract
* @package Client
*/
abstract class AbstractClientDispatcher implements \JPT\SocketFramework\Client\ClientDispatcherInterface {
/**
* @var int
*/
protected $ID;
/**
* @var string
*/
protected $group;
/**
* Contains a reference to the ClientManager in order to change stuff on the fly.
*
* @var \JPT\SocketFramework\Client\ClientManager
*/
protected $clientManager;
/**
* Forwards incoming data to the ProtocolHandler
* Let's the ProtocolHandler do all the work and forward its results to the Clients.
* Return resulting raw data.
*
* @param string $rawData
* @return string
*/
public function processRawData($rawData) {
}
/**
* This function will load the given config.
*
* @param array $config
* @return void
*/
abstract public function loadConfig($config);
/**
* Will reset the connectionStatus of the client.
* Implementation is optional and depends on the client.
* Should be used to reset internal variables.
*
* @return void
*/
public function resetConnectionStatus() {
}
/**
* This function gets called every time, the connection is established.
* This allows the client to send initial data.
*
* @return void
*/
public function initializeConnection() {
}
/**
* Sets the ID.
*
* @param int $id
* @return void
*/
public function setID($id) {
$this->ID = $id;
}
/**
* Sets the group.
*
* @param string $group
* @return void
*/
public function setGroup($group) {
$this->group = $group;
}
/**
* Injects ClientManager
*
* @param \JPT\SocketFramework\Client\ClientManager $clientManager
* @return void
* @throws \JPT\SocketFramework\Exception\WrongDatatypeException
*/
public function injectClientManager($clientManager) {
if(!$clientManager instanceof \JPT\SocketFramework\Client\ClientManager) {
throw new Exception_WrongDatatypeException("Cannot inject ClientManager! Expected an instance of \JPT\SocketFramework\Client\ClientManager, '" . get_type($clientManager) . "' given.", 1291156565);
}
$this->clientManager = $clientManager;
}
}
?>

View File

@ -0,0 +1,74 @@
<?php
namespace JPT\SocketFramework\Client;
/**
* Interface that includes the real must-have stuff.
*
* @author jpt
* @interface
* @package Client
*/
interface ClientDispatcherInterface {
/**
* Forwards incoming data to the ProtocolHandler
* Let's the ProtocolHandler do all the work and forward its results to the Clients.
* Return resulting raw data.
*
* @param string $rawData
* @return string
*/
public function processRawData($rawData);
/**
* This function will load the given config.
*
* @param array $config
* @return void
*/
abstract public function loadConfig($config);
/**
* Will reset the connectionStatus of the client.
* Implementation is optional and depends on the client.
* Should be used to reset internal variables.
*
* @return void
*/
public function resetConnectionStatus();
/**
* This function gets called every time, the connection is established.
* This allows the client to send initial data.
*
* @return void
*/
public function initializeConnection();
/**
* Sets the ID.
*
* @param int $id
* @return void
*/
public function setID($id);
/**
* Sets the group.
*
* @param string $group
* @return void
*/
public function setGroup($group);
/**
* Injects ClientManager.
*
* @param \JPT\SocketFramework\Client\ClientManager $clientManager
* @return void
* @throws \JPT\SocketFramework\Exception\WrongDatatypeException
*/
public function injectClientManager($clientManager);
}
?>

View File

@ -1,34 +1,48 @@
<?php <?php
namespace JPT\SocketFramework\Client;
/** /**
* This class contains the connection handler. * This class contains the connection handler.
* It handles all the incoming and outgoing data by forwarding it * It handles all the incoming and outgoing data by forwarding it
* to the clients for the specific connection. * to the clients for the specific connection.
*
* @author jpt * @author jpt
* @package Client * @package Client
* @depends Connection * @depends Connection
* TODO: implement routing with some more detailed rules, remove hardcoded client-based routing.
*/ */
class Client_ClientManager { class ClientManager {
/** /**
* @var Connection_ConnectionPool * @var \JPT\SocketFramework\Connection\ConnectionPool
*/ */
protected $connectionPool; protected $connectionPool;
/** /**
* An array that contains all client instances for each connection. * An array that contains all client instances for each connection.
*
* @var array * @var array
*/ */
protected $clientPool; protected $clientDispatcherPool;
/** /**
* An array that contains all protocols we have clients for. * An array that contains all client dispatchers we have for certain "protocols".
* How it works: Each independent client dispatcher is assigned a - hopefully unique -
* identifier for the protocol it implements.
* e.g. ["MyAwesomeIrcClientStuff"] -> $classNameOfMyClientDispatcher
* This way, we can register any client dispatcher from even seperate projects,
* so we can use them within this framework.
*
* Ah, maybe this is important to understand it:
* The protocol identifier pointing to the class name of the client dispatcher
* is used to bind the client dispatcher to a connection created.
*
* @var array * @var array
*/ */
protected $registeredProtocols; protected $registeredClientDispatchers;
/** /**
* Contains Connection-attached configurations. * Contains Connection-attached configurations.
*
* @var array * @var array
*/ */
protected $configPool; protected $configPool;
@ -36,21 +50,23 @@ class Client_ClientManager {
/** /**
* Default constructor. * Default constructor.
*
* @return void * @return void
*/ */
public function __construct() { public function __construct() {
$this->connectionPool = new Connection_ConnectionPool(); $this->connectionPool = new \JPT\SocketFramework\Connection\ConnectionPool();
$this->clientPool = array(); $this->clientDispatcherPool = array();
$this->registeredProtocols = array(); $this->registeredProtocols = array();
$this->configPool = array(); $this->configPool = array();
} }
/** /**
* Default destructor. Closes connections before being killed. * Default destructor. Closes connections before being killed.
*
* @return void * @return void
*/ */
public function __destruct() { public function __destruct() {
unset($this->clientPool); unset($this->clientDispatcherPool);
unset($this->connectionPool); unset($this->connectionPool);
} }
@ -64,7 +80,8 @@ class Client_ClientManager {
/** /**
* Returns the amount of connected connection handlers. * Returns the amount of connected connection handlers.
* @see Connection_ConnectionPool *
* @see \JPT\SocketFramework\Connection\ConnectionPool
* @return int * @return int
*/ */
public function countActiveConnections() { public function countActiveConnections() {
@ -74,12 +91,13 @@ class Client_ClientManager {
/** /**
* Main worker procedure. * Main worker procedure.
* Processes incoming data, calls clients and much more. * Processes incoming data, calls clients and much more.
*
* @return void * @return void
*/ */
public function work() { public function work() {
//first, create clients for connections that do NOT have one already. //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->clientDispatcherPool[$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.
$this->addClientForConnectionHandler($connectionHandler); $this->addClientForConnectionHandler($connectionHandler);
//allow client to send initial stuff right after connecting to the server. //allow client to send initial stuff right after connecting to the server.
@ -113,7 +131,7 @@ class Client_ClientManager {
unset($data); unset($data);
//call the registered client //call the registered client
if(isset($this->clientPool[$connectionHandler->getID()])) $outgoingData .= $this->clientPool[$connectionHandler->getID()]->processRawData($incomingData); if(isset($this->clientDispatcherPool[$connectionHandler->getID()])) $outgoingData .= $this->clientDispatcherPool[$connectionHandler->getID()]->processRawData($incomingData);
//i don't know what to do here... maybe call a connection bridge? //i don't know what to do here... maybe call a connection bridge?
@ -125,10 +143,11 @@ class Client_ClientManager {
/** /**
* Calls ConnectionPool in order to create a simple connection. * Calls ConnectionPool in order to create a simple connection.
* @see Connection_ConnectionPool *
* @see \JPT\SocketFramework\Connection\ConnectionPool
* @param string $group * @param string $group
* @param boolean $IPv6 * @param boolean $IPv6
* @return Connection_ConnectionHandler * @return \JPT\SocketFramework\Connection\ConnectionHandler
*/ */
public function createTcpConnection($group = "", $protocol = "RAW", $IPv6 = FALSE) { public function createTcpConnection($group = "", $protocol = "RAW", $IPv6 = FALSE) {
return $this->connectionPool->createTcpConnection($group, $protocol, $IPv6); return $this->connectionPool->createTcpConnection($group, $protocol, $IPv6);
@ -136,39 +155,43 @@ class Client_ClientManager {
/** /**
* Closes and removes the connection. * Closes and removes the connection.
* @param Connection_ConnectionHandler $connectionHandler *
* @param \JPT\SocketFramework\Connection\ConnectionHandler $connectionHandler
* @return void * @return void
*/ */
public function removeConnection($connectionHandler) { public function removeConnection($connectionHandler) {
unset($this->configPool[$connectionHandler->getID()]); unset($this->configPool[$connectionHandler->getID()]);
unset($this->clientPool[$connectionHandler->getID()]); unset($this->clientDispatcherPool[$connectionHandler->getID()]);
$this->connectionPool->removeConnectionHandler($connectionHandler); $this->connectionPool->removeConnectionHandler($connectionHandler);
} }
/** /**
* Registers a protocol with a specific client. * Registers a protocol identifier and its specific client dispatcher.
* @param string $protocol *
* @param string $protocolIdentifier
* @param string $className * @param string $className
* @return void * @return void
*/ */
public function registerProtocol($protocol, $classNamePartial) { public function registerClientDispatcherByProtocol($protocolIdentifier, $className) {
$this->registeredProtocols[$protocol] = $classNamePartial; $this->registeredClientDispatchers[$protocolIdentifier] = $className;
} }
/** /**
* Unregisters a protocol. * Unregisters a client dispatcher by its protocol identifier.
*
* @param string $protocol * @param string $protocol
* @return void * @return void
*/ */
public function unregisterProtocol($protocol) { public function unregisterClientDispatcherByProtocol($protocolIdentifier) {
unset($this->registeredProtocols[$protocol]); unset($this->registeredClientDispatchers[$protocolIdentifier]);
} }
/** /**
* Attaches a configuration to a connection. * Attaches a configuration to a connection.
* Will overwrite the existing configuration for the connection. * Will overwrite the existing configuration for the connection.
*
* @param array $config * @param array $config
* @param Connection_ConnectionHandler $connectionHandler * @param \JPT\SocketFramework\Connection\ConnectionHandler $connectionHandler
* @return void * @return void
*/ */
public function attachConfig($config, $connectionHandler) { public function attachConfig($config, $connectionHandler) {
@ -176,8 +199,9 @@ class Client_ClientManager {
} }
/** /**
* Calls Connection_Pool * Calls ConnectionPool.
* @see Connection_ConnectionPool *
* @see \JPT\SocketFramework\Connection\ConnectionPool
* @param int $id * @param int $id
* @param string $data * @param string $data
* @return void * @return void
@ -188,6 +212,7 @@ class Client_ClientManager {
/** /**
* Returns a list of available connection IDs. * Returns a list of available connection IDs.
*
* @return array * @return array
*/ */
public function getIDs() { public function getIDs() {
@ -198,6 +223,7 @@ class Client_ClientManager {
/** /**
* Returns a list of available connection groups. * Returns a list of available connection groups.
*
* @return array * @return array
*/ */
public function getGroups() { public function getGroups() {
@ -207,8 +233,9 @@ class Client_ClientManager {
} }
/** /**
* Calls Connection_Pool * Calls Connection_Pool.
* @see Connection_ConnectionPool *
* @see \JPT\SocketFramework\Connection\ConnectionPool
* @param string $group * @param string $group
* @param string $data * @param string $data
* @return void * @return void
@ -218,56 +245,59 @@ class Client_ClientManager {
} }
/** /**
* Searches for the registered client for the given protocol. * Searches the registered client dispatcher for the given protocol.
* Returns a client instance or void. * Returns a client dispatcher instance or void.
*
* @param string $protocol * @param string $protocol
* @return mixed * @return mixed
* @throws Exception_GeneralException * @throws \JPT\SocketFramework\Exception\GeneralException
*/ */
protected function createClientForProtocol($protocol) { protected function createClientDispatcherForProtocol($protocolIdentifier) {
//look for the protocol //look for the protocol
if(!in_array($protocol, $this->registeredProtocols)) { if(!in_array($protocolIdentifier, $this->registeredClientDispatchers)) {
throw new Exception_GeneralException("No client registered for protocol: '" . $protocol . "'!", 1290271651); throw new \JPT\SocketFramework\Exception\GeneralException("No client dispatcher is registered for protocol: '" . $protocol . "'!", 1290271651);
} }
$className = "Client_" . $protocol . "Client"; $className = $this->registeredClientDispatchers[$protocolIdentifier];
//look for the class //look for the class
if(class_exists($className)) { if(class_exists($className)) {
//TODO: This looks ugly and wrong. Ideas, anyone? //TODO: This looks ugly and wrong. Ideas, anyone?
$client = new $className(); $clientDispatcher = new $className();
if(!$client instanceof Client_AbstractClient) throw new Exception_GeneralException("Class '" . $className . "' does not implement the AbstractClient-API!", 1294837055); if(!$clientDispatcher instanceof \JPT\SocketFramework\Client\AbstractClientDispatcher) throw new \JPT\SocketFramework\Exception\GeneralException("Class '" . $className . "' does not implement the AbstractClientDispatcher-API!", 1294837055);
return $client; return $clientDispatcher;
} else { } else {
throw new Exception_GeneralException("Registered class name '" . $className . "' does not exist for protocol: '" . $protocol . "'!", 1290271773); throw new \JPT\SocketFramework\Exception\GeneralException("Registered class name '" . $className . "' does not exist for protocol: '" . $protocol . "'!", 1290271773);
} }
} }
/** /**
* Initializes the client of a given connectionHandler. * Initializes the client dispatcher of a given connectionHandler.
* This should be done after establishing a connection. * This should be done after establishing a connection.
* @param Connection_ConnectionHandler $connectionHandler *
* @param \JPT\SocketFramework\Connection\ConnectionHandler $connectionHandler
* @return void * @return void
*/ */
protected function initializeClientForConnectionHandler($connectionHandler) { protected function initializeClientForConnectionHandler($connectionHandler) {
$this->clientPool[$connectionHandler->getID()]->initializeConnection(); $this->clientDispatcherPool[$connectionHandler->getID()]->initializeConnection();
//after initializing, have it process an empty string in order to get stuff to write. >.< //after initializing, have it process an empty string in order to get stuff to write. >.<
$result = $this->clientPool[$connectionHandler->getID()]->processRawData(""); $result = $this->clientDispatcherPool[$connectionHandler->getID()]->processRawData("");
if($result !== "") $connectionHandler->write($result); if($result !== "") $connectionHandler->write($result);
} }
/** /**
* Creates a new connectionHandler, resets the clients connection status, * Creates a new connectionHandler, resets the client dispatchers connection status,
* copies the old config and assigns the new connectionHandler to the client. * copies the old config and assigns the new connectionHandler to the clientdispatcher.
* @param Connection_ConnectionHandler $connectionHandler *
* @param \JPT\SocketFramework\Connection\ConnectionHandler $connectionHandler
* @return void * @return void
*/ */
protected function handleReconnectForConnectionHandler($connectionHandler) { protected function handleReconnectForConnectionHandler($connectionHandler) {
//have the connectionHandler do the reconnect. //have the connectionHandler do the reconnect.
$newConnectionHandler = $connectionHandler->reconnect(); $newConnectionHandler = $connectionHandler->reconnect();
//get the client and reset it. //get the client and reset it.
$client = $this->clientPool[$connectionHandler->getID()]; $client = $this->clientDispatcherPool[$connectionHandler->getID()];
$client->resetConnectionStatus(); $client->resetConnectionStatus();
//assign the client to the new connectionHandler. //assign the client to the new connectionHandler.
$this->clientPool[$newConnectionHandler->getID()] = $client; $this->clientDispatcherPool[$newConnectionHandler->getID()] = $client;
//get the config and assign it to the new connectionHandler, too. //get the config and assign it to the new connectionHandler, too.
$config = $this->configPool[$connectionHandler->getID()]; $config = $this->configPool[$connectionHandler->getID()];
$this->configPool[$newConnectionHandler->getID()] = $config; $this->configPool[$newConnectionHandler->getID()] = $config;
@ -278,35 +308,35 @@ class Client_ClientManager {
} }
/** /**
* Adds a client to our client pool. * Adds a client dispatcher to our client dispatcher pool.
* @param Connection_ConnectionHandler $connectionHandler * The "RAW" protocol identifier is used to not create a client.
*
* @param \JPT\SocketFramework\Connection\ConnectionHandler $connectionHandler
* @return void * @return void
*/ */
protected function addClientForConnectionHandler($connectionHandler) { protected function addClientDispatcherForConnectionHandler($connectionHandler) {
$protocol = $this->registeredProtocols[$connectionHandler->getProtocol()]; $protocolIdentifier = $this->registeredProtocols[$connectionHandler->getProtocol()];
//do not try this for raw connections //do not try this for "RAW" connections - might or might not be useful.
if($protocol === "RAW") return; if($protocolIdentifier === "RAW") return;
$client = $this->createClientForProtocol($protocol); $clientDispatcher = $this->createClientDispatcherForProtocol($protocolIdentifier);
if(isset($this->configPool[$connectionHandler->getID()])) { if(isset($this->configPool[$connectionHandler->getID()])) {
$client->loadConfig($this->configPool[$connectionHandler->getID()]); $clientDispatcher->loadConfig($this->configPool[$connectionHandler->getID()]);
} }
$client->injectClientManager($this); $clientDispatcher->injectClientManager($this);
$protocolHandlerClass = "Protocol_".$protocol."ProtocolHandler"; $clientDispatcher->setID($connectionHandler->getID());
//TODO: This looks ugly, too. Is there a better way? $clientDispatcher->setGroup($connectionHandler->getGroup());
$protocolHandler = new $protocolHandlerClass(); $this->clientDispatcherPool[$connectionHandler->getID()] = $clientDispatcher;
$client->injectProtocolHandler($protocolHandler);
$client->setID($connectionHandler->getID());
$client->setGroup($connectionHandler->getGroup());
$this->clientPool[$connectionHandler->getID()] = $client;
} }
/** /**
* Removes a client from our client pool. * Removes a client dispatcher from our client dispatcher pool.
* @param Connection_ConnectionHandler $connectionHandler *
* @param \JPT\SocketFramework\Connection\ConnectionHandler $connectionHandler
* @return void * @return void
* @throws \JPT\SocketFramework\Exception\WrongDatatypeException
*/ */
protected function removeClientForConnectionHandler($connectionHandler) { protected function removeClientForConnectionHandler($connectionHandler) {
unset($this->clientPool[$connectionHandler->getID()]); unset($this->clientDispatcherPool[$connectionHandler->getID()]);
$this->connectionPool->removeConnectionHandler($connectionHandler); $this->connectionPool->removeConnectionHandler($connectionHandler);
} }

View File

@ -1,20 +1,24 @@
<?php <?php
namespace JPT\SocketFramework\Connection;
/** /**
* This class is a decorator for the SocketHandler class. * This class is a decorator for the SocketHandler class.
* It provides a buffer for the SocketHandler, so reading and writing * It provides a buffer for the SocketHandler, so reading and writing
* a full line won't be that much pain. * a full line won't be that much pain.
*
* @author jpt * @author jpt
* @package Connection * @package Connection
* @depends Socket * @depends Socket
* @depends Misc * @depends Misc
*/ */
class Connection_ConnectionHandler { class ConnectionHandler {
/** /**
* Buffer that contains incoming data. * Buffer that contains incoming data.
* Contents were received from the SocketHandler. * Contents were received from the SocketHandler.
* This buffer does not use a linebreak, it's a temporary store for data. * This buffer does not use a linebreak, it's a temporary store for data.
* @var Misc_Buffer *
* @var \JPT\SocketFramework\Misc\Buffer
*/ */
protected $bufferIncoming; protected $bufferIncoming;
@ -22,36 +26,41 @@ class Connection_ConnectionHandler {
* Buffer that contains outgoing data. * Buffer that contains outgoing data.
* Contents will be sent to the SocketHandler. * Contents will be sent to the SocketHandler.
* This buffer does not use a linebreak, it's a temporary store for data. * This buffer does not use a linebreak, it's a temporary store for data.
* @var Misc_Buffer *
* @var \JPT\SocketFramework\Misc\Buffer
*/ */
protected $bufferOutgoing; protected $bufferOutgoing;
/** /**
* Contains the instance of the SocketHandler class. * Contains the instance of the SocketHandler class.
* According to the Liskov substitution principle, decoration pattern must be used in this case. * According to the Liskov substitution principle, decoration pattern must be used in this case.
* @var Socket_SocketHandler *
* @var \JPT\SocketFramework\Socket\SocketHandler
*/ */
protected $socketHandler; protected $socketHandler;
/** /**
* @var Connection_ConnectionPool * @var \JPT\SocketFramework\Connection\ConnectionPool
*/ */
protected $connectionPool; protected $connectionPool;
/** /**
* A boolean that indicates whether this Connection is a listening server socket or a usual client socket. * A boolean that indicates whether this Connection is a listening server socket or a usual client socket.
*
* @var boolean * @var boolean
*/ */
protected $isServer; protected $isServer;
/** /**
* Unique Connection ID. * Unique Connection ID.
*
* @var int * @var int
*/ */
protected $id; protected $id;
/** /**
* Connection Group. * Connection Group.
*
* @var string * @var string
*/ */
protected $group; protected $group;
@ -83,14 +92,15 @@ class Connection_ConnectionHandler {
/** /**
* Calls parent constructor. * Calls parent constructor.
*
* @param $socket * @param $socket
* @param $linebreak * @param $linebreak
* @return void * @return void
*/ */
public function __construct($socket, $id, $group = "", $protocol = "") { public function __construct($socket, $id, $group = "", $protocol = "") {
$this->bufferIncoming = new Misc_Buffer(); $this->bufferIncoming = new \JPT\SocketFramework\Misc\Buffer();
$this->bufferOutgoing = new Misc_Buffer(); $this->bufferOutgoing = new \JPT\SocketFramework\Misc\Buffer();
$this->socketHandler = new Socket_SocketHandler($socket); $this->socketHandler = new \JPT\SocketFramework\Socket\SocketHandler($socket);
$this->id = $id; $this->id = $id;
$this->group = $group; $this->group = $group;
$this->protocol = $protocol; $this->protocol = $protocol;
@ -103,6 +113,7 @@ class Connection_ConnectionHandler {
/** /**
* Calls parent destructor. * Calls parent destructor.
*
* @return void * @return void
*/ */
public function __destruct() { public function __destruct() {
@ -111,7 +122,8 @@ class Connection_ConnectionHandler {
/** /**
* Injector for the internal ConnectionPool access. * Injector for the internal ConnectionPool access.
* @param Connection_ConnectionPool $connectionPool *
* @param \JPT\SocketFramework\Connection\ConnectionPool $connectionPool
* @return void * @return void
*/ */
public function injectConnectionPool($connectionPool) { public function injectConnectionPool($connectionPool) {
@ -141,6 +153,7 @@ class Connection_ConnectionHandler {
/** /**
* Returns whether this connection is a listening socket or a general client connection socket. * Returns whether this connection is a listening socket or a general client connection socket.
*
* @return boolean * @return boolean
*/ */
public function isServer() { public function isServer() {
@ -149,6 +162,7 @@ class Connection_ConnectionHandler {
/** /**
* Sets the IPv6-flag. * Sets the IPv6-flag.
*
* @param boolean $IPv6 * @param boolean $IPv6
* @return void * @return void
*/ */
@ -158,6 +172,7 @@ class Connection_ConnectionHandler {
/** /**
* Sets reconnectOnDisconnect flag. * Sets reconnectOnDisconnect flag.
*
* @param boolean $reconnect * @param boolean $reconnect
* @return void * @return void
*/ */
@ -167,6 +182,7 @@ class Connection_ConnectionHandler {
/** /**
* Gets reconnectOnDisconnect flag. * Gets reconnectOnDisconnect flag.
*
* @return boolean * @return boolean
*/ */
public function getReconnect() { public function getReconnect() {
@ -176,6 +192,7 @@ class Connection_ConnectionHandler {
/** /**
* This function is called when socket_read() or socket_write() fail. * This function is called when socket_read() or socket_write() fail.
* It creates a new ConnectionHandler that will reconnect. * It creates a new ConnectionHandler that will reconnect.
*
* @return void * @return void
*/ */
protected function shutdown() { protected function shutdown() {
@ -186,7 +203,8 @@ class Connection_ConnectionHandler {
/** /**
* Reads from SocketHandler, writes into bufferIncoming. * Reads from SocketHandler, writes into bufferIncoming.
* Returns a boolean that will indicate whether the socket is still okay. * Returns a boolean that will indicate whether the socket is still okay.
* @throws Exception_SocketException *
* @throws \JPT\SocketFramework\Exception\SocketException
* @return boolean * @return boolean
*/ */
public function readToBuffer() { public function readToBuffer() {
@ -203,7 +221,8 @@ class Connection_ConnectionHandler {
/** /**
* Writes the bufferOutgoing to the SocketHandler. * Writes the bufferOutgoing to the SocketHandler.
* Returns a boolean that will indicate whether the socket is still okay. * Returns a boolean that will indicate whether the socket is still okay.
* @throws Exception_SocketException *
* @throws \JPT\SocketFramework\Exception\SocketException
* @return boolean * @return boolean
*/ */
public function writeFromBuffer() { public function writeFromBuffer() {
@ -219,8 +238,9 @@ class Connection_ConnectionHandler {
} }
/** /**
* Calls error() on Socket_SocketHandler. * Calls error() on \JPT\SocketFramework\Socket\SocketHandler.
* @throws Socket_SocketExceptions *
* @throws \JPT\SocketFramework\Exception\SocketException
* @return void * @return void
*/ */
public function handleSocketError() { public function handleSocketError() {
@ -229,6 +249,7 @@ class Connection_ConnectionHandler {
/** /**
* Determines whether this ConnectionHandler has data to read. * Determines whether this ConnectionHandler has data to read.
*
* @return boolean * @return boolean
*/ */
public function canRead() { public function canRead() {
@ -237,6 +258,7 @@ class Connection_ConnectionHandler {
/** /**
* Determines whether this ConnectionHandler has data to write. * Determines whether this ConnectionHandler has data to write.
*
* @return boolean * @return boolean
*/ */
public function canWrite() { public function canWrite() {
@ -246,6 +268,7 @@ class Connection_ConnectionHandler {
/** /**
* Reads new data into bufferIncoming. * Reads new data into bufferIncoming.
* Returns a full line from bufferIncoming. * Returns a full line from bufferIncoming.
*
* @return string * @return string
*/ */
public function read() { public function read() {
@ -255,6 +278,7 @@ class Connection_ConnectionHandler {
/** /**
* Writes data into bufferOutgoing. * Writes data into bufferOutgoing.
* Sends data from bufferOutgoing to the SocketHandler. * Sends data from bufferOutgoing to the SocketHandler.
*
* @param $data * @param $data
* @return void * @return void
*/ */
@ -263,9 +287,10 @@ class Connection_ConnectionHandler {
} }
/** /**
* Calls SocketHandler * Calls SocketHandler.
* @see Socket_SocketHandler *
* @throws Exception_SocketException * @see \JPT\SocketFramework\Socket\SocketHandler
* @throws \JPT\SocketFramework\Exception\SocketException
* @return ressource * @return ressource
*/ */
public function accept() { public function accept() {
@ -273,9 +298,10 @@ class Connection_ConnectionHandler {
} }
/** /**
* Calls SocketHandler * Calls SocketHandler.
* @see Socket_SocketHandler *
* @throws Exception_SocketException * @see \JPT\SocketFramework\Socket\SocketHandler
* @throws \JPT\SocketFramework\Exception\SocketException
* @return string * @return string
*/ */
public function getRemoteName() { public function getRemoteName() {
@ -283,9 +309,10 @@ class Connection_ConnectionHandler {
} }
/** /**
* Calls SocketHandler * Calls SocketHandler.
* @see Socket_SocketHandler *
* @throws Exception_SocketException * @see \JPT\SocketFramework\Socket\SocketHandler
* @throws \JPT\SocketFramework\Exception\SocketException
* @return string * @return string
*/ */
public function getLocalName() { public function getLocalName() {
@ -293,9 +320,10 @@ class Connection_ConnectionHandler {
} }
/** /**
* Calls SocketHandler * Calls SocketHandler.
* @see Socket_SocketHandler *
* @throws Exception_SocketException * @see \JPT\SocketFramework\Socket\SocketHandler
* @throws \JPT\SocketFramework\Exception\SocketException
* @return void * @return void
*/ */
public function close() { public function close() {
@ -304,8 +332,9 @@ class Connection_ConnectionHandler {
/** /**
* Calls SocketHandler, stores connection data. * Calls SocketHandler, stores connection data.
* @see Socket_SocketHandler *
* @throws Exception_SocketException * @see \JPT\SocketFramework\Socket\SocketHandler
* @throws \JPT\SocketFramework\Exception\SocketException
* @param string $address * @param string $address
* @param int $port * @param int $port
* @return void * @return void
@ -318,15 +347,16 @@ class Connection_ConnectionHandler {
/** /**
* Calls SocketHandler, uses stored connection data to fork a new instance of itself. * Calls SocketHandler, uses stored connection data to fork a new instance of itself.
* @see Socket_SocketHandler *
* @throws Exception_SocketException * @see \JPT\SocketFramework\Socket\SocketHandler
* @throws Exception_GeneralException * @throws \JPT\SocketFramework\Exception\SocketException
* @return Connection_ConnectionHandler * @throws \JPT\SocketFramework\Exception\GeneralException
* @return \JPT\SocketFramework\Connection\ConnectionHandler
*/ */
public function reconnect() { public function reconnect() {
if($this->reconnectOnDisconnect === FALSE) throw new Exception_GeneralException("Cannot reconnect: Reconnect-Flag not set!", 1290951385); if($this->reconnectOnDisconnect === FALSE) throw new \JPT\SocketFramework\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->host) === TRUE) throw new \JPT\SocketFramework\Exception\GeneralException("Cannot reconnect: No host specified.", 1290950818);
if(empty($this->port) === TRUE) throw new Exception_GeneralException("Cannot reconnect: No port specified.", 1290950844); 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 = $this->connectionPool->createTcpConnection($this->group, $this->protocol, $this->IPv6);
$newConnectionHandler->setReconnect($this->getReconnect()); $newConnectionHandler->setReconnect($this->getReconnect());
$newConnectionHandler->connect($this->host, $this->port); $newConnectionHandler->connect($this->host, $this->port);
@ -334,9 +364,10 @@ class Connection_ConnectionHandler {
} }
/** /**
* Calls SocketHandler * Calls SocketHandler.
* @see Socket_SocketHandler *
* @throws Exception_SocketException * @see \JPT\SocketFramework\Socket\SocketHandler
* @throws \JPT\SocketFramework\Exception\SocketException
* @return void * @return void
*/ */
public function bind($address, $port) { public function bind($address, $port) {
@ -346,9 +377,10 @@ class Connection_ConnectionHandler {
} }
/** /**
* Calls SocketHandler * Calls SocketHandler.
* @see Socket_SocketHandler *
* @throws Exception_SocketException * @see \JPT\SocketFramework\Socket\SocketHandler
* @throws \JPT\SocketFramework\Exception\SocketException
* @return void * @return void
*/ */
public function listen() { public function listen() {
@ -357,7 +389,7 @@ class Connection_ConnectionHandler {
} }
/** /**
* @see Socket_SocketHandler * @see \JPT\SocketFramework\Socket\SocketHandler
* @return boolean * @return boolean
*/ */
public function isConnected() { public function isConnected() {
@ -366,7 +398,8 @@ class Connection_ConnectionHandler {
/** /**
* Sets the is_connected-flag in the socket handler. * Sets the is_connected-flag in the socket handler.
* @see Socket_SocketHandler *
* @see \JPT\SocketFramework\Socket\SocketHandler
* @param boolean $connected * @param boolean $connected
* @return void * @return void
*/ */
@ -375,7 +408,7 @@ class Connection_ConnectionHandler {
} }
/** /**
* @see Socket_SocketHandler * @see \JPT\SocketFramework\Socket\SocketHandler
* @return boolean * @return boolean
*/ */
public function isListening() { public function isListening() {
@ -383,9 +416,10 @@ class Connection_ConnectionHandler {
} }
/** /**
* Calls SocketHandler * Calls SocketHandler.
* @see Socket_SocketHandler *
* @throws Exception_SocketException * @see \JPT\SocketFramework\Socket\SocketHandler
* @throws \JPT\SocketFramework\Exception\SocketException
* @return ressource * @return ressource
*/ */
public function getSocket() { public function getSocket() {
@ -393,15 +427,16 @@ class Connection_ConnectionHandler {
} }
/** /**
* @return Socket_SocketHandler * @return \JPT\SocketFramework\Socket\SocketHandler
*/ */
public function getSocketHandler() { public function getSocketHandler() {
return $this->socketHandler; return $this->socketHandler;
} }
/** /**
* Calls SocketHandler * Calls SocketHandler.
* @see Socket_SocketHandler *
* @see \JPT\SocketFramework\Socket\SocketHandler
* @return void * @return void
*/ */
public function hasBeenAccepted() { public function hasBeenAccepted() {

View File

@ -1,20 +1,25 @@
<?php <?php
namespace JPT\SocketFramework\Connection;
/** /**
* Connection pool class. Contains the SocketPool. * Connection pool class. Contains the SocketPool.
*
* @author jpt * @author jpt
* @package Connection * @package Connection
* @depends Socket * @depends Socket
*/ */
class Connection_ConnectionPool { class ConnectionPool {
/** /**
* SocketPool instance. * SocketPool instance.
* @var Socket_SocketPool *
* @var \JPT\SocketFramework\Socket\SocketPool
*/ */
protected $socketPool; protected $socketPool;
/** /**
* Contains all ConnectionHandler instances. * Contains all ConnectionHandler instances.
*
* @var array * @var array
*/ */
protected $connectionHandlers; protected $connectionHandlers;
@ -25,17 +30,19 @@ class Connection_ConnectionPool {
protected $nextID; protected $nextID;
/** /**
* Creates an Instance of SocketPool * Creates an Instance of SocketPool.
*
* @return void * @return void
*/ */
public function __construct() { public function __construct() {
$this->connectionHandlers = array(); $this->connectionHandlers = array();
$this->socketPool = new Socket_SocketPool(); $this->socketPool = new \JPT\SocketFramework\Socket\SocketPool();
$this->nextID = 1; $this->nextID = 1;
} }
/** /**
* Destroys the SocketPool * Destroys the SocketPool.
*
* @return void * @return void
*/ */
public function __destruct() { public function __destruct() {
@ -44,12 +51,13 @@ class Connection_ConnectionPool {
/** /**
* Creates a new TcpConnection. * Creates a new TcpConnection.
*
* @param boolean $IPv6 will determine whether the socket uses IPv4 or IPv6. * @param boolean $IPv6 will determine whether the socket uses IPv4 or IPv6.
* @return Connection_ConnectionHandler * @return \JPT\SocketFramework\Connection\ConnectionHandler
*/ */
public function createTcpConnection($group = "", $protocol = "RAW", $IPv6 = FALSE) { public function createTcpConnection($group = "", $protocol = "RAW", $IPv6 = FALSE) {
$socket = $this->socketPool->createTcpSocket($IPv6); $socket = $this->socketPool->createTcpSocket($IPv6);
$connectionHandler = new Connection_ConnectionHandler($socket, $this->nextID, $group, $protocol); $connectionHandler = new \JPT\SocketFramework\Connection\ConnectionHandler($socket, $this->nextID, $group, $protocol);
$connectionHandler->setIPv6($IPv6); $connectionHandler->setIPv6($IPv6);
$connectionHandler->injectConnectionPool($this); $connectionHandler->injectConnectionPool($this);
$this->addConnectionHandler($connectionHandler); $this->addConnectionHandler($connectionHandler);
@ -58,6 +66,7 @@ class Connection_ConnectionPool {
/** /**
* Returns the array with the current connectionHandlers. * Returns the array with the current connectionHandlers.
*
* @return array * @return array
*/ */
public function getConnectionHandlers() { public function getConnectionHandlers() {
@ -66,7 +75,8 @@ class Connection_ConnectionPool {
/** /**
* Adds a ConnectionHandler to the pool. * Adds a ConnectionHandler to the pool.
* @param Connection_ConnectionHandler $addConnectionHandler *
* @param \JPT\SocketFramework\Connection\ConnectionHandler $addConnectionHandler
* @return void * @return void
*/ */
public function addConnectionHandler($addConnectionHandler) { public function addConnectionHandler($addConnectionHandler) {
@ -76,7 +86,8 @@ class Connection_ConnectionPool {
/** /**
* Removes a ConnectionHandler from the pool. * Removes a ConnectionHandler from the pool.
* @param Connection_ConnectionHandler $removeConnectionHandler *
* @param \JPT\SocketFramework\Connection\ConnectionHandler $removeConnectionHandler
* @return void * @return void
*/ */
public function removeConnectionHandler($removeConnectionHandler) { public function removeConnectionHandler($removeConnectionHandler) {
@ -91,8 +102,9 @@ class Connection_ConnectionPool {
/** /**
* Returns ConnectionHandler for the given socket ressource. * Returns ConnectionHandler for the given socket ressource.
*
* @param ressource $socketRessource * @param ressource $socketRessource
* @return Connection_ConnectionHandler * @return \JPT\SocketFramework\Connection\ConnectionHandler
*/ */
protected function getConnectionHandlerForSocketRessource($socketRessource) { protected function getConnectionHandlerForSocketRessource($socketRessource) {
foreach($this->connectionHandlers AS $connectionHandler) { foreach($this->connectionHandlers AS $connectionHandler) {
@ -105,9 +117,10 @@ class Connection_ConnectionPool {
/** /**
* Calls select() on SocketPool and updates the ConnectionHandler. * Calls select() on SocketPool and updates the ConnectionHandler.
* Will also accept incoming connections and add them to the pool. * Will also accept incoming connections and add them to the pool.
* @return array An array containing the Connection_ConnectionHandler for each Socket with new data. *
* @throws Exception_GeneralException * @return array An array containing the ConnectionHandler for each Socket with new data.
* @throws Exception_SocketException * @throws \JPT\SocketFramework\Exception\GeneralException
* @throws \JPT\SocketFramework\Exception\SocketException
*/ */
public function select() { public function select() {
$read = array(); $read = array();
@ -135,7 +148,7 @@ class Connection_ConnectionPool {
case "read": case "read":
if($connectionHandler->isServer() === TRUE) { if($connectionHandler->isServer() === TRUE) {
$acceptedSocket = $connectionHandler->accept(); $acceptedSocket = $connectionHandler->accept();
$acceptedSocketHandler = new Connection_ConnectionHandler($acceptedSocket, $this->nextID, $connectionHandler->getGroup(), $connectionHandler->getProtocol()); $acceptedSocketHandler = new \JPT\SocketFramework\Connection\ConnectionHandler($acceptedSocket, $this->nextID, $connectionHandler->getGroup(), $connectionHandler->getProtocol());
$acceptedSocketHandler->hasBeenAccepted(); $acceptedSocketHandler->hasBeenAccepted();
$this->addConnectionHandler($acceptedSocketHandler); $this->addConnectionHandler($acceptedSocketHandler);
} else { } else {
@ -163,9 +176,10 @@ class Connection_ConnectionPool {
/** /**
* Writes the given data to all sockets in $group. * Writes the given data to all sockets in $group.
*
* @param string $group * @param string $group
* @param string $data * @param string $data
* @throws Exception_SocketException * @throws \JPT\SocketFramework\Exception\SocketException
* @return void * @return void
*/ */
public function writeToGroup($group, $data) { public function writeToGroup($group, $data) {
@ -178,6 +192,7 @@ class Connection_ConnectionPool {
/** /**
* Writes the given data to the socket with $id. * Writes the given data to the socket with $id.
*
* @param int $id * @param int $id
* @param string $data * @param string $data
* @return void * @return void
@ -191,7 +206,7 @@ class Connection_ConnectionPool {
} }
/** /**
* @see Socket_SocketPool * @see \JPT\SocketFramework\Socket\SocketPool
* @return int * @return int
*/ */
public function countSockets() { public function countSockets() {
@ -201,6 +216,7 @@ class Connection_ConnectionPool {
/** /**
* Returns the amount of active connections. * Returns the amount of active connections.
* A connection is active when the handler is connected. * A connection is active when the handler is connected.
*
* @return int * @return int
*/ */
public function countActiveConnections() { public function countActiveConnections() {

View File

@ -0,0 +1,44 @@
<?php
namespace JPT\SocketFramework\Core;
/**
* Simple class to capsule the classloader so more than one autoload function can be used.
*
* @author JPT
*/
class ClassLoader {
/**
* Autoloader function.
* All classes will be in Classes/$Package$/$ClassName$.php
*
* @throws \Exception
* @param string $className
* @return void
*/
public function loadClass($className) {
if(substr($className, 0, 19) !== "JPT\\SocketFramework") return;
$className = substr($className, 20);
$fileName = "SocketFramework" . \DIRECTORY_SEPARATOR . "Classes";
$fileName .= \DIRECTORY_SEPARATOR . str_replace("\\", \DIRECTORY_SEPARATOR, $className) . ".php";
if(file_exists($fileName) === TRUE) {
require_once($fileName);
} else {
throw new \Exception("Could not load class: '" . $className . "' - File does not exist: '" . $fileName . "'!", 1289659295);
}
}
/**
* Simple function that registers loadClass as an autoloader.
*
* @return void
* @throws Exception in case spl_autoload_register fails.
*/
public function initialize() {
spl_autoload_register(array($this, 'loadClass'), TRUE, TRUE);
}
}
?>

View File

@ -1,14 +1,18 @@
<?php <?php
namespace JPT\SocketFramework\Exception;
/** /**
* General exception class. * General exception.
*
* @author jpt * @author jpt
* @package Exception * @package Exception
* @exception * @exception
*/ */
class Exception_GeneralException extends Exception { class GeneralException extends \Exception {
/** /**
* Constructor. Throws a new exception in case the exception is unknown. * Constructor. Throws a new exception in case the exception is unknown.
*
* @throws UnknownException * @throws UnknownException
* @param string $message * @param string $message
* @param int $code * @param int $code
@ -23,6 +27,7 @@ class Exception_GeneralException extends Exception {
/** /**
* Puts all the important information into a string. * Puts all the important information into a string.
*
* @return string * @return string
*/ */
public function __toString() { public function __toString() {

View File

@ -0,0 +1,12 @@
<?php
namespace JPT\SocketFramework\Exception;
/**
* SocketException
*
* @author jpt
* @package Exception
* @exception
*/
class SocketException extends GeneralException {}
?>

View File

@ -0,0 +1,12 @@
<?php
namespace JPT\SocketFramework\Exception;
/**
* WrongDatatypeException
*
* @author jpt
* @package Exception
* @exception
*/
class WrongDatatypeException extends GeneralException {}
?>

View File

@ -1,27 +1,33 @@
<?php <?php
namespace JPT\SocketFramework\Misc;
/** /**
* Buffer class for a string. * Buffer class for a string.
* Will fix issues with sockets that don't care about linebreaks. * Will fix issues with sockets that don't care about linebreaks.
* Can also be used for all kinds of purpose. * Can also be used for all kinds of purpose.
*
* @author jpt * @author jpt
* @package Misc * @package Misc
*/ */
class Misc_Buffer { class Buffer {
/** /**
* Bufferstring - contains all the data. * Bufferstring - contains all the data.
*
* @var string * @var string
*/ */
protected $buffer; protected $buffer;
/** /**
* Linebreak to use for pulling lines out of the buffer. * Linebreak to use for pulling lines out of the buffer.
*
* @var string * @var string
*/ */
protected $linebreak; protected $linebreak;
/** /**
* Default constructor. Sets a default linebreak and initializes the buffer. * Default constructor. Sets a default linebreak and initializes the buffer.
*
* @param $linebreak * @param $linebreak
* @return void * @return void
*/ */
@ -32,6 +38,7 @@ class Misc_Buffer {
/** /**
* Prunes empty lines out of the buffer. * Prunes empty lines out of the buffer.
*
* @return void * @return void
*/ */
protected function pruneEmptyLines() { protected function pruneEmptyLines() {
@ -51,6 +58,7 @@ class Misc_Buffer {
/** /**
* Appends data to the buffer. * Appends data to the buffer.
*
* @param string $data * @param string $data
* @return void * @return void
*/ */
@ -61,6 +69,7 @@ class Misc_Buffer {
/** /**
* Appends data + linebreak to the buffer. * Appends data + linebreak to the buffer.
*
* @param string $data * @param string $data
* @return void * @return void
*/ */
@ -71,6 +80,7 @@ class Misc_Buffer {
/** /**
* Returns the next line in the buffer and removes it from the buffer. * Returns the next line in the buffer and removes it from the buffer.
*
* @return string * @return string
* @throws Exception_GeneralException * @throws Exception_GeneralException
*/ */
@ -86,6 +96,7 @@ class Misc_Buffer {
/** /**
* Returns the length of the buffer. * Returns the length of the buffer.
*
* @return int * @return int
*/ */
public function getBufferLength() { public function getBufferLength() {
@ -94,11 +105,12 @@ class Misc_Buffer {
/** /**
* Returns the next $length chars from the buffer or throws an exception if this cannot be done. * Returns the next $length chars from the buffer or throws an exception if this cannot be done.
*
* @throws Exception_GeneralException * @throws Exception_GeneralException
* @return string * @return string
*/ */
public function getNextChars($length) { public function getNextChars($length) {
if(strlen($this->buffer) < $length) throw new Exception_GeneralException("Cannot return ".$length." chars, there are only ".strlen($this->buffer)." chars left!", 1292780386); if(strlen($this->buffer) < $length) throw new \JPT\SocketFramework\Exception\GeneralException("Cannot return ".$length." chars, there are only ".strlen($this->buffer)." chars left!", 1292780386);
$data = substr($this->buffer, 0, $length); $data = substr($this->buffer, 0, $length);
$this->buffer = substr($this->buffer, $length); $this->buffer = substr($this->buffer, $length);
return $data; return $data;
@ -106,16 +118,18 @@ class Misc_Buffer {
/** /**
* Checks whether the buffer contains more lines to process. * Checks whether the buffer contains more lines to process.
*
* @return boolean * @return boolean
* @throws Exception_GeneralException * @throws Exception_GeneralException
*/ */
public function hasLines() { public function hasLines() {
if($this->linebreak === "") throw new Exception_GeneralException("Cannot tell whether the buffer has lines - no linebreak set!", 1290964243); if($this->linebreak === "") throw new \JPT\SocketFramework\Exception\GeneralException("Cannot tell whether the buffer has lines - no linebreak set!", 1290964243);
return (trim(strstr($this->buffer, $this->linebreak, TRUE)) !== "") ? TRUE : FALSE; return (trim(strstr($this->buffer, $this->linebreak, TRUE)) !== "") ? TRUE : FALSE;
} }
/** /**
* Returns TRUE when there is data in the buffer. * Returns TRUE when there is data in the buffer.
*
* @return boolean * @return boolean
*/ */
public function hasData() { public function hasData() {
@ -125,6 +139,7 @@ class Misc_Buffer {
/** /**
* Returns the full buffer contents. * Returns the full buffer contents.
* Also truncates the buffer. * Also truncates the buffer.
*
* @return string * @return string
*/ */
public function getAllBufferContents() { public function getAllBufferContents() {

View File

@ -1,14 +1,18 @@
<?php <?php
namespace JPT\SocketFramework\Socket;
/** /**
* The SocketHandler class will handle a single socket. * The SocketHandler class will handle a single socket.
* It takes care of the very basic socket functions. * It takes care of the very basic socket functions.
*
* @author jpt * @author jpt
* @package Socket * @package Socket
*/ */
class Socket_SocketHandler { class SocketHandler {
/** /**
* Socket ressource * Socket ressource
*
* @var ressource * @var ressource
*/ */
protected $socket; protected $socket;
@ -30,12 +34,13 @@ class Socket_SocketHandler {
/** /**
* Default constructor. Sets the socket. * Default constructor. Sets the socket.
* @throws Exception_SocketException *
* @throws \JPT\SocketFramework\Exception\SocketException
* @param ressource $socket * @param ressource $socket
* @return void * @return void
*/ */
public function __construct($socket) { public function __construct($socket) {
if(is_resource($socket) === FALSE) throw new Exception_SocketException("Invalid socket ressource!", 1289663108); if(is_resource($socket) === FALSE) throw new \JPT\SocketFramework\Exception\SocketException("Invalid socket ressource!", 1289663108);
$this->socket = $socket; $this->socket = $socket;
$this->isBound = FALSE; $this->isBound = FALSE;
$this->isConnected = FALSE; $this->isConnected = FALSE;
@ -44,6 +49,7 @@ class Socket_SocketHandler {
/** /**
* Destructor. Closes socket. * Destructor. Closes socket.
*
* @return void * @return void
*/ */
public function __destruct() { public function __destruct() {
@ -51,7 +57,8 @@ class Socket_SocketHandler {
} }
/** /**
* Returns socket * Returns socket.
*
* @return ressource * @return ressource
*/ */
public function getSocket() { public function getSocket() {
@ -67,6 +74,7 @@ class Socket_SocketHandler {
/** /**
* Sets isConnected-flag. * Sets isConnected-flag.
*
* @param boolean $connected * @param boolean $connected
* @return void * @return void
*/ */
@ -83,13 +91,14 @@ class Socket_SocketHandler {
/** /**
* Connects to a specified address. * Connects to a specified address.
* @throws Exception_SocketException *
* @throws \JPT\SocketFramework\Exception\SocketException
* @param string $address * @param string $address
* @param int $port * @param int $port
* @return void * @return void
*/ */
public function connect($address, $port) { public function connect($address, $port) {
if($this->isConnected === TRUE) throw new Exception_SocketException("Socket is already connected!", 1289663170); if($this->isConnected === TRUE) throw new \JPT\SocketFramework\Exception\SocketException("Socket is already connected!", 1289663170);
$result = socket_connect($this->socket, $address, $port); $result = socket_connect($this->socket, $address, $port);
if($result === FALSE) $this->handleSocketError(); if($result === FALSE) $this->handleSocketError();
$this->isConnected = TRUE; $this->isConnected = TRUE;
@ -97,14 +106,15 @@ class Socket_SocketHandler {
/** /**
* Binds the socket to an address + port. * Binds the socket to an address + port.
* @throws Exception_SocketException *
* @throws \JPT\SocketFramework\Exception\SocketException
* @param string $address * @param string $address
* @param int $port * @param int $port
* @return void * @return void
*/ */
public function bind($address, $port) { public function bind($address, $port) {
if($this->isBound === TRUE) throw new Exception_SocketException("Socket is already bound!", 1289663212); if($this->isBound === TRUE) throw new \JPT\SocketFramework\Exception\SocketException("Socket is already bound!", 1289663212);
$result = socket_set_option($this->socket, SOL_SOCKET, SO_REUSEADDR, 1); $result = socket_set_option($this->socket, \SOL_SOCKET, \SO_REUSEADDR, 1);
if($result === FALSE) $this->handleSocketError(); if($result === FALSE) $this->handleSocketError();
$result = socket_bind($this->socket, $address, $port); $result = socket_bind($this->socket, $address, $port);
if($result === FALSE) $this->handleSocketError(); if($result === FALSE) $this->handleSocketError();
@ -113,11 +123,12 @@ class Socket_SocketHandler {
/** /**
* Let's the socket listen for incoming connections. * Let's the socket listen for incoming connections.
* @throws Exception_SocketException *
* @throws \JPT\SocketFramework\Exception\SocketException
* @return void * @return void
*/ */
public function listen() { public function listen() {
if($this->isBound === FALSE) throw new Exception_SocketException("Cannot listen on unbound socket!", 1289663220); if($this->isBound === FALSE) throw new \JPT\SocketFramework\Exception\SocketException("Cannot listen on unbound socket!", 1289663220);
$result = socket_listen($this->socket); $result = socket_listen($this->socket);
if($result === FALSE) $this->handleSocketError(); if($result === FALSE) $this->handleSocketError();
$this->isListening = TRUE; $this->isListening = TRUE;
@ -125,6 +136,7 @@ class Socket_SocketHandler {
/** /**
* Tells the SocketHandler that he got accepted. * Tells the SocketHandler that he got accepted.
*
* @return void * @return void
*/ */
public function hasBeenAccepted() { public function hasBeenAccepted() {
@ -132,14 +144,15 @@ class Socket_SocketHandler {
} }
/** /**
* Accepts a connection to a socket that is bound and listens * Accepts a connection to a socket that is bound and listens.
* The ressource has to be added to the SocketPool manually * The ressource has to be added to the SocketPool manually.
* @throws Exception_SocketException *
* @throws \JPT\SocketFramework\Exception\SocketException
* @return ressource * @return ressource
*/ */
public function accept() { public function accept() {
if($this->isBound === FALSE) throw new Exception_SocketException("Cannot accept connections from unbound socket!", 1289663239); if($this->isBound === FALSE) throw new \JPT\SocketFramework\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); if($this->isListening === FALSE) throw new \JPT\SocketFramework\Exception\SocketException("Cannot accept connections from socket that is not listening!", 1289663241);
$accept = socket_accept($this->socket); $accept = socket_accept($this->socket);
if($accept === FALSE) $this->handleSocketError(); if($accept === FALSE) $this->handleSocketError();
return $accept; return $accept;
@ -147,11 +160,12 @@ class Socket_SocketHandler {
/** /**
* Returns ip + port of the remote socket. * Returns ip + port of the remote socket.
* @throws Exception_SocketException *
* @throws \JPT\SocketFramework\Exception\SocketException
* @return string * @return string
*/ */
public function getRemoteName() { public function getRemoteName() {
if($this->isConnected === FALSE) throw new Exception_SocketException("Socket not connected, cannot retrieve remote name!", 1289928192); if($this->isConnected === FALSE) throw new \JPT\SocketFramework\Exception\SocketException("Socket not connected, cannot retrieve remote name!", 1289928192);
$result = socket_getpeername($this->socket, $address, $port); $result = socket_getpeername($this->socket, $address, $port);
if($result === FALSE) $this->handleSocketError(); if($result === FALSE) $this->handleSocketError();
return $address . ":" . $port; return $address . ":" . $port;
@ -159,11 +173,12 @@ class Socket_SocketHandler {
/** /**
* Returns ip + port of the local socket. * Returns ip + port of the local socket.
* @throws Exception_SocketException *
* @throws \JPT\SocketFramework\Exception\SocketException
* @return string * @return string
*/ */
public function getLocalName() { public function getLocalName() {
if($this->isBound === FALSE ^ $this->isConnected === 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 \JPT\SocketFramework\Exception\SocketException("Socket is not bound or connected, no local name available!", 1289928256);
$result = socket_getsockname($this->socket, $address, $port); $result = socket_getsockname($this->socket, $address, $port);
if($result === FALSE) $this->handleSocketError(); if($result === FALSE) $this->handleSocketError();
return $address . ":" . $port; return $address . ":" . $port;
@ -171,7 +186,8 @@ class Socket_SocketHandler {
/** /**
* Writes data to the socket. * Writes data to the socket.
* @throws Exception_SocketException *
* @throws \JPT\SocketFramework\Exception\SocketException
* @param string $data * @param string $data
* @return void * @return void
*/ */
@ -182,23 +198,25 @@ class Socket_SocketHandler {
/** /**
* Reads data from the socket. * Reads data from the socket.
* @throws Exception_SocketException *
* @throws \JPT\SocketFramework\Exception\SocketException
* @param int $length * @param int $length
* @return string * @return string
*/ */
public function read($length = 16384) { public function read($length = 16384) {
$result = socket_read($this->socket, $length, PHP_BINARY_READ); $result = socket_read($this->socket, $length, \PHP_BINARY_READ);
if($result === FALSE) $this->handleSocketError(); if($result === FALSE) $this->handleSocketError();
return $result; return $result;
} }
/** /**
* Shuts the socket down after waiting a bit (waiting might be optional). * Shuts the socket down after waiting a bit (waiting might be optional).
* @throws Exception_SocketException *
* @throws \JPT\SocketFramework\Exception\SocketException
* @return void * @return void
*/ */
public function close() { public function close() {
usleep(100000); usleep(100000); //we'll wait here since socket_shutdown _might_ fail without it.
if($this->isConnected === TRUE) { if($this->isConnected === TRUE) {
$result = socket_shutdown($this->socket); $result = socket_shutdown($this->socket);
if($result === FALSE) $this->handleSocketError(); if($result === FALSE) $this->handleSocketError();
@ -212,16 +230,17 @@ class Socket_SocketHandler {
/** /**
* Gets last error from socket. * Gets last error from socket.
* @throws Exception_SocketException *
* @throws \JPT\SocketFramework\Exception\SocketException
* @return void * @return void
*/ */
protected function handleSocketError() { protected function handleSocketError() {
if(is_resource($this->socket) === FALSE) throw new Exception_SocketException("No socket resource available!", 1290954177); if(is_resource($this->socket) === FALSE) throw new \JPT\SocketFramework\Exception\SocketException("No socket resource available!", 1290954177);
$errno = socket_last_error($this->socket); $errno = socket_last_error($this->socket);
$error = socket_strerror($errno); $error = socket_strerror($errno);
socket_clear_error(); socket_clear_error();
$errormsg = "[" . $errno . "] " . $error; $errormsg = "[" . $errno . "] " . $error;
throw new Exception_SocketException("A socket error occured: " . $errormsg, 1289663360); throw new \JPT\SocketFramework\Exception\SocketException("A socket error occured: " . $errormsg, 1289663360);
} }
} }
?> ?>

View File

@ -1,20 +1,25 @@
<?php <?php
namespace JPT\SocketFramework\Socket;
/** /**
* The SocketPool class that will handle all the sockets. * The SocketPool class that will handle all the sockets.
* Manages a pool of socket ressources with socket_select() * Manages a pool of socket ressources with socket_select().
*
* @author jpt * @author jpt
* @package Socket * @package Socket
*/ */
class Socket_SocketPool { class SocketPool {
/** /**
* Pool that contains socket ressources. * Pool that contains socket ressources.
*
* @var array * @var array
*/ */
protected $sockets; protected $sockets;
/** /**
* Creates sockets. * Creates sockets.
*
* @return void * @return void
*/ */
public function __construct() { public function __construct() {
@ -23,6 +28,7 @@ class Socket_SocketPool {
/** /**
* Closes all connections. * Closes all connections.
*
* @return void * @return void
*/ */
public function __destruct() { public function __destruct() {
@ -31,6 +37,7 @@ class Socket_SocketPool {
/** /**
* Returns the amount of active sockets. * Returns the amount of active sockets.
*
* @return int * @return int
*/ */
public function countSockets() { public function countSockets() {
@ -38,7 +45,8 @@ class Socket_SocketPool {
} }
/** /**
* Adds a socket to the pool * Adds a socket to the pool.
*
* @param ressource $addSocket * @param ressource $addSocket
* @return void * @return void
*/ */
@ -49,6 +57,7 @@ class Socket_SocketPool {
/** /**
* Removes the given socket from the pool. * Removes the given socket from the pool.
* The socket will be shutdown. * The socket will be shutdown.
*
* @param ressource $removeSocket * @param ressource $removeSocket
* @return void * @return void
*/ */
@ -66,31 +75,33 @@ class Socket_SocketPool {
/** /**
* Creates a new TcpSocket and adds it to the pool. * Creates a new TcpSocket and adds it to the pool.
* @throws Exception_SocketException *
* @throws \JPT\SocketFramework\Exception\SocketException
* @param boolean $IPv6 will determine whether the socket uses IPv4 or IPv6. * @param boolean $IPv6 will determine whether the socket uses IPv4 or IPv6.
* @return ressource * @return ressource
*/ */
public function createTcpSocket($IPv6 = FALSE) { public function createTcpSocket($IPv6 = FALSE) {
$domain = ($IPv6) ? AF_INET6 : AF_INET; $domain = ($IPv6) ? \AF_INET6 : \AF_INET;
$socket = socket_create($domain, SOCK_STREAM, SOL_TCP); $socket = socket_create($domain, \SOCK_STREAM, \SOL_TCP);
socket_set_block($socket); socket_set_block($socket);
if($socket === FALSE) throw new Exception_SocketException("socket_create() failed!", 1290273709); if($socket === FALSE) throw new \JPT\SocketFramework\Exception\SocketException("socket_create() failed!", 1290273709);
$this->addSocket($socket); $this->addSocket($socket);
return $socket; return $socket;
} }
/** /**
* Returns an array of sockets one can read from. * Returns an array of sockets one can read from.
*
* @param array $read * @param array $read
* @param array $write * @param array $write
* @param array $except * @param array $except
* @param int $timeout * @param int $timeout
* @throws Exception_SocketException * @throws \JPT\SocketFramework\Exception\SocketException
* @return array * @return array
*/ */
public function select($read, $write, $except, $timeout = NULL) { public function select($read, $write, $except, $timeout = NULL) {
$n = socket_select($read, $write, $except, $timeout); $n = socket_select($read, $write, $except, $timeout);
if($n === FALSE) throw new Exception_SocketException("socket_select() failed! - Error: " . socket_strerror(socket_last_error()), 1290273693); if($n === FALSE) throw new \JPT\SocketFramework\Exception\SocketException("socket_select() failed! - Error: " . socket_strerror(socket_last_error()), 1290273693);
if($n === 0) return array("read" => array(), "write" => array(), "except" => array()); if($n === 0) return array("read" => array(), "write" => array(), "except" => array());
return array("read" => $read, "write" => $write, "except" => $except); return array("read" => $read, "write" => $write, "except" => $except);
} }

View File

@ -1,5 +1,5 @@
<?php <?php
$clientManager = new Client_ClientManager(); $clientManager = new \JPT\SocketFramework\Client\ClientManager();
$clientManager->registerProtocol("irc", "Irc"); $clientManager->registerProtocol("irc", "Irc");
$clientManager->registerProtocol("jpt", "Bot"); $clientManager->registerProtocol("jpt", "Bot");