def pollOnce(self):
        try:
            rlist = select.select([self.listenerSocket] +
                                  self.connections.keys(), [], [])[0]
        except:
            try:
                self.listenerSocket.fileno()
            except:
                self.notify.error('The listener socket is no longer valid!')

            for socket in self.connections.keys():
                try:
                    socket.fileno()
                    socket.getpeername()
                except:
                    del self.connections[socket]
                    if socket in self.dispatchThreads:
                        del self.dispatchThreads[socket]

            return

        if self.listenerSocket in rlist:
            self.handleNewConnection()
        for socket in rlist:
            connection = self.connections.get(socket)
            if connection is None:
                continue
            if socket in self.dispatchThreads:
                continue
            self.dispatchThreads[socket] = threading.Thread(
                target=self.dispatchThread, args=[socket])
            self.dispatchThreads[socket].start()
Esempio n. 2
0
def checkConnection(socket, host, port):
    while True:
        try:
            socket.fileno() > 0
        except:
            print("server disconnected.")
            sys.exit(-1)
Esempio n. 3
0
    def loop(self):
        
        mask = select.POLLHUP | select.POLLERR

        self.epoll.register(self.server.fileno(), select.POLLIN | mask)

        while True:
            events = self.epoll.poll(self.EVENT_TIMEOUT_SECONDS)
            for fileno, event in events:
                if fileno == self.server.fileno():
                    try:
                        socket, client_address = self.server.accept()
                        socket.setblocking(0)
                        connection = Connection(socket)
                        self.connections[socket.fileno()] = connection
                        self.epoll.register(socket.fileno(), select.POLLIN | mask)
                        connection.last_event_time = time.time()
                    except:
                        pass
                else:
                    connection = self.connections[fileno]
                    connection.last_event_time = time.time()
                    if event & select.EPOLLERR or event & select.EPOLLHUP:
                        self.drop_connection(connection)
                    if event & select.POLLIN:
                        try:
                            data = connection.socket.recv(4096)
                        except:
                            print 'sock err'
                            self.drop_connection(connection)
                            continue
                        if data and data != '':
                            connection.on_recv(data)

                            if connection.state == connection.STATE_RESPONDING:
                                self.epoll.modify(fileno, select.EPOLLOUT | mask)
                            elif connection.state == connection.STATE_DONE:
                                self.drop_connection(connection)
                                continue
                        else:
                            self.drop_connection(connection)
                            continue

                    elif event & select.EPOLLOUT:
                        connection.respond()
                        if connection.state == connection.STATE_DONE:
                            self.drop_connection(connection)
                            continue

            now = time.time()
            for conn in self.connections.values():
                if now - conn.last_event_time > self.EVENT_TIMEOUT_SECONDS:
                    self.drop_connection(conn)
Esempio n. 4
0
    def get_client_depuis_socket(self, socket):
        """Cette méthode retourne le client connecté, en fonction du
        socket passé en paramètre. On se base sur le fileno() du socket
        pour retrouver l'ID du client et sur le dictionnaire filenos
        faisant la correspondance.

        On retourne le client trouvé.

        """
        try:
            return self.clients[self.filenos[socket.fileno()]]
        except KeyError:
            raise KeyError("Le socket n. {0} n'est pas un socket client" \
                    .format(socket.fileno()))
Esempio n. 5
0
    def get_client_depuis_socket(self, socket):
        """Cette méthode retourne le client connecté, en fonction du
        socket passé en paramètre. On se base sur le fileno() du socket
        pour retrouver l'ID du client et sur le dictionnaire filenos
        faisant la correspondance.

        On retourne le client trouvé.

        """
        try:
            return self.clients[self.filenos[socket.fileno()]]
        except KeyError:
            raise KeyError("Le socket n. {0} n'est pas un socket client" \
                    .format(socket.fileno()))
Esempio n. 6
0
    def __init__(self, poller, onMessageReceived = None, onConnected = None, onDisconnected = None,
                 socket=None, timeout=10.0, sendBufferSize = 2 ** 13, recvBufferSize = 2 ** 13):

        self.sendRandKey = None
        self.recvRandKey = None
        self.encryptor = None

        self.__socket = socket
        self.__readBuffer = bytes()
        self.__writeBuffer = bytes()
        self.__lastReadTime = time.time()
        self.__timeout = timeout
        self.__poller = poller
        if socket is not None:
            self.__socket = socket
            self.__fileno = socket.fileno()
            self.__state = CONNECTION_STATE.CONNECTED
            self.__poller.subscribe(self.__fileno,
                                     self.__processConnection,
                                     POLL_EVENT_TYPE.READ | POLL_EVENT_TYPE.WRITE | POLL_EVENT_TYPE.ERROR)
        else:
            self.__state = CONNECTION_STATE.DISCONNECTED
            self.__fileno = None
            self.__socket = None

        self.__onMessageReceived = onMessageReceived
        self.__onConnected = onConnected
        self.__onDisconnected = onDisconnected
        self.__sendBufferSize = sendBufferSize
        self.__recvBufferSize = recvBufferSize
