예제 #1
0
    def __init__(self, context, name=""):

        # Variables to hold threads
        self._sendingThread = None
        self._receivingThread = None

        # Create queue for outgoing packages.
        self._qout = TinyPackageQueue(64, *context._queue_params)

        # Init normally (calls _set_status(0)
        Connection.__init__(self, context, name)
예제 #2
0
 def __init__(self, context, name=''):
     
     # Variables to hold threads
     self._sendingThread = None
     self._receivingThread = None
     
     # Create queue for outgoing packages.
     self._qout = TinyPackageQueue(64, *context._queue_params)
     
     # Init normally (calls _set_status(0)
     Connection.__init__(self, context, name)
예제 #3
0
    def _set_status(self, status, bsd_socket=None):
        """_connected(status, bsd_socket=None)

        This method is called when a connection is made.

        Private method to apply the bsd_socket.
        Sets the socket and updates the status.
        Also instantiates the IO threads.

        """

        # Lock the connection while we change its status
        self._lock.acquire()

        # Update hostname and port number; for hosting connections the port
        # may be different if max_tries > 0. Each client connection will be
        # assigned a different ephemeral port number.
        # http://www.tcpipguide.com/free/t_TCPPortsConnectionsandConnectionIdentification-2.htm
        # Also get hostname and port for other end
        if bsd_socket is not None:
            if True:
                self._hostname1, self._port1 = bsd_socket.getsockname()
            if status != STATUS_WAITING:
                self._hostname2, self._port2 = bsd_socket.getpeername()

        # Set status as normal
        Connection._set_status(self, status)

        try:

            if status in [STATUS_HOSTING, STATUS_CONNECTED]:
                # Really connected

                # Store socket
                self._bsd_socket = bsd_socket

                # Set socket to blocking. Needed explicitly on Windows
                # One of these things it takes you hours to find out ...
                bsd_socket.setblocking(1)

                # Create and start io threads
                self._sendingThread = SendingThread(self)
                self._receivingThread = ReceivingThread(self)
                #
                self._sendingThread.start()
                self._receivingThread.start()

            if status == 0:

                # Close bsd socket
                try:
                    self._bsd_socket.shutdown()
                except Exception:
                    pass
                try:
                    self._bsd_socket.close()
                except Exception:
                    pass
                self._bsd_socket = None

                # Remove references to threads
                self._sendingThread = None
                self._receivingThread = None

        finally:
            self._lock.release()
예제 #4
0
 def _set_status(self, status, bsd_socket=None):
     """ _connected(status, bsd_socket=None)
     
     This method is called when a connection is made.
     
     Private method to apply the bsd_socket.
     Sets the socket and updates the status.
     Also instantiates the IO threads.
     
     """
     
     # Lock the connection while we change its status
     self._lock.acquire()
     
     # Update hostname and port number; for hosting connections the port
     # may be different if max_tries > 0. Each client connection will be
     # assigned a different ephemeral port number.
     # http://www.tcpipguide.com/free/t_TCPPortsConnectionsandConnectionIdentification-2.htm
     # Also get hostname and port for other end
     if bsd_socket is not None:
         if True:
             self._hostname1, self._port1 = bsd_socket.getsockname()
         if status != STATUS_WAITING:
             self._hostname2, self._port2 = bsd_socket.getpeername()
     
     # Set status as normal
     Connection._set_status(self, status)
     
     try:
         
         if status in [STATUS_HOSTING, STATUS_CONNECTED]:
             # Really connected
             
             # Store socket
             self._bsd_socket = bsd_socket
             
             # Set socket to blocking. Needed explicitly on Windows
             # One of these things it takes you hours to find out ...
             bsd_socket.setblocking(1)
             
             # Create and start io threads
             self._sendingThread = SendingThread(self)
             self._receivingThread = ReceivingThread(self)
             #
             self._sendingThread.start()
             self._receivingThread.start()
         
         if status == 0:
             
             # Close bsd socket
             try:
                 self._bsd_socket.shutdown()
             except Exception:
                 pass
             try:
                 self._bsd_socket.close()
             except Exception:
                 pass
             self._bsd_socket = None
             
             # Remove references to threads
             self._sendingThread = None
             self._receivingThread = None
     
     finally:
         self._lock.release()