func (this *UnregisterComponent) unregisterClient(buffPool *buffpool.BuffPool, msg *protocol.Message) error {
	if buffPool == nil || msg == nil {
		err := errors.New("UnregisterComponent.unregisterClient() Error: invalid argument")
		fmt.Println(err.Error())
		return err
	}
	clientConn := buffPool.GetClient(msg.Number, msg.UserUuid, msg.DeviceId, msg.Token)
	if clientConn == nil {
		return nil
	}

	//only remove conn from connPool, but do not disconnect this connection
	err := buffPool.RemoveClient(msg.Number, msg.UserUuid, msg.DeviceId, msg.Token)
	(*clientConn.Conn).Close()
	if err != nil {
		err = errors.New("UnregisterComponent.unregisterClient() Error: " + err.Error())
		fmt.Println(err.Error())
		//send failed response to client
		resp, err2 := protocol.NewMessage(protocol.VERSION, protocol.MT_ERROR, protocol.PT_PUSHSERVER, msg.Token, msg.Number, uint16(0),
			P_UID, msg.UserUuid, msg.DeviceId, nil)
		if err2 != nil {
			err2 = errors.New("UnregisterComponent.unregisterClient() Error:" + err2.Error())
			fmt.Println(err2.Error())
			return err2
		}
		err3 := sendResponse(clientConn.Conn, resp)
		if err3 != nil {
			err3 = errors.New("UnregisterComponent.unregisterClient() Error:, " + err3.Error())
			fmt.Println(err3.Error())
			return err3
		}
		return err
	}

	//offline broadcast
	err = this.offlineBroadcast(buffPool, msg.UserUuid, msg.DeviceId)
	if err != nil {
		err = errors.New("UnregisterComponent.unregisterClient() Error: offlineBroadcast send failed, " + err.Error())
		fmt.Println(err.Error())
	}

	resp, err := protocol.NewMessage(protocol.VERSION, protocol.MT_UNREGISTER, protocol.PT_PUSHSERVER, msg.Token, msg.Number, uint16(0),
		P_UID, msg.UserUuid, msg.DeviceId, nil)
	if err != nil {
		err = errors.New("UnregisterComponent.unregisterClient() Error: conn is removed from BuffPool, but can not send response to service" + err.Error())
		fmt.Println(err.Error())
		return err
	}
	err2 := sendResponse(clientConn.Conn, resp)
	if err2 != nil {
		err2 = errors.New("UnregisterComponent.unregisterClient() Error:conn is reomved from BuffPool, but failed when send response to service" + err2.Error())
		return err2
	}
	return nil
}
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 *QueryComponent) InitMessage(info *push_model.QueryInfo) (*Message, error) {
	queryRequest := model.QueryDeviceRequest{
	// model.QueryInfo{
	// 	model.Request{account.QUERYDEVICE_REQ},
	// 	info.UserUuid,
	// },
	// info.DeviceNumber,
	// info.Token,
	// info.Number,
	}
	packet, err := queryRequest.Encode()
	if err != nil {
		err = fmt.Errorf("QueryComponent InitMessage Error : %v", err)
		return nil, err
	}

	message, err := protocol.NewMessage(VERSION, MT_PUSH, PT_PUSHSERVER,
		TOKEN, N_ACCOUNT_SYS, uint16(len(packet)), USER_UUID, DEST_UUID, DEVICE_ID, packet)
	if err != nil {
		err = fmt.Errorf("QueryComponent InitMessage Error : %v", err)
		return nil, err
	}

	return message, nil
}
func (this *UnregisterComponent) unregisterService(buffPool *buffpool.BuffPool, msg *protocol.Message) error {
	if buffPool == nil || msg == nil {
		err := errors.New("UnregisterComponent unregisterService() Error: invalid argument")
		return err
	}
	conn := buffPool.QueryService(msg.Number, msg.UserUuid)
	if conn == nil {
		return nil
	}
	//only remove conn from connPool, but do not disconnect this connection
	err := buffPool.DeleteService(msg.Number, msg.UserUuid)
	if err != nil {
		err = errors.New("UnregisterComponent unregisterService() Error: " + err.Error())
		//send failed response to client
		resp, err2 := protocol.NewMessage(protocol.VERSION, protocol.MT_ERROR, protocol.PT_PUSHSERVER, msg.Token, msg.Number, uint16(0),
			P_UID, msg.UserUuid, msg.DeviceId, nil)
		if err2 != nil {
			err2 = errors.New("UnregisterComponent.unregisterService() Error: " + err2.Error())
			return err2
		}
		err3 := sendResponse(conn, resp)
		if err3 != nil {
			err3 = errors.New("UnregisterComponent.unregisterService() Error: " + err3.Error())
			return err3
		}
		return err
	}

	resp, err := protocol.NewMessage(protocol.VERSION, protocol.MT_UNREGISTER, protocol.PT_PUSHSERVER, msg.Token, msg.Number, uint16(0),
		P_UID, msg.UserUuid, msg.DeviceId, nil)
	if err != nil {
		err = errors.New("UnregisterComponent unregisterService() Error:conn is removed from BuffPool, but can not send response to service" + err.Error())
		return err
	}
	err2 := sendResponse(conn, resp)
	if err2 != nil {
		err2 = errors.New("UnregisterComponent unregisterService() Error: conn is reomved from BuffPool, but failed when send response to service" + err2.Error())
		return err2
	}
	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
}
Beispiel #6
0
func (pusher *Pusher) register() error {
	msg, err := protocol.NewMessage(uint8(protocol.VERSION), uint8(protocol.MT_REGISTER), uint8(protocol.PT_SERVICE), uint8(0), pusher.Number, uint16(P_SN_LEN),
		pusher.UserUuid, P_DEST_UUID, uint32(0), []byte(P_SN))
	if err != nil {
		err := errors.New("Pusher register Error: " + err.Error())
		return err
	}

	_, _, err = pusher.Connector.Register(msg, pusher.conn)
	if err != nil {
		err = errors.New("Pusher register Error: " + err.Error())
		return err
	}
	return nil
}
func (this *RegisterComponent) sendFailedRegisterResponse(conn *net.Conn, num uint16, userUuid string) error {
	response, err := protocol.NewMessage(protocol.VERSION, protocol.MT_ERROR, protocol.PT_PUSHSERVER, 0, num, 0,
		P_UID, userUuid, 0, nil)
	if err != nil {
		err = errors.New("RegisterComponent sendFailedRegisterResponse() Error: " + err.Error())
		return err
	}

	err = sendResponse(conn, response)
	if err != nil {
		err = errors.New("RegisterComponent sendFailedRegisterResponse() Error: " + err.Error())
		return err
	}
	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 *RegisterComponent) checkAccountServer(buffPool *buffpool.BuffPool, message *protocol.Message, conn *net.Conn) bool {
	if buffPool == nil || message == nil || conn == nil {
		fmt.Println("RegisterComponent checkAccountServer() Error: invalid argument")
		return false
	}
	connService := buffPool.GetService(protocol.N_ACCOUNT_SYS)
	if connService == nil {
		fmt.Println("RegisterComponent checkAccountServer() Error: can not get the account_sys server")
		response, _ := protocol.NewMessage(protocol.VERSION, protoocl.MT_ERROR, protocol.PT_PUSHSERVER, message.Token, message.Number, 0, P_UID,
			message.UserUuid, 0, nil)
		err := sendResponse(conn, response)
		if err != nil {
			err = fmt.Errorf("RegisterComponent checkAccountServer() Error:%v", err)
			fmt.Println(err.Error())
		}
		return false
	}
	return true
}
Beispiel #10
0
func (pusher *Pusher) createMsg() (msg *protocol.Message, err error) {
	pushObj := <-pusher.pushQueue
	if pushObj == nil {
		return nil, nil
	}
	var buffer []byte
	if pushObj.PushInfo == nil {
		buffer = nil
	} else {
		buffer, err = pushObj.PushInfo.Encode()
		if err != nil {
			err = errors.New("Pusher createMsg Error: " + err.Error())
			return nil, err
		}
	}
	msg, err = protocol.NewMessage(uint8(protocol.VERSION), pushObj.MsgType, uint8(protocol.PT_SERVICE), pushObj.Token,
		pushObj.Number, uint16(len(buffer)), pusher.UserUuid, pushObj.DestUuid, pushObj.DeviceId, buffer)
	if err != nil {
		err = errors.New("Pusher createMsg Error: " + err.Error())
		return nil, err
	}
	return msg, nil
}