Esempio n. 7
0
    def serviceClientsIn(self):
        data = ""
        while 1:
            ### check the readable status of each socket(has client sent anything)
            #print "server in sleep"
            #time.sleep(.1) #needed to avoid a hang btween server/client?

            while not self.sockets:
                pass
            #self.socketsLock.acquire()
            r, w, e = select.select(self.sockets.values(), [], [], .1)
            #self.socketsLock.release()
            #print "serviceClientsIn loop"
            if r:  #Data has been sent on the following sockets
                try:
                    self.socketsLock.acquire()
                    for socket in r:
                        data = socket.recv(self.size)
                        while data[-4:] != self.term:
                            if BotWars.VERBOSE:
                                print "server retrieving rest of unsent data"
                            data += socket.recv(self.size)
                        self.indivRequestsLock.acquire()
                        self.indivRequests.append((socket.fileno(), data[:-4]))
                        if BotWars.VERBOSE:
                            #print "Server Recieve: " +pickle.loads(data[:-1])[0]
                            pass
                        self.indivRequestsLock.release()
                        self.ismore.set()
                    self.socketsLock.release()
                except error, ae:
                    self.handleError(socket)
Esempio n. 8
0
    def __init__(self, socket, state, application_context, pollables):
        ## Application_context
        self._application_context = application_context

        ## Request context - important info from request
        self._request_context = {
            "headers": {},
            "args": [],
            "method": "uknown",
            "uri": "uknown",
        }

        ## Socket to work with
        self._socket = socket

        ## File descriptor of socket
        self._fd = socket.fileno()

        ## Data that the socket has recieved
        self._recvd_data = ""

        ## Data that the socket wishes to send
        self._data_to_send = ""

        ## Current state of the ServiceSocket. Initialized to the first state
        self._state = state

        ## Service that has been chosen to handle the request, based on uri
        self._service = base_service.BaseService()

        ## Dict of all the pollables in the server
        self._pollables = pollables
Esempio n. 9
0
def set_close_on_exec(socket):
    """Set the FD_CLOEXEC flag."""
    if os.name == 'posix' and hasattr(fcntl, 'F_SETFD'):
        fd = socket.fileno()
        flags = fcntl.fcntl(fd, fcntl.F_GETFD, 0)
        flags = flags | fcntl.FD_CLOEXEC
        fcntl.fcntl(fd, fcntl.F_SETFD, flags)
Esempio n. 10
0
def pub_message(source_socket, msg, server_socket):
    for user in USERS_LIST:
        if user[1] == str(source_socket.fileno()):
            nickname = user[3]
            break
    # Do not send the message to server socket and the client who has
    # sent the message
    for socket in SOCKET_LIST:
        if socket != server_socket and socket != source_socket:
            try:
                for user in USERS_LIST:
                    # The user for each specific socket
                    if user[1] == str(socket.fileno()):
                        SYMM_KEY = user[2]
                        CIPHER = AES.new(SYMM_KEY)
                        h_msg = PUB_MESSAGE + ',' + nickname + ',' + msg
                        msg_enc = EncodeAES(CIPHER, h_msg)
                        socket.send(msg_enc)
            except:
                print 'fedeu'
                # broken socket connection may be, chat client pressed ctrl+c
                # for example
                # Remove user from USERS_LIST
                for index, user in enumerate(USERS_LIST):
                    if user[1] == str(source_socket.fileno()):
                        # Close connection
                        source_socket.close()
                        # Remove socket from SOCKET_LIST
                        SOCKET_LIST.remove(source_socket)
                        del USERS_LIST[index]
                        break
Esempio n. 11
0
def broadcast(source_socket, message, server_socket):
    """Function to broadcast chat messages to all connected clients"""
    # Do not send the message to server socket and the client who has
    # sent the message
    for socket in SOCKET_LIST:
        if socket != server_socket and socket != source_socket:
            try:
                for user in USERS_LIST:
                    # The user for each specific socket
                    if user[1] == str(socket.fileno()):
                        SYMM_KEY = user[2]
                        CIPHER = AES.new(SYMM_KEY)
                        h_msg = BROADCAST + message
                        msg_enc = EncodeAES(CIPHER, h_msg)
                        socket.send(msg_enc)
            except:
                # broken socket connection may be, chat client pressed ctrl+c
                # for example
                # Remove user from USERS_LIST
                for index, user in enumerate(USERS_LIST):
                    if user[1] == str(source_socket.fileno()):
                        # Close connection
                        source_socket.close()
                        # Remove socket from SOCKET_LIST
                        SOCKET_LIST.remove(source_socket)
                        del USERS_LIST[index]
                        break
