class RequestSender(object): def __init__(self, event_loop, sid, server_addr, client_data=None): self._sid = sid self._client_data = client_data self._codec = ZLengthHeadCodec(self._on_string_message) self._tcp_client = ZTcpClient(event_loop, server_addr) self._tcp_client.set_connection_callback(self._on_connection) self._tcp_client.set_connection_error_callback(self._on_error_connection) self._tcp_client.set_message_callback(self._codec.on_message) self._connection_callback = None self._connection_error_callback = None self._message_callback = None def connect(self): self._tcp_client.connect() def disconnect(self): self._tcp_client.disconnect() def set_connection_callback(self, cb): self._connection_callback = cb def set_connection_error_callback(self, cb): self._connection_error_callback = cb def set_message_callback(self, cb): self._message_callback = cb def send(self, tcp_conn, msg): self._codec.send(tcp_conn, msg) def get_id(self): return self._sid def get_client_data(self): return self._client_data def _on_connection(self, tcp_conn): if self._connection_callback: self._connection_callback(self, tcp_conn) def _on_error_connection(self, errno): if self._connection_error_callback: self._connection_error_callback(self, errno) else: logging.error('failed to connect to %s, errno=(%d)' %str(server_addr), errno) def _on_string_message(self, tcp_conn, message, receive_time): if self._message_callback: self._message_callback(self, tcp_conn, message, receive_time)
def __init__(self, event_loop, sid, server_addr, client_data=None): self._sid = sid self._client_data = client_data self._codec = ZLengthHeadCodec(self._on_string_message) self._tcp_client = ZTcpClient(event_loop, server_addr) self._tcp_client.set_connection_callback(self._on_connection) self._tcp_client.set_connection_error_callback(self._on_error_connection) self._tcp_client.set_message_callback(self._codec.on_message) self._connection_callback = None self._connection_error_callback = None self._message_callback = None