thriftproto

package
v5.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2018 License: Apache-2.0 Imports: 5 Imported by: 0

README

thriftproto

thriftproto is implemented thrift communication protocol.

Message IDL

struct payload {
    1: binary  meta,
    2: binary  xferPipe,
    3: i32     bodyCodec,
    4: binary  body
}

Usage

import "github.com/henrylee2cn/teleport/proto/thriftproto"

Test
package thriftproto_test

import (
	"testing"
	"time"

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

type Home struct {
	tp.CallCtx
}

func (h *Home) Test(arg *map[string]string) (map[string]interface{}, *tp.Rerror) {
	h.Session().Push("Push.Test", map[string]string{
		"your_id": string(h.PeekMeta("peer_id")),
	})
	return map[string]interface{}{
		"arg": *arg,
	}, nil
}

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

	// server
	srv := tp.NewPeer(tp.PeerConfig{ListenPort: 9090})
	srv.RouteCall(new(Home))
	go srv.ListenAndServe(thriftproto.NewTProtoFactory())
	time.Sleep(1e9)

	// client
	cli := tp.NewPeer(tp.PeerConfig{})
	cli.RoutePush(new(Push))
	sess, err := cli.Dial(":9090", thriftproto.NewTProtoFactory())
	if err != nil {
		t.Error(err)
	}
	var result interface{}
	rerr := sess.Call("Home.Test",
		map[string]string{
			"author": "henrylee2cn",
		},
		&result,
		tp.WithAddMeta("peer_id", "110"),
		tp.WithXferPipe('g'),
	).Rerror()
	if rerr != nil {
		t.Error(rerr)
	}
	t.Logf("result:%v", result)
	time.Sleep(2e9)
}

type Push struct {
	tp.PushCtx
}

func (p *Push) Test(arg *map[string]string) *tp.Rerror {
	tp.Infof("receive push(%s):\narg: %#v\n", p.IP(), arg)
	return nil
}

test command:

go test -v -run=TestTProto

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewTProtoFactory

func NewTProtoFactory(factory ...thrift.TProtocolFactory) tp.ProtoFunc

NewTProtoFactory creates tp.ProtoFunc of Thrift protocol. NOTE:

If @factory is not provided, use the default binary protocol.

Types

This section is empty.

Directories

Path Synopsis
gen-go

Jump to

Keyboard shortcuts

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