Esempio n. 12
0
 def __init__(self, socket, cxn_map):
     self.cxn_map = cxn_map
     self.socket = socket
     self.fd = socket.fileno()
     self.cxn_map[self.fd] = self
     self.done_reading = False
     self.read_buffer = []
Esempio n. 13
0
    def _dispatch(self):
        removals = []
        for fno, cliobj in self.clients.iteritems():
            if not cliobj.socket:
                removals.append(fno)

        for removal in removals:
            del self.clients[removal]
                
        rl, wl, _ = select.select([self.socket.fileno(), ] + self.clients.keys(),
                                   map(lambda f: f.socket.fileno(), filter(lambda f: f.has_write(), self.clients.values())),
                                   [])

        for s in rl:
            if s == self.socket.fileno():
                # New client
                socket, address = self.socket.accept()
                self.clients[socket.fileno()] = self.CONNECTION_CLASS(server=self,
                                                                      sock=socket,
                                                                      address=address)
            else:
                if not self.clients[s].handle_read():
                    del self.clients[s]
        
        for s in wl:
            if self.clients.has_key(s) and not self.clients[s].handle_write():
                del self.clients[s]
Esempio n. 14
0
def init_connection(server, sockets, clients, data_in, data_out, epoll):
    socket, address = server.accept()
    socket.setblocking(0)

    fd = socket.fileno()
    epoll.register(fd, select.EPOLLOUT | select.EPOLLIN | select.EPOLLET)
    sockets[fd] = socket
    clients.append(fd)
    data_in[fd] = b''

    light_message = light_pb2.LightsUpdateMessage()
    light_message.type = light_pb2.SET_LIGHTS
    for fixture in lights:
        light_message.lights.extend([fixture.to_message()])

    data_out[fd] = make_message(light_message, "light_update")

    global audio_url
    global audio_on
    if audio_on == True:
        audio_message = audio_pb2.AudioMessage()
        audio_message.type = audio_pb2.PLAY_AUDIO
        audio_message.url = audio_url
        data_out[fd] += make_message(audio_message, "audio")

    print("Accepted new client {:02d}".format(fd))
Esempio n. 15
0
def set_close_on_exec(socket):
    """Set the FD_CLOEXEC flag."""
    if os.name == 'posix' and hasattr(fcntl, 'F_SETFD'):
        fd = socket.fileno()
        flags = fcntl.fcntl(fd, fcntl.F_GETFD, 0)
        flags = flags | fcntl.FD_CLOEXEC
        fcntl.fcntl(fd, fcntl.F_SETFD, flags)
def send_keepalive_msg(socket, msg, peer):
    while running:
        print("Sending " + msg + " to target " + peer[0] + " socket no: " +
              str(socket.fileno()) + "\n")
        # need to encode msg since byte-like obj is required
        socket.sendto(msg.encode(), peer)
        time.sleep(timeout)
Esempio n. 17
0
 def run(self):
     """
     Run the mailbox to attend petitions and manage the status of sent messages.
     """
     events = self._select.select(0)
     for key, mask in events:
         socket = key.fileobj
         if socket.fileno() == -1:
             continue
         data, address = socket.recvfrom(1024)
         if not len(data) >= header.size:
             continue
         id, type = header.unpack_from(data)
         if type == DATA:
             payload = data[header.size:]
             #Do something with the payload
             socket.sendto(header.pack(id, ACK), address)
             if not id in self._received:
                 if callable(self._protocol):
                     self._protocol(payload, address, self)
                 message = self._messages.pop()
                 message.id = id
                 message.data = payload
                 self._received[id] = message
                 self._mysched.enter(1, 1, self._remove_message, argument=(id,))
         elif type == ACK:
             if id not in self._sent:
                 continue
             event = self._sent[id].event
             if event in self._mysched.queue:
                 self._mysched.cancel(event)
             message = self._sent.pop(id)
             message.reset()
             self._messages.append(message)
     self._mysched.run(False)
Esempio n. 18
0
File: tcp.py Progetto: snower/sevent
    def __init__(self, loop=None, socket=None, address=None, dns_resolver = None, max_buffer_size = None):
        super(Socket, self).__init__()
        self._loop =loop or instance()
        self._socket = socket
        self._fileno = socket.fileno() if socket else 0
        self._address = address
        self._dns_resolver = dns_resolver or DNSResolver.default()
        self._connect_handler = False
        self._connect_timeout = 5
        self._connect_timeout_handler = None
        self._read_handler = False
        self._write_handler = False
        self._max_buffer_size = max_buffer_size or self.MAX_BUFFER_SIZE
        self._rbuffers = Buffer(max_buffer_size= self._max_buffer_size)
        self._wbuffers = None
        self._state = STATE_INITIALIZED
        self._is_enable_fast_open = False
        self._is_enable_nodelay = False
        self._is_resolve = False
        self._has_drain_event = False

        if self._socket:
            self._state = STATE_STREAMING
            self._socket.setblocking(False)
            self._read_handler = self._loop.add_fd(self._socket, MODE_IN, self._read_cb)
