protected bool ProcessGetForQueue(MQProtocolSession session, MQDataInfo mQDataInfo) { try { string data = mQDataInfo.Body.DecodeToString(); mqServer.Logger.Info($"客户端:【{session.RemoteEndPoint}】消息类型:【{mQDataInfo.Type}】{CommanCode.GetForQueue} :【{mQDataInfo}】消息内容:【{data}】"); string msgs = ""; //Newtonsoft.Json.JsonConvert.SerializeObject(mqServer.TopicMessageQueueDict); var msg = MQTools.GetSendMessage((ushort)CommanCode.GetForQueue, msgs, 0); var r = session.TrySend(msg); mqServer.Logger.Info($"客户端:【{session.RemoteEndPoint}】请求队列 【{msgs}】【确认】消息结果:【{r}】"); return(true); } catch (Exception e) { mqServer.Logger.Error(mQDataInfo, new Exception("处理队列确认消息失败", e)); session.Close(CSuperSocket.SocketBase.CloseReason.InternalError); return(false); } }
protected bool ProcessPullMessage(MQProtocolSession session, MQDataInfo mQDataInfo) { try { string data = mQDataInfo.Body.DecodeToString(); mqServer.Logger.Info($"客户端:【{session.RemoteEndPoint}】消息类型:【{mQDataInfo.Type}】{CommanCode.PullMessage} :【{mQDataInfo}】消息内容:【{data}】"); MQDataInfo msg; if (session.PeekAckMessageQueue(out msg)) { byte[] buffer; msg.GetSendByte(out buffer); bool r = session.TrySend(buffer, 0, buffer.Length); mqServer.Logger.Info($"客户端:【{session.RemoteEndPoint}】拉取 【{msg}】【确认】消息结果:【{r}】队列:【{session.AckMessageQueueCount}】"); } return(true); } catch (Exception e) { mqServer.Logger.Error(mQDataInfo, new Exception("处理队列确认消息失败", e)); session.Close(CSuperSocket.SocketBase.CloseReason.InternalError); return(false); } }
protected bool ProcessMessage(MQProtocolSession session, MQDataInfo mQDataInfo) { try { string data = mQDataInfo.Body.DecodeToString(); mqServer.Logger.Info($"客户端:【{session.RemoteEndPoint}】消息类型:【{mQDataInfo.Type}】{CommanCode.SendMessage}:【{mQDataInfo}】消息内容:【{data}】"); var model = Newtonsoft.Json.JsonConvert.DeserializeObject <MQ.Common.Mode.MQModel>(data); //自己 if (!(mqServer.TopicMessageQueueDict.ContainsKey("") && mqServer.TopicMessageQueueDict[""].ContainsKey(model.Topic) && mqServer.TopicMessageQueueDict[""][model.Topic].ContainsKey(model.Tag))) { return(false); } var tt = mqServer.TopicMessageQueueDict[""][model.Topic][model.Tag]; byte[] buffer; mQDataInfo.GetSendByte(out buffer); List <MQProtocolSession> remove = new List <MQProtocolSession>(); foreach (var t in tt.Where(d => d != session)) { bool r = t.TrySend(buffer, 0, buffer.Length); //if (!r) //{ // //移除队列 // remove.Add(t); //} mqServer.Logger.Info($"客户端:【{t.RemoteEndPoint}】房间:【默认】主题:【{model.Topic}】标签:【{model.Tag}】推送【普通】消息结果:【{r}】"); } //if (remove.Count > 0) //{ // mqServer.Logger.Warn($"开始清理僵尸客户端关注主题……"); // remove.ForEach(d => // { // mqServer.Logger.Warn($"客户端:【{d.RemoteEndPoint}】房间:【默认】主题:【{model.Topic}】标签:【{model.Tag}】推送【普通】消息失败,将被移除队列!"); // tt.Remove(d); // }); //} mqServer.TopicMessageQueueDict[""][model.Topic].TryUpdate(model.Tag, tt, tt); return(true); } catch (Exception e) { mqServer.Logger.Error(mQDataInfo, new Exception("处理队列消息失败", e)); session.Close(CSuperSocket.SocketBase.CloseReason.InternalError); return(false); } }
protected bool ProcessLogin(MQProtocolSession session, MQDataInfo mQDataInfo) { try { string data = mQDataInfo.Body.DecodeToString(); mqServer.Logger.Info($"客户端:【{session.RemoteEndPoint}】{CommanCode.Login} :【{mQDataInfo}】登陆信息:【{data}】"); //解析认证 session.IsLogin = true; return(true); } catch (Exception e) { mqServer.Logger.Error(mQDataInfo, new Exception("处理登陆消息失败", e)); session.Close(CSuperSocket.SocketBase.CloseReason.InternalError); return(false); } }
protected bool ProcessHeartbeat(MQProtocolSession session, MQDataInfo mQDataInfo) { try { mqServer.Logger.Warn($"客户端:【{session.RemoteEndPoint}】{CommanCode.Heartbeat} : {mQDataInfo}】心跳信息"); //byte[] buffer; //mQDataInfo.GetSendByte(out buffer); //session.TrySend(buffer, 0, buffer.Length); return(true); } catch (Exception e) { mqServer.Logger.Error(mQDataInfo, new Exception("处理心跳消息失败", e)); session.Close(CSuperSocket.SocketBase.CloseReason.InternalError); return(false); } }
protected bool ProcessCreateTopic(MQProtocolSession session, MQDataInfo mQDataInfo) { try { string data = mQDataInfo.Body.DecodeToString(); mqServer.Logger.Info($"客户端:【{session.RemoteEndPoint}】消息类型:【{mQDataInfo.Type}】{CommanCode.CreateTopic} :【{mQDataInfo}】消息内容:【{data}】"); var model = Newtonsoft.Json.JsonConvert.DeserializeObject <MQ.Common.Mode.TopicMode>(data); foreach (var r in model.TopicDic.Keys) //房间 { if (mqServer.TopicMessageQueueDict.ContainsKey(r)) //存在房间 { foreach (var t in model.TopicDic[r].Keys) { if (mqServer.TopicMessageQueueDict[r].ContainsKey(t)) //存在主题 { foreach (var g in model.TopicDic[r][t]) //tag { if (mqServer.TopicMessageQueueDict[r][t].Keys.Contains(g)) //存在标签 { if (!mqServer.TopicMessageQueueDict[r][t][g].Contains(session)) { mqServer.TopicMessageQueueDict[r][t][g].Add(session); } } else //不存在标签 { mqServer.TopicMessageQueueDict[r][t].TryAdd(g, new List <MQProtocolSession>() { session }); } } } else//不存在主题 { ConcurrentDictionary <string, List <MQProtocolSession> > concurrentDictionaryq = new ConcurrentDictionary <string, List <MQProtocolSession> >(); foreach (var d in model.TopicDic[r][t])//tag { concurrentDictionaryq.TryAdd(d, new List <MQProtocolSession>() { session }); } mqServer.TopicMessageQueueDict[r].TryAdd(t, concurrentDictionaryq); } } } else//不存在房间 { ConcurrentDictionary <string, ConcurrentDictionary <string, List <MQProtocolSession> > > concurrentDictionary = new ConcurrentDictionary <string, ConcurrentDictionary <string, List <MQProtocolSession> > >(); foreach (var k in model.TopicDic[r].Keys) { ConcurrentDictionary <string, List <MQProtocolSession> > concurrentDictionaryq = new ConcurrentDictionary <string, List <MQProtocolSession> >(); foreach (var d in model.TopicDic[r][k]) { concurrentDictionaryq.TryAdd(d, new List <MQProtocolSession>() { session }); } concurrentDictionary.TryAdd(k, concurrentDictionaryq); } mqServer.TopicMessageQueueDict.TryAdd(r, concurrentDictionary); } //注意处理线程同步 if (session.TopicMode.TopicDic.ContainsKey(r)) { foreach (var t in model.TopicDic[r].Keys) { if (session.TopicMode.TopicDic[r].ContainsKey(t)) { foreach (var g in model.TopicDic[r][t])//tag { if (!session.TopicMode.TopicDic[r][t].Contains(g)) { session.TopicMode.TopicDic[r][t].Add(g); } } } else { session.TopicMode.TopicDic[r].Add(t, model.TopicDic[r][t]); } } } else { session.TopicMode.TopicDic.Add(r, model.TopicDic[r]); } } return(true); } catch (Exception e) { mqServer.Logger.Error(mQDataInfo, new Exception("更新字典失败", e)); session.Close(CSuperSocket.SocketBase.CloseReason.InternalError); return(false); } }