Documentation
¶
Overview ¶
Example ¶
package main import ( "fmt" "log" "net" "time" "github.com/smallnest/net-rpc-gencode" "github.com/smallnest/rpcx/core" ) type MyArith int func (t *MyArith) Mul(args *gencodec.Args, reply *gencodec.Reply) error { reply.C = args.A * args.B return nil } func main() { core.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 ¶
- func Dial(network, address string) (*core.Client, error)
- func DialTimeout(network, address string, timeout time.Duration) (*core.Client, error)
- func NewClient(conn io.ReadWriteCloser) *core.Client
- func NewGencodeClientCodec(rwc io.ReadWriteCloser) core.ClientCodec
- func NewGencodeServerCodec(rwc io.ReadWriteCloser) core.ServerCodec
- func ServeConn(conn io.ReadWriteCloser)
- type DecodeReader
- type RequestHeader
- type ResponseHeader
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DialTimeout ¶
DialTimeout connects to a Gencode-RPC server at the specified network address with timeout.
func NewClient ¶
func NewClient(conn io.ReadWriteCloser) *core.Client
NewClient returns a new core.Client to handle requests to the set of services at the other end of the connection.
func NewGencodeClientCodec ¶
func NewGencodeClientCodec(rwc io.ReadWriteCloser) core.ClientCodec
NewGencodeClientCodec returns a new core.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) core.ServerCodec
NewGencodeServerCodec returns a new core.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 ¶
func (*RequestHeader) Size ¶
func (d *RequestHeader) Size() (s uint64)
type ResponseHeader ¶
func (*ResponseHeader) Size ¶
func (d *ResponseHeader) Size() (s uint64)