def _MsgReadAck(from_user_id, session_id, msg_id, session_type=BaseDefine_pb2.SESSION_TYPE_SINGLE): log.debug('_MsgReadAck, from: {}, to: {}, msg_id: {}'.format( from_user_id, session_id, msg_id)) req = Message_pb2.IMMsgDataReadAck() req.Clear() req.user_id = from_user_id req.session_id = session_id req.session_type = session_type req.msg_id = msg_id pdu = ImPdu.ImPdu() pdu.setMsg(req.SerializeToString()) pdu.setServiceId(BaseDefine_pb2.SID_MSG) pdu.setCommandId(BaseDefine_pb2.CID_MSG_READ_ACK) return pdu.SerializeToString()
def _MsgData(from_user_id, to_user_id, msg_data, msg_type=BaseDefine_pb2.MSG_TYPE_SINGLE_TEXT): log.info('_MsgData, from {} -> {}'.format(from_user_id, to_user_id)) req = Message_pb2.IMMsgData() req.Clear() req.msg_data = msg_data req.from_user_id = from_user_id req.to_session_id = to_user_id req.msg_id = 0 req.msg_type = msg_type req.create_time = int(time.time()) pdu = ImPdu.ImPdu() pdu.setMsg(req.SerializeToString()) pdu.setServiceId(BaseDefine_pb2.SID_MSG) pdu.setCommandId(BaseDefine_pb2.CID_MSG_DATA) return pdu.SerializeToString()
def _Login(username, password, client_type=BaseDefine_pb2.CLIENT_TYPE_IOS): loginReq = Login_pb2.IMLoginReq() loginReq.Clear() pdu = ImPdu.ImPdu() try: loginReq.user_name = username loginReq.password = password loginReq.online_status = BaseDefine_pb2.USER_STATUS_ONLINE loginReq.client_type = client_type loginReq.SerializeToString() loginReq.client_version = 'v1.1.0' except Exception as e: print traceback.format_exc() raise e else: pdu.setMsg(loginReq.SerializeToString()) pdu.setServiceId(BaseDefine_pb2.SID_LOGIN) pdu.setCommandId(BaseDefine_pb2.CID_LOGIN_REQ_USERLOGIN) return pdu.SerializeToString()
def recvData(self): pdu = ImPdu() try: pduheaderbuf = self._socket.recv(16) except Exception as e: if e.errno == errno.ECONNRESET: log.error('receive failed: {}'.format(traceback.format_exc())) self._connected = False else: if len(pduheaderbuf) != 16: log.error("read pdu len errror, len: {}".format( len(pduheaderbuf))) self._connected = False return try: pdu.FromString(pduheaderbuf) except Exception: log.error('unpack pdu length error: {}'.format( traceback.format_exc())) else: buf = self._socket.recv(pdu.length - 16) self._buffer = pduheaderbuf self._buffer += buf self.handlePdu()