Esempio n. 19
0
def _read_chunk_from_socket(socket):
    """
    (coroutine)
    Turn socket reading into coroutine.
    """
    fd = socket.fileno()
    f = Future()

    def read_callback():
        get_event_loop().remove_reader(fd)

        # Read next chunk.
        try:
            data = socket.recv(1024)
        except OSError as e:
            # On OSX, when we try to create a new window by typing "pymux
            # new-window" in a centain pane, very often we get the following
            # error: "OSError: [Errno 9] Bad file descriptor."
            # This doesn't seem very harmful, and we can just try again.
            logger.warning('Got OSError while reading data from client: %s. '
                           'Trying again.', e)
            f.set_result('')
            return

        if data:
            f.set_result(data)
        else:
            f.set_exception(BrokenPipeError)

    get_event_loop().add_reader(fd, read_callback)

    return f
Esempio n. 20
0
def bind_and_listen_on_posix_socket(socket_name, accept_callback):
    """
    :param accept_callback: Called with `PosixSocketConnection` when a new
        connection is established.
    """
    assert socket_name is None or isinstance(socket_name, six.text_type)
    assert callable(accept_callback)

    # Py2 uses 0027 and Py3 uses 0o027, but both know
    # how to create the right value from the string '0027'.
    old_umask = os.umask(int('0027', 8))

    # Bind socket.
    socket_name, socket = _bind_posix_socket(socket_name)

    _ = os.umask(old_umask)

    # Listen on socket.
    socket.listen(0)

    def _accept_cb():
        connection, client_address = socket.accept()
        # Note: We don't have to put this socket in non blocking mode.
        #       This can cause crashes when sending big packets on OS X.

        posix_connection = PosixSocketConnection(connection)

        accept_callback(posix_connection)

    get_event_loop().add_reader(socket.fileno(), _accept_cb)

    logger.info('Listening on %r.' % socket_name)
    return socket_name
Esempio n. 21
0
    def __init__(self, backend, socket, peer, rules):
        self._backend = backend
        self._socket = socket
        self._socket.setblocking(0)
        self._peer = peer

        self._rbuf = ""
        self._wbuf = ""
        self._read = 0
        self._written = 0

        self._read_delimiter = "\r\n\r\n"
        self._bytes_to_read = 0
        self._rcb = self.on_headers

        self._is_alive = True

        self._rules = rules

        self._in_id = gobject.io_add_watch(socket.fileno(),
                                           gobject.IO_IN | gobject.IO_PRI,
                                           self.on_read)
        self._out_id = gobject.io_add_watch(self._socket.fileno(),
                                            gobject.IO_OUT, self.on_write)
        self._err_id = gobject.io_add_watch(self._socket.fileno(),
                                            gobject.IO_ERR, self.on_error)
        self._hup_id = gobject.io_add_watch(self._socket.fileno(),
                                            gobject.IO_HUP, self.on_hang_up)
Esempio n. 22
0
def broadcast(source_socket, message, server_socket):
    """Function to broadcast chat messages to all connected clients"""
    # Do not send the message to server socket and the client who has
    # sent the message
    for socket in SOCKET_LIST:
        if socket != server_socket and socket != source_socket:
            try:
                for user in USERS_LIST:
                    # The user for each specific socket
                    if user[1] == str(socket.fileno()):
                        SYMM_KEY = user[2]
                        CIPHER = AES.new(SYMM_KEY)
                        h_msg = BROADCAST + message
                        msg_enc = EncodeAES(CIPHER, h_msg)
                        socket.send(msg_enc)
            except:
                # broken socket connection may be, chat client pressed ctrl+c
                # for example
                # Remove user from USERS_LIST
                for index, user in enumerate(USERS_LIST):
                    if user[1] == str(source_socket.fileno()):
                        # Close connection
                        source_socket.close()
                        # Remove socket from SOCKET_LIST
                        SOCKET_LIST.remove(source_socket)
                        del USERS_LIST[index]
                        break
