+<?php
+/**
+ * A general listener class
+ *
+ * @author Roland Haeder <webmaster@shipsimu.org>
+ * @version 0.0.0
+ * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
+ * @license GNU GPL 3.0 or any newer version
+ * @link http://www.shipsimu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+class BaseListener extends BaseFrameworkSystem implements Visitable {
+ // Exception code constants
+ const EXCEPTION_INVALID_SOCKET = 0xa00;
+ const EXCEPTION_SOCKET_ALREADY_REGISTERED = 0xa01;
+ const EXCEPTION_SOCKET_CREATION_FAILED = 0xa02;
+ const EXCEPTION_NO_SOCKET_ERROR = 0xa03;
+ const EXCEPTION_CONNECTION_ALREADY_REGISTERED = 0xa04;
+ const EXCEPTION_UNEXPECTED_PACKAGE_STATUS = 0xa05;
+ const EXCEPTION_UNSUPPORTED_PACKAGE_CODE_HANDLER = 0xa06;
+ const EXCEPTION_FINAL_CHUNK_VERIFICATION = 0xa07;
+ const EXCEPTION_INVALID_DATA_CHECKSUM = 0xa08;
+
+ /**
+ * Address (IP mostly) we shall listen on
+ */
+ private $listenAddress = '0.0.0.0'; // This is the default and listens on all interfaces
+
+ /**
+ * Port we shall listen on (or wait for incoming data)
+ */
+ private $listenPort = 0; // This port MUST be changed by your application
+
+ /**
+ * Whether we are in blocking or non-blocking mode (default: non-blocking
+ */
+ private $blockingMode = FALSE;
+
+ /**
+ * A peer pool instance
+ */
+ private $poolInstance = NULL;
+
+ /**
+ * Protected constructor
+ *
+ * @param $className Name of the class
+ * @return void
+ */
+ protected function __construct ($className) {
+ // Call parent constructor
+ parent::__construct($className);
+ }
+
+ /**
+ * Checks whether the given socket resource is a server socket
+ *
+ * @param $socketResource A valid socket resource
+ * @return $isServerSocket Whether the socket resource is a server socket
+ */
+ protected function isServerSocketResource ($socketResource) {
+ // Check it
+ $isServerSocket = ((is_resource($socketResource)) && (!@socket_getpeername($socketResource, $peerName)));
+
+ // We need to clear the error here if it is a resource
+ if ($isServerSocket === TRUE) {
+ // Clear the error
+ //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('socketResource[]=' . gettype($socketResource));
+ socket_clear_error($socketResource);
+ } // END - if
+
+ // Check peer name, it must be empty
+ $isServerSocket = (($isServerSocket) && (empty($peerName)));
+
+ // Return result
+ return $isServerSocket;
+ }
+
+ /**
+ * Setter for listen address
+ *
+ * @param $listenAddress The address this listener should listen on
+ * @return void
+ */
+ protected final function setListenAddress ($listenAddress) {
+ $this->listenAddress = (string) $listenAddress;
+ }
+
+ /**
+ * Getter for listen address
+ *
+ * @return $listenAddress The address this listener should listen on
+ */
+ public final function getListenAddress () {
+ return $this->listenAddress;
+ }
+
+ /**
+ * Setter for listen port
+ *
+ * @param $listenPort The port this listener should listen on
+ * @return void
+ */
+ protected final function setListenPort ($listenPort) {
+ $this->listenPort = (int) $listenPort;
+ }
+
+ /**
+ * Getter for listen port
+ *
+ * @return $listenPort The port this listener should listen on
+ */
+ public final function getListenPort () {
+ return $this->listenPort;
+ }
+
+ /**
+ * "Setter" to set listen address from configuration entry
+ *
+ * @param $configEntry The configuration entry holding our listen address
+ * @return void
+ */
+ public final function setListenAddressByConfiguration ($configEntry) {
+ $this->setListenAddress($this->getConfigInstance()->getConfigEntry($configEntry));
+ }
+
+ /**
+ * "Setter" to set listen port from configuration entry
+ *
+ * @param $configEntry The configuration entry holding our listen port
+ * @return void
+ */
+ public final function setListenPortByConfiguration ($configEntry) {
+ $this->setListenPort($this->getConfigInstance()->getConfigEntry($configEntry));
+ }
+
+ /**
+ * Setter for blocking-mode
+ *
+ * @param $blockingMode Whether blocking-mode is disabled (default) or enabled
+ * @return void
+ */
+ protected final function setBlockingMode ($blockingMode) {
+ $this->blockingMode = (boolean) $blockingMode;
+ }
+
+ /**
+ * Checks whether blocking-mode is enabled or disabled
+ *
+ * @return $blockingMode Whether blocking mode is disabled or enabled
+ */
+ public final function isBlockingModeEnabled () {
+ return $this->blockingMode;
+ }
+
+ /**
+ * Setter for peer pool instance
+ *
+ * @param $poolInstance The peer pool instance we shall set
+ * @return void
+ */
+ protected final function setPoolInstance (PoolablePeer $poolInstance) {
+ $this->poolInstance = $poolInstance;
+ }
+
+ /**
+ * Getter for peer pool instance
+ *
+ * @return $poolInstance The peer pool instance we shall set
+ */
+ public final function getPoolInstance () {
+ return $this->poolInstance;
+ }
+
+ /**
+ * Getter for connection type
+ *
+ * @return $connectionType Connection type for this listener
+ */
+ public final function getConnectionType () {
+ // Wrap the real getter
+ return $this->getProtocolName();
+ }
+
+ /**
+ * Registeres the given socket resource for "this" listener instance. This
+ * will be done in a seperate class to allow package writers to use it
+ * again.
+ *
+ * @param $socketResource A valid server socket resource
+ * @return void
+ * @throws InvalidServerSocketException If the given resource is no server socket
+ * @throws SocketAlreadyRegisteredException If the given resource is already registered
+ */
+ protected function registerServerSocketResource ($socketResource) {
+ // First check if it is valid
+ if (!$this->isServerSocketResource($socketResource)) {
+ // No server socket
+ throw new InvalidServerSocketException(array($this, $socketResource), self::EXCEPTION_INVALID_SOCKET);
+ } elseif ($this->isServerSocketRegistered($socketResource)) {
+ // Already registered
+ throw new SocketAlreadyRegisteredException($this, self::EXCEPTION_SOCKET_ALREADY_REGISTERED);
+ }
+
+ // Get a socket registry instance (singleton)
+ $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
+
+ // Get a connection info instance
+ $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($this->getProtocolName(), 'listener');
+
+ // Will the info instance with listener data
+ $infoInstance->fillWithListenerInformation($this);
+
+ // Register the socket
+ $registryInstance->registerSocket($infoInstance, $socketResource);
+
+ // And set it here
+ $this->setSocketResource($socketResource);
+ }
+
+ /**
+ * Checks whether given socket resource is registered in socket registry
+ *
+ * @param $socketResource A valid server socket resource
+ * @return $isRegistered Whether given server socket is registered
+ */
+ protected function isServerSocketRegistered ($socketResource) {
+ // Get a socket registry instance (singleton)
+ $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
+
+ // Get a connection info instance
+ $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($this->getProtocolName(), 'listener');
+
+ // Will the info instance with listener data
+ $infoInstance->fillWithListenerInformation($this);
+
+ // Check it
+ $isRegistered = $registryInstance->isSocketRegistered($infoInstance, $socketResource);
+
+ // Return result
+ return $isRegistered;
+ }
+
+ /**
+ * Accepts the visitor to process the visit "request"
+ *
+ * @param $visitorInstance An instance of a Visitor class
+ * @return void
+ */
+ public function accept (Visitor $visitorInstance) {
+ // Debug message
+ //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($this->getProtocolName()) . '-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited ' . $this->__toString() . ' - CALLED!');
+
+ // Visit this listener
+ $visitorInstance->visitListener($this);
+
+ // Visit the pool if set
+ if ($this->getPoolInstance() instanceof Poolable) {
+ $this->getPoolInstance()->accept($visitorInstance);
+ } // END - if
+
+ // Debug message
+ //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($this->getProtocolName()) . '-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: ' . $visitorInstance->__toString() . ' has visited ' . $this->__toString() . ' - EXIT!');
+ }
+
+ /**
+ * Monitors incoming raw data from the handler and transfers it to the
+ * given receiver instance. This method should not be called, please call
+ * the decorator's version instead to separator node/client traffic.
+ *
+ * @return void
+ * @throws UnsupportedOperatorException If this method is called by a mistake
+ */
+ public function monitorIncomingRawData () {
+ throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
+ }
+
+ /**
+ * Constructs a callable method name from given socket error code. If the
+ * method is not found, a generic one is used.
+ *
+ * @param $errorCode Error code from socket_last_error()
+ * @return $handlerName Call-back method name for the error handler
+ * @throws UnsupportedSocketErrorHandlerException If the error handler is not implemented
+ */
+ protected function getSocketErrorHandlerFromCode ($errorCode) {
+ // Create a name from translated error code
+ $handlerName = 'socketError' . self::convertToClassName($this->translateSocketErrorCodeToName($errorCode)) . 'Handler';
+
+ // Is the call-back method there?
+ if (!method_exists($this, $handlerName)) {
+ // Please implement this
+ throw new UnsupportedSocketErrorHandlerException(array($this, $handlerName, $errorCode), BaseConnectionHelper::EXCEPTION_UNSUPPORTED_ERROR_HANDLER);
+ } // END - if
+
+ // Return it
+ return $handlerName;
+ }
+
+ /**
+ * Translates socket error codes into our own internal names which can be
+ * used for call-backs.
+ *
+ * @param $errorCode The error code from socket_last_error() to be translated
+ * @return $errorName The translated name (all lower-case, with underlines)
+ */
+ public function translateSocketErrorCodeToName ($errorCode) {
+ // Nothing bad happened by default
+ $errorName = BaseRawDataHandler::SOCKET_CONNECTED;
+
+ // Is the code a number, then we have to change it
+ switch ($errorCode) {
+ case 0: // Silently ignored, the socket is connected
+ break;
+
+ case 11: // "Resource temporary unavailable"
+ $errorName = BaseRawDataHandler::SOCKET_ERROR_RESOURCE_UNAVAILABLE;
+ break;
+
+ case 13: // "Permission denied"
+ $errorName = BaseRawDataHandler::SOCKET_ERROR_PERMISSION_DENIED;
+ break;
+
+ case 32: // "Broken pipe"
+ $errorName = BaseRawDataHandler::SOCKET_ERROR_BROKEN_PIPE;
+ break;
+
+ case 104: // "Connection reset by peer"
+ $errorName = BaseRawDataHandler::SOCKET_ERROR_CONNECTION_RESET_BY_PEER;
+ break;
+
+ case 107: // "Transport end-point not connected"
+ case 134: // On some (?) systems for 'transport end-point not connected'
+ // @TODO On some systems it is 134, on some 107?
+ $errorName = BaseRawDataHandler::SOCKET_ERROR_TRANSPORT_ENDPOINT;
+ break;
+
+ case 110: // "Connection timed out"
+ $errorName = BaseRawDataHandler::SOCKET_ERROR_CONNECTION_TIMED_OUT;
+ break;
+
+ case 111: // "Connection refused"
+ $errorName = BaseRawDataHandler::SOCKET_ERROR_CONNECTION_REFUSED;
+ break;
+
+ case 113: // "No route to host"
+ $errorName = BaseRawDataHandler::SOCKET_ERROR_NO_ROUTE_TO_HOST;
+ break;
+
+ case 114: // "Operation already in progress"
+ $errorName = BaseRawDataHandler::SOCKET_ERROR_OPERATION_ALREADY_PROGRESS;
+ break;
+
+ case 115: // "Operation now in progress"
+ $errorName = BaseRawDataHandler::SOCKET_ERROR_OPERATION_IN_PROGRESS;
+ break;
+
+ default: // Everything else <> 0
+ // Unhandled error code detected, so first debug it because we may want to handle it like the others
+ self::createDebugInstance(__CLASS__)->debugOutput('BASE-HUB[' . __METHOD__ . ':' . __LINE__ . '] UNKNOWN ERROR CODE = ' . $errorCode . ', MESSAGE = ' . socket_strerror($errorCode));
+
+ // Change it only in this class
+ $errorName = BaseRawDataHandler::SOCKET_ERROR_UNKNOWN;
+ break;
+ }
+
+ // Return translated name
+ return $errorName;
+ }
+
+ /**
+ * Shuts down a given socket resource. This method does only ease calling
+ * the right visitor.
+ *
+ * @param $socketResource A valid socket resource
+ * @return void
+ */
+ public function shutdownSocket ($socketResource) {
+ // Debug message
+ self::createDebugInstance(__CLASS__)->debugOutput('HUB-SYSTEM: Shutting down socket resource ' . $socketResource . ' with state ' . $this->getPrintableState() . ' ...');
+
+ // Set socket resource
+ $this->setSocketResource($socketResource);
+
+ // Get a visitor instance
+ $visitorInstance = ObjectFactory::createObjectByConfiguredName('shutdown_socket_visitor_class');
+
+ // Debug output
+ self::createDebugInstance(__CLASS__)->debugOutput('HUB-SYSTEM:' . $this->__toString() . ': visitorInstance=' . $visitorInstance->__toString());
+
+ // Call the visitor
+ $this->accept($visitorInstance);
+ }
+
+ /**
+ * Half-shuts down a given socket resource. This method does only ease calling
+ * an other visitor than shutdownSocket() does.
+ *
+ * @param $socketResource A valid socket resource
+ * @return void
+ */
+ public function halfShutdownSocket ($socketResource) {
+ // Debug message
+ self::createDebugInstance(__CLASS__)->debugOutput('HUB-SYSTEM: Half-shutting down socket resource ' . $socketResource . ' with state ' . $this->getPrintableState() . ' ...');
+
+ // Set socket resource
+ $this->setSocketResource($socketResource);
+
+ // Get a visitor instance
+ $visitorInstance = ObjectFactory::createObjectByConfiguredName('half_shutdown_socket_visitor_class');
+
+ // Debug output
+ self::createDebugInstance(__CLASS__)->debugOutput('HUB-SYSTEM:' . $this->__toString() . ': visitorInstance=' . $visitorInstance->__toString());
+
+ // Call the visitor
+ $this->accept($visitorInstance);
+ }
+
+ // ************************************************************************
+ // Socket error handler call-back methods
+ // ************************************************************************
+
+ /**
+ * Handles socket error 'permission denied', but does not clear it for
+ * later debugging purposes.
+ *
+ * @param $socketResource A valid socket resource
+ * @param $socketData A valid socket data array (0 = IP/file name, 1 = port)
+ * @return void
+ * @throws SocketBindingException The socket could not be bind to
+ */
+ protected function socketErrorPermissionDeniedHandler ($socketResource, array $socketData) {
+ // Get socket error code for verification
+ $socketError = socket_last_error($socketResource);
+
+ // Get error message
+ $errorMessage = socket_strerror($socketError);
+
+ // Shutdown this socket
+ $this->shutdownSocket($socketResource);
+
+ // Throw it again
+ throw new SocketBindingException(array($this, $socketData, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+ }
+
+ /**
+ * "Listens" for incoming network packages
+ *
+ * @param $peerSuffix Suffix for peer name (e.g. :0 for TCP(/UDP?) connections)
+ * @return void
+ * @throws InvalidSocketException If an invalid socket resource has been found
+ */
+ protected function doListenSocketSelect ($peerSuffix) {
+ // Check on all instances
+ assert($this->getPoolInstance() instanceof Poolable);
+ assert(is_resource($this->getSocketResource()));
+
+ // Get all readers
+ $readers = $this->getPoolInstance()->getAllSingleSockets();
+ $writers = array();
+ $excepts = array();
+
+ // Check if we have some peers left
+ $left = socket_select(
+ $readers,
+ $writers,
+ $excepts,
+ 0,
+ 150
+ );
+
+ // Some new peers found?
+ if ($left < 1) {
+ // Debug message
+ //* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: left=' . $left . ',serverSocket=' . $this->getSocketResource() . ',readers=' . print_r($readers, TRUE));
+
+ // Nothing new found
+ return;
+ } // END - if
+
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: serverSocket=' . $this->getSocketResource() . ',readers=' . print_r($readers, TRUE));
+
+ // Do we have changed peers?
+ if (in_array($this->getSocketResource(), $readers)) {
+ /*
+ * Then accept it, if this socket is set to non-blocking IO and the
+ * connection is NOT sending any data, socket_read() may throw
+ * error 11 (Resource temporary unavailable). This really nasty
+ * because if you have blocking IO socket_read() will wait and wait
+ * and wait ...
+ */
+ $newSocket = socket_accept($this->getSocketResource());
+
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: newSocket=' . $newSocket . ',serverSocket=' .$this->getSocketResource());
+
+ // Array for timeout settings
+ $options = array(
+ // Seconds
+ 'sec' => $this->getConfigInstance()->getConfigEntry('tcp_socket_accept_wait_sec'),
+ // Milliseconds
+ 'usec' => $this->getConfigInstance()->getConfigEntry('tcp_socket_accept_wait_usec')
+ );
+
+ // Set timeout to configured seconds
+ // @TODO Does this work on Windozer boxes???
+ if (!socket_set_option($newSocket, SOL_SOCKET, SO_RCVTIMEO, $options)) {
+ // Handle this socket error with a faked recipientData array
+ $this->handleSocketError(__METHOD__, __LINE__, $newSocket, array('0.0.0.0', '0'));
+ } // END - if
+
+ // Output result (only for debugging!)
+ /*
+ $option = socket_get_option($newSocket, SOL_SOCKET, SO_RCVTIMEO);
+ self::createDebugInstance(__CLASS__)->debugOutput('SO_RCVTIMEO[' . gettype($option) . ']=' . print_r($option, TRUE));
+ */
+
+ // Enable SO_OOBINLINE
+ if (!socket_set_option($newSocket, SOL_SOCKET, SO_OOBINLINE ,1)) {
+ // Handle this socket error with a faked recipientData array
+ $this->handleSocketError(__METHOD__, __LINE__, $newSocket, array('0.0.0.0', '0'));
+ } // END - if
+
+ // Set non-blocking
+ if (!socket_set_nonblock($newSocket)) {
+ // Handle this socket error with a faked recipientData array
+ $this->handleSocketError(__METHOD__, __LINE__, $newSocket, array('0.0.0.0', '0'));
+ } // END - if
+
+ // Add it to the peers
+ $this->getPoolInstance()->addPeer($newSocket, BaseConnectionHelper::CONNECTION_TYPE_INCOMING);
+
+ // Get peer name
+ if (!socket_getpeername($newSocket, $peerName)) {
+ // Handle this socket error with a faked recipientData array
+ $this->handleSocketError(__METHOD__, __LINE__, $newSocket, array('0.0.0.0', '0'));
+ } // END - if
+
+ // Get node instance
+ $nodeInstance = NodeObjectFactory::createNodeInstance();
+
+ // Create a faked package data array
+ $packageData = array(
+ NetworkPackage::PACKAGE_DATA_SENDER => $peerName . $peerSuffix,
+ NetworkPackage::PACKAGE_DATA_RECIPIENT => $nodeInstance->getSessionId(),
+ NetworkPackage::PACKAGE_DATA_STATUS => NetworkPackage::PACKAGE_STATUS_FAKED
+ );
+
+ // Get a connection info instance
+ $infoInstance = ConnectionInfoFactory::createConnectionInfoInstance($this->getProtocolName(), 'listener');
+
+ // Will the info instance with listener data
+ $infoInstance->fillWithListenerInformation($this);
+
+ // Get a socket registry
+ $registryInstance = SocketRegistryFactory::createSocketRegistryInstance();
+
+ // Register the socket with the registry and with the faked array
+ $registryInstance->registerSocket($infoInstance, $newSocket, $packageData);
+ } // END - if
+
+ // Do we have to rewind?
+ if (!$this->getIteratorInstance()->valid()) {
+ // Rewind the list
+ $this->getIteratorInstance()->rewind();
+ } // END - if
+
+ // Get the current value
+ $currentSocket = $this->getIteratorInstance()->current();
+
+ // Handle it here, if not main server socket
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('TCP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: currentSocket=' . $currentSocket[BasePool::SOCKET_ARRAY_RESOURCE] . ',type=' . $currentSocket[BasePool::SOCKET_ARRAY_CONN_TYPE] . ',serverSocket=' . $this->getSocketResource());
+ if (($currentSocket[BasePool::SOCKET_ARRAY_CONN_TYPE] != BaseConnectionHelper::CONNECTION_TYPE_SERVER) && ($currentSocket[BasePool::SOCKET_ARRAY_RESOURCE] != $this->getSocketResource())) {
+ // ... or else it will raise warnings like 'Transport endpoint is not connected'
+ $this->getHandlerInstance()->processRawDataFromResource($currentSocket);
+ } // END - if
+
+ // Advance to next entry. This should be the last line.
+ $this->getIteratorInstance()->next();
+ }
+}
+
+// [EOF]
+?>