You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1055 lines
32 KiB

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#375EAB">
<title>rpc - The Go Programming Language</title>
<link type="text/css" rel="stylesheet" href="../../../lib/godoc/style.css">
<link rel="stylesheet" href="../../../lib/godoc/jquery.treeview.css">
<script type="text/javascript">window.initFuncs = [];</script>
</head>
<body>
<div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
...
</div><!-- #lowframe -->
<div id="topbar" class="wide"><div class="container">
<div class="top-heading" id="heading-wide"><a href="http://localhost:6060/">The Go Programming Language</a></div>
<div class="top-heading" id="heading-narrow"><a href="http://localhost:6060/">Go</a></div>
<a href="index.html#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
<form method="GET" action="http://localhost:6060/search">
<div id="menu">
<a href="http://localhost:6060/doc/">Documents</a>
<a href="http://localhost:6060/pkg/">Packages</a>
<a href="http://localhost:6060/project/">The Project</a>
<a href="http://localhost:6060/help/">Help</a>
<a href="http://localhost:6060/blog/">Blog</a>
<input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
</div>
</form>
</div></div>
<div id="page" class="wide">
<div class="container">
<h1>Package rpc</h1>
<div id="nav"></div>
<!--
Copyright 2009 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
-->
<!--
Note: Static (i.e., not template-generated) href and id
attributes start with "pkg-" to make it impossible for
them to conflict with generated attributes (some of which
correspond to Go identifiers).
-->
<script type='text/javascript'>
document.ANALYSIS_DATA = null;
document.CALLGRAPH = null;
</script>
<div id="short-nav">
<dl>
<dd><code>import "net/rpc"</code></dd>
</dl>
<dl>
<dd><a href="index.html#pkg-overview" class="overviewLink">Overview</a></dd>
<dd><a href="index.html#pkg-index" class="indexLink">Index</a></dd>
<dd><a href="index.html#pkg-subdirectories">Subdirectories</a></dd>
</dl>
</div>
<!-- The package's Name is printed as title by the top-level template -->
<div id="pkg-overview" class="toggleVisible">
<div class="collapsed">
<h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
</div>
<div class="expanded">
<h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
<p>
Package rpc provides access to the exported methods of an object across a
network or other I/O connection. A server registers an object, making it visible
as a service with the name of the type of the object. After registration, exported
methods of the object will be accessible remotely. A server may register multiple
objects (services) of different types but it is an error to register multiple
objects of the same type.
</p>
<p>
Only methods that satisfy these criteria will be made available for remote access;
other methods will be ignored:
</p>
<pre>- the method&#39;s type is exported.
- the method is exported.
- the method has two arguments, both exported (or builtin) types.
- the method&#39;s second argument is a pointer.
- the method has return type error.
</pre>
<p>
In effect, the method must look schematically like
</p>
<pre>func (t *T) MethodName(argType T1, replyType *T2) error
</pre>
<p>
where T, T1 and T2 can be marshaled by encoding/gob.
These requirements apply even if a different codec is used.
(In the future, these requirements may soften for custom codecs.)
</p>
<p>
The method&#39;s first argument represents the arguments provided by the caller; the
second argument represents the result parameters to be returned to the caller.
The method&#39;s return value, if non-nil, is passed back as a string that the client
sees as if created by errors.New. If an error is returned, the reply parameter
will not be sent back to the client.
</p>
<p>
The server may handle requests on a single connection by calling ServeConn. More
typically it will create a network listener and call Accept or, for an HTTP
listener, HandleHTTP and http.Serve.
</p>
<p>
A client wishing to use the service establishes a connection and then invokes
NewClient on the connection. The convenience function Dial (DialHTTP) performs
both steps for a raw network connection (an HTTP connection). The resulting
Client object has two methods, Call and Go, that specify the service and method to
call, a pointer containing the arguments, and a pointer to receive the result
parameters.
</p>
<p>
The Call method waits for the remote call to complete while the Go method
launches the call asynchronously and signals completion using the Call
structure&#39;s Done channel.
</p>
<p>
Unless an explicit codec is set up, package encoding/gob is used to
transport the data.
</p>
<p>
Here is a simple example. A server wishes to export an object of type Arith:
</p>
<pre>package server
type Args struct {
A, B int
}
type Quotient struct {
Quo, Rem int
}
type Arith int
func (t *Arith) Multiply(args *Args, reply *int) error {
*reply = args.A * args.B
return nil
}
func (t *Arith) Divide(args *Args, quo *Quotient) error {
if args.B == 0 {
return errors.New(&#34;divide by zero&#34;)
}
quo.Quo = args.A / args.B
quo.Rem = args.A % args.B
return nil
}
</pre>
<p>
The server calls (for HTTP service):
</p>
<pre>arith := new(Arith)
rpc.Register(arith)
rpc.HandleHTTP()
l, e := net.Listen(&#34;tcp&#34;, &#34;:1234&#34;)
if e != nil {
log.Fatal(&#34;listen error:&#34;, e)
}
go http.Serve(l, nil)
</pre>
<p>
At this point, clients can see a service &#34;Arith&#34; with methods &#34;Arith.Multiply&#34; and
&#34;Arith.Divide&#34;. To invoke one, a client first dials the server:
</p>
<pre>client, err := rpc.DialHTTP(&#34;tcp&#34;, serverAddress + &#34;:1234&#34;)
if err != nil {
log.Fatal(&#34;dialing:&#34;, err)
}
</pre>
<p>
Then it can make a remote call:
</p>
<pre>// Synchronous call
args := &amp;server.Args{7,8}
var reply int
err = client.Call(&#34;Arith.Multiply&#34;, args, &amp;reply)
if err != nil {
log.Fatal(&#34;arith error:&#34;, err)
}
fmt.Printf(&#34;Arith: %d*%d=%d&#34;, args.A, args.B, reply)
</pre>
<p>
or
</p>
<pre>// Asynchronous call
quotient := new(Quotient)
divCall := client.Go(&#34;Arith.Divide&#34;, args, quotient, nil)
replyCall := &lt;-divCall.Done // will be equal to divCall
// check errors, print, etc.
</pre>
<p>
A server implementation will often provide a simple, type-safe wrapper for the
client.
</p>
</div>
</div>
<div id="pkg-index" class="toggleVisible">
<div class="collapsed">
<h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
</div>
<div class="expanded">
<h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
<!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
<div id="manual-nav">
<dl>
<dd><a href="index.html#pkg-constants">Constants</a></dd>
<dd><a href="index.html#pkg-variables">Variables</a></dd>
<dd><a href="index.html#Accept">func Accept(lis net.Listener)</a></dd>
<dd><a href="index.html#HandleHTTP">func HandleHTTP()</a></dd>
<dd><a href="index.html#Register">func Register(rcvr interface{}) error</a></dd>
<dd><a href="index.html#RegisterName">func RegisterName(name string, rcvr interface{}) error</a></dd>
<dd><a href="index.html#ServeCodec">func ServeCodec(codec ServerCodec)</a></dd>
<dd><a href="index.html#ServeConn">func ServeConn(conn io.ReadWriteCloser)</a></dd>
<dd><a href="index.html#ServeRequest">func ServeRequest(codec ServerCodec) error</a></dd>
<dd><a href="index.html#Call">type Call</a></dd>
<dd><a href="index.html#Client">type Client</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Dial">func Dial(network, address string) (*Client, error)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#DialHTTP">func DialHTTP(network, address string) (*Client, error)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#DialHTTPPath">func DialHTTPPath(network, address, path string) (*Client, error)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#NewClient">func NewClient(conn io.ReadWriteCloser) *Client</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#NewClientWithCodec">func NewClientWithCodec(codec ClientCodec) *Client</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Client.Call">func (client *Client) Call(serviceMethod string, args interface{}, reply interface{}) error</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Client.Close">func (client *Client) Close() error</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Client.Go">func (client *Client) Go(serviceMethod string, args interface{}, reply interface{}, done chan *Call) *Call</a></dd>
<dd><a href="index.html#ClientCodec">type ClientCodec</a></dd>
<dd><a href="index.html#Request">type Request</a></dd>
<dd><a href="index.html#Response">type Response</a></dd>
<dd><a href="index.html#Server">type Server</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#NewServer">func NewServer() *Server</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Server.Accept">func (server *Server) Accept(lis net.Listener)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Server.HandleHTTP">func (server *Server) HandleHTTP(rpcPath, debugPath string)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Server.Register">func (server *Server) Register(rcvr interface{}) error</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Server.RegisterName">func (server *Server) RegisterName(name string, rcvr interface{}) error</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Server.ServeCodec">func (server *Server) ServeCodec(codec ServerCodec)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Server.ServeConn">func (server *Server) ServeConn(conn io.ReadWriteCloser)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Server.ServeHTTP">func (server *Server) ServeHTTP(w http.ResponseWriter, req *http.Request)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Server.ServeRequest">func (server *Server) ServeRequest(codec ServerCodec) error</a></dd>
<dd><a href="index.html#ServerCodec">type ServerCodec</a></dd>
<dd><a href="index.html#ServerError">type ServerError</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#ServerError.Error">func (e ServerError) Error() string</a></dd>
</dl>
</div><!-- #manual-nav -->
<h4>Package files</h4>
<p>
<span style="font-size:90%">
<a href="http://localhost:6060/src/net/rpc/client.go">client.go</a>
<a href="http://localhost:6060/src/net/rpc/debug.go">debug.go</a>
<a href="http://localhost:6060/src/net/rpc/server.go">server.go</a>
</span>
</p>
</div><!-- .expanded -->
</div><!-- #pkg-index -->
<div id="pkg-callgraph" class="toggle" style="display: none">
<div class="collapsed">
<h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
</div> <!-- .expanded -->
<div class="expanded">
<h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
<p>
In the call graph viewer below, each node
is a function belonging to this package
and its children are the functions it
calls&mdash;perhaps dynamically.
</p>
<p>
The root nodes are the entry points of the
package: functions that may be called from
outside the package.
There may be non-exported or anonymous
functions among them if they are called
dynamically from another package.
</p>
<p>
Click a node to visit that function's source code.
From there you can visit its callers by
clicking its declaring <code>func</code>
token.
</p>
<p>
Functions may be omitted if they were
determined to be unreachable in the
particular programs or tests that were
analyzed.
</p>
<!-- Zero means show all package entry points. -->
<ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
</div>
</div> <!-- #pkg-callgraph -->
<h2 id="pkg-constants">Constants</h2>
<pre>const (
<span class="comment">// Defaults used by HandleHTTP</span>
<span id="DefaultRPCPath">DefaultRPCPath</span> = &#34;/_goRPC_&#34;
<span id="DefaultDebugPath">DefaultDebugPath</span> = &#34;/debug/rpc&#34;
)</pre>
<h2 id="pkg-variables">Variables</h2>
<pre>var <span id="DefaultServer">DefaultServer</span> = <a href="index.html#NewServer">NewServer</a>()</pre>
<p>
DefaultServer is the default instance of *Server.
</p>
<pre>var <span id="ErrShutdown">ErrShutdown</span> = <a href="../../errors/index.html">errors</a>.<a href="../../errors/index.html#New">New</a>(&#34;connection is shut down&#34;)</pre>
<h2 id="Accept">func <a href="http://localhost:6060/src/net/rpc/server.go?s=20231:20260#L667">Accept</a></h2>
<pre>func Accept(lis <a href="../index.html">net</a>.<a href="../index.html#Listener">Listener</a>)</pre>
<p>
Accept accepts connections on the listener and serves requests
to DefaultServer for each incoming connection.
Accept blocks; the caller typically invokes it in a go statement.
</p>
<h2 id="HandleHTTP">func <a href="http://localhost:6060/src/net/rpc/server.go?s=21491:21508#L700">HandleHTTP</a></h2>
<pre>func HandleHTTP()</pre>
<p>
HandleHTTP registers an HTTP handler for RPC messages to DefaultServer
on DefaultRPCPath and a debugging handler on DefaultDebugPath.
It is still necessary to invoke http.Serve(), typically in a go statement.
</p>
<h2 id="Register">func <a href="http://localhost:6060/src/net/rpc/server.go?s=18260:18297#L619">Register</a></h2>
<pre>func Register(rcvr interface{}) <a href="../../builtin/index.html#error">error</a></pre>
<p>
Register publishes the receiver&#39;s methods in the DefaultServer.
</p>
<h2 id="RegisterName">func <a href="http://localhost:6060/src/net/rpc/server.go?s=18456:18510#L623">RegisterName</a></h2>
<pre>func RegisterName(name <a href="../../builtin/index.html#string">string</a>, rcvr interface{}) <a href="../../builtin/index.html#error">error</a></pre>
<p>
RegisterName is like Register but uses the provided name for the type
instead of the receiver&#39;s concrete type.
</p>
<h2 id="ServeCodec">func <a href="http://localhost:6060/src/net/rpc/server.go?s=19757:19791#L654">ServeCodec</a></h2>
<pre>func ServeCodec(codec <a href="index.html#ServerCodec">ServerCodec</a>)</pre>
<p>
ServeCodec is like ServeConn but uses the specified codec to
decode requests and encode responses.
</p>
<h2 id="ServeConn">func <a href="http://localhost:6060/src/net/rpc/server.go?s=19576:19615#L648">ServeConn</a></h2>
<pre>func ServeConn(conn <a href="../../io/index.html">io</a>.<a href="../../io/index.html#ReadWriteCloser">ReadWriteCloser</a>)</pre>
<p>
ServeConn runs the DefaultServer on a single connection.
ServeConn blocks, serving the connection until the client hangs up.
The caller typically invokes ServeConn in a go statement.
ServeConn uses the gob wire format (see package gob) on the
connection. To use an alternate codec, use ServeCodec.
</p>
<h2 id="ServeRequest">func <a href="http://localhost:6060/src/net/rpc/server.go?s=19956:19998#L660">ServeRequest</a></h2>
<pre>func ServeRequest(codec <a href="index.html#ServerCodec">ServerCodec</a>) <a href="../../builtin/index.html#error">error</a></pre>
<p>
ServeRequest is like ServeCodec but synchronously serves a single request.
It does not close the codec upon completion.
</p>
<h2 id="Call">type <a href="http://localhost:6060/src/net/rpc/client.go?s=540:900#L19">Call</a></h2>
<pre>type Call struct {
ServiceMethod <a href="../../builtin/index.html#string">string</a> <span class="comment">// The name of the service and method to call.</span>
Args interface{} <span class="comment">// The argument to the function (*struct).</span>
Reply interface{} <span class="comment">// The reply from the function (*struct).</span>
Error <a href="../../builtin/index.html#error">error</a> <span class="comment">// After completion, the error status.</span>
Done chan *<a href="index.html#Call">Call</a> <span class="comment">// Strobes when call is complete.</span>
}</pre>
<p>
Call represents an active RPC.
</p>
<h2 id="Client">type <a href="http://localhost:6060/src/net/rpc/client.go?s=1084:1360#L31">Client</a></h2>
<pre>type Client struct {
<span class="comment">// contains filtered or unexported fields</span>
}</pre>
<p>
Client represents an RPC Client.
There may be multiple outstanding Calls associated
with a single Client, and a Client may be used by
multiple goroutines simultaneously.
</p>
<h3 id="Dial">func <a href="http://localhost:6060/src/net/rpc/client.go?s=7222:7273#L259">Dial</a></h3>
<pre>func Dial(network, address <a href="../../builtin/index.html#string">string</a>) (*<a href="index.html#Client">Client</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
<p>
Dial connects to an RPC server at the specified network address.
</p>
<h3 id="DialHTTP">func <a href="http://localhost:6060/src/net/rpc/client.go?s=6275:6330#L226">DialHTTP</a></h3>
<pre>func DialHTTP(network, address <a href="../../builtin/index.html#string">string</a>) (*<a href="index.html#Client">Client</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
<p>
DialHTTP connects to an HTTP RPC server at the specified network address
listening on the default HTTP RPC path.
</p>
<h3 id="DialHTTPPath">func <a href="http://localhost:6060/src/net/rpc/client.go?s=6484:6549#L232">DialHTTPPath</a></h3>
<pre>func DialHTTPPath(network, address, path <a href="../../builtin/index.html#string">string</a>) (*<a href="index.html#Client">Client</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
<p>
DialHTTPPath connects to an HTTP RPC server
at the specified network address and path.
</p>
<h3 id="NewClient">func <a href="http://localhost:6060/src/net/rpc/client.go?s=5073:5120#L178">NewClient</a></h3>
<pre>func NewClient(conn <a href="../../io/index.html">io</a>.<a href="../../io/index.html#ReadWriteCloser">ReadWriteCloser</a>) *<a href="index.html#Client">Client</a></pre>
<p>
NewClient returns a new Client to handle requests to the
set of services at the other end of the connection.
It adds a buffer to the write side of the connection so
the header and payload are sent as a unit.
</p>
<h3 id="NewClientWithCodec">func <a href="http://localhost:6060/src/net/rpc/client.go?s=5394:5444#L186">NewClientWithCodec</a></h3>
<pre>func NewClientWithCodec(codec <a href="index.html#ClientCodec">ClientCodec</a>) *<a href="index.html#Client">Client</a></pre>
<p>
NewClientWithCodec is like NewClient but uses the specified
codec to encode requests and decode responses.
</p>
<h3 id="Client.Call">func (*Client) <a href="http://localhost:6060/src/net/rpc/client.go?s=8636:8727#L304">Call</a></h3>
<pre>func (client *<a href="index.html#Client">Client</a>) Call(serviceMethod <a href="../../builtin/index.html#string">string</a>, args interface{}, reply interface{}) <a href="../../builtin/index.html#error">error</a></pre>
<p>
Call invokes the named function, waits for it to complete, and returns its error status.
</p>
<h3 id="Client.Close">func (*Client) <a href="http://localhost:6060/src/net/rpc/client.go?s=7387:7422#L267">Close</a></h3>
<pre>func (client *<a href="index.html#Client">Client</a>) Close() <a href="../../builtin/index.html#error">error</a></pre>
<h3 id="Client.Go">func (*Client) <a href="http://localhost:6060/src/net/rpc/client.go?s=7910:8016#L282">Go</a></h3>
<pre>func (client *<a href="index.html#Client">Client</a>) Go(serviceMethod <a href="../../builtin/index.html#string">string</a>, args interface{}, reply interface{}, done chan *<a href="index.html#Call">Call</a>) *<a href="index.html#Call">Call</a></pre>
<p>
Go invokes the function asynchronously. It returns the Call structure representing
the invocation. The done channel will signal when the call is complete by returning
the same Call object. If done is nil, Go will allocate a new channel.
If non-nil, done must be buffered or Go will deliberately crash.
</p>
<h2 id="ClientCodec">type <a href="http://localhost:6060/src/net/rpc/client.go?s=1823:2059#L52">ClientCodec</a></h2>
<pre>type ClientCodec interface {
<span class="comment">// WriteRequest must be safe for concurrent use by multiple goroutines.</span>
WriteRequest(*<a href="index.html#Request">Request</a>, interface{}) <a href="../../builtin/index.html#error">error</a>
ReadResponseHeader(*<a href="index.html#Response">Response</a>) <a href="../../builtin/index.html#error">error</a>
ReadResponseBody(interface{}) <a href="../../builtin/index.html#error">error</a>
Close() <a href="../../builtin/index.html#error">error</a>
}</pre>
<p>
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. ReadResponseBody may be called with a nil
argument to force the body of the response to be read and then
discarded.
</p>
<h2 id="Request">type <a href="http://localhost:6060/src/net/rpc/server.go?s=5048:5234#L158">Request</a></h2>
<pre>type Request struct {
ServiceMethod <a href="../../builtin/index.html#string">string</a> <span class="comment">// format: &#34;Service.Method&#34;</span>
Seq <a href="../../builtin/index.html#uint64">uint64</a> <span class="comment">// sequence number chosen by client</span>
<span class="comment">// contains filtered or unexported fields</span>
}</pre>
<p>
Request is a header written before every RPC call. It is used internally
but documented here as an aid to debugging, such as when analyzing
network traffic.
</p>
<h2 id="Response">type <a href="http://localhost:6060/src/net/rpc/server.go?s=5406:5635#L167">Response</a></h2>
<pre>type Response struct {
ServiceMethod <a href="../../builtin/index.html#string">string</a> <span class="comment">// echoes that of the Request</span>
Seq <a href="../../builtin/index.html#uint64">uint64</a> <span class="comment">// echoes that of the request</span>
Error <a href="../../builtin/index.html#string">string</a> <span class="comment">// error, if any.</span>
<span class="comment">// contains filtered or unexported fields</span>
}</pre>
<p>
Response is a header written before every RPC return. It is used internally
but documented here as an aid to debugging, such as when analyzing
network traffic.
</p>
<h2 id="Server">type <a href="http://localhost:6060/src/net/rpc/server.go?s=5673:5909#L175">Server</a></h2>
<pre>type Server struct {
<span class="comment">// contains filtered or unexported fields</span>
}</pre>
<p>
Server represents an RPC Server.
</p>
<h3 id="NewServer">func <a href="http://localhost:6060/src/net/rpc/server.go?s=5946:5970#L185">NewServer</a></h3>
<pre>func NewServer() *<a href="index.html#Server">Server</a></pre>
<p>
NewServer returns a new Server.
</p>
<h3 id="Server.Accept">func (*Server) <a href="http://localhost:6060/src/net/rpc/server.go?s=17995:18041#L607">Accept</a></h3>
<pre>func (server *<a href="index.html#Server">Server</a>) Accept(lis <a href="../index.html">net</a>.<a href="../index.html#Listener">Listener</a>)</pre>
<p>
Accept accepts connections on the listener and serves requests
for each incoming connection. Accept blocks until the listener
returns a non-nil error. The caller typically invokes Accept in a
go statement.
</p>
<h3 id="Server.HandleHTTP">func (*Server) <a href="http://localhost:6060/src/net/rpc/server.go?s=21135:21194#L692">HandleHTTP</a></h3>
<pre>func (server *<a href="index.html#Server">Server</a>) HandleHTTP(rpcPath, debugPath <a href="../../builtin/index.html#string">string</a>)</pre>
<p>
HandleHTTP registers an HTTP handler for RPC messages on rpcPath,
and a debugging handler on debugPath.
It is still necessary to invoke http.Serve(), typically in a go statement.
</p>
<h3 id="Server.Register">func (*Server) <a href="http://localhost:6060/src/net/rpc/server.go?s=7103:7157#L218">Register</a></h3>
<pre>func (server *<a href="index.html#Server">Server</a>) Register(rcvr interface{}) <a href="../../builtin/index.html#error">error</a></pre>
<p>
Register publishes in the server the set of methods of the
receiver value that satisfy the following conditions:
</p>
<pre>- exported method of exported type
- two arguments, both of exported type
- the second argument is a pointer
- one return value, of type error
</pre>
<p>
It returns an error if the receiver is not an exported type or has
no suitable methods. It also logs the error using package log.
The client accesses each method using a string of the form &#34;Type.Method&#34;,
where Type is the receiver&#39;s concrete type.
</p>
<h3 id="Server.RegisterName">func (*Server) <a href="http://localhost:6060/src/net/rpc/server.go?s=7321:7392#L224">RegisterName</a></h3>
<pre>func (server *<a href="index.html#Server">Server</a>) RegisterName(name <a href="../../builtin/index.html#string">string</a>, rcvr interface{}) <a href="../../builtin/index.html#error">error</a></pre>
<p>
RegisterName is like Register but uses the provided name for the type
instead of the receiver&#39;s concrete type.
</p>
<h3 id="Server.ServeCodec">func (*Server) <a href="http://localhost:6060/src/net/rpc/server.go?s=13822:13873#L449">ServeCodec</a></h3>
<pre>func (server *<a href="index.html#Server">Server</a>) ServeCodec(codec <a href="index.html#ServerCodec">ServerCodec</a>)</pre>
<p>
ServeCodec is like ServeConn but uses the specified codec to
decode requests and encode responses.
</p>
<h3 id="Server.ServeConn">func (*Server) <a href="http://localhost:6060/src/net/rpc/server.go?s=13479:13535#L436">ServeConn</a></h3>
<pre>func (server *<a href="index.html#Server">Server</a>) ServeConn(conn <a href="../../io/index.html">io</a>.<a href="../../io/index.html#ReadWriteCloser">ReadWriteCloser</a>)</pre>
<p>
ServeConn runs the server on a single connection.
ServeConn blocks, serving the connection until the client hangs up.
The caller typically invokes ServeConn in a go statement.
ServeConn uses the gob wire format (see package gob) on the
connection. To use an alternate codec, use ServeCodec.
</p>
<h3 id="Server.ServeHTTP">func (*Server) <a href="http://localhost:6060/src/net/rpc/server.go?s=20463:20536#L673">ServeHTTP</a></h3>
<pre>func (server *<a href="index.html#Server">Server</a>) ServeHTTP(w <a href="../http/index.html">http</a>.<a href="../http/index.html#ResponseWriter">ResponseWriter</a>, req *<a href="../http/index.html">http</a>.<a href="../http/index.html#Request">Request</a>)</pre>
<p>
ServeHTTP implements an http.Handler that answers RPC requests.
</p>
<h3 id="Server.ServeRequest">func (*Server) <a href="http://localhost:6060/src/net/rpc/server.go?s=14536:14595#L474">ServeRequest</a></h3>
<pre>func (server *<a href="index.html#Server">Server</a>) ServeRequest(codec <a href="index.html#ServerCodec">ServerCodec</a>) <a href="../../builtin/index.html#error">error</a></pre>
<p>
ServeRequest is like ServeCodec but synchronously serves a single request.
It does not close the codec upon completion.
</p>
<h2 id="ServerCodec">type <a href="http://localhost:6060/src/net/rpc/server.go?s=19024:19260#L634">ServerCodec</a></h2>
<pre>type ServerCodec interface {
ReadRequestHeader(*<a href="index.html#Request">Request</a>) <a href="../../builtin/index.html#error">error</a>
ReadRequestBody(interface{}) <a href="../../builtin/index.html#error">error</a>
<span class="comment">// WriteResponse must be safe for concurrent use by multiple goroutines.</span>
WriteResponse(*<a href="index.html#Response">Response</a>, interface{}) <a href="../../builtin/index.html#error">error</a>
Close() <a href="../../builtin/index.html#error">error</a>
}</pre>
<p>
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. ReadRequestBody may be called with a nil
argument to force the body of the request to be read and discarded.
</p>
<h2 id="ServerError">type <a href="http://localhost:6060/src/net/rpc/client.go?s=365:388#L10">ServerError</a></h2>
<pre>type ServerError <a href="../../builtin/index.html#string">string</a></pre>
<p>
ServerError represents an error that has been returned from
the remote side of the RPC connection.
</p>
<h3 id="ServerError.Error">func (ServerError) <a href="http://localhost:6060/src/net/rpc/client.go?s=390:425#L12">Error</a></h3>
<pre>func (e <a href="index.html#ServerError">ServerError</a>) Error() <a href="../../builtin/index.html#string">string</a></pre>
<h2 id="pkg-subdirectories">Subdirectories</h2>
<div class="pkg-dir">
<table>
<tr>
<th class="pkg-name">Name</th>
<th class="pkg-synopsis">Synopsis</th>
</tr>
<tr>
<td colspan="2"><a href="../index.html">..</a></td>
</tr>
<tr>
<td class="pkg-name" style="padding-left: 0px;">
<a href="jsonrpc/index.html">jsonrpc</a>
</td>
<td class="pkg-synopsis">
Package jsonrpc implements a JSON-RPC ClientCodec and ServerCodec for the rpc package.
</td>
</tr>
</table>
</div>
<div id="footer">
Build version go1.6.<br>
Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
the content of this page is licensed under the
Creative Commons Attribution 3.0 License,
and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
<a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
<a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
</div>
</div><!-- .container -->
</div><!-- #page -->
<!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
<script type="text/javascript" src="../../../lib/godoc/jquery.js"></script>
<script type="text/javascript" src="../../../lib/godoc/jquery.treeview.js"></script>
<script type="text/javascript" src="../../../lib/godoc/jquery.treeview.edit.js"></script>
<script type="text/javascript" src="../../../lib/godoc/godocs.js"></script>
</body>
</html>