Esempio n. 23
0
def _read_chunk_from_socket(socket):
    """
    (coroutine)
    Turn socket reading into coroutine.
    """
    fd = socket.fileno()
    f = Future()

    def read_callback():
        get_event_loop().remove_reader(fd)

        # Read next chunk.
        try:
            data = socket.recv(1024)
        except OSError as e:
            # On OSX, when we try to create a new window by typing "pymux
            # new-window" in a centain pane, very often we get the following
            # error: "OSError: [Errno 9] Bad file descriptor."
            # This doesn't seem very harmful, and we can just try again.
            logger.warning(
                'Got OSError while reading data from client: %s. '
                'Trying again.', e)
            f.set_result('')
            return

        if data:
            f.set_result(data)
        else:
            f.set_exception(BrokenPipeError)

    get_event_loop().add_reader(fd, read_callback)

    return f
Esempio n. 24
0
 def _socketListRemove(self, socket, socketsList) :
     self._opLock.acquire()
     ok = (socket.fileno() in self._asyncSockets and socket in socketsList)
     if ok :
         socketsList.remove(socket)
     self._opLock.release()
     return ok
Esempio n. 25
0
 def _add_connection(self, socket):
     host, port = socket.getpeername()
     self.log.debug("Adding connection from %s:%d", host, port)
     socket.setblocking(0)
     self._pending_connections[socket.fileno()] = (time.time(),
                                                   socket)
     self._poller.register(socket, self.READ_ONLY_MASK)
Esempio n. 26
0
 def serviceClientsIn(self):
     data = ""
     while 1:
         ### check the readable status of each socket(has client sent anything)
         #print "server in sleep"
         #time.sleep(.1) #needed to avoid a hang btween server/client?
         
         while not self.sockets:
             pass
         #self.socketsLock.acquire()
         r,w,e = select.select(self.sockets.values(),[],[],.1)
         #self.socketsLock.release()
         #print "serviceClientsIn loop"
         if r:  #Data has been sent on the following sockets
             try:
                 self.socketsLock.acquire()
                 for socket in r:
                     data = socket.recv(self.size)
                     while data[-4:] != self.term:
                         if BotWars.VERBOSE:
                             print "server retrieving rest of unsent data"
                         data +=  socket.recv(self.size)
                     self.indivRequestsLock.acquire()
                     self.indivRequests.append((socket.fileno(),data[:-4]))
                     if BotWars.VERBOSE:
                         #print "Server Recieve: " +pickle.loads(data[:-1])[0]
                         pass
                     self.indivRequestsLock.release()
                     self.ismore.set()
                 self.socketsLock.release()
             except error, ae:
                 self.handleError(socket)
 def preexec_fn():
     fd = socket.fileno()
     os.environ['LISTEN_FDS'] = '1'
     os.environ['LISTEN_PID'] = str(os.getpid())
     if fd != 3:
         os.dup2(fd, 3)
     os.closerange(4, 100)
Esempio n. 28
0
 def _socketListAdd(self, socket, socketsList) :
     self._opLock.acquire()
     ok = (socket.fileno() in self._asyncSockets and socket not in socketsList)
     if ok :
         socketsList.append(socket)
     self._opLock.release()
     return ok
def send_keepalive_msg(socket, msg, peser):
    while running:
        # print("Sending " + msg + " to target " + peer[0] + " socket no: " + str(socket.fileno()) + "\n")
        print("Sending " + str(msg) + " to target " + str(peer[0]) +
              " socket no: " + str(socket.fileno()) + "\n")
        socket.sendto(msg, peer)
        time.sleep(timeout)
Esempio n. 30
0
    def __init__(self, backend, socket, peer, rules):
        self._backend = backend
        self._socket = socket
        self._socket.setblocking(0)
        self._peer = peer

        self._rbuf = ""
        self._wbuf = ""
        self._read = 0
        self._written = 0

        self._read_delimiter = "\r\n\r\n"
        self._bytes_to_read = 0
        self._rcb = self.on_headers

        self._is_alive = True

        self._rules = rules

        self._in_id = gobject.io_add_watch(socket.fileno(),
                             gobject.IO_IN | gobject.IO_PRI,
                             self.on_read)
        self._out_id = gobject.io_add_watch(self._socket.fileno(),
                             gobject.IO_OUT,
                             self.on_write)
        self._err_id = gobject.io_add_watch(self._socket.fileno(),
                             gobject.IO_ERR,
                             self.on_error)
        self._hup_id = gobject.io_add_watch(self._socket.fileno(),
                             gobject.IO_HUP,
                             self.on_hang_up)
Esempio n. 31
0
 def add(self, target, socket=None):
     if not socket:
         socket = target.sockets()
     if self.use_poll:
         self.poller.register(socket, select.POLLIN)
     #dbg("add device on fileno: %s" % socket.fileno() )
     self.targets[socket.fileno()] = target
