func (this *DispatcherOutComponent) checkConnActivity(Time time.Time, client *model.Client, buffPool *buffpool.BuffPool, number uint16, userUuid string, devID uint32, token uint8) bool { //5分钟 if time.Now().Sub(Time) > define.KEEP_ALIVE_PEORID { if client.Conn != nil { (*client.Conn).Close() } if token > 0 { err := buffPool.RemoveClient(number, userUuid, devID, token) if err != nil { err = errors.New("DispatcherComponent.checkConnActivity() Error:can not remove invalid client's connection, " + err.Error()) fmt.Println(err.Error()) return false } clientArr := buffPool.QueryForDeviceId(number, userUuid, devID) if clientArr == nil || len(clientArr) == 0 { //offline broadcast unregister := GetUnregisterComponent() if unregister != nil { err2 := unregister.offlineBroadcast(buffPool, userUuid, devID) if err2 != nil { err2 = errors.New("cDispatcherComponent.checkConnActivity() Error: " + err2.Error()) fmt.Println(err2.Error()) } } else { fmt.Println("DispatcherComponent.checkConnActivity() Error: one client disconnected, but offline broadcast send failed, ") } } } return false } return true }
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 *DispatcherOutComponent) sendToClient(msg *protocol.Message, buffPool *buffpool.BuffPool) error { if msg == nil || buffPool == nil { err := errors.New("DispatcherOutComponent.sendToClient() 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) } } else if msg.Token == 0 && msg.DeviceId > 0 { clientConnArr = buffPool.QueryForDeviceId(msg.Number, msg.DestUuid, msg.DeviceId) } else if msg.Token == 0 && msg.DeviceId == 0 { clientConnArr = buffPool.QueryForUuid(msg.Number, msg.DestUuid) } if clientConnArr != nil && len(clientConnArr) <= 0 { err := errors.New("DispatcherOutComponent.sendToClient() Error: can not find client's connection") fmt.Println(err.Error()) return err } err := errors.New("DispatcherOutComponent.sendToClient() 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.DestId, msg.DeviceId, msg.Token) { err := errors.New("DispatcherOutComponent.sendToClient() 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.sendToClient() Error: can not send message to client's connection") fmt.Println(err.Error()) continue } } return err }
func (this *RegisterComponent) listenClient(conn *net.Conn, buffPool *buffpool.BuffPool, number uint16, userUuid string, deviceID uint32, token uint8) error { if buffPool == nil { err := errors.New("RegisterComponent listenClient() Error: invalid argument") fmt.Println(err.Error()) return err } if conn == nil { err := errors.New("RegisterComponent listenClient() Error:invalid argument") fmt.Println(err.Error()) fmt.Println("the invalid client connection will be removed") err2 := buffPool.RemoveClient(number, userUuid, deviceID, token) if err2 != nil { err2 = errors.New("RegisterComponent listenClient() Error:remove the invalid client's connection failed" + err2.Error()) fmt.Println(err2.Error()) return err2 } fmt.Println("the invalid client connection has been removed") return err } var dispatcher *DispatcherComponent for { dispatcher = GetDispatcherComponent() if dispatcher != nil { break } else { fmt.Println("RegisterComponent listenClient() Error:can not get DispatcherComponent, will try again") } } for { msg, err := this.readMessage(conn) if err != nil && err != io.EOF && conn != nil { err = errors.New("RegisterComponent listenClient() Error " + err.Error()) fmt.Println(err.Error()) continue } else if err != nil && (err == io.EOF || conn == nil) { err = errors.New("RegisterComponent listenClient() Error the connection is invalid, " + err.Error()) err2 := buffPool.RemoveClient(number, userUuid, deviceID, token) if conn != nil { (*conn).Close() } if err2 != nil { err2 = errors.New("RegisterComponent listenClient() Error: can not remove invalid client's connection, " + err2.Error()) fmt.Println(err2.Error()) return err2 } clientArr := buffPool.QueryForDeviceId(number, userUuid, deviceID) if clientArr == nil || len(clientArr) == 0 { //offline broadcast unregister := GetUnregisterComponent() if unregister != nil { err3 := unregister.offlineBroadcast(buffPool, userUuid, deviceID) if err3 != nil { err3 = errors.New("RegisterComponent listenClient() Error:one client disconnected but offlineBroadcast send failed, " + err3.Error()) fmt.Println(err3.Error()) } } else { fmt.Println("RegisterComponent listenClient() Error:one client disconnected but offlineBroadcast send failed") } } return err } //push to DispatcherQueue if msg == nil { continue } dispatcher.NPushBack(msg, true) //need forward } return nil }