//===========================[ History Messages Utilities ]=========================// private HistoryMessageResult BuildHistoryMessageResult(Answer answer) { HistoryMessageResult result = new HistoryMessageResult(); result.count = answer.Want <int>("num"); result.lastId = answer.Want <long>("lastid"); result.beginMsec = answer.Want <long>("begin"); result.endMsec = answer.Want <long>("end"); result.messages = new List <HistoryMessage>(); List <object> messages = (List <object>)answer.Want("msgs"); foreach (List <object> items in messages) { bool deleted = (bool)Convert.ChangeType(items[4], TypeCode.Boolean); if (deleted) { continue; } HistoryMessage message = new HistoryMessage(); message.id = (long)Convert.ChangeType(items[0], TypeCode.Int64); message.fromUid = (long)Convert.ChangeType(items[1], TypeCode.Int64); message.mtype = (byte)Convert.ChangeType(items[2], TypeCode.Byte); message.mid = (long)Convert.ChangeType(items[3], TypeCode.Int64); if (message.mtype == MessageMType_Audio) { if (CheckBinaryType(items[5])) { message.binaryMessage = (byte[])items[5]; } else { message.binaryMessage = ConvertStringToByteArray((string)Convert.ChangeType(items[5], TypeCode.String)); } } else { if (!CheckBinaryType(items[5])) { message.stringMessage = (string)Convert.ChangeType(items[5], TypeCode.String); } else { message.binaryMessage = (byte[])items[5]; } } message.attrs = (string)Convert.ChangeType(items[6], TypeCode.String); message.mtime = (long)Convert.ChangeType(items[7], TypeCode.Int64); result.messages.Add(message); } result.count = result.messages.Count; return(result); }
/* * internal static byte[] ConvertStringToByteArray(string data) * { * //-- Please refer com.fpnn.msgpack.MsgPacker::UnpackString(...) * * UTF8Encoding utf8Encoding = new UTF8Encoding(false, true); //-- NO BOM. * return utf8Encoding.GetBytes(data); * } */ //===========================[ History Messages Utilities ]=========================// private HistoryMessageResult BuildHistoryMessageResult(long toId, Answer answer) { HistoryMessageResult result = new HistoryMessageResult(); result.count = answer.Want <int>("num"); result.lastCursorId = answer.Want <long>("lastid"); result.beginMsec = answer.Want <long>("begin"); result.endMsec = answer.Want <long>("end"); result.messages = new List <HistoryMessage>(); List <object> messages = (List <object>)answer.Want("msgs"); foreach (List <object> items in messages) { bool deleted = (bool)Convert.ChangeType(items[4], TypeCode.Boolean); if (deleted) { continue; } HistoryMessage message = new HistoryMessage(); message.cursorId = (long)Convert.ChangeType(items[0], TypeCode.Int64); message.fromUid = (long)Convert.ChangeType(items[1], TypeCode.Int64); message.toId = toId; message.messageType = (byte)Convert.ChangeType(items[2], TypeCode.Byte); message.messageId = (long)Convert.ChangeType(items[3], TypeCode.Int64); if (!CheckBinaryType(items[5])) { message.stringMessage = (string)Convert.ChangeType(items[5], TypeCode.String); } else { message.binaryMessage = (byte[])items[5]; } message.attrs = (string)Convert.ChangeType(items[6], TypeCode.String); message.modifiedTime = (long)Convert.ChangeType(items[7], TypeCode.Int64); if (message.messageType >= 40 && message.messageType <= 50) { RTMClient.BuildFileInfo(message, errorRecorder); } result.messages.Add(message); } result.count = result.messages.Count; return(result); }
//-------------[ P2P History Messages ]---------------------// public bool GetP2PMessage(HistoryMessageDelegate callback, long peerUid, bool desc, int count, long beginMsec = 0, long endMsec = 0, long lastId = 0, List <byte> mtypes = null, int timeout = 0) { TCPClient client = GetCoreClient(); if (client == null) { return(false); } Quest quest = new Quest("getp2pmsg"); quest.Param("ouid", peerUid); quest.Param("desc", desc); quest.Param("num", count); quest.Param("begin", beginMsec); quest.Param("end", endMsec); quest.Param("lastid", lastId); if (mtypes != null) { quest.Param("mtypes", mtypes); } return(client.SendQuest(quest, (Answer answer, int errorCode) => { if (errorCode == fpnn.ErrorCode.FPNN_EC_OK) { HistoryMessageResult result; try { result = BuildHistoryMessageResult(answer); AdjustHistoryMessageResultForP2PMessage(uid, peerUid, result); } catch (Exception) { errorCode = fpnn.ErrorCode.FPNN_EC_CORE_INVALID_PACKAGE; result = new HistoryMessageResult(); } callback(result.count, result.lastId, result.beginMsec, result.endMsec, result.messages, errorCode); } else { callback(0, 0, 0, 0, null, errorCode); } }, timeout)); }
public int GetP2PMessage(out HistoryMessageResult result, long peerUid, bool desc, int count, long beginMsec = 0, long endMsec = 0, long lastId = 0, List <byte> mtypes = null, int timeout = 0) { result = null; TCPClient client = GetCoreClient(); if (client == null) { return(fpnn.ErrorCode.FPNN_EC_CORE_INVALID_CONNECTION); } Quest quest = new Quest("getp2pmsg"); quest.Param("ouid", peerUid); quest.Param("desc", desc); quest.Param("num", count); quest.Param("begin", beginMsec); quest.Param("end", endMsec); quest.Param("lastid", lastId); if (mtypes != null) { quest.Param("mtypes", mtypes); } Answer answer = client.SendQuest(quest, timeout); if (answer.IsException()) { return(answer.ErrorCode()); } try { result = BuildHistoryMessageResult(0, answer); AdjustHistoryMessageResultForP2PMessage(uid, peerUid, result); return(fpnn.ErrorCode.FPNN_EC_OK); } catch (Exception) { return(fpnn.ErrorCode.FPNN_EC_CORE_INVALID_PACKAGE); } }
//-------------[ Broadcast History Messages ]---------------------// public bool GetBroadcastMessage(HistoryMessageDelegate callback, bool desc, int count, long beginMsec = 0, long endMsec = 0, long lastId = 0, List <byte> mtypes = null, int timeout = 0) { TCPClient client = GetCoreClient(); if (client == null) { if (RTMConfig.triggerCallbackIfAsyncMethodReturnFalse) { ClientEngine.RunTask(() => { callback(0, 0, 0, 0, null, fpnn.ErrorCode.FPNN_EC_CORE_INVALID_CONNECTION); }); } return(false); } Quest quest = new Quest("getbroadcastmsg"); quest.Param("desc", desc); quest.Param("num", count); quest.Param("begin", beginMsec); quest.Param("end", endMsec); quest.Param("lastid", lastId); if (mtypes != null) { quest.Param("mtypes", mtypes); } bool asyncStarted = client.SendQuest(quest, (Answer answer, int errorCode) => { if (errorCode == fpnn.ErrorCode.FPNN_EC_OK) { HistoryMessageResult result; try { result = BuildHistoryMessageResult(0, answer); } catch (Exception) { errorCode = fpnn.ErrorCode.FPNN_EC_CORE_INVALID_PACKAGE; result = new HistoryMessageResult(); } callback(result.count, result.lastCursorId, result.beginMsec, result.endMsec, result.messages, errorCode); } else { callback(0, 0, 0, 0, null, errorCode); } }, timeout); if (!asyncStarted && RTMConfig.triggerCallbackIfAsyncMethodReturnFalse) { ClientEngine.RunTask(() => { callback(0, 0, 0, 0, null, fpnn.ErrorCode.FPNN_EC_CORE_INVALID_CONNECTION); }); } return(asyncStarted); }
private void AdjustHistoryMessageResultForP2PMessage(long selfUid, long peerUid, HistoryMessageResult result) { foreach (HistoryMessage hm in result.messages) { if (hm.fromUid == 1) { hm.fromUid = selfUid; hm.toId = peerUid; } else if (hm.fromUid == 2) { hm.fromUid = peerUid; hm.toId = selfUid; } } }
public int GetP2PChat(out HistoryMessageResult result, long peerUid, bool desc, int count, long beginMsec = 0, long endMsec = 0, long lastId = 0, int timeout = 0) { return(GetP2PMessage(out result, peerUid, desc, count, beginMsec, endMsec, lastId, chatMTypes, timeout)); }