Martin Gergov avatar Martin Gergov committed cc3baa6

Fix for client not knowing when to timeout connection and when connection crumbles.

Comments (0)

Files changed (6)

 =======
  Usage
 =======
+
 #) Basically use it like you will use TCP in twisted, but all sockets
    will be in SOCK_DGRAM mode only. Also :code:`write()` has ttl and
    inorder options.
-2013-02-14 16:44:45+0200 [-] Log opened.
-2013-02-14 16:44:45+0200 [-] ADDING:6 with flags:1
-2013-02-14 16:44:45+0200 [-] Hello!
-2013-02-14 16:44:45+0200 [-] EchoFactory starting on 5000
-2013-02-14 16:44:45+0200 [-] Starting factory <__main__.EchoFactory instance at 0x16bd098>
-2013-02-14 16:44:45+0200 [-] ADDING:569511510 with flags:1
-2013-02-14 16:44:45+0200 [-] <<class 'udt4twisted.udt.Port'> of __main__.EchoFactory on 5000>
-2013-02-14 16:44:48+0200 [__main__.EchoFactory] ACCEPT FD:<udt4.pyudt.UdtSocket object at 0x16d8c10> ADDR:('127.0.0.1', 62931)
-2013-02-14 16:44:48+0200 [__main__.EchoFactory] ADDING:569511509 with flags:1
-2013-02-14 16:44:48+0200 [__main__.EchoFactory] CONN MADE!
-2013-02-14 16:44:48+0200 [__main__.EchoFactory] MODIFY:569511509 with flags:5
-2013-02-14 16:44:48+0200 [__main__.EchoFactory] ACCEPT FD:<udt4.pyudt.UdtSocket object at 0x16d8e10> ADDR:('127.0.0.1', 3727)
-2013-02-14 16:44:48+0200 [__main__.EchoFactory] ADDING:569511508 with flags:1
-2013-02-14 16:44:48+0200 [__main__.EchoFactory] CONN MADE!
-2013-02-14 16:44:48+0200 [__main__.EchoFactory] MODIFY:569511508 with flags:5
+2013-02-18 16:23:49+0200 [-] Log opened.
+2013-02-18 16:23:49+0200 [-] ADDING:6 with flags:1
+2013-02-18 16:23:50+0200 [-] Hello!
+2013-02-18 16:23:50+0200 [-] Starting factory <__main__.EchoFactory instance at 0x1bada70>
+2013-02-18 16:23:50+0200 [-] Started to connect.
+2013-02-18 16:23:50+0200 [-] <udt4twisted.udt.Connector instance at 0x1badb00>
+2013-02-18 16:23:50+0200 [-] ADDING:984591679 with flags:4
+2013-02-18 16:23:50+0200 [Uninitialized] Connected.
+2013-02-18 16:23:50+0200 [Uninitialized] CONN MADE!
+2013-02-18 16:23:50+0200 [Uninitialized] REMOVING:984591679 with flags:4
+2013-02-18 16:23:50+0200 [Uninitialized] ADDING:984591679 with flags:1
+2013-02-18 16:23:50+0200 [Echo,client] received An apple a day keeps the doctor away
+	 from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!!bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:50+0200 [Echo,client] received bomb!!! from IPv4Address(UDP, '127.0.0.1', 5000)
+2013-02-18 16:23:52+0200 [Echo,client] REMOVING:984591679 with flags:1
+2013-02-18 16:23:52+0200 [Echo,client] Connection failed. Reason: [Failure instance: Traceback (failure with no frames): <class 'udt4.UDTException'>: (5004, 'Operation not supported: Invalid socket ID.')
+2013-02-18 16:23:52+0200 [Echo,client] ]
+2013-02-18 16:23:52+0200 [Echo,client] Stopping factory <__main__.EchoFactory instance at 0x1bada70>

examples/newclient.py

     port = None
     def connect_to_udt_port(self, res):
         self.echo = EchoFactory()
-        self.port = reactor.connectUDT("127.0.0.1", 5000, self.echo)
+        self.port = reactor.connectUDT("127.0.0.1", 5000, self.echo, 5)
         print self.port
 
     def print_hello(self, res):
 VERSION = {
         'major' : 0,
         'minor' : 2,
-        'patch' : 0,
+        'patch' : 1,
         }
 # Utility function to read the README file.
 # Used for the long_description.  It's nice, because now 1) we have a top level

