]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/listener/udp/class_UdpListener.php
Updated 'core'.
[hub.git] / application / hub / main / listener / udp / class_UdpListener.php
index 04c791eede8aa685ee3ced9aaaa097a297d847ce..0eed46a90c23d57f1822051832f57c87f2faea39 100644 (file)
@@ -2,11 +2,11 @@
 /**
  * An UDP connection listener
  *
- * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Hub Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
  * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.ship-simu.org
+ * @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
@@ -30,6 +30,9 @@ class UdpListener extends BaseListener implements Listenable {
        protected function __construct () {
                // Call parent constructor
                parent::__construct(__CLASS__);
+
+               // Set the protocol to UDP
+               $this->setProtocolName('udp');
        }
 
        /**
@@ -42,12 +45,6 @@ class UdpListener extends BaseListener implements Listenable {
                // Get new instance
                $listenerInstance = new UdpListener();
 
-               // Set the application instance
-               $listenerInstance->setNodeInstance($nodeInstance);
-
-               // Set the protocol to UDP
-               $listenerInstance->setProtocol('udp');
-
                // Return the prepared instance
                return $listenerInstance;
        }
@@ -66,12 +63,19 @@ class UdpListener extends BaseListener implements Listenable {
                // Is the socket a valid resource or do we have any error?
                if (!is_resource($mainSocket)) {
                        // Then throw an InvalidSocketException
-                       throw new InvalidSocketException(array($this, gettype($mainSocket), $errno, $errstr), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       throw new InvalidSocketException(array($this, $mainSocket), BaseListener::EXCEPTION_INVALID_SOCKET);
                } // END - if
 
-               // Set the option to reuse the port
-               $this->debugOutput('LISTENER: Setting re-use address option.');
-               if (!socket_set_option($mainSocket, SOL_SOCKET, SO_REUSEADDR, 1)) {
+               /*
+                * "Bind" the socket to the given address, on given port so this means
+                * that all connections on this port are now our resposibility to
+                * send/recv data, disconnect, etc..
+                */
+               self::createDebugInstance(__CLASS__)->debugOutput('UDP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Binding to address ' . $this->getListenAddress() . ':' . $this->getListenPort());
+               if (!socket_bind($mainSocket, $this->getListenAddress(), $this->getListenPort())) {
+                       // Handle the socket error with a faked recipientData array
+                       $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
+                       /*
                        // Get socket error code for verification
                        $socketError = socket_last_error($mainSocket);
 
@@ -82,16 +86,18 @@ class UdpListener extends BaseListener implements Listenable {
                        $this->shutdownSocket($mainSocket);
 
                        // And throw again
-                       throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       */
                } // END - if
 
