func (this *CacheComponent2) Dispatch2(msg *protocol.Message, flag bool) { cached := uint16(1 << 15) if (msg.Number & cached) != cached { return } msg.Number &= HEAD_RESET msg, err := this.MakeCacheMessage(msg) if err != nil { fmt.Println("CacheCompoennt Dispatch2 Error:" + err.Error()) return } data1, err := msg.Encode() if err != nil { fmt.Println("CacheCompoennt Dispatch2 Error:", err) return } data2 := make([]byte, len(data1)+8) copy(data2[:8], msg.Data[0:8]) copy(data2[8:], data1) snode_component.GetNodeComponent().Forward(stree_define.ADD_CACHE_ENTRY, msg.DestUuid, msg.DeviceId, data2) //if this message is a contact sync message, send it to root first. if (msg.Number == protocol.N_ACCOUNT_SYS || msg.DeviceId == 0) && flag { snode_component.GetNodeComponent().Forward(stree_define.BROADCAST_TO_CLIENT, msg.DestUuid, msg.DeviceId, data1) } else { GetDispatcherOutComponent().NPushBack(msg, flag) } return }
func (this *DispatcherOutComponent) sendToClient2(msg *protocol.Message, forward bool, buffPool *buffpool.BuffPool) error { if msg == nil || buffPool == nil { err := errors.New("DispatcherOutComponent.sendToClient2() Error: invalid argument") fmt.Println(err.Error()) return err } clientConnArr := make([]*model.Client, 0) if msg.Token > 0 { clientConn := buffPool.GetClient(msg.Number, msg.DestUuid, msg.DeviceId, msg.Token) if clientConn != nil && clientConn.Conn != nil { clientConnArr = append(clientConnArr, clientConn) } if (clientConnArr == nil || len(clientConnArr) <= 0) && forward && GetRunWithSnode().Flag { buf, err := msg.Encode() if err != nil { fmt.Println("DispatcherOutComponent.sendToClient2() Error: " + err.Error()) fmt.Println("\n", time.Now().String(), "Not find user, not forward due to encode failed.", "\n") return err } snode_component.GetNodeComponent().Forward(stree_define.FORWARD, msg.DestUuid, msg.DeviceId, buf) return nil } } else if msg.Token == 0 && msg.DeviceId > 0 { clientConnArr = buffPool.QueryForDeviceId(msg.Number, msg.DestUuid, msg.DeviceId) if (clientConnArr == nil || len(clientConnArr) <= 0) && forward && GetRunWithSnode().Flag { buf, err := msg.Encode() if err != nil { fmt.Println("DispatcherOutComponent.sendToClient2() Error:" + err.Error()) fmt.Println("\n", time.Now().String(), "Not find user, not forward due to encode failed.", "\n") return err } snode_component.GetNodeComponent().Forward(stree_define.FORWARD, msg.DestUuid, msg.DeviceId, buf) return nil } } else if msg.Token == 0 && msg.DeviceId == 0 { clientConnArr = buffPool.QueryForUuid(msg.Number, msg.DestUuid) } err := errors.New("DispatcherOutComponent.sendToClient2() Error: the client's connection not pass authorization yet") for _, client := range clientConnArr { if client == nil || client.Conn == nil { continue } if !this.checkConnActivity(client.Time, client, buffPool, msg.Number, msg.DestUuid, msg.DeviceId, msg.Token) { err := errors.New("DispatcherOutComponent.sendToClient2() Error: client's connection is not alive") fmt.Println(err.Error()) continue } err2 := this.sendMessage(msg, client.Conn) if err2 != nil { err2 = errors.New("DispatcherOutComponent.sendToClient2() Error: can not send message to client's connection") fmt.Println(err.Error()) continue } } return nil }
func (this *UnregisterComponent) offlineBroadcast(buffPool *buffpool.BuffPool, userUuid string, devID uint32) error { if GetRunWithSnode().Flag { snode_component.GetNodeComponent().Logout(userID, devID) snode_component.GetNodeComponent().DecUsersNumber() snode_component.GetNodeComponent().DecConnNumber() } if buffPool == nil { err := errors.New("UnRegisterComponent offlineBroadcast Error: invalid argument") return err } var dispatcherOut *DispatcherOutComponent for { dispatcherOut = GetDispatcherOutComponent() if dispatcherOut != nil { break } else { fmt.Println("UnregisterComponent.offlineBroadcast() Error:can not get a dispatcher, will try again") } } brMsg := protocol.BRMsg{ UserUuid: userUuid, DeviceId: devID, } brData, err := brMsg.Encode() if err != nil { err := errors.New("UnregisterComponent.offlienBroadcast() Error: " + err.Error()) return err } servNoUuidArr := buffPool.GetAllServices() for num, uuidArr := range servNoUuidArr { for _, uuid := range uuidArr { offlineMsg, err := protocol.NewMessage(protocol.VERSION, protocol.MT_BROADCAST_OFFLINE, protocol.PT_PUSHSERVER, 0, num, uint16(len(brData)), P_UID, uuid, 0, brData) if err != nil { err = errors.New("UnregisterComponent offlineBroadcast() Error: " + err.Error()) return err } dispatcherOut.NPushBack(offlineMsg, false) //not need forward } } return nil }
func (this *DispatcherComponent) dispatch(msg *protocol.Message, flag bool, buffPool *buffpool.BuffPool, respComp *ResponseComponent) { switch msg.MsgType { case protocol.MT_UNREGISTER: this.handleMTUnregister(msg) case protocol.MT_PUSH: this.handleMTPush(msg, buffPool, respComp) case protocol.MT_KEEP_ALIVE: this.handleKeepAlive(msg) //MT_DEL_CACHE_ENTRY from other servers, such as gconnect, MT_PUSH_WITH_RESPONSE from clients for contact sync case protocol.MT_DEL_CACHE_ENTRY, protocol.MT_PUSH_WITH_RESPONSE: if GetRunWithSnode().Flag { //since the response is sent by client, the packet's uuid should be the push-message's UserId snode_component.GetNodeComponent().Forward(stree_define.DEL_CACHE_ENTRY, msg.UserUuid, msg.DeviceId, msg.Data) } else { GetCacheComponent().RemoveCached(msg) } case protocol.MT_PUBLISH: //only PUBLISH type may forward a message cached := uint16(1 << 15) if (msg.Number & cached) == cached { //only service can send MT_PUBLISH and Number >> 14 == 1 if GetRunWithSnode().Flag { GetCacheComponent2().MsgPush2(msg, flag) } else { this.handleContactSync(msg, flag) } } else { this.handleMTPublish(msg, flag, buffPool) } case protocol.MT_QUERY_USER: if GetRunWithSnode().Flag { this.handleQueryUser(msg) } } }
func (this *RegisterComponent) handleService(buffPool *buffpool.BuffPool, message *protocol.Message, conn *net.Conn) { if buffPool == nil || message == nil || conn == nil { err := errors.New("RegisterComponent handleService Error: the argument is nil") return err } err := buffPool.AddService(message.Number, message.UserUuid, conn) if err != nil { err = fmt.Errorf("RegisterCompoennt handleService Error: %v", err) return err } responseMessage, err := protocol.NewMessage(protocol.VERSION, protocol.MT_REGISTER, protocol.PT_PUSHSERVER, 0, message.Number, 0, P_UID, message.UserUuid, 0, nil) if err != nil { err = fmt.Errorf("RegisterComponent handleService Error:%v", err) return err } GetDispatcherOutComponent().NPushBack(responseMessage, false) if GetRunWithSnode().Flag { onlineInfo := structure.ClientOnlineInfo{ PushID: this.PushID, ParentID: this.PushID, } data, err := onlineInfo.Encode() if err != nil { err = fmt.Errorf("RegisterCompoennt handleService Error:%v", err) return err } snode_component.GetNodeComponent().Forward(stree_define.ONLINE, msg.UserId, 0, data) //added for QueryUser } go this.listenService(conn, buffPool, message.Number, message.UserUuid) return nil }
func (this *RegisterComponent) onlineBroadcast(buffPool *buffpool.BuffPool, dispatcherOut *DispatcherOutComponent, userUuid string, deviceID uint32) error { if GetRunWithSnode().Flag { onlineInfo := structure.ClientOnlineInfo{ PushID: this.PushID, ParentID: this.PushID, } data, err := onlineInfo.Encode() if err != nil { err = errors.New("RegisterComponent onlineBroadcast() Error: " + err.Error()) return err } snode_component.GetNodeComponent().Forward(define.ONLINE, userUuid, deviceID, data) snode_component.GetNodeComponent().AddUsersNumber() snode_component.GetNodeComponent().AddConnNumber() } if buffPool == nil || dispatcherOut == nil { err := errors.New("RegisterComponent onlineBroadcast() Error: invalid argument") return err } brMsg := protocol.BRMsg{ userUuid, deviceID, } brData, err := brMsg.Encode() if err != nil { err := errors.New("RegisterCompoent onlineBroadcast() Error: " + err.Error()) return err } servNoUuidArr := buffPool.GetAllServices() for num, uuidArr := range servNoUuidArr { for _, uuid := range uuidArr { onlineMsg, err := protocol.NewMessage(protocol.VERSION, protocol.MT_BROADCAST_ONLINE, protocol.PT_PUSHSERVER, 0, num, uint16(len(brData)), P_UID, uuid, 0, brData) if err != nil { err = errors.New("RegisterComponent onlineBroadcast() Error: " + err.Error()) return err } dispatcherOut.NPushBack(onlineMsg, false) //not need forward } } return nil }
func (this *DispatcherComponent) handleMTPublish(msg *protocol.Message, flag bool, buffPool *buffpool.BuffPool) error { if msg.DeviceId == 0 && flag && GetRunWithSnode().Flag { buf, err := msg.Encode() if err != nil { err = errors.New("DispatcherComponnet.handleMTPublish() Error: " + err.Error()) fmt.Println(err.Error()) return err } snode_component.GetNodeComponent().Forward(stree_define.BROADCAST_TO_CLIENT, msg.DestUuid, msg.DeviceId, buf) return nil } else { GetDispatcherOutComponent().NPushBack(msg, flag) } return nil }
func (this *DispatcherComponent) handleQueryUser(msg *protocol.Message) { if msg == nil { return } data := make([]byte, stree_define.QUERY_USER_REQUEST_LEN) copy(data, msg.Data[:4]) serverID, err := connect_protocol.StringToUuid(msg.UserUuid) if err != nil { err = errors.New("DispatcherComponent.handleQueryUser() Error: " + err.Error()) fmt.Println(err.Error()) return } copy(data[4:20], serverID) binary.BigEndian.PutUint16(data[20:], msg.Number) snode_component.GetNodeComponent().Forward(stree_define.QUERY_USER, msg.DestUuid, msg.DeviceId, data) return }
func (this *DispatcherOutComponent) sendToService2(msg *protocol.Message, forward bool, buffPool *buffpool.BuffPool) (*net.Conn, error) { if msg == nil || buffPool == nil { err := errors.New("DispatcherOutComponent.sendToService() Error: invalid argument") fmt.Println(err.Error()) return nil, err } var conn *net.Conn if msg.DestUuid == P_UID { conn = buffPool.GetService(msg.Number) } else { conn = buffPool.QueryService(msg.Number, msg.DestUuid) if conn == nil && forward && GetRunWithSnode().Flag { buf, err := msg.Encode() if err != nil { fmt.Println(err) return nil, err } snode_component.GetNodeComponent().Forward(stree_define.FORWARD, msg.DestUuid, msg.DeviceId, buf) return nil, nil } } if conn == nil { err := errors.New("DispatcherOutComponent.sendToService() Error: can not find dest server") fmt.Println(err.Error()) return nil, err } err := this.sendMessage(msg, conn) if err != nil { err = errors.New("DispatcherOutComponent.sendToService() Error: can not send message to server's connection") fmt.Println(err.Error()) return conn, err } if msg.MsgType == protocol.MT_PUBLISH { fmt.Println("\n", time.Now().String(), "MT_PUBLISH: sendToService2 Directly OK", "\n") } return conn, nil }
func (this *RegisterComponent) ServiceOffline(userUuid string) { snode_component.GetNodeComponent().Forward(define.OFFLINE, userUuid, 0, nil) }