當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Message.Code方法代碼示例

本文整理匯總了Golang中github.com/gsrpc/gorpc.Message.Code方法的典型用法代碼示例。如果您正苦於以下問題:Golang Message.Code方法的具體用法?Golang Message.Code怎麽用?Golang Message.Code使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/gsrpc/gorpc.Message的用法示例。


在下文中一共展示了Message.Code方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: forward

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
}
開發者ID:gsdocker,項目名稱:gsproxy,代碼行數:31,代碼來源:handlers.go

示例2: SendMessage

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
}
開發者ID:gsdocker,項目名稱:gsproxy,代碼行數:23,代碼來源:tunnel.go

示例3: MessageReceived

func (handler *_CryptoServer) MessageReceived(context gorpc.Context, message *gorpc.Message) (*gorpc.Message, error) {

	if message.Code == gorpc.CodeHeartbeat {
		return message, nil
	}

	if handler.block == nil {

		handler.V("expect WhoAmI message")
		// expect whoAmI message
		if message.Code != gorpc.CodeWhoAmI {
			context.Close()
			return nil, gserrors.Newf(gorpc.ErrRPC, "expect WhoAmI message but got(%s)", message.Code)
		}

		handler.V("parse WhoAmI message")

		whoAmI, err := gorpc.ReadWhoAmI(bytes.NewBuffer(message.Content))

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

		val, ok := new(big.Int).SetString(string(whoAmI.Context), 0)

		if !ok {
			context.Close()
			return nil, gserrors.Newf(gorpc.ErrRPC, "parse WhoAmI#Context as big.Int error")
		}

		dhKey, err := handler.resovler.Resolve(whoAmI.ID)

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

		message.Code = gorpc.CodeAccept

		message.Content = []byte(dhKey.Exchange().String())

		context.Send(message)

		key := make([]byte, des.BlockSize)

		keyval := dhKey.Gen(val).Uint64()

		binary.BigEndian.PutUint64(key[:8], keyval)

		handler.V("shared key \n\t%d\n\t%v ", keyval, key)

		block, err := des.NewCipher(key)

		if err != nil {
			context.Close()
			return nil, gserrors.Newf(err, "create new des Cipher error")
		}

		handler.block = block

		handler.device = whoAmI.ID

		context.FireActive()

		for _, message := range handler.cached {
			message, _ = handler.MessageSending(context, message)
			context.Send(message)
		}

		handler.cached = nil

		handler.V("%s handshake -- success", context.Name())

		return nil, nil

	}

	blocksize := handler.block.BlockSize()

	if len(message.Content)%blocksize != 0 {
		context.Close()
		return nil, gserrors.Newf(gorpc.ErrRPC, "invalid encrypt data")
	}

	blocks := len(message.Content) / blocksize

	for i := 0; i < blocks; i++ {
		offset := i * blocksize
		v := message.Content[offset : offset+blocksize]
		handler.block.Decrypt(v, v)
	}

	message.Content = PKCS5UnPadding(message.Content)

	return message, nil
}
開發者ID:gsrpc,項目名稱:gorpc,代碼行數:97,代碼來源:dh.go

示例4: MessageReceived

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
}
開發者ID:gsdocker,項目名稱:gsproxy,代碼行數:70,代碼來源:handlers.go


注:本文中的github.com/gsrpc/gorpc.Message.Code方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。