コード例 #1
0
func (this *DispatcherOutComponent) sendToService(msg *protocol.Message, 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 {
			conn = buffPool.GetService(msg.Number)
		}
	}

	if conn == nil {
		err := errors.New("DispatcherOutComponent.sendToService() Error:  can not get a server connection from buffpool")
		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
	}
	return conn, nil
}
コード例 #2
0
func (this *DispatcherOutComponent) handleMTRegister(msg *protocol.Message, buffPool *buffpool.BuffPool, respComp *ResponseComponent) error {
	if msg.PushType == protocol.PT_PUSHSERVER {
		conn := buffPool.QueryService(msg.Number, msg.DestUuid)
		if conn == nil {
			clientConn := buffPool.GetClient(msg.Number, msg.DestUuid, msg.DeviceId, msg.Token)
			if clientConn == nil || clientConn.Conn == nil {
				err := errors.New("DispatcherOutComponent.handleMTRegister() Error: can not find the dest conn")
				fmt.Println(err.Error())
				return err
			}
			conn = clientConn.Conn
		}
		if conn == nil {
			err := errors.New("DispatcherOutComponent.handleMTRegister() Error: can not find the dest conn")
			fmt.Println(err.Error())
			return err
		}
		err := this.sendMessage(msg, conn)
		if err != nil {
			err = errors.New("DispatcherComponnet.handleMTRegister() Error: can not send message out, " + err.Error())
			fmt.Println(err.Error())
			return err
		}
	}
	return nil
}
コード例 #3
0
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
}
コード例 #4
0
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
}