thriftproto

package
Version: v6.2.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2019 License: Apache-2.0 Imports: 9 Imported by: 0

README

thriftproto

thriftproto is implemented thrift communication protocol.

Example

package thriftproto_test

import (
	"testing"
	"time"

	tp "github.com/henrylee2cn/teleport/v6"
	"github.com/henrylee2cn/teleport/v6/proto/thriftproto"
	"github.com/henrylee2cn/teleport/v6/xfer/gzip"
)

type Home struct {
	tp.CallCtx
}

func (h *Home) Test(arg *Test) (*Test, *tp.Status) {
	if string(h.PeekMeta("peer_id")) != "110" {
		panic("except meta: peer_id=110")
	}
	return &Test{
		Author: arg.Author + "->OK",
	}, nil
}

func TestBinaryProto(t *testing.T) {
	gzip.Reg('g', "gizp-5", 5)

	// server
	srv := tp.NewPeer(tp.PeerConfig{ListenPort: 9090, DefaultBodyCodec: "thrift"})
	srv.RouteCall(new(Home))
	go srv.ListenAndServe(thriftproto.NewBinaryProtoFunc())
	defer srv.Close()
	time.Sleep(1e9)

	// client
	cli := tp.NewPeer(tp.PeerConfig{DefaultBodyCodec: "thrift"})
	sess, stat := cli.Dial(":9090", thriftproto.NewBinaryProtoFunc())
	if !stat.OK() {
		t.Fatal(stat)
	}
	var result Test
	stat = sess.Call("Home.Test",
		&Test{Author: "henrylee2cn"},
		&result,
		tp.WithAddMeta("peer_id", "110"),
		tp.WithXferPipe('g'),
	).Status()
	if !stat.OK() {
		t.Error(stat)
	}
	if result.Author != "henrylee2cn->OK" {
		t.FailNow()
	}
	t.Logf("result:%v", result)
}

func TestStructProto(t *testing.T) {
	// server
	srv := tp.NewPeer(tp.PeerConfig{ListenPort: 9090})
	srv.RouteCall(new(Home))
	go srv.ListenAndServe(thriftproto.NewStructProtoFunc())
	defer srv.Close()
	time.Sleep(1e9)

	// client
	cli := tp.NewPeer(tp.PeerConfig{})
	sess, stat := cli.Dial(":9090", thriftproto.NewStructProtoFunc())
	if !stat.OK() {
		t.Fatal(stat)
	}
	var result Test
	stat = sess.Call("Home.Test",
		&Test{Author: "henrylee2cn"},
		&result,
		tp.WithAddMeta("peer_id", "110"),
	).Status()
	if !stat.OK() {
		t.Error(stat)
	}
	if result.Author != "henrylee2cn->OK" {
		t.FailNow()
	}
	t.Logf("result:%v", result)
}

test command:

go test -v -run=TestBinaryProto
go test -v -run=TestStructProto

Documentation

Index

Constants

View Source
const (
	// HeaderStatus the Status key in header of thrift message
	HeaderStatus = "Tp-Status"
	// HeaderMeta the Meta key in header of thrift message
	HeaderMeta = "Tp-Meta"
	// HeaderBodyCodec the BodyCodec key in header of thrift message
	HeaderBodyCodec = "Tp-BodyCodec"
	// HeaderXferPipe the XferPipe key in header of thrift message
	HeaderXferPipe = "Tp-XferPipe"
)

Variables

This section is empty.

Functions

func NewBinaryProtoFunc

func NewBinaryProtoFunc() tp.ProtoFunc

NewBinaryProtoFunc creates tp.ProtoFunc of Thrift protocol. NOTE:

Marshal the body into binary;
Support the Meta, BodyCodec and XferPipe.

func NewStructProtoFunc

func NewStructProtoFunc() tp.ProtoFunc

NewStructProtoFunc creates tp.ProtoFunc of Thrift protocol. NOTE:

The body codec must be thrift, directly encoded as a thrift.TStruct;
Support the Meta, but not support the BodyCodec and XferPipe.

Types

type BaseTTransport

type BaseTTransport struct {
	*utils.ReadWriteCounter
}

BaseTTransport the base thrift transport

func (*BaseTTransport) Close

func (*BaseTTransport) Close() error

Close close the transport.

func (*BaseTTransport) Flush

Flush flushing a memory buffer is a no-op.

func (*BaseTTransport) IsOpen

func (*BaseTTransport) IsOpen() bool

IsOpen returns true if the transport is open.

func (*BaseTTransport) Open

func (*BaseTTransport) Open() error

Open opens the transport for communication.

func (*BaseTTransport) RemainingBytes

func (*BaseTTransport) RemainingBytes() (numBytes uint64)

RemainingBytes returns the number of remaining bytes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL