qmsk/net/transport/tcp.py
changeset 37 14db3fe42b6c
parent 28 020c89baaa33
child 45 bb49bf8222ed
--- a/qmsk/net/transport/tcp.py	Sun Aug 23 22:43:39 2009 +0300
+++ b/qmsk/net/transport/tcp.py	Sun Aug 23 22:59:40 2009 +0300
@@ -33,22 +33,27 @@
     """
         An implementation of Service for TCP sockets.
     """
+    
+    _SOCKTYPE = socket.SOCK_STREAM
 
-    def __init__ (self, endpoint, af=socket.AF_UNSPEC, listen_backlog=LISTEN_BACKLOG) :
+    def __init__ (self, endpoint, listen_backlog=LISTEN_BACKLOG, family=None) :
         """
             Construct a service, bound to the given local endpoint and listening for incoming connections using the
             given backlog.
 
                 endpoint        - local Endpoint to bind() to. Usually, it is enough to just specify the port.
-                af              - the socket address family to use (one of qmsk.net.socket.constants.AF_*)
                 listen_backlog  - backlog length argument to use for socket.listen()
+                family          - (optional) address family to use if no endpoint is given
             
+            Note that as a special case, it is possible to construct a service without an Endpoint (i.e. None).
+            In this case, there will be no socket.bind() call, instead, a socket is created with the given address
+            family (which *MUST* be given), and .listen() causes the OS to pick a local address to use.
 
             This will raise an error if the bind() or listen() operations fail.
         """
         
         # construct a suitable socket bound to the given endpoint
-        self._init_endpoint(endpoint, af, socket.SOCK_STREAM)
+        self._init_endpoint(endpoint, family=family)
 
         # make us listen
         self._listen(listen_backlog)
@@ -82,19 +87,19 @@
         An implementation of Client for TCP sockets.
     """
 
-    def __init__ (self, connect_endpoint, af=socket.AF_UNSPEC, bind_endpoint=None) :
+    _SOCKTYPE = socket.SOCK_STREAM
+
+    def __init__ (self, connect_endpoint, bind_endpoint=None) :
         """
             Construct a client, connecting to the given remote endpoint.
 
                 connect_endpoint    - remote Endpoint to connect() to.
-                af                  - socket address family to use (one of qmsk.net.socket.constants.AF_*)
                 bind_endpoint       - (optional) local Endpoint to bind() to before connecting.
 
         """
 
         # store
         self.connect_endpoint = connect_endpoint
-        self.af = af
         self.bind_endpoint = bind_endpoint
 
     def connect (self, cls=Connection) :
@@ -104,14 +109,14 @@
 
         if self.bind_endpoint :
             # construct a suitable local socket, bound to a specific endpoint
-            sock = self._bind_endpoint(self.bind_endpoint, self.af, socket.SOCK_STREAM)
+            sock = self._bind_endpoint(self.bind_endpoint)
 
             # connect it to the remote endpoint
-            self._connect_sock_endpoint(sock, self.connect_endpoint, self.af, socket.SOCK_STREAM)
+            self._connect_sock_endpoint(sock, self.connect_endpoint)
 
         else :
             # let _init_connect_endpoint pick a socket to use
-            sock = self._connect_endpoint(self.connect_endpoint, self.af, socket.SOCK_STREAM)
+            sock = self._connect_endpoint(self.connect_endpoint)
         
         # construct
         return cls(sock)