]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/listener/udp/class_UdpListener.php
Package class renamed, continued (sorry for lame description)
[hub.git] / application / hub / main / listener / udp / class_UdpListener.php
index 764b6bac28601a70a6c00d51d2823fb914f949bc..866d4c855aed7c3c8adc47a9bffe181189b16191 100644 (file)
@@ -71,6 +71,22 @@ class UdpListener extends BaseListener implements Listenable {
                        throw new InvalidSocketException(array($this, gettype($socket), $errno, $errstr), BaseListener::EXCEPTION_INVALID_SOCKET);
                } // END - if
 
+               // Now, we want non-blocking mode
+               $this->debugOutput('LISTENER: Setting non-blocking mode.');
+               if (!stream_set_blocking($socket, false)) {
+                       // Get socket error code for verification
+                       $socketError = socket_last_error($socket);
+
+                       // Get error message
+                       $errorMessage = socket_strerror($socketError);
+
+                       // Shutdown this socket
+                       $this->shutdownSocket($socket);
+
+                       // And throw again
+                       throw new InvalidSocketException(array($this, gettype($socket), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+               } // END - if
+
                // Remember the socket in our class
                $this->setSocketResource($socket);
 
@@ -82,10 +98,20 @@ class UdpListener extends BaseListener implements Listenable {
         * "Listens" for incoming network packages
         *
         * @return      void
-        * @todo        0% done
+        * @todo        ~50% done
         */
        public function doListen() {
-               $this->partialStub('Need to implement this method.');
+               // Read a package and determine the client
+               $pkt = trim(stream_socket_recvfrom($this->getSocketResource(), 1500, 0, $peer));
+
+               // Zero sized packages/peer names are usual in non-blocking mode
+               if ((empty($pkt)) || (trim($peer) == '')) {
+                       // Skip here
+                       return;
+               } // END - if
+
+               // Debug only
+               $this->debugOutput('LISTENER: Handling UDP package with size ' . strlen($pkt) . ' from peer ' . $peer);
        }
 }