示例#1
0
func (handler *_TransProxyHandler) forward(server Server, message *gorpc.Message) error {
	handler.V("forward tunnel(%s) message", handler.device)

	tunnel := gorpc.NewTunnel()

	tunnel.ID = handler.device

	tunnel.Message = message

	var buff bytes.Buffer

	err := gorpc.WriteTunnel(&buff, tunnel)

	if err != nil {
		return err
	}

	message.Code = gorpc.CodeTunnel

	message.Content = buff.Bytes()

	err = server.SendMessage(message)

	if err == nil {
		handler.V("forward tunnel(%s) message(%p) -- success", handler.device, message)
	} else {
		handler.E("forward tunnel(%s) message -- failed\n%s", handler.device, err)
	}

	return err
}
示例#2
0
func (handler *_TunnelClient) SendMessage(device *gorpc.Device, message *gorpc.Message) error {

	tunnel := gorpc.NewTunnel()

	tunnel.ID = device

	tunnel.Message = message

	var buff bytes.Buffer

	err := gorpc.WriteTunnel(&buff, tunnel)

	if err != nil {
		return err
	}

	message.Code = gorpc.CodeTunnel

	message.Content = buff.Bytes()

	handler.context.Send(message)
	return nil
}
示例#3
0
func (handler *_TransProxyHandler) MessageReceived(context gorpc.Context, message *gorpc.Message) (*gorpc.Message, error) {

	if message.Code == gorpc.CodeResponse {

		if server, ok := handler.tunnel(message.Agent); ok {

			err := handler.forward(server, message)

			if err != nil {
				context.Close()
			}

			return nil, err
		}

		return message, nil

	}

	if message.Code != gorpc.CodeRequest {

		return message, nil
	}

	request, err := gorpc.ReadRequest(bytes.NewBuffer(message.Content))

	if err != nil {
		handler.E("[%s] unmarshal request error\n%s", handler.proxy.name, err)
		return nil, err
	}

	service := request.Service

	if transproxy, ok := handler.transproxy(service); ok {

		handler.V("forward tunnel(%s) message", handler.device)

		tunnel := gorpc.NewTunnel()

		tunnel.ID = handler.device

		tunnel.Message = message

		var buff bytes.Buffer

		err := gorpc.WriteTunnel(&buff, tunnel)

		if err != nil {
			return nil, err
		}

		message.Code = gorpc.CodeTunnel

		message.Content = buff.Bytes()

		err = transproxy.SendMessage(message)

		if err != nil {
			context.Close()
			handler.V("forward tunnel(%s) message(%p) -- failed\n%s", handler.device, message, err)
			return nil, err
		}

		handler.V("forward tunnel(%s) message(%p) -- success", handler.device, message)

		return nil, err
	}

	return message, nil
}