* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Hub Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.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 . */ class UdpListener extends BaseListener implements Listenable { /** * Protected constructor * * @return void */ protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); } /** * Creates an instance of this class * * @param $nodeInstance A NodeHelper instance * @return $listenerInstance An instance a prepared listener class */ public static final function createUdpListener (NodeHelper $nodeInstance) { // 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; } /** * Initializes the listener by setting up the required socket server * * @return void * @throws InvalidSocketException Thrown if the socket is invalid or an * error was detected. */ public function initListener () { // Try to open a UDP socket $mainSocket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); // 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); } // 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)) { // Get socket error code for verification $socketError = socket_last_error($mainSocket); // Get error message $errorMessage = socket_strerror($socketError); // Shutdown this socket $this->shutdownSocket($mainSocket); // And throw again throw new InvalidSocketException(array($this, gettype($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())) { // Get socket error code for verification $socketError = socket_last_error($mainSocket); // Get error message $errorMessage = socket_strerror($socketError); // Shutdown this socket $this->shutdownSocket($mainSocket); // And throw again throw new InvalidSocketException(array($this, gettype($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)) { // Get socket error code for verification $socketError = socket_last_error($socket); // Get error message $errorMessage = socket_strerror($socketError); // Shutdown this socket $this->shutdownSocket($mainSocket); // And throw again throw new InvalidSocketException(array($this, gettype($mainSocket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET); } // END - if // Remember the socket in our class $this->registerServerSocketResource($mainSocket); // Output message $this->debugOutput('LISTENER: UDP listener now ready on IP ' . $this->getListenAddress() . ', port ' . $this->getListenPort() . ' for service.'); } /** * "Listens" for incoming network packages * * @return void * @todo ~50% done */ public function doListen() { // Read a package and determine the peer $amount = @socket_recvfrom($this->getSocketResource(), $pkt, 1500, 0, $peer, $port); // Get last error $lastError = socket_last_error($this->getSocketResource()); // Do we have an error at the line? if ($lastError == 11) { /* * 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... */ socket_clear_error($this->getSocketResource()); // Skip further processing return; } elseif ($lastError > 0) { // Other error detected $this->debugOutput('LISTENER: Error detected: ' . socket_strerror($lastError)); // Skip further processing return; } elseif ((empty($pkt)) || (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); } /** * Checks wether the listener would accept the given package data array * * @param $packageData Raw package data * @return $accepts Wether this listener does accept */ function ifListenerAcceptsPackageData (array $packageData) { $this->partialStub('This call should not happen. Please report it.'); } } // [EOF] ?>