Esempio n. 32
0
    def __init__(self, poller, onMessageReceived = None, onConnected = None, onDisconnected = None,
                 socket=None, timeout=10.0, sendBufferSize = 2 ** 13, recvBufferSize = 2 ** 13):

        self.sendRandKey = None
        self.recvRandKey = None
        self.encryptor = None

        self.__socket = socket
        self.__readBuffer = bytes()
        self.__writeBuffer = bytes()
        self.__lastReadTime = time.time()
        self.__timeout = timeout
        self.__poller = poller
        if socket is not None:
            self.__socket = socket
            self.__fileno = socket.fileno()
            self.__state = CONNECTION_STATE.CONNECTED
            self.__poller.subscribe(self.__fileno,
                                     self.__processConnection,
                                     POLL_EVENT_TYPE.READ | POLL_EVENT_TYPE.WRITE | POLL_EVENT_TYPE.ERROR)
        else:
            self.__state = CONNECTION_STATE.DISCONNECTED
            self.__fileno = None
            self.__socket = None

        self.__onMessageReceived = onMessageReceived
        self.__onConnected = onConnected
        self.__onDisconnected = onDisconnected
        self.__sendBufferSize = sendBufferSize
        self.__recvBufferSize = recvBufferSize
Esempio n. 33
0
def bind_and_listen_on_posix_socket(socket_name, accept_callback):
    """
    :param accept_callback: Called with `PosixSocketConnection` when a new
        connection is established.
    """
    assert socket_name is None or isinstance(socket_name, six.text_type)
    assert callable(accept_callback)

    # Py2 uses 0027 and Py3 uses 0o027, but both know
    # how to create the right value from the string '0027'.
    old_umask = os.umask(int('0027', 8))

    # Bind socket.
    socket_name, socket = _bind_posix_socket(socket_name)

    _ = os.umask(old_umask)

    # Listen on socket.
    socket.listen(0)

    def _accept_cb():
        connection, client_address = socket.accept()
        # Note: We don't have to put this socket in non blocking mode.
        #       This can cause crashes when sending big packets on OS X.

        posix_connection = PosixSocketConnection(connection)

        accept_callback(posix_connection)

    get_event_loop().add_reader(socket.fileno(), _accept_cb)

    logger.info('Listening on %r.' % socket_name)
    return socket_name
Esempio n. 34
0
def pub_message(source_socket, msg, server_socket):
    for user in USERS_LIST:
        if user[1] == str(source_socket.fileno()):
            nickname = user[3]
            break
    # Do not send the message to server socket and the client who has
    # sent the message
    for socket in SOCKET_LIST:
        if socket != server_socket and socket != source_socket:
            try:
                for user in USERS_LIST:
                    # The user for each specific socket
                    if user[1] == str(socket.fileno()):
                        SYMM_KEY = user[2]
                        CIPHER = AES.new(SYMM_KEY)
                        h_msg = PUB_MESSAGE + ',' + nickname + ',' + msg
                        msg_enc = EncodeAES(CIPHER, h_msg)
                        socket.send(msg_enc)
            except:
                print 'fedeu'
                # broken socket connection may be, chat client pressed ctrl+c
                # for example
                # Remove user from USERS_LIST
                for index, user in enumerate(USERS_LIST):
                    if user[1] == str(source_socket.fileno()):
                        # Close connection
                        source_socket.close()
                        # Remove socket from SOCKET_LIST
                        SOCKET_LIST.remove(source_socket)
                        del USERS_LIST[index]
                        break
Esempio n. 35
0
    def _doReadMaapi(self, tagValueList, keyPath, transactionHandle, socket):
        for logFunc in self._log("do-read-maapi").debug3Func():
            logFunc(
                "called. keyPath=%s, tagValueList=%s, transactionHandler=%s, socket=%s",
                keyPath, tagValueList, transactionHandle, socket.fileno())

        if keyPath.getLen() == 0 and tagValueList.getLen() != 0:
            (xmlTag, val) = tagValueList.getAt(0)
            (tag, ns) = xmlTag
            res = pyconfdlib.maapi_set_namespace(socket, transactionHandle, ns)
            if res != pyconfdlib.CONFD_OK:
                for logFunc in self._log(
                        "do-read-maapi-set-namespace-failed").errorFunc():
                    logFunc("maapi_set_namespace() failed. ns=%s, error=%s",
                            ns, Utils.getConfdErrStr())
                return ReturnCodes.kGeneralError

        res = a.sys.confd.pyconfdlib.pyconfdlib_high.maapi_get_values(
            socket, transactionHandle, tagValueList, keyPath)
        if res != ReturnCodes.kOk:
            for logFunc in self._log(
                    "do-read-maapi-get-values-failed").errorFunc():
                logFunc("maapi_get_values() failed. error=%s",
                        Utils.getConfdErrStr())
            return ReturnCodes.kGeneralError

        return ReturnCodes.kOk
Esempio n. 36
0
 def __init__(self, socket, server, handler):
     self.socket = socket
     self.server = server
     self.fileno = socket.fileno()
     self.pending_data = b''
     self.handler = handler
     self.requests = []
