thriftproto

package
v7.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

README

thriftproto

thriftproto is implemented thrift communication protocol.

Example

package thriftproto_test

import (
	"testing"
	"time"

	"github.com/andeya/erpc/v7"
	"github.com/andeya/erpc/v7/proto/thriftproto"
	"github.com/andeya/erpc/v7/xfer/gzip"
)

type Home struct {
	erpc.CallCtx
}

func (h *Home) Test(arg *Test) (*Test, *erpc.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 := erpc.NewPeer(erpc.PeerConfig{ListenPort: 9090, DefaultBodyCodec: "thrift"})
	srv.RouteCall(new(Home))
	go srv.ListenAndServe(thriftproto.NewBinaryProtoFunc())
	defer srv.Close()
	time.Sleep(1e9)

	// client
	cli := erpc.NewPeer(erpc.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: "andeya"},
		&result,
		erpc.WithAddMeta("peer_id", "110"),
		erpc.WithXferPipe('g'),
	).Status()
	if !stat.OK() {
		t.Error(stat)
	}
	if result.Author != "andeya->OK" {
		t.FailNow()
	}
	t.Logf("result:%v", result)
}

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

	// client
	cli := erpc.NewPeer(erpc.PeerConfig{})
	sess, stat := cli.Dial(":9090", thriftproto.NewStructProtoFunc())
	if !stat.OK() {
		t.Fatal(stat)
	}
	var result Test
	stat = sess.Call("Home.Test",
		&Test{Author: "andeya"},
		&result,
		erpc.WithAddMeta("peer_id", "110"),
	).Status()
	if !stat.OK() {
		t.Error(stat)
	}
	if result.Author != "andeya->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() erpc.ProtoFunc

NewBinaryProtoFunc creates erpc.ProtoFunc of Thrift protocol. NOTE:

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

func NewStructProtoFunc

func NewStructProtoFunc() erpc.ProtoFunc

NewStructProtoFunc creates erpc.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.

Directories

Path Synopsis
gen-go

Jump to

Keyboard shortcuts

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