gencodec

package module
v0.0.0-...-2634c6e Latest Latest
Warning

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

Go to latest
Published: May 21, 2016 License: Apache-2.0 Imports: 9 Imported by: 0

README

net-rpc-gencode

GoDoc Drone Build Status Go Report Card

This library provides the same functions as net/rpc/jsonrpc but for communicating with gencode instead. The library is modeled directly after the Go standard library so it should be easy to use and obvious.

See the GoDoc for API documentation.

according to my test: Golang Serializer Benchmark Comparison, gencode is very faster than other serializers.

Example

gencode Schema

arith_test.schema:

struct Args {
	A int32
	B int32
}

struct Reply {
	C int32
}

run gencode go -schema arith_test.schema -package gencodec to generate data

RPC Server
package gencodec

import (
	"log"
	"net"
	"net/rpc"
	"github.com/smallnest/net-rpc-gencode"
)

type Arith int

func (t *Arith) Mul(args *Args, reply *Reply) error {
	reply.C = args.A * args.B
	return nil
}

func main() {
	rpc.Register(new(Arith))
	ln, e := net.Listen("tcp", "127.0.0.1:5432") // any available address
	if e != nil {
		log.Fatalf("net.Listen tcp :0: %v", e)
	}
	defer ln.Close()

	for {
		c, err := ln.Accept()
		if err != nil {
			continue
		}
		go gencodec.ServeConn(c)

	}
}
Client
package gencodec

import (
	"log"
	"net"
	"time"
	"github.com/smallnest/net-rpc-gencode"
)

func main() {
	client, err := gencodec.DialTimeout("tcp", "127.0.0.1:5432", time.Minute)
	if err != nil {
		fmt.Println("dialing:", err)
	}

	defer client.Close()

	// Synchronous call
	args := &gencodec.Args{7, 8}
	var reply Reply
	err = client.Call("Arith.Mul", args, &reply)
	if err != nil {
		fmt.Println("arith error:", err)
	} else {
		fmt.Printf("Arith: %d*%d=%d\n", args.A, args.B, reply.C)
	}
}

Documentation

Overview

Example
package main

import (
	"fmt"
	"github.com/smallnest/net-rpc-gencode"
	"log"
	"net"
	"net/rpc"
	"time"
)

type MyArith int

func (t *MyArith) Mul(args *gencodec.Args, reply *gencodec.Reply) error {
	reply.C = args.A * args.B
	return nil
}

func main() {
	rpc.Register(new(MyArith))
	ln, e := net.Listen("tcp", "127.0.0.1:0") // any available address
	if e != nil {
		log.Fatalf("net.Listen tcp :0: %v", e)
	}
	address := ln.Addr().String()
	defer ln.Close()

	go func() {
		for {
			c, err := ln.Accept()
			if err != nil {
				continue
			}
			go gencodec.ServeConn(c)

		}
	}()

	client, err := gencodec.DialTimeout("tcp", address, time.Minute)
	if err != nil {
		fmt.Println("dialing:", err)
	}

	defer client.Close()

	// Synchronous call
	args := &gencodec.Args{7, 8}
	var reply gencodec.Reply
	err = client.Call("MyArith.Mul", args, &reply)
	if err != nil {
		fmt.Println("arith error:", err)
	} else {
		fmt.Printf("Arith: %d*%d=%d\n", args.A, args.B, reply.C)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dial

func Dial(network, address string) (*rpc.Client, error)

Dial connects to a Gencode-RPC server at the specified network address.

func DialTimeout

func DialTimeout(network, address string, timeout time.Duration) (*rpc.Client, error)

DialTimeout connects to a Gencode-RPC server at the specified network address with timeout.

func NewClient

func NewClient(conn io.ReadWriteCloser) *rpc.Client

NewClient returns a new rpc.Client to handle requests to the set of services at the other end of the connection.

func NewGencodeClientCodec

func NewGencodeClientCodec(rwc io.ReadWriteCloser) rpc.ClientCodec

NewGencodeClientCodec returns a new rpc.Client.

A ClientCodec implements writing of RPC requests and reading of RPC responses for the client side of an RPC session. The client calls WriteRequest to write a request to the connection and calls ReadResponseHeader and ReadResponseBody in pairs to read responses. The client calls Close when finished with the connection.

func NewGencodeServerCodec

func NewGencodeServerCodec(rwc io.ReadWriteCloser) rpc.ServerCodec

NewGencodeServerCodec returns a new rpc.ServerCodec.

A ServerCodec implements reading of RPC requests and writing of RPC responses for the server side of an RPC session. The server calls ReadRequestHeader and ReadRequestBody in pairs to read requests from the connection, and it calls WriteResponse to write a response back. The server calls Close when finished with the connection.

func ServeConn

func ServeConn(conn io.ReadWriteCloser)

ServeConn runs the Gencode-RPC server on a single connection. ServeConn blocks, serving the connection until the client hangs up. The caller typically invokes ServeConn in a go statement.

Types

type DecodeReader

type DecodeReader interface {
	io.ByteReader
	io.Reader
}

DecodeReader manages the receipt of type and data information read from the remote side of a connection.

type RequestHeader

type RequestHeader struct {
	ServiceMethod string
	Seq           uint64
}

func (*RequestHeader) Marshal

func (d *RequestHeader) Marshal(buf []byte) ([]byte, error)

func (*RequestHeader) Size

func (d *RequestHeader) Size() (s uint64)

func (*RequestHeader) Unmarshal

func (d *RequestHeader) Unmarshal(buf []byte) (uint64, error)

type ResponseHeader

type ResponseHeader struct {
	ServiceMethod string
	Seq           uint64
	Error         string
}

func (*ResponseHeader) Marshal

func (d *ResponseHeader) Marshal(buf []byte) ([]byte, error)

func (*ResponseHeader) Size

func (d *ResponseHeader) Size() (s uint64)

func (*ResponseHeader) Unmarshal

func (d *ResponseHeader) Unmarshal(buf []byte) (uint64, error)

Jump to

Keyboard shortcuts

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