Esempio n. 37
0
def getSocket(fileNo, sockets):
    """
    gets the socket from from the given list that corresponds to the given fileNo, and returns its index
    """
    for index, socket in enumerate(sockets):  #get the socket and index
        if socket.fileno() == fileNo:
            return socket, index
Esempio n. 38
0
 def on_ssl_servername(self, socket, server_name, ssl_context):
     c = self.connection_map.get(socket.fileno())
     if getattr(c, 'ssl_handshake_done', False):
         c.ready = False
         c.ssl_terminated = True
         # We do not allow client initiated SSL renegotiation
         return ssl.ALERT_DESCRIPTION_NO_RENEGOTIATION
Esempio n. 39
0
 def preexec_fn():
     fd = socket.fileno()
     os.environ['LISTEN_FDS'] = '1'
     os.environ['LISTEN_PID'] = str(os.getpid())
     if fd != 3:
         os.dup2(fd, 3)
     os.closerange(4, 100)
Esempio n. 40
0
 def _add_connection(self, socket):
     host, port = socket.getpeername()
     self.log.debug("Adding connection from %s:%d", host, port)
     socket.setblocking(0)
     self._pending_connections[socket.fileno()] = (time.time(),
                                                   socket)
     self._poller.register(socket, self.READ_ONLY_MASK)
Esempio n. 41
0
 def on_ssl_servername(self, socket, server_name, ssl_context):
     c = self.connection_map.get(socket.fileno())
     if getattr(c, 'ssl_handshake_done', False):
         c.ready = False
         c.ssl_terminated = True
         # We do not allow client initiated SSL renegotiation
         return ssl.ALERT_DESCRIPTION_NO_RENEGOTIATION
Esempio n. 42
0
def _is_readable(socket):
    """Return True if there is data to be read on the socket."""

    timeout = 0
    (rlist, wlist, elist) = select.select([socket.fileno()], [], [], timeout)

    return bool(rlist)
Esempio n. 43
0
 def remove(self, target, socket=None):
     if not socket:
         socket = target.sockets()
     if self.use_poll:
         self.poller.unregister(socket)
     #dbg("remove device on fileno: %s" % socket.fileno() )
     del (self.targets[socket.fileno()])
Esempio n. 44
0
 def unregister_connection(self, socket):
     fd = socket.fileno()
     try:
         self.poller.unregister(fd)
         del self.connection_map[fd]
     except KeyError:
         pass
Esempio n. 45
0
 def _processWaitEvents(self) :
     self._incThreadsCount()
     timeSec = perf_counter()
     while self._processing :
         try :
             rd, wr, ex = select( self._readList,
                                  self._writeList,
                                  self._readList,
                                  self._CHECK_SEC_INTERVAL )
         except :
             continue
         if not self._processing :
             break
         for socketsList in ex, wr, rd :
             for socket in socketsList :
                 asyncSocket = self._asyncSockets.get(socket.fileno(), None)
                 if asyncSocket and self._socketListAdd(socket, self._handlingList) :
                     if socketsList is ex :
                         asyncSocket.OnExceptionalCondition()
                     elif socketsList is wr :
                         asyncSocket.OnReadyForWriting()
                     else :
                         asyncSocket.OnReadyForReading()
                     self._socketListRemove(socket, self._handlingList)
         sec = perf_counter()
         if sec > timeSec + self._CHECK_SEC_INTERVAL :
             timeSec = sec
             for asyncSocket in list(self._asyncSockets.values()) :
                 if asyncSocket.ExpireTimeSec and \
                    timeSec > asyncSocket.ExpireTimeSec :
                     asyncSocket._close(XClosedReason.Timeout)
     self._decThreadsCount()
Esempio n. 46
0
 def __str__(self):
     if self.socket != None:
         sstr = "%d", socket.fileno()
     else:
         sstr = "<nosock>"
     return "OutSocket(%s,%s,%s,%f,%s)" % (
         self.peer_addr, sstr, self.connected, self.next_connection_time,
         self.out_buf)
Esempio n. 47
0
    def startMaapiReadTransaction(self, socket):
        for logFunc in self._log("start-maapi-read-transaction").debug2Func():
            logFunc("called. socket=%s", socket.fileno())

        db = pyconfdlib.CONFD_RUNNING
        transactionHandle = pyconfdlib.maapi_start_trans(
            socket, db, pyconfdlib.CONFD_READ)
        if not transactionHandle:
            for logFunc in self._log(
                    'start-maapi-read-transaction').errorFunc():
                logFunc('pyconfdlib.maapi_start_trans() failed. error=%s',
                        Utils.getConfdErrStr())
            return None

        for logFunc in self._log("start-maapi-read-transaction").debug2Func():
            logFunc("done. socket=%s", socket.fileno())
        return transactionHandle