udt4twisted/udt.py

         s = '<%s to %s at %x>' % (self.__class__, self.addr, unsignedID(self))
         return s
 
-
-    def __init__(self, host, port, bindAddress, connector, reactor=None):
+    def __init__(self, host, port, bindAddress, connector, reactor=None,
+                 timeout=-1):
         # BaseClient.__init__ is invoked later
         self.connector = connector
         self.addr = (host, port)
+        self.timeout = timeout
+        self.reactor = reactor
 
         whenDone = self.resolveAddress
         err = None
             # was scheduled via a callLater in self._finishInit
             return
 
-        # err = self.socket.getsockopt(udt.UDT_STATE)
-        # if err == udt.UDTSTATUS_CONNECTED:
-        #     self.failIfNotConnected(error.getConnectError((
-        #                 err, err)))
-        #     return
+        status = self.socket.getsockopt(udt.UDT_STATE)
+        if (status != udt.UDTSTATUS_CONNECTED) and \
+                (status != udt.UDTSTATUS_INIT):
+            self.failIfNotConnected(error.getConnectError((
+                        staus)))
+            return
 
         # doConnect gets called twice.  The first time we actually need to
         # start the connection attempt.  The second time we don't really
         # is not /particularly/ detrimental to do so.  This should get
         # cleaned up some day, though.
         try:
-            #FIXME connection happens in background,
-            #epoll monitoring needed
             connectResult = self.socket.connect_ex(self.realAddress)
         except udt.UDTException as ue:
             connectResult = ue[0]
                             connectResult, connectResult)))
                 return
 
-        # If I have reached this point without raising or returning, that means
-        # that the socket is connected.
-        del self.doWrite
-        del self.doRead
-        # we first stop and then start, to reset
-        #any references to the old doRead
-        self.stopReading()
-        self.stopWriting()
-        self._connectDone()
+
+
+        status = self.socket.getsockopt(udt.UDT_STATE)
+        if status == udt.UDTSTATUS_CONNECTED:
+            # If I have reached this point without raising
+            #or returning, that means
+            # that the socket is connected.
+            del self.doWrite
+            del self.doRead
+            # we first stop and then start, to reset
+            #any references to the old doRead
+            self.startReading()
+            self.startWriting()
+            self._connectDone()
+        else:
+            # If I have reached this point without raising
+            #or returning, that means
+            # that the socket is connected.
+            del self.doWrite
+            del self.doRead
+            # we first stop and then start, to reset
+            #any references to the old doRead
+            #self.startReading()
+            self.startWriting()
 
     def _connectDone(self):
         """
         self.startReading()
         self.protocol.makeConnection(self)
 
+    def doWrite(self):
+        try:
+            state = self.socket.getsockopt(udt.UDT_STATE)
+        except udt.UDTException as ue:
+            self.connected = False
+            self.failIfNotConnected(ue)
+        else:
+            if (state == udt.UDTSTATUS_CONNECTED) and (self.connected == False):
+                self._connectDone()
+                self.stopReading()
+                self.stopWriting()
+                self.startReading()
+                self.startWriting()
+                return
+            Connection.doWrite(self)
+
+
 class Connector(base.BaseConnector):
     """
     A L{Connector} provides of L{twisted.internet.interfaces.IConnector} for
     def __init__(self, host, port, factory, timeout, bindAddress, reactor=None):
         self.host, self.port = host, port
         self.bindAddress = bindAddress
+        self.timeout = timeout
         base.BaseConnector.__init__(self, factory, timeout, reactor)
 
 
         @rtype: L{Client}
         """
         return Client(self.host, self.port,
-                      self.bindAddress, self, self.reactor)
+                      self.bindAddress, self, self.reactor, self.timeout)
 
 
     def getDestination(self):

udt4twisted/udtepollreactor.py

                         why = selectable.doWrite()
                         inRead = False
                         if self._isUDT(selectable):
-                            #FIXME BIG ISSUE epoll keeps notifying for write
-                            #if client connects and doesn't recvmsg
-                            #bad things happen.
                             gc.collect()
             except:
                 # Any exception from application code gets logged and will
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.