/* 通过Router转发消息(对终端开发者不可见) Router -> MsgServer ROUTE_SEND_TOPIC_MSG_CMD arg0: Msg //消息内容 arg1: TopicName //群组名 arg2: ClientID //发送方用户ID arg3: ClientType //发送方终端类型,是client还是device */ func (self *ProtoProc) procRouteTopicMsg(cmd protocol.Cmd, session *libnet.Session) error { log.Info("procRouteTopicMsg") var err error //send2Msg := cmd.GetArgs()[0] topicName := cmd.GetArgs()[1] //fromID := cmd.GetArgs()[2] //fromType := cmd.GetArgs()[3] // check whether the topic exist topicCacheData, err := self.msgServer.topicCache.Get(topicName) if topicCacheData == nil { log.Warningf("TOPIC %s not exist: %s", topicName, err.Error()) return common.TOPIC_NOT_EXIST } cmd.ChangeCmdName(protocol.REQ_SEND_TOPIC_MSG_CMD) // exactly in this server, just broadcasting log.Warningf("topic %s has %d member(s) in this server", topicName, topicCacheData.AliveMemberNumMap[self.msgServer.cfg.LocalIP]) for _, mID := range topicCacheData.MemberList { if self.msgServer.sessions[mID.ID] != nil { self.msgServer.sessions[mID.ID].Send(libnet.Json(cmd)) if err != nil { log.Fatalln(err.Error()) } } } return nil }
/* MsgServer -> Router REQ_SEND_P2P_MSG_CMD arg0: Sent2ID //接收方用户ID arg1: Msg //消息内容 arg2: FromID //发送方用户ID arg3: uuid //MsgServer分配的消息uuid */ func (self *ProtoProc) procSendMsgP2P(cmd protocol.Cmd, session *libnet.Session) error { log.Info("procSendMsgP2P") var err error send2ID := cmd.GetArgs()[0] send2Msg := cmd.GetArgs()[1] log.Info(send2Msg) self.Router.readMutex.Lock() defer self.Router.readMutex.Unlock() cacheSession, err := self.Router.sessionCache.Get(send2ID) if cacheSession == nil || cacheSession.Alive == false { storeSession, err := self.Router.mongoStore.GetSessionFromCid(send2ID) if err != nil { log.Warningf("ID %s not registered, msg dropped", send2ID) return err } log.Info(storeSession) log.Warningf("ID registered but offline: %s", send2ID) cmd.ChangeCmdName(protocol.ROUTE_SEND_P2P_MSG_CMD) //ms := self.Router.cfg.MsgServerList[0] ms := session.Conn().RemoteAddr().String() err = self.Router.msgServerClientMap[ms].Send(libnet.Json(cmd)) if err != nil { log.Error("error:", err) return err } } else { log.Info(cacheSession.MsgServerAddr) cmd.ChangeCmdName(protocol.ROUTE_SEND_P2P_MSG_CMD) log.Info(cmd) err = self.Router.msgServerClientMap[cacheSession.MsgServerAddr].Send(libnet.Json(cmd)) if err != nil { log.Error("error:", err) return err } } return nil }
/* Router -> MsgServer ROUTE_SEND_TOPIC_MSG_CMD arg0: ClientID //发送方用户ID arg1: ClientType //发送方终端类型,是client还是device arg2: Msg //消息内容 arg3: TopicName //群组名, device无须提供 */ func (self *ProtoProc) procSendMsgTopic(cmd protocol.Cmd, session *libnet.Session) error { log.Info("procSendMsgTopic") var err error ms := session.Conn().RemoteAddr().String() //send2Msg := cmd.GetArgs()[0] topicName := cmd.GetArgs()[1] //fromID := cmd.GetArgs()[2] //fromType := cmd.GetArgs()[3] // check whether the topic exist topicCacheData, err := self.Router.topicCache.Get(topicName) if topicCacheData == nil { log.Warningf("TOPIC %s not exist: %s", topicName, err.Error()) return err } cmd.ChangeCmdName(protocol.ROUTE_SEND_TOPIC_MSG_CMD) log.Info(protocol.ROUTE_SEND_TOPIC_MSG_CMD) log.Info(cmd) for ip, num := range topicCacheData.AliveMemberNumMap { if num > 0 { log.Warningf("topic %s has %d member(s) in ip %s", topicName, num, ip) if ip != ms { // not in this server, routing it err = self.Router.msgServerClientMap[ip].Send(libnet.Json(cmd)) if err != nil { log.Error("error:", err) return err } } } } return nil }