Esempio n. 48
0
 def __init__(self, socket=None, timeout=10.0):
     self.__socket = socket
     self.__readBuffer = ''
     self.__writeBuffer = ''
     self.__lastReadTime = time.time()
     self.__timeout = timeout
     self.__disconnected = socket is None
     self.__fileno = None if socket is None else socket.fileno()
Esempio n. 49
0
	def __init__(self, socket, address):
		print 'connect from', address
		self.socket = socket
		self.address = address
		self.fd = socket.fileno()
		self.rbuf = ''
		self.xbuf = ''
		self.watch = {}
Esempio n. 50
0
	def __init__(self, socket = None, timeout = 10.0):
		self.__socket = socket
		self.__readBuffer = ''
		self.__writeBuffer = ''
		self.__lastReadTime = time.time()
		self.__timeout = timeout
		self.__disconnected = socket is None
		self.__fileno = None if socket is None else socket.fileno()
Esempio n. 51
0
File: server.py Progetto: idiap/mash
 def __init__(self, socket, channel):
     Thread.__init__(self)
     self.deamon     = True
     self.socket     = socket
     self.channel    = channel
     self.outStream  = OutStream()
     self.buffer     = ''
     self.identifier = socket.fileno()
Esempio n. 52
0
    def __init__(self, socket, dest_port=None, state=None):
        self._socket = socket
        self._dest_port = dest_port
        self._state = state(self)

        self.handle = socket.fileno()
        self.__buffer = bytes()
        self._closed = False
Esempio n. 53
0
def _is_readable(socket):
    """Return True if there is data to be read on the socket."""

    timeout = 0
    (rlist, wlist, elist) = select.select(
        [socket.fileno()], [], [], timeout)
    
    return bool(rlist)
Esempio n. 54
0
	def __init__(self, socket, addr, aes):
		self.header = HTTPRequest("")
		self.socket = socket
		self.addr = addr
		self.aes = aes
		self.fileno = socket.fileno()
		self.header_is_ok = False
		self.request = ""
		self.data_len = "0"
Esempio n. 55
0
 def register_connection(self, socket, on_read=None, on_write=None, on_error=None):
     eventmask = select.POLLERR
     if on_read:
         eventmask |= select.POLLIN
     if on_write:
         eventmask |= select.POLLOUT
     fd = socket.fileno()
     self.poller.register(fd, eventmask)
     self.connection_map[fd] = SocketCallbackInfo(on_read, on_write, on_error)
Esempio n. 56
0
 def __init__(self, socket, addr):
     self.addr, self.port = addr
     self.socket = socket
     self.id = socket.fileno()
     self.out_queue = Queue()
     self.out_buffer = ""
     self.in_queue = Queue()
     self.in_buffer = ""
     self.timestamp = time.time()
     self.timepinged = 0
Esempio n. 57
0
 def _add_connection(self, socket):
     host, port = socket.getpeername()
     self.log.debug("Adding connection %s:%d", host, port)
     socket.setblocking(0)
     self._pending_connections[socket.fileno()] = (time.time(),
                                                   socket)
     if _is_handshaking(socket):
         self._poller.register(socket, select.POLLIN | select.POLLOUT)
     else:
         self._poller.register(socket, select.POLLIN)
Esempio n. 58
0
    def test_magic(self):
        socket = mock.MagicMock()
        socket.fileno.return_value = 25

        channel = SocketChannel(socket, io_loop=self.io_loop)

        event_router = ChannelEventRouter(io_loop=self.io_loop)
        event_router.register(channel)

        self.io_loop.add_handler.assert_called_once_with(socket.fileno(),
            mock.ANY, self.io_loop.ERROR)
Esempio n. 59
0
 def _fill_in_buff(cls, socket, timeout, chunk_size, buffer_size):
     revents = yield from coroutine.wait(socket.fileno(), READ, next(timeout))
     if revents & READ:
         block_size = buffer_size - len(cls._in_buffer)
         block_size = block_size if block_size < chunk_size else chunk_size
         received = socket.recv(block_size)
         if received:
             cls._in_buffer += received
         else:
             cls._set_eof(True)
     elif revents & TIMEOUT:
         raise OSError(errno.ETIMEDOUT, "Connection timed out.")
Esempio n. 60
0
 def __add( self, socket, action, halt=None ):
     socket_handle = socket.fileno()
     def handle( handle, events ):
         assert handle == socket_handle
         return handle_socket(handle, events, {
             select.EPOLLIN: action,
             select.EPOLLHUP: halt
             # todo: use cb_halt for handing EPOLLHUP and EPOLLERR
         })
     self.__actions[socket_handle] = handle
     self.__poll.register(socket, select.EPOLLIN)
     return socket_handle