Example #1
0
 def _handle_new_conn(self, newsock, peer):
     self._mlock.acquire()
     self._set_sock_params(newsock)
     self._set_sock_nonblocking(newsock)
     context = sockcontext.CConnContext()
     context.set_sock(newsock)
     context.set_conn_man(self)
     context.set_peerinfo(peer)
     self._epoll.register(newsock.fileno(),
                          select.EPOLLIN | select.EPOLLET | select.EPOLLERR)
     self._fileno2context[newsock.fileno()] = context
     self._peer2context[peer] = context
     self._context2fileno_peer[context] = (newsock.fileno(), peer)
     log.info('a new connection: %s:%s' % (peer[0], peer[1]))
     self._mlock.release()
Example #2
0
 def push_msg2sendqueue(self, msg):
     """
     push msg into the send queue
     """
     ret = 0
     if msg is None:
         log.warn('put a None into msg send queue. return')
         ret = -1
         return ret
     valid, errmsg = msg.is_valid4send(msg)
     if not valid:
         log.error('failed to send msg as msg is not valid to send')
         return -1
     flag = msg.get_flag()
     peer = msg.get_to_addr()[0]
     new_created = False
     context = None
     sock = None
     if isinstance(msg, async_msg.CNeedAckMsg):
         log.debug('CNeedAckMsg is to be sent. msg_type:%d,'
                   'msg_flag:%d, msg_dest:%s, uniqid:%d' %
                   (msg.get_msg_type(), msg.get_flag(),
                    str(msg.get_to_addr()), msg.get_uniq_id()))
         # no need head by default
         # msg.set_need_head(b_need=False)
         if msg.get_last_retry_time() is None:
             msg.set_last_retry_time(time.time())
         # if not in the self._needack_context_dict
         if msg.get_retry_times() <= 0:
             self._needack_context_queue.put(msg)
     try:
         context = self._peer2context[peer]
     except KeyError:
         log.info('To create a new context for the sock:{0}'.format(peer))
         self._mlock.acquire()
         if peer not in self._peer2context:
             sock = self.connect(peer)
             if sock is not None:
                 context = sockcontext.CConnContext()
                 context.set_conn_man(self)
                 context.set_sock(sock)
                 context.set_peerinfo(peer)
                 fileno = sock.fileno()
                 self._peer2context[peer] = context
                 self._fileno2context[fileno] = context
                 self._context2fileno_peer[context] = (fileno, peer)
                 log.info('created context for the new sock')
                 ret = 0
                 try:
                     self._epoll.register(sock.fileno(),
                                          self._epoll_write_params())
                 except Exception as error:  # pylint: disable=W0703
                     log.warn(
                         'failed to register the socket fileno, err_msg:%s,'
                         'perinfo:%s:%s. To epoll modify it' %
                         (str(error), peer[0], peer[1]))
                     self._epoll.modify(sock.fileno(),
                                        self._epoll_write_params())
             else:
                 log.error(
                     'failed to post msg. Connect failed. peer info:{0}.'
                     ' msg_type:{1}'.format(str(peer), msg.get_msg_type()))
                 ret = -1
         else:
             context = self._peer2context[peer]
         self._mlock.release()
     else:
         context = self._peer2context[peer]
     if ret != 0:
         return ret
     if not context.is_detroying():
         if context.put_msg(flag, msg) == 0:
             ret = 0
             # set up last modify
         else:
             ret = -1
         log.debug('start handle new send.')
         self._handle_new_send(context)
         return ret