Exemplo n.º 1
0
func (this *RegisterComponent) listenService(conn *net.Conn, buffPool *buffpool.BuffPool, number uint16, userUuid string) error {
	if buffPool == nil {
		err := errors.New("RegisterComponent listenService Error: buffpool is nil")
		return err
	}
	if conn == nil {
		err := errors.New("RegisterComponent listenService Error: the conn is nil")
		if GetRunWithSnode().Flag {
			this.ServiceOffline(userUuid)
		}

		err = buffPool.DeleteService(number, userUuid)
		if err != nil {
			err = fmt.Errorf("RegisterComponent listenService Error:%v", err)
			return err
		}
		return err
	}
	var dispatcher *DispatcherComponent
	for {
		dispatcher = GetDispatcherComponent()
		if dispatcher != nil {
			break
		} else {
			fmt.Println("RegisterComponent listenService Error: can not get DispatcherCompoennt")
		}
	}
	for {
		message, err := this.readMessage(conn)
		if number == protocol.N_ACCOUNT_SYS {

		}
		if err != nil && err != io.EOF && conn != nil {
			fmt.Println("RegisterComponent listenService Error: read message fail")
			continue
		} else if err != nil && (err == io.EOF|conn == nil) {
			err = errors.New("RegisterComponent listenService Error:the connection is invalid")
			err2 := buffPool.DeleteService(number, userUuid)

			if GetRunWithSnode().Flag {
				this.ServiceOffline(userUuid)
			}

			if err2 != nil {
				err2 = fmt.Errorf("RegisterComponent listenService Error: %v", err)
				fmt.Println(err2.Error())
				return err2
			}
			fmt.Println(err.Error())
			return err
		}

		if message == nil {
			continue
		}
		dispatcher.NPushBack(message, true)
	}
	return 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
}