-               // "Bind" the socket to the given address, on given port so this means
-               // that all connections on this port are now our resposibility to
-               // send/recv data, disconnect, etc..
-               $this->debugOutput('LISTENER: Binding to address ' . $this->getListenAddress() . ':' . $this->getListenPort());
-               if (!socket_bind($mainSocket, $this->getListenAddress(), $this->getListenPort())) {
+               // Now, we want non-blocking mode
+               self::createDebugInstance(__CLASS__)->debugOutput('UDP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Setting non-blocking mode.');
+               if (!socket_set_nonblock($mainSocket)) {
+                       // Handle the socket error with a faked recipientData array
+                       $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
+                       /*
                        // Get socket error code for verification
-                       $socketError = socket_last_error($mainSocket);
+                       $socketError = socket_last_error($socket);
 
                        // Get error message
                        $errorMessage = socket_strerror($socketError);
@@ -100,14 +106,18 @@ class UdpListener extends BaseListener implements Listenable {
                        $this->shutdownSocket($mainSocket);
 
                        // And throw again
-                       throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       */
                } // END - if
 
-               // Now, we want non-blocking mode
-               $this->debugOutput('LISTENER: Setting non-blocking mode.');
-               if (!socket_set_nonblock($mainSocket)) {
+               // Set the option to reuse the port
+               self::createDebugInstance(__CLASS__)->debugOutput('UDP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Setting re-use address option.');
+               if (!socket_set_option($mainSocket, SOL_SOCKET, SO_REUSEADDR, 1)) {
+                       // Handle the socket error with a faked recipientData array
+                       $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
+                       /*
                        // Get socket error code for verification
-                       $socketError = socket_last_error($socket);
+                       $socketError = socket_last_error($mainSocket);
 
                        // Get error message
                        $errorMessage = socket_strerror($socketError);
@@ -116,14 +126,21 @@ class UdpListener extends BaseListener implements Listenable {
                        $this->shutdownSocket($mainSocket);
 
                        // And throw again
-                       throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       throw new InvalidSocketException(array($this, $mainSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       */
                } // END - if
 
                // Remember the socket in our class
                $this->registerServerSocketResource($mainSocket);
 
+               // Initialize the network package handler
+               $handlerInstance = ObjectFactory::createObjectByConfiguredName('udp_raw_data_handler_class');
+
+               // Set it in this class
+               $this->setHandlerInstance($handlerInstance);
+
                // Output message
-               $this->debugOutput('LISTENER: UDP listener now ready on IP ' . $this->getListenAddress() . ', port ' . $this->getListenPort() . ' for service.');
+               self::createDebugInstance(__CLASS__)->debugOutput('UDP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: UDP listener now ready on IP ' . $this->getListenAddress() . ', port ' . $this->getListenPort() . ' for service.');
        }
 
        /**
@@ -134,7 +151,7 @@ class UdpListener extends BaseListener implements Listenable {
         */
        public function doListen() {
                // Read a package and determine the peer
-               $amount = @socket_recvfrom($this->getSocketResource(), $pkt, 1500, 0, $peer, $port);
+               $amount = @socket_recvfrom($this->getSocketResource(), $rawData, $this->getConfigInstance()->getConfigEntry('udp_buffer_length'), MSG_DONTWAIT, $peer, $port);
 
                // Get last error
                $lastError = socket_last_error($this->getSocketResource());
@@ -145,7 +162,7 @@ class UdpListener extends BaseListener implements Listenable {
                         * This (resource temporary unavailable) can be safely ignored on
                         * "listening" UDP ports. If we don't clear the error here, our UDP
                         * "listener" won't read any packages except if the UDP sender
-                        * starts the transmission before this "listener came up...
+                        * starts the transmission before this "listener" came up...
                         */
                        socket_clear_error($this->getSocketResource());
 
@@ -153,24 +170,24 @@ class UdpListener extends BaseListener implements Listenable {
                        return;
                } elseif ($lastError > 0) {
                        // Other error detected
-                       $this->debugOutput('LISTENER: Error detected: ' . socket_strerror($lastError));
+                       self::createDebugInstance(__CLASS__)->debugOutput('UDP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Error detected: ' . socket_strerror($lastError));
 
                        // Skip further processing
                        return;
-               } elseif ((empty($pkt)) || (trim($peer) == '')) {
+               } elseif ((empty($rawData)) || (trim($peer) == '')) {
                        // Zero sized packages/peer names are usual in non-blocking mode
                        return;
                } // END - if
 
                // Debug only
-               $this->debugOutput('LISTENER: Handling UDP package with size ' . strlen($pkt) . ' from peer ' . $peer . ':' . $port);
+               self::createDebugInstance(__CLASS__)->debugOutput('UDP-LISTENER[' . __METHOD__ . ':' . __LINE__ . ']: Handling UDP package with size ' . strlen($rawData) . ' from peer ' . $peer . ':' . $port);
        }
 
        /**
-        * Checks wether the listener would accept the given package data array
+        * Checks whether the listener would accept the given package data array
         *
         * @param       $packageData    Raw package data
-        * @return      $accepts                Wether this listener does accept
+        * @return      $accepts                Whether this listener does accept
         */
        function ifListenerAcceptsPackageData (array $packageData) {
                $this->partialStub('This call should not happen. Please report it.');