Commits

Martin Gergov committed fb3436d

Added a Connector class for udt and some fixes for when connection is lost.

Comments (0)

Files changed (3)

examples/client.py

 
     def datagramReceived(self, data, (host, port)):
         print "Received %r" % data
-        self.transport.write("bomb!@!!!!!", ("127.0.0.1", 5000))
+        self.transport.write("bomb!!!", ("127.0.0.1", 5000))
 
 udtclient = UDTClient()
 reactor.connectUDT("127.0.0.1", 5000, udtclient)

udt4twisted/udt.py

         self.protocol.makeConnection(self)
 
 
-    def doConnect(self, fd=None):
-        if not fd:
-            self.doRead = self.doConnect
-            self.socket = self.createInternetSocket()
-            self.fileno = self.socket.fileno
-            self.socket.connect_ex(self._connectedAddr)
-            self._connectDone()
-            return
-        try:
-            data = udt.recvmsg(fd, self.maxPacketSize)
-            addr = self._connectedAddr
-        except udt.UDTException as ue:
-            #Reset socket monitoring
-            print ue
-            self.reactor._poller.remove_usock(self.socket.UDTSOCKET, self.reactor._POLL_IN)
-            #6002
-            if ue[0] == EASYNCRCV:
-                self.reactor._poller.add_usock(self.socket.UDTSOCKET, self.reactor._POLL_IN)
-            #2001
-            if ue[0] == ECONNLOST:
-                self.stopListening()
-            gc.collect()
-            
-        else:
-            try:
-                self.protocol.datagramReceived(data, addr)
-            except:
-                log.err()
-
         
 
     def connect(self, host, port):
         self.socket.listen(self.backlog)
 
 
-    def doRead(self, fd=None):
+    def doRead(self, fd=None, addr=None):
         """
         Called when my socket is ready for reading.
         """
         try:
-            print "recv from:", fd, " size:", udt.UDT_RCVDATE
-            #FIXME
+            #print "recv from:", fd, " size:", self.maxPacketSize
             data = udt.recvmsg(fd, self.maxPacketSize)
-            addr = self.addresses[fd.UDTSOCKET][1]
+            if addr == None:
+                addr = self.addresses[fd.UDTSOCKET][1]
         except udt.UDTException as ue:
+            print ue
             return ue
         except socket.error as se:
             pass
         @param addr: A tuple of (I{stringified dotted-quad IP address},
             I{integer port number}); can be C{None} in connected mode.
         """
-        if self._connectedAddr:
-            print "Hello!"
-            self.socket.sendmsg(datagram)
-            return
         assert addr != None
-        
         if not addr[0].replace(".", "").isdigit() and addr[0] != "<broadcast>":
             warnings.warn("Please only pass IPs to write(), not hostnames",
                           DeprecationWarning, stacklevel=2)
         try:
-            print "sending..."
+            if self._connectedAddr:
+                self.socket.sendmsg(datagram)
+                return
             socket = [v[0] for k, v in self.addresses.iteritems() if  v[1] == addr][0]
             return udt.sendmsg(socket, datagram, len(datagram))
-        # except socket.error as se:
-            #     no = se.args[0]
-            #     if no == EINTR:
-            #         return self.write(datagram, addr)
-            #     elif no == EMSGSIZE:
-            #         raise error.MessageLengthError("message too long")
-            #     elif no == ECONNREFUSED:
-            #         # in non-connected UDP ECONNREFUSED is platform dependent, I
-            #         # think and the info is not necessarily useful. Nevertheless
-            #         # maybe we should call connectionRefused? XXX
-            #         return
-            #     else:
-            #         raise
         except KeyError:
             print "No key found!"
-        except udt.UDTException:
-            print "!!!"
-        
+        except udt.UDTException as ue:
+            print ue
+            if ue[0] == ECONNLOST:
+                del self.reactor._udtsockets[socket.UDTSOCKET]
+                self.stopListening()
 
 
     def _connectToProtocol(self):
         self.protocol.makeConnection(self)
         self.startReading()
+
+
+
+class Connector(Port):
+    
+    def doConnect(self, fd=None):
+        if not fd:
+            self.socket = self.createInternetSocket()
+            self.fileno = self.socket.fileno
+            self.socket.connect_ex(self._connectedAddr)
+            self._connectDone()
+            return
+
+        self._read(fd, self._connectedAddr)
+
+    def doRead(self, fd=None):
+        why = Port.doRead(self, self.socket.UDTSOCKET, self._connectedAddr)
+        #FIXME
+        if why:
+            if (why[0] == ECONNLOST):
+                self.stopListening()
+        return why

udt4twisted/udtepollreactor.py

 from udt4 import pyudt, accept, sendmsg, recvmsg, UDT_RCVDATE, UDTException
 from udt4.pyudt import UdtSocket
 from udt4twisted import udt
+from udt4 import EASYNCRCV, ECONNLOST
 import socket as s
 import gc
 
                 log.err()
         if why:
             if isinstance(fd, udt4.UDTSOCKET):
-                self._poller.add_usock(fd, self._POLL_IN)
-                if why[0] == 6002:
+                self._poller.remove_usock(fd, self._POLL_IN)
+                if why[0] == EASYNCRCV:
+                    #Reset monitoring if no data is available
                     self._poller.add_usock(fd, self._POLL_IN)
-                    gc.collect()
+                    
+                if why[0] == ECONNLOST:
+                    del self._udtsockets[fd]
+                gc.collect()
                 return
+            print "dissconnect..."
             self._disconnectSelectable(selectable, why, inRead)
 
 
                    bindAddress = (0, "127.0.0.1"),
                    maxPacketSize=8192):
 
-        c = udt.Port(bindAddress[0], protocol, bindAddress[1],
+        c = udt.Connector(bindAddress[0], protocol, bindAddress[1],
                      maxPacketSize, self, 0)
         c.connect(host, port)
         return c