def _on_read(self, conn): if conn.status == ConnStatus.Closed: return err = None is_closed = False try: data = conn.sock.recv(1024 * 1024) if data == b'': is_closed = True else: conn.readbuf.extend(data) except Exception as e: if not is_socket_exception_wouldblock(e): err = str(e) while len(conn.readbuf) > 0: head_len = get_message_head_len() if len(conn.readbuf) < head_len: break head_dict = parse_head(conn.readbuf[:head_len]) body_len = head_dict['body_len'] if len(conn.readbuf) < head_len + body_len: break rsp_body = conn.readbuf[head_len:head_len+body_len] del conn.readbuf[:head_len+body_len] self._on_packet(conn, head_dict, Err.Ok.code, '', rsp_body) if is_closed: self.close(conn.conn_id) conn.handler.on_error(conn.conn_id, Err.ConnectionClosed.text) elif err: self.close(conn.conn_id) conn.handler.on_error(conn.conn_id, err)
def _on_read(self, conn): start_time = time.time() recv_len = 0 buf_len = 0 packet_count = 0 if conn.status == ConnStatus.Closed: return err = None is_closed = False try: data = conn.sock.recv(128 * 1024) if data == b'': is_closed = True else: conn.readbuf.extend(data) recv_len = len(data) buf_len = len(conn.readbuf) except Exception as e: if not is_socket_exception_wouldblock(e): err = str(e) while len(conn.readbuf) > 0: head_len = get_message_head_len() if len(conn.readbuf) < head_len: break head_dict = parse_head(conn.readbuf[:head_len]) body_len = head_dict['body_len'] if len(conn.readbuf) < head_len + body_len: break rsp_body = conn.readbuf[head_len:head_len + body_len] del conn.readbuf[:head_len + body_len] packet_count += 1 self._on_packet(conn, head_dict, Err.Ok.code, '', rsp_body) if is_closed: self.close(conn.conn_id) conn.handler.on_error(conn.conn_id, Err.ConnectionClosed.text) elif err: self.close(conn.conn_id) conn.handler.on_error(conn.conn_id, err) end_time = time.time() logger.debug( 'conn_id={}; elapsed={}; recv_len={}; buf_len={}; packet={};'. format(conn.conn_id, end_time - start_time, recv_len, buf_len, packet_count))
def _on_read(self, conn): start_time = time.time() recv_len = 0 buf_len = 0 packet_count = 0 if conn.status == ConnStatus.Closed: return err = None is_closed = False try: data = conn.sock.recv(128 * 1024) if data == b'': is_closed = True else: conn.readbuf.extend(data) recv_len = len(data) buf_len = len(conn.readbuf) except Exception as e: if not is_socket_exception_wouldblock(e): err = str(e) while len(conn.readbuf) > 0: head_len = get_message_head_len() if len(conn.readbuf) < head_len: break head_dict = parse_head(conn.readbuf[:head_len]) body_len = head_dict['body_len'] if len(conn.readbuf) < head_len + body_len: break rsp_body = conn.readbuf[head_len:head_len + body_len] del conn.readbuf[:head_len + body_len] packet_count += 1 self._on_packet(conn, head_dict, Err.Ok.code, '', rsp_body) if packet_count >= 10: self._pending_read_conns.add(conn) self._w_sock.send(b'1') break #收10个包强制跳出循环,避免长时间解包导致无法发送心跳 if is_closed: self.close(conn.conn_id) conn.handler.on_error(conn.conn_id, Err.ConnectionClosed.text) elif err: self.close(conn.conn_id) conn.handler.on_error(conn.conn_id, err)
def _on_read(self, conn): if conn.status == ConnStatus.Closed: return packet_count = 0 msg = '' is_closed = False try: data = conn.sock.recv(128 * 1024) if data == b'': is_closed = True else: conn.readbuf.extend(data) except Exception as e: if not is_socket_exception_wouldblock(e): is_closed = True msg = str(e) while len(conn.readbuf) > 0: head_len = get_message_head_len() if len(conn.readbuf) < head_len: break head_dict = parse_head(conn.readbuf[:head_len]) body_len = head_dict['body_len'] if len(conn.readbuf) < head_len + body_len: break rsp_body = conn.readbuf[head_len:head_len+body_len] del conn.readbuf[:head_len+body_len] packet_count += 1 self._on_packet(conn, head_dict, PacketErr.Ok, '', rsp_body) if packet_count >= 10: if len(conn.readbuf) > 0: self._pending_read_conns.add(conn) self._w_sock.send(b'1') break # 收10个包强制跳出循环,避免长时间解包导致无法发送心跳 if is_closed: if msg == '': self._do_close(conn.conn_id, CloseReason.RemoteClose, msg, True) else: self._do_close(conn.conn_id, CloseReason.ReadFail, msg, True)
def _parse_req_head(self, req_str): head_len = get_message_head_len() req_head_dict = parse_head(req_str[:head_len]) return req_head_dict