[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:
parent
8de6bb29b6
commit
16ff75eb1d
|
@ -1 +1,3 @@
|
|||
/.project
|
||||
.project
|
||||
.settings
|
||||
.buildpath
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
?>
|
|
@ -1,9 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* ProtocolHandlerException
|
||||
* @author jpt
|
||||
* @package Exception
|
||||
* @exception
|
||||
*/
|
||||
class Exception_ProtocolHandlerException extends Exception_GeneralException {}
|
||||
?>
|
|
@ -1,9 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* SocketException
|
||||
* @author jpt
|
||||
* @package Exception
|
||||
* @exception
|
||||
*/
|
||||
class Exception_SocketException extends Exception_GeneralException {}
|
||||
?>
|
|
@ -1,9 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* WrongDatatypeException
|
||||
* @author jpt
|
||||
* @package Exception
|
||||
* @exception
|
||||
*/
|
||||
class Exception_WrongDatatypeException extends Exception_GeneralException {}
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
?>
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
?>
|
|
@ -1,8 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Raw ContentObject
|
||||
* @author JPT
|
||||
* @package Protocol
|
||||
*/
|
||||
class Protocol_BotProtocolContentObject extends Protocol_AbstractProtocolContentObject {}
|
||||
?>
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
|
@ -1,8 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Raw ContentObject
|
||||
* @author JPT
|
||||
* @package Protocol
|
||||
*/
|
||||
class Protocol_RawProtocolContentObject extends Protocol_AbstractProtocolContentObject {}
|
||||
?>
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
?>
|
4
Doxyfile
4
Doxyfile
|
@ -38,7 +38,7 @@ PROJECT_NUMBER =
|
|||
# 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.
|
||||
|
||||
OUTPUT_DIRECTORY = Documentation/Doxygen/
|
||||
OUTPUT_DIRECTORY = SocketFramework/Documentation/Doxygen/
|
||||
|
||||
# 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
|
||||
|
@ -60,7 +60,7 @@ CREATE_SUBDIRS = YES
|
|||
# Polish, Portuguese, Romanian, Russian, Serbian, Serbian-Cyrilic, Slovak,
|
||||
# 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
|
||||
# include brief member descriptions after the members that are listed in
|
||||
|
|
2
Main.php
2
Main.php
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
error_reporting(E_ALL);
|
||||
require_once('AutoLoader.php');
|
||||
require_once('SocketFramework' . DIRECTORY_SEPARATOR . 'Bootstrap.php');
|
||||
|
||||
try {
|
||||
require_once('Testcode' . DIRECTORY_SEPARATOR . 'Client' . DIRECTORY_SEPARATOR . 'ClientManagerTest.php');
|
||||
|
|
|
@ -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();
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
?>
|
|
@ -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);
|
||||
|
||||
}
|
||||
?>
|
|
@ -1,34 +1,48 @@
|
|||
<?php
|
||||
namespace JPT\SocketFramework\Client;
|
||||
|
||||
/**
|
||||
* This class contains the connection handler.
|
||||
* It handles all the incoming and outgoing data by forwarding it
|
||||
* to the clients for the specific connection.
|
||||
*
|
||||
* @author jpt
|
||||
* @package Client
|
||||
* @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;
|
||||
|
||||
/**
|
||||
* An array that contains all client instances for each connection.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
protected $registeredProtocols;
|
||||
protected $registeredClientDispatchers;
|
||||
|
||||
/**
|
||||
* Contains Connection-attached configurations.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $configPool;
|
||||
|
@ -36,21 +50,23 @@ class Client_ClientManager {
|
|||
|
||||
/**
|
||||
* Default constructor.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->connectionPool = new Connection_ConnectionPool();
|
||||
$this->clientPool = array();
|
||||
$this->connectionPool = new \JPT\SocketFramework\Connection\ConnectionPool();
|
||||
$this->clientDispatcherPool = array();
|
||||
$this->registeredProtocols = array();
|
||||
$this->configPool = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Default destructor. Closes connections before being killed.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __destruct() {
|
||||
unset($this->clientPool);
|
||||
unset($this->clientDispatcherPool);
|
||||
unset($this->connectionPool);
|
||||
}
|
||||
|
||||
|
@ -64,7 +80,8 @@ class Client_ClientManager {
|
|||
|
||||
/**
|
||||
* Returns the amount of connected connection handlers.
|
||||
* @see Connection_ConnectionPool
|
||||
*
|
||||
* @see \JPT\SocketFramework\Connection\ConnectionPool
|
||||
* @return int
|
||||
*/
|
||||
public function countActiveConnections() {
|
||||
|
@ -74,12 +91,13 @@ class Client_ClientManager {
|
|||
/**
|
||||
* Main worker procedure.
|
||||
* Processes incoming data, calls clients and much more.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function work() {
|
||||
//first, create clients for connections that do NOT have one already.
|
||||
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.
|
||||
$this->addClientForConnectionHandler($connectionHandler);
|
||||
//allow client to send initial stuff right after connecting to the server.
|
||||
|
@ -113,7 +131,7 @@ class Client_ClientManager {
|
|||
unset($data);
|
||||
|
||||
//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?
|
||||
|
||||
|
@ -125,10 +143,11 @@ class Client_ClientManager {
|
|||
|
||||
/**
|
||||
* Calls ConnectionPool in order to create a simple connection.
|
||||
* @see Connection_ConnectionPool
|
||||
*
|
||||
* @see \JPT\SocketFramework\Connection\ConnectionPool
|
||||
* @param string $group
|
||||
* @param boolean $IPv6
|
||||
* @return Connection_ConnectionHandler
|
||||
* @return \JPT\SocketFramework\Connection\ConnectionHandler
|
||||
*/
|
||||
public function createTcpConnection($group = "", $protocol = "RAW", $IPv6 = FALSE) {
|
||||
return $this->connectionPool->createTcpConnection($group, $protocol, $IPv6);
|
||||
|
@ -136,39 +155,43 @@ class Client_ClientManager {
|
|||
|
||||
/**
|
||||
* Closes and removes the connection.
|
||||
* @param Connection_ConnectionHandler $connectionHandler
|
||||
*
|
||||
* @param \JPT\SocketFramework\Connection\ConnectionHandler $connectionHandler
|
||||
* @return void
|
||||
*/
|
||||
public function removeConnection($connectionHandler) {
|
||||
unset($this->configPool[$connectionHandler->getID()]);
|
||||
unset($this->clientPool[$connectionHandler->getID()]);
|
||||
unset($this->clientDispatcherPool[$connectionHandler->getID()]);
|
||||
$this->connectionPool->removeConnectionHandler($connectionHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a protocol with a specific client.
|
||||
* @param string $protocol
|
||||
* Registers a protocol identifier and its specific client dispatcher.
|
||||
*
|
||||
* @param string $protocolIdentifier
|
||||
* @param string $className
|
||||
* @return void
|
||||
*/
|
||||
public function registerProtocol($protocol, $classNamePartial) {
|
||||
$this->registeredProtocols[$protocol] = $classNamePartial;
|
||||
public function registerClientDispatcherByProtocol($protocolIdentifier, $className) {
|
||||
$this->registeredClientDispatchers[$protocolIdentifier] = $className;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregisters a protocol.
|
||||
* Unregisters a client dispatcher by its protocol identifier.
|
||||
*
|
||||
* @param string $protocol
|
||||
* @return void
|
||||
*/
|
||||
public function unregisterProtocol($protocol) {
|
||||
unset($this->registeredProtocols[$protocol]);
|
||||
public function unregisterClientDispatcherByProtocol($protocolIdentifier) {
|
||||
unset($this->registeredClientDispatchers[$protocolIdentifier]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attaches a configuration to a connection.
|
||||
* Will overwrite the existing configuration for the connection.
|
||||
*
|
||||
* @param array $config
|
||||
* @param Connection_ConnectionHandler $connectionHandler
|
||||
* @param \JPT\SocketFramework\Connection\ConnectionHandler $connectionHandler
|
||||
* @return void
|
||||
*/
|
||||
public function attachConfig($config, $connectionHandler) {
|
||||
|
@ -176,8 +199,9 @@ class Client_ClientManager {
|
|||
}
|
||||
|
||||
/**
|
||||
* Calls Connection_Pool
|
||||
* @see Connection_ConnectionPool
|
||||
* Calls ConnectionPool.
|
||||
*
|
||||
* @see \JPT\SocketFramework\Connection\ConnectionPool
|
||||
* @param int $id
|
||||
* @param string $data
|
||||
* @return void
|
||||
|
@ -188,6 +212,7 @@ class Client_ClientManager {
|
|||
|
||||
/**
|
||||
* Returns a list of available connection IDs.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getIDs() {
|
||||
|
@ -198,6 +223,7 @@ class Client_ClientManager {
|
|||
|
||||
/**
|
||||
* Returns a list of available connection groups.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getGroups() {
|
||||
|
@ -207,8 +233,9 @@ class Client_ClientManager {
|
|||
}
|
||||
|
||||
/**
|
||||
* Calls Connection_Pool
|
||||
* @see Connection_ConnectionPool
|
||||
* Calls Connection_Pool.
|
||||
*
|
||||
* @see \JPT\SocketFramework\Connection\ConnectionPool
|
||||
* @param string $group
|
||||
* @param string $data
|
||||
* @return void
|
||||
|
@ -218,56 +245,59 @@ class Client_ClientManager {
|
|||
}
|
||||
|
||||
/**
|
||||
* Searches for the registered client for the given protocol.
|
||||
* Returns a client instance or void.
|
||||
* Searches the registered client dispatcher for the given protocol.
|
||||
* Returns a client dispatcher instance or void.
|
||||
*
|
||||
* @param string $protocol
|
||||
* @return mixed
|
||||
* @throws Exception_GeneralException
|
||||
* @throws \JPT\SocketFramework\Exception\GeneralException
|
||||
*/
|
||||
protected function createClientForProtocol($protocol) {
|
||||
protected function createClientDispatcherForProtocol($protocolIdentifier) {
|
||||
//look for the protocol
|
||||
if(!in_array($protocol, $this->registeredProtocols)) {
|
||||
throw new Exception_GeneralException("No client registered for protocol: '" . $protocol . "'!", 1290271651);
|
||||
if(!in_array($protocolIdentifier, $this->registeredClientDispatchers)) {
|
||||
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
|
||||
if(class_exists($className)) {
|
||||
//TODO: This looks ugly and wrong. Ideas, anyone?
|
||||
$client = new $className();
|
||||
if(!$client instanceof Client_AbstractClient) throw new Exception_GeneralException("Class '" . $className . "' does not implement the AbstractClient-API!", 1294837055);
|
||||
return $client;
|
||||
$clientDispatcher = new $className();
|
||||
if(!$clientDispatcher instanceof \JPT\SocketFramework\Client\AbstractClientDispatcher) throw new \JPT\SocketFramework\Exception\GeneralException("Class '" . $className . "' does not implement the AbstractClientDispatcher-API!", 1294837055);
|
||||
return $clientDispatcher;
|
||||
} 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.
|
||||
* @param Connection_ConnectionHandler $connectionHandler
|
||||
*
|
||||
* @param \JPT\SocketFramework\Connection\ConnectionHandler $connectionHandler
|
||||
* @return void
|
||||
*/
|
||||
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. >.<
|
||||
$result = $this->clientPool[$connectionHandler->getID()]->processRawData("");
|
||||
$result = $this->clientDispatcherPool[$connectionHandler->getID()]->processRawData("");
|
||||
if($result !== "") $connectionHandler->write($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new connectionHandler, resets the clients connection status,
|
||||
* copies the old config and assigns the new connectionHandler to the client.
|
||||
* @param Connection_ConnectionHandler $connectionHandler
|
||||
* Creates a new connectionHandler, resets the client dispatchers connection status,
|
||||
* copies the old config and assigns the new connectionHandler to the clientdispatcher.
|
||||
*
|
||||
* @param \JPT\SocketFramework\Connection\ConnectionHandler $connectionHandler
|
||||
* @return void
|
||||
*/
|
||||
protected function handleReconnectForConnectionHandler($connectionHandler) {
|
||||
//have the connectionHandler do the reconnect.
|
||||
$newConnectionHandler = $connectionHandler->reconnect();
|
||||
//get the client and reset it.
|
||||
$client = $this->clientPool[$connectionHandler->getID()];
|
||||
$client = $this->clientDispatcherPool[$connectionHandler->getID()];
|
||||
$client->resetConnectionStatus();
|
||||
//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.
|
||||
$config = $this->configPool[$connectionHandler->getID()];
|
||||
$this->configPool[$newConnectionHandler->getID()] = $config;
|
||||
|
@ -278,35 +308,35 @@ class Client_ClientManager {
|
|||
}
|
||||
|
||||
/**
|
||||
* Adds a client to our client pool.
|
||||
* @param Connection_ConnectionHandler $connectionHandler
|
||||
* Adds a client dispatcher to our client dispatcher pool.
|
||||
* The "RAW" protocol identifier is used to not create a client.
|
||||
*
|
||||
* @param \JPT\SocketFramework\Connection\ConnectionHandler $connectionHandler
|
||||
* @return void
|
||||
*/
|
||||
protected function addClientForConnectionHandler($connectionHandler) {
|
||||
$protocol = $this->registeredProtocols[$connectionHandler->getProtocol()];
|
||||
//do not try this for raw connections
|
||||
if($protocol === "RAW") return;
|
||||
$client = $this->createClientForProtocol($protocol);
|
||||
protected function addClientDispatcherForConnectionHandler($connectionHandler) {
|
||||
$protocolIdentifier = $this->registeredProtocols[$connectionHandler->getProtocol()];
|
||||
//do not try this for "RAW" connections - might or might not be useful.
|
||||
if($protocolIdentifier === "RAW") return;
|
||||
$clientDispatcher = $this->createClientDispatcherForProtocol($protocolIdentifier);
|
||||
if(isset($this->configPool[$connectionHandler->getID()])) {
|
||||
$client->loadConfig($this->configPool[$connectionHandler->getID()]);
|
||||
$clientDispatcher->loadConfig($this->configPool[$connectionHandler->getID()]);
|
||||
}
|
||||
$client->injectClientManager($this);
|
||||
$protocolHandlerClass = "Protocol_".$protocol."ProtocolHandler";
|
||||
//TODO: This looks ugly, too. Is there a better way?
|
||||
$protocolHandler = new $protocolHandlerClass();
|
||||
$client->injectProtocolHandler($protocolHandler);
|
||||
$client->setID($connectionHandler->getID());
|
||||
$client->setGroup($connectionHandler->getGroup());
|
||||
$this->clientPool[$connectionHandler->getID()] = $client;
|
||||
$clientDispatcher->injectClientManager($this);
|
||||
$clientDispatcher->setID($connectionHandler->getID());
|
||||
$clientDispatcher->setGroup($connectionHandler->getGroup());
|
||||
$this->clientDispatcherPool[$connectionHandler->getID()] = $clientDispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a client from our client pool.
|
||||
* @param Connection_ConnectionHandler $connectionHandler
|
||||
* Removes a client dispatcher from our client dispatcher pool.
|
||||
*
|
||||
* @param \JPT\SocketFramework\Connection\ConnectionHandler $connectionHandler
|
||||
* @return void
|
||||
* @throws \JPT\SocketFramework\Exception\WrongDatatypeException
|
||||
*/
|
||||
protected function removeClientForConnectionHandler($connectionHandler) {
|
||||
unset($this->clientPool[$connectionHandler->getID()]);
|
||||
unset($this->clientDispatcherPool[$connectionHandler->getID()]);
|
||||
$this->connectionPool->removeConnectionHandler($connectionHandler);
|
||||
}
|
||||
|
|
@ -1,20 +1,24 @@
|
|||
<?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 Connection_ConnectionHandler {
|
||||
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 Misc_Buffer
|
||||
*
|
||||
* @var \JPT\SocketFramework\Misc\Buffer
|
||||
*/
|
||||
protected $bufferIncoming;
|
||||
|
||||
|
@ -22,36 +26,41 @@ class Connection_ConnectionHandler {
|
|||
* 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 Misc_Buffer
|
||||
*
|
||||
* @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 Socket_SocketHandler
|
||||
*
|
||||
* @var \JPT\SocketFramework\Socket\SocketHandler
|
||||
*/
|
||||
protected $socketHandler;
|
||||
|
||||
/**
|
||||
* @var Connection_ConnectionPool
|
||||
* @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;
|
||||
|
@ -83,14 +92,15 @@ class Connection_ConnectionHandler {
|
|||
|
||||
/**
|
||||
* Calls parent constructor.
|
||||
*
|
||||
* @param $socket
|
||||
* @param $linebreak
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($socket, $id, $group = "", $protocol = "") {
|
||||
$this->bufferIncoming = new Misc_Buffer();
|
||||
$this->bufferOutgoing = new Misc_Buffer();
|
||||
$this->socketHandler = new Socket_SocketHandler($socket);
|
||||
$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;
|
||||
|
@ -103,6 +113,7 @@ class Connection_ConnectionHandler {
|
|||
|
||||
/**
|
||||
* Calls parent destructor.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __destruct() {
|
||||
|
@ -111,7 +122,8 @@ class Connection_ConnectionHandler {
|
|||
|
||||
/**
|
||||
* Injector for the internal ConnectionPool access.
|
||||
* @param Connection_ConnectionPool $connectionPool
|
||||
*
|
||||
* @param \JPT\SocketFramework\Connection\ConnectionPool $connectionPool
|
||||
* @return void
|
||||
*/
|
||||
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.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isServer() {
|
||||
|
@ -149,6 +162,7 @@ class Connection_ConnectionHandler {
|
|||
|
||||
/**
|
||||
* Sets the IPv6-flag.
|
||||
*
|
||||
* @param boolean $IPv6
|
||||
* @return void
|
||||
*/
|
||||
|
@ -158,6 +172,7 @@ class Connection_ConnectionHandler {
|
|||
|
||||
/**
|
||||
* Sets reconnectOnDisconnect flag.
|
||||
*
|
||||
* @param boolean $reconnect
|
||||
* @return void
|
||||
*/
|
||||
|
@ -167,6 +182,7 @@ class Connection_ConnectionHandler {
|
|||
|
||||
/**
|
||||
* Gets reconnectOnDisconnect flag.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function getReconnect() {
|
||||
|
@ -176,6 +192,7 @@ class Connection_ConnectionHandler {
|
|||
/**
|
||||
* This function is called when socket_read() or socket_write() fail.
|
||||
* It creates a new ConnectionHandler that will reconnect.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function shutdown() {
|
||||
|
@ -186,7 +203,8 @@ class Connection_ConnectionHandler {
|
|||
/**
|
||||
* Reads from SocketHandler, writes into bufferIncoming.
|
||||
* Returns a boolean that will indicate whether the socket is still okay.
|
||||
* @throws Exception_SocketException
|
||||
*
|
||||
* @throws \JPT\SocketFramework\Exception\SocketException
|
||||
* @return boolean
|
||||
*/
|
||||
public function readToBuffer() {
|
||||
|
@ -203,7 +221,8 @@ class Connection_ConnectionHandler {
|
|||
/**
|
||||
* Writes the bufferOutgoing to the SocketHandler.
|
||||
* Returns a boolean that will indicate whether the socket is still okay.
|
||||
* @throws Exception_SocketException
|
||||
*
|
||||
* @throws \JPT\SocketFramework\Exception\SocketException
|
||||
* @return boolean
|
||||
*/
|
||||
public function writeFromBuffer() {
|
||||
|
@ -219,8 +238,9 @@ class Connection_ConnectionHandler {
|
|||
}
|
||||
|
||||
/**
|
||||
* Calls error() on Socket_SocketHandler.
|
||||
* @throws Socket_SocketExceptions
|
||||
* Calls error() on \JPT\SocketFramework\Socket\SocketHandler.
|
||||
*
|
||||
* @throws \JPT\SocketFramework\Exception\SocketException
|
||||
* @return void
|
||||
*/
|
||||
public function handleSocketError() {
|
||||
|
@ -229,6 +249,7 @@ class Connection_ConnectionHandler {
|
|||
|
||||
/**
|
||||
* Determines whether this ConnectionHandler has data to read.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function canRead() {
|
||||
|
@ -237,6 +258,7 @@ class Connection_ConnectionHandler {
|
|||
|
||||
/**
|
||||
* Determines whether this ConnectionHandler has data to write.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function canWrite() {
|
||||
|
@ -246,6 +268,7 @@ class Connection_ConnectionHandler {
|
|||
/**
|
||||
* Reads new data into bufferIncoming.
|
||||
* Returns a full line from bufferIncoming.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function read() {
|
||||
|
@ -255,6 +278,7 @@ class Connection_ConnectionHandler {
|
|||
/**
|
||||
* Writes data into bufferOutgoing.
|
||||
* Sends data from bufferOutgoing to the SocketHandler.
|
||||
*
|
||||
* @param $data
|
||||
* @return void
|
||||
*/
|
||||
|
@ -263,9 +287,10 @@ class Connection_ConnectionHandler {
|
|||
}
|
||||
|
||||
/**
|
||||
* Calls SocketHandler
|
||||
* @see Socket_SocketHandler
|
||||
* @throws Exception_SocketException
|
||||
* Calls SocketHandler.
|
||||
*
|
||||
* @see \JPT\SocketFramework\Socket\SocketHandler
|
||||
* @throws \JPT\SocketFramework\Exception\SocketException
|
||||
* @return ressource
|
||||
*/
|
||||
public function accept() {
|
||||
|
@ -273,9 +298,10 @@ class Connection_ConnectionHandler {
|
|||
}
|
||||
|
||||
/**
|
||||
* Calls SocketHandler
|
||||
* @see Socket_SocketHandler
|
||||
* @throws Exception_SocketException
|
||||
* Calls SocketHandler.
|
||||
*
|
||||
* @see \JPT\SocketFramework\Socket\SocketHandler
|
||||
* @throws \JPT\SocketFramework\Exception\SocketException
|
||||
* @return string
|
||||
*/
|
||||
public function getRemoteName() {
|
||||
|
@ -283,9 +309,10 @@ class Connection_ConnectionHandler {
|
|||
}
|
||||
|
||||
/**
|
||||
* Calls SocketHandler
|
||||
* @see Socket_SocketHandler
|
||||
* @throws Exception_SocketException
|
||||
* Calls SocketHandler.
|
||||
*
|
||||
* @see \JPT\SocketFramework\Socket\SocketHandler
|
||||
* @throws \JPT\SocketFramework\Exception\SocketException
|
||||
* @return string
|
||||
*/
|
||||
public function getLocalName() {
|
||||
|
@ -293,9 +320,10 @@ class Connection_ConnectionHandler {
|
|||
}
|
||||
|
||||
/**
|
||||
* Calls SocketHandler
|
||||
* @see Socket_SocketHandler
|
||||
* @throws Exception_SocketException
|
||||
* Calls SocketHandler.
|
||||
*
|
||||
* @see \JPT\SocketFramework\Socket\SocketHandler
|
||||
* @throws \JPT\SocketFramework\Exception\SocketException
|
||||
* @return void
|
||||
*/
|
||||
public function close() {
|
||||
|
@ -304,8 +332,9 @@ class Connection_ConnectionHandler {
|
|||
|
||||
/**
|
||||
* 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 int $port
|
||||
* @return void
|
||||
|
@ -318,15 +347,16 @@ class Connection_ConnectionHandler {
|
|||
|
||||
/**
|
||||
* Calls SocketHandler, uses stored connection data to fork a new instance of itself.
|
||||
* @see Socket_SocketHandler
|
||||
* @throws Exception_SocketException
|
||||
* @throws Exception_GeneralException
|
||||
* @return Connection_ConnectionHandler
|
||||
*
|
||||
* @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 Exception_GeneralException("Cannot reconnect: Reconnect-Flag not set!", 1290951385);
|
||||
if(empty($this->host) === TRUE) throw new Exception_GeneralException("Cannot reconnect: No host specified.", 1290950818);
|
||||
if(empty($this->port) === TRUE) throw new Exception_GeneralException("Cannot reconnect: No port specified.", 1290950844);
|
||||
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);
|
||||
|
@ -334,9 +364,10 @@ class Connection_ConnectionHandler {
|
|||
}
|
||||
|
||||
/**
|
||||
* Calls SocketHandler
|
||||
* @see Socket_SocketHandler
|
||||
* @throws Exception_SocketException
|
||||
* Calls SocketHandler.
|
||||
*
|
||||
* @see \JPT\SocketFramework\Socket\SocketHandler
|
||||
* @throws \JPT\SocketFramework\Exception\SocketException
|
||||
* @return void
|
||||
*/
|
||||
public function bind($address, $port) {
|
||||
|
@ -346,9 +377,10 @@ class Connection_ConnectionHandler {
|
|||
}
|
||||
|
||||
/**
|
||||
* Calls SocketHandler
|
||||
* @see Socket_SocketHandler
|
||||
* @throws Exception_SocketException
|
||||
* Calls SocketHandler.
|
||||
*
|
||||
* @see \JPT\SocketFramework\Socket\SocketHandler
|
||||
* @throws \JPT\SocketFramework\Exception\SocketException
|
||||
* @return void
|
||||
*/
|
||||
public function listen() {
|
||||
|
@ -357,7 +389,7 @@ class Connection_ConnectionHandler {
|
|||
}
|
||||
|
||||
/**
|
||||
* @see Socket_SocketHandler
|
||||
* @see \JPT\SocketFramework\Socket\SocketHandler
|
||||
* @return boolean
|
||||
*/
|
||||
public function isConnected() {
|
||||
|
@ -366,7 +398,8 @@ class Connection_ConnectionHandler {
|
|||
|
||||
/**
|
||||
* Sets the is_connected-flag in the socket handler.
|
||||
* @see Socket_SocketHandler
|
||||
*
|
||||
* @see \JPT\SocketFramework\Socket\SocketHandler
|
||||
* @param boolean $connected
|
||||
* @return void
|
||||
*/
|
||||
|
@ -375,7 +408,7 @@ class Connection_ConnectionHandler {
|
|||
}
|
||||
|
||||
/**
|
||||
* @see Socket_SocketHandler
|
||||
* @see \JPT\SocketFramework\Socket\SocketHandler
|
||||
* @return boolean
|
||||
*/
|
||||
public function isListening() {
|
||||
|
@ -383,9 +416,10 @@ class Connection_ConnectionHandler {
|
|||
}
|
||||
|
||||
/**
|
||||
* Calls SocketHandler
|
||||
* @see Socket_SocketHandler
|
||||
* @throws Exception_SocketException
|
||||
* Calls SocketHandler.
|
||||
*
|
||||
* @see \JPT\SocketFramework\Socket\SocketHandler
|
||||
* @throws \JPT\SocketFramework\Exception\SocketException
|
||||
* @return ressource
|
||||
*/
|
||||
public function getSocket() {
|
||||
|
@ -393,15 +427,16 @@ class Connection_ConnectionHandler {
|
|||
}
|
||||
|
||||
/**
|
||||
* @return Socket_SocketHandler
|
||||
* @return \JPT\SocketFramework\Socket\SocketHandler
|
||||
*/
|
||||
public function getSocketHandler() {
|
||||
return $this->socketHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls SocketHandler
|
||||
* @see Socket_SocketHandler
|
||||
* Calls SocketHandler.
|
||||
*
|
||||
* @see \JPT\SocketFramework\Socket\SocketHandler
|
||||
* @return void
|
||||
*/
|
||||
public function hasBeenAccepted() {
|
|
@ -1,20 +1,25 @@
|
|||
<?php
|
||||
namespace JPT\SocketFramework\Connection;
|
||||
|
||||
/**
|
||||
* Connection pool class. Contains the SocketPool.
|
||||
*
|
||||
* @author jpt
|
||||
* @package Connection
|
||||
* @depends Socket
|
||||
*/
|
||||
class Connection_ConnectionPool {
|
||||
class ConnectionPool {
|
||||
|
||||
/**
|
||||
* SocketPool instance.
|
||||
* @var Socket_SocketPool
|
||||
*
|
||||
* @var \JPT\SocketFramework\Socket\SocketPool
|
||||
*/
|
||||
protected $socketPool;
|
||||
|
||||
/**
|
||||
* Contains all ConnectionHandler instances.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $connectionHandlers;
|
||||
|
@ -25,17 +30,19 @@ class Connection_ConnectionPool {
|
|||
protected $nextID;
|
||||
|
||||
/**
|
||||
* Creates an Instance of SocketPool
|
||||
* Creates an Instance of SocketPool.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->connectionHandlers = array();
|
||||
$this->socketPool = new Socket_SocketPool();
|
||||
$this->socketPool = new \JPT\SocketFramework\Socket\SocketPool();
|
||||
$this->nextID = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys the SocketPool
|
||||
* Destroys the SocketPool.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __destruct() {
|
||||
|
@ -44,12 +51,13 @@ class Connection_ConnectionPool {
|
|||
|
||||
/**
|
||||
* Creates a new TcpConnection.
|
||||
*
|
||||
* @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) {
|
||||
$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->injectConnectionPool($this);
|
||||
$this->addConnectionHandler($connectionHandler);
|
||||
|
@ -58,6 +66,7 @@ class Connection_ConnectionPool {
|
|||
|
||||
/**
|
||||
* Returns the array with the current connectionHandlers.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getConnectionHandlers() {
|
||||
|
@ -66,7 +75,8 @@ class Connection_ConnectionPool {
|
|||
|
||||
/**
|
||||
* Adds a ConnectionHandler to the pool.
|
||||
* @param Connection_ConnectionHandler $addConnectionHandler
|
||||
*
|
||||
* @param \JPT\SocketFramework\Connection\ConnectionHandler $addConnectionHandler
|
||||
* @return void
|
||||
*/
|
||||
public function addConnectionHandler($addConnectionHandler) {
|
||||
|
@ -76,7 +86,8 @@ class Connection_ConnectionPool {
|
|||
|
||||
/**
|
||||
* Removes a ConnectionHandler from the pool.
|
||||
* @param Connection_ConnectionHandler $removeConnectionHandler
|
||||
*
|
||||
* @param \JPT\SocketFramework\Connection\ConnectionHandler $removeConnectionHandler
|
||||
* @return void
|
||||
*/
|
||||
public function removeConnectionHandler($removeConnectionHandler) {
|
||||
|
@ -91,8 +102,9 @@ class Connection_ConnectionPool {
|
|||
|
||||
/**
|
||||
* Returns ConnectionHandler for the given socket ressource.
|
||||
*
|
||||
* @param ressource $socketRessource
|
||||
* @return Connection_ConnectionHandler
|
||||
* @return \JPT\SocketFramework\Connection\ConnectionHandler
|
||||
*/
|
||||
protected function getConnectionHandlerForSocketRessource($socketRessource) {
|
||||
foreach($this->connectionHandlers AS $connectionHandler) {
|
||||
|
@ -105,9 +117,10 @@ class Connection_ConnectionPool {
|
|||
/**
|
||||
* 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 Connection_ConnectionHandler for each Socket with new data.
|
||||
* @throws Exception_GeneralException
|
||||
* @throws Exception_SocketException
|
||||
*
|
||||
* @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();
|
||||
|
@ -135,7 +148,7 @@ class Connection_ConnectionPool {
|
|||
case "read":
|
||||
if($connectionHandler->isServer() === TRUE) {
|
||||
$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();
|
||||
$this->addConnectionHandler($acceptedSocketHandler);
|
||||
} else {
|
||||
|
@ -163,9 +176,10 @@ class Connection_ConnectionPool {
|
|||
|
||||
/**
|
||||
* Writes the given data to all sockets in $group.
|
||||
*
|
||||
* @param string $group
|
||||
* @param string $data
|
||||
* @throws Exception_SocketException
|
||||
* @throws \JPT\SocketFramework\Exception\SocketException
|
||||
* @return void
|
||||
*/
|
||||
public function writeToGroup($group, $data) {
|
||||
|
@ -178,6 +192,7 @@ class Connection_ConnectionPool {
|
|||
|
||||
/**
|
||||
* Writes the given data to the socket with $id.
|
||||
*
|
||||
* @param int $id
|
||||
* @param string $data
|
||||
* @return void
|
||||
|
@ -191,7 +206,7 @@ class Connection_ConnectionPool {
|
|||
}
|
||||
|
||||
/**
|
||||
* @see Socket_SocketPool
|
||||
* @see \JPT\SocketFramework\Socket\SocketPool
|
||||
* @return int
|
||||
*/
|
||||
public function countSockets() {
|
||||
|
@ -201,6 +216,7 @@ class Connection_ConnectionPool {
|
|||
/**
|
||||
* Returns the amount of active connections.
|
||||
* A connection is active when the handler is connected.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function countActiveConnections() {
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,14 +1,18 @@
|
|||
<?php
|
||||
namespace JPT\SocketFramework\Exception;
|
||||
|
||||
/**
|
||||
* General exception class.
|
||||
* General exception.
|
||||
*
|
||||
* @author jpt
|
||||
* @package Exception
|
||||
* @exception
|
||||
*/
|
||||
class Exception_GeneralException extends Exception {
|
||||
class GeneralException extends \Exception {
|
||||
|
||||
/**
|
||||
* Constructor. Throws a new exception in case the exception is unknown.
|
||||
*
|
||||
* @throws UnknownException
|
||||
* @param string $message
|
||||
* @param int $code
|
||||
|
@ -23,6 +27,7 @@ class Exception_GeneralException extends Exception {
|
|||
|
||||
/**
|
||||
* Puts all the important information into a string.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString() {
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
namespace JPT\SocketFramework\Exception;
|
||||
|
||||
/**
|
||||
* SocketException
|
||||
*
|
||||
* @author jpt
|
||||
* @package Exception
|
||||
* @exception
|
||||
*/
|
||||
class SocketException extends GeneralException {}
|
||||
?>
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
namespace JPT\SocketFramework\Exception;
|
||||
|
||||
/**
|
||||
* WrongDatatypeException
|
||||
*
|
||||
* @author jpt
|
||||
* @package Exception
|
||||
* @exception
|
||||
*/
|
||||
class WrongDatatypeException extends GeneralException {}
|
||||
?>
|
|
@ -1,27 +1,33 @@
|
|||
<?php
|
||||
namespace JPT\SocketFramework\Misc;
|
||||
|
||||
/**
|
||||
* Buffer class for a string.
|
||||
* Will fix issues with sockets that don't care about linebreaks.
|
||||
* Can also be used for all kinds of purpose.
|
||||
*
|
||||
* @author jpt
|
||||
* @package Misc
|
||||
*/
|
||||
class Misc_Buffer {
|
||||
class Buffer {
|
||||
|
||||
/**
|
||||
* Bufferstring - contains all the data.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $buffer;
|
||||
|
||||
/**
|
||||
* Linebreak to use for pulling lines out of the buffer.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $linebreak;
|
||||
|
||||
/**
|
||||
* Default constructor. Sets a default linebreak and initializes the buffer.
|
||||
*
|
||||
* @param $linebreak
|
||||
* @return void
|
||||
*/
|
||||
|
@ -32,6 +38,7 @@ class Misc_Buffer {
|
|||
|
||||
/**
|
||||
* Prunes empty lines out of the buffer.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function pruneEmptyLines() {
|
||||
|
@ -51,6 +58,7 @@ class Misc_Buffer {
|
|||
|
||||
/**
|
||||
* Appends data to the buffer.
|
||||
*
|
||||
* @param string $data
|
||||
* @return void
|
||||
*/
|
||||
|
@ -61,6 +69,7 @@ class Misc_Buffer {
|
|||
|
||||
/**
|
||||
* Appends data + linebreak to the buffer.
|
||||
*
|
||||
* @param string $data
|
||||
* @return void
|
||||
*/
|
||||
|
@ -71,6 +80,7 @@ class Misc_Buffer {
|
|||
|
||||
/**
|
||||
* Returns the next line in the buffer and removes it from the buffer.
|
||||
*
|
||||
* @return string
|
||||
* @throws Exception_GeneralException
|
||||
*/
|
||||
|
@ -86,6 +96,7 @@ class Misc_Buffer {
|
|||
|
||||
/**
|
||||
* Returns the length of the buffer.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
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.
|
||||
*
|
||||
* @throws Exception_GeneralException
|
||||
* @return string
|
||||
*/
|
||||
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);
|
||||
$this->buffer = substr($this->buffer, $length);
|
||||
return $data;
|
||||
|
@ -106,16 +118,18 @@ class Misc_Buffer {
|
|||
|
||||
/**
|
||||
* Checks whether the buffer contains more lines to process.
|
||||
*
|
||||
* @return boolean
|
||||
* @throws Exception_GeneralException
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns TRUE when there is data in the buffer.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function hasData() {
|
||||
|
@ -125,6 +139,7 @@ class Misc_Buffer {
|
|||
/**
|
||||
* Returns the full buffer contents.
|
||||
* Also truncates the buffer.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAllBufferContents() {
|
|
@ -1,14 +1,18 @@
|
|||
<?php
|
||||
namespace JPT\SocketFramework\Socket;
|
||||
|
||||
/**
|
||||
* The SocketHandler class will handle a single socket.
|
||||
* It takes care of the very basic socket functions.
|
||||
*
|
||||
* @author jpt
|
||||
* @package Socket
|
||||
*/
|
||||
class Socket_SocketHandler {
|
||||
class SocketHandler {
|
||||
|
||||
/**
|
||||
* Socket ressource
|
||||
*
|
||||
* @var ressource
|
||||
*/
|
||||
protected $socket;
|
||||
|
@ -30,12 +34,13 @@ class Socket_SocketHandler {
|
|||
|
||||
/**
|
||||
* Default constructor. Sets the socket.
|
||||
* @throws Exception_SocketException
|
||||
*
|
||||
* @throws \JPT\SocketFramework\Exception\SocketException
|
||||
* @param ressource $socket
|
||||
* @return void
|
||||
*/
|
||||
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->isBound = FALSE;
|
||||
$this->isConnected = FALSE;
|
||||
|
@ -44,6 +49,7 @@ class Socket_SocketHandler {
|
|||
|
||||
/**
|
||||
* Destructor. Closes socket.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __destruct() {
|
||||
|
@ -51,7 +57,8 @@ class Socket_SocketHandler {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns socket
|
||||
* Returns socket.
|
||||
*
|
||||
* @return ressource
|
||||
*/
|
||||
public function getSocket() {
|
||||
|
@ -67,6 +74,7 @@ class Socket_SocketHandler {
|
|||
|
||||
/**
|
||||
* Sets isConnected-flag.
|
||||
*
|
||||
* @param boolean $connected
|
||||
* @return void
|
||||
*/
|
||||
|
@ -83,13 +91,14 @@ class Socket_SocketHandler {
|
|||
|
||||
/**
|
||||
* Connects to a specified address.
|
||||
* @throws Exception_SocketException
|
||||
*
|
||||
* @throws \JPT\SocketFramework\Exception\SocketException
|
||||
* @param string $address
|
||||
* @param int $port
|
||||
* @return void
|
||||
*/
|
||||
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);
|
||||
if($result === FALSE) $this->handleSocketError();
|
||||
$this->isConnected = TRUE;
|
||||
|
@ -97,14 +106,15 @@ class Socket_SocketHandler {
|
|||
|
||||
/**
|
||||
* Binds the socket to an address + port.
|
||||
* @throws Exception_SocketException
|
||||
*
|
||||
* @throws \JPT\SocketFramework\Exception\SocketException
|
||||
* @param string $address
|
||||
* @param int $port
|
||||
* @return void
|
||||
*/
|
||||
public function bind($address, $port) {
|
||||
if($this->isBound === TRUE) throw new Exception_SocketException("Socket is already bound!", 1289663212);
|
||||
$result = socket_set_option($this->socket, SOL_SOCKET, SO_REUSEADDR, 1);
|
||||
if($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);
|
||||
if($result === FALSE) $this->handleSocketError();
|
||||
$result = socket_bind($this->socket, $address, $port);
|
||||
if($result === FALSE) $this->handleSocketError();
|
||||
|
@ -113,11 +123,12 @@ class Socket_SocketHandler {
|
|||
|
||||
/**
|
||||
* Let's the socket listen for incoming connections.
|
||||
* @throws Exception_SocketException
|
||||
*
|
||||
* @throws \JPT\SocketFramework\Exception\SocketException
|
||||
* @return void
|
||||
*/
|
||||
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);
|
||||
if($result === FALSE) $this->handleSocketError();
|
||||
$this->isListening = TRUE;
|
||||
|
@ -125,6 +136,7 @@ class Socket_SocketHandler {
|
|||
|
||||
/**
|
||||
* Tells the SocketHandler that he got accepted.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function hasBeenAccepted() {
|
||||
|
@ -132,14 +144,15 @@ class Socket_SocketHandler {
|
|||
}
|
||||
|
||||
/**
|
||||
* Accepts a connection to a socket that is bound and listens
|
||||
* The ressource has to be added to the SocketPool manually
|
||||
* @throws Exception_SocketException
|
||||
* Accepts a connection to a socket that is bound and listens.
|
||||
* The ressource has to be added to the SocketPool manually.
|
||||
*
|
||||
* @throws \JPT\SocketFramework\Exception\SocketException
|
||||
* @return ressource
|
||||
*/
|
||||
public function accept() {
|
||||
if($this->isBound === FALSE) throw new Exception_SocketException("Cannot accept connections from unbound socket!", 1289663239);
|
||||
if($this->isListening === FALSE) throw new Exception_SocketException("Cannot accept connections from socket that is not listening!", 1289663241);
|
||||
if($this->isBound === FALSE) throw new \JPT\SocketFramework\Exception\SocketException("Cannot accept connections from unbound socket!", 1289663239);
|
||||
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);
|
||||
if($accept === FALSE) $this->handleSocketError();
|
||||
return $accept;
|
||||
|
@ -147,11 +160,12 @@ class Socket_SocketHandler {
|
|||
|
||||
/**
|
||||
* Returns ip + port of the remote socket.
|
||||
* @throws Exception_SocketException
|
||||
*
|
||||
* @throws \JPT\SocketFramework\Exception\SocketException
|
||||
* @return string
|
||||
*/
|
||||
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);
|
||||
if($result === FALSE) $this->handleSocketError();
|
||||
return $address . ":" . $port;
|
||||
|
@ -159,11 +173,12 @@ class Socket_SocketHandler {
|
|||
|
||||
/**
|
||||
* Returns ip + port of the local socket.
|
||||
* @throws Exception_SocketException
|
||||
*
|
||||
* @throws \JPT\SocketFramework\Exception\SocketException
|
||||
* @return string
|
||||
*/
|
||||
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);
|
||||
if($result === FALSE) $this->handleSocketError();
|
||||
return $address . ":" . $port;
|
||||
|
@ -171,7 +186,8 @@ class Socket_SocketHandler {
|
|||
|
||||
/**
|
||||
* Writes data to the socket.
|
||||
* @throws Exception_SocketException
|
||||
*
|
||||
* @throws \JPT\SocketFramework\Exception\SocketException
|
||||
* @param string $data
|
||||
* @return void
|
||||
*/
|
||||
|
@ -182,23 +198,25 @@ class Socket_SocketHandler {
|
|||
|
||||
/**
|
||||
* Reads data from the socket.
|
||||
* @throws Exception_SocketException
|
||||
*
|
||||
* @throws \JPT\SocketFramework\Exception\SocketException
|
||||
* @param int $length
|
||||
* @return string
|
||||
*/
|
||||
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();
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shuts the socket down after waiting a bit (waiting might be optional).
|
||||
* @throws Exception_SocketException
|
||||
*
|
||||
* @throws \JPT\SocketFramework\Exception\SocketException
|
||||
* @return void
|
||||
*/
|
||||
public function close() {
|
||||
usleep(100000);
|
||||
usleep(100000); //we'll wait here since socket_shutdown _might_ fail without it.
|
||||
if($this->isConnected === TRUE) {
|
||||
$result = socket_shutdown($this->socket);
|
||||
if($result === FALSE) $this->handleSocketError();
|
||||
|
@ -212,16 +230,17 @@ class Socket_SocketHandler {
|
|||
|
||||
/**
|
||||
* Gets last error from socket.
|
||||
* @throws Exception_SocketException
|
||||
*
|
||||
* @throws \JPT\SocketFramework\Exception\SocketException
|
||||
* @return void
|
||||
*/
|
||||
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);
|
||||
$error = socket_strerror($errno);
|
||||
socket_clear_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);
|
||||
}
|
||||
}
|
||||
?>
|
|
@ -1,20 +1,25 @@
|
|||
<?php
|
||||
namespace JPT\SocketFramework\Socket;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @package Socket
|
||||
*/
|
||||
class Socket_SocketPool {
|
||||
class SocketPool {
|
||||
|
||||
/**
|
||||
* Pool that contains socket ressources.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $sockets;
|
||||
|
||||
/**
|
||||
* Creates sockets.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct() {
|
||||
|
@ -23,6 +28,7 @@ class Socket_SocketPool {
|
|||
|
||||
/**
|
||||
* Closes all connections.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __destruct() {
|
||||
|
@ -31,6 +37,7 @@ class Socket_SocketPool {
|
|||
|
||||
/**
|
||||
* Returns the amount of active sockets.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
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
|
||||
* @return void
|
||||
*/
|
||||
|
@ -49,6 +57,7 @@ class Socket_SocketPool {
|
|||
/**
|
||||
* Removes the given socket from the pool.
|
||||
* The socket will be shutdown.
|
||||
*
|
||||
* @param ressource $removeSocket
|
||||
* @return void
|
||||
*/
|
||||
|
@ -66,31 +75,33 @@ class Socket_SocketPool {
|
|||
|
||||
/**
|
||||
* 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.
|
||||
* @return ressource
|
||||
*/
|
||||
public function createTcpSocket($IPv6 = FALSE) {
|
||||
$domain = ($IPv6) ? AF_INET6 : AF_INET;
|
||||
$socket = socket_create($domain, SOCK_STREAM, SOL_TCP);
|
||||
$domain = ($IPv6) ? \AF_INET6 : \AF_INET;
|
||||
$socket = socket_create($domain, \SOCK_STREAM, \SOL_TCP);
|
||||
socket_set_block($socket);
|
||||
if($socket === FALSE) throw new Exception_SocketException("socket_create() failed!", 1290273709);
|
||||
if($socket === FALSE) throw new \JPT\SocketFramework\Exception\SocketException("socket_create() failed!", 1290273709);
|
||||
$this->addSocket($socket);
|
||||
return $socket;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of sockets one can read from.
|
||||
*
|
||||
* @param array $read
|
||||
* @param array $write
|
||||
* @param array $except
|
||||
* @param int $timeout
|
||||
* @throws Exception_SocketException
|
||||
* @throws \JPT\SocketFramework\Exception\SocketException
|
||||
* @return array
|
||||
*/
|
||||
public function select($read, $write, $except, $timeout = NULL) {
|
||||
$n = socket_select($read, $write, $except, $timeout);
|
||||
if($n === FALSE) throw new Exception_SocketException("socket_select() failed! - 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());
|
||||
return array("read" => $read, "write" => $write, "except" => $except);
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
<?php
|
||||
$clientManager = new Client_ClientManager();
|
||||
$clientManager = new \JPT\SocketFramework\Client\ClientManager();
|
||||
$clientManager->registerProtocol("irc", "Irc");
|
||||
$clientManager->registerProtocol("jpt", "Bot");
|
||||
|
||||
|
|
Loading…
Reference in New Issue