1, rpc package
rpc package provides access to the connection method of deriving an object over a network or other I / O.
The method must meet these standards can be used for remote access, and the rest will be ignored in:
(1) The method is derived
(2) There are two parameters are derived type, or built-in type
(3) method of the second parameter is a pointer
(4) is only one type of return value of the interface error
func (t *T) MethodName(argType T1, replyType *T2) error
Wherein T, T1 and T2 can be encoding / gob of packet sequence.
Parameter parameter represents a method for providing a caller; the second parameter represents the parameter back to the caller.
The return value of the method, if non-nil, as a string return will be, as it appears to the client and errors.New created.
If it returns an error, return parameters will not be sent to the client.
Code Example:
main Package Import ( "errors" "FMT" "log" "NET" "NET / HTTP" "NET / RPC" ) // the Args parameter type the Args struct { A, B int } // Quotient quotient, a remainder type Quotient struct { Quo, Rem int } // Arith arithmetic service type Arith int // the multiply multiplication service FUNC (* Arith) the multiply (* args the args, Reply * int) {error * Reply * = args.A args.B return nil } // divide division service FUNC (* Arith) divide (* args the args, Quo Quotient *) error { IF args.B == 0 { return errors.New ( "can not be zero divisor") } Quo.Quo = args.A / args.B = args.A% args.B quo.Rem return nil } // error checking FUNC checkErr (ERR error) { IF ERR = nil {! log.Fatalln (ERR) } } FUNC main () { // Open Service arith: new new = (arith) // using the default service object ERR: = rpc.Register (arith) checkErr (ERR) // default path // const ( // DefaultRPCPath = "/ _goRPC_" // DefaultDebugPath = "/ Debug / rpc" //) rpc.HandleHTTP () // set the monitor LIS, ERR: = net.Listen ( "tcp", ": 1234") checkErr (ERR) Go http.Serve (LIS, nil) // client requests service client , err: = rpc.DialHTTP ( "tcp ", ": 1234") checkErr(err) the defer Client.Use Close () // multiplication args := &Args{A: 17, B: 3} var reply int err = client.Call("Arith.Multiply", args, &reply) checkErr(err) fmt.Printf("Arith.Multiply:%d * %d = %d\n", args.A, args.B, reply) //除法 quotient := new(Quotient) call := client.Go("Arith.Divide", args, quotient, nil) <-call.Done fmt.Printf("Arith.Divide:%d / %d = %d .... %d\n", args.A, args.B, quotient.Quo, quotient.Rem) } //输出 //Arith.Multiply:17 * 3 = 51 // Arith.Divide:17 / 3 = 5 .... 2
2, the client
2.1、type Client struct{}
Client type represents RPC client.
The same client may have multiple calls not returned, it may be more go away the same time.
2.2, a common method
(1)func NewClient(conn io.ReadWriteCloser) *Client
NewClient returns a new Client, to manage requests for services connected to the other end.
(2)func Dial(network, address string) (*Client, error)
Dial connected to the specified network address and the RPC server.
(3)func DialHTTP(network, address string) (*Client, error)
DialHTTP connected to the specified network and addresses in the default path HTTP RPC listens HTTP RPC server.
(4)func DialHTTPPath(network, address, path string) (*Client, error)
DialHTTPPath at the specified network address and a path connected to the HTTP RPC server.
(5)func (client *Client) Call(serviceMethod string, args interface{}, reply interface{}) error
Call call the specified method, wait call returns, the result is written reply, then return error status execution.
(6)func (client *Client) Go(serviceMethod string, args interface{}, reply interface{}, done chan *Call) *Call
Go asynchronous function call. The method of the present structure type Call return value represents a pointer to the secondary remote call.
Types of parameters will be done channel signals (Go through this method returns the return value) at the time of completion of this call.
If done is nil, Go will apply for a new channel (write Done return value of the field); if done non-nil, done must have a buffer, or the Go method will deliberately crash.
(7)func (client *Client) Close() error
Close the client.
3, the server
3.1、type Server struct{}
rpc package provides default service objects , can be directly through the use of "rpc.".
3.2, a common method
(1)func NewServer() *Server
Creates and returns a * Server.
(2)func (server *Server) Register(rcvr interface{}) error
Registration services.
If a method is not rcvr value derived type, or the type does not meet the requirements, Register returns an error.
Register also use log packet error written to the log.
The client can use the format "Type.Method" string access methods, particularly where Type is the type of rcvr.
(3)func (server *Server) RegisterName(name string, rcvr interface{}) error
RegisterName similar Register, but instead of using the name provided rcvr specific type name as the service name.
(4)func (server *Server) Accept(lis net.Listener)
Accept l listener receiver acquires connection, then each connection service.
Accept will block the caller should open another thread.
(5)func (server *Server) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP implements the interface http.Handler respond RPC requests.
(6)func (server *Server) HandleHTTP(rpcPath, debugPath string)
HandleHTTP the RPC server registration information corresponding to the HTTP handler rpcPath, the debug information of registered HTTP server processor corresponds to debugPath.
HandleHTTP will be registered to http.DefaultServeMux. After that, still we need to call http.Serve () , normally open another thread: "go http.Serve (l, nil )"