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

8 years ago
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <meta name="theme-color" content="#375EAB">
  7. <title>rpc - The Go Programming Language</title>
  8. <link type="text/css" rel="stylesheet" href="../../../lib/godoc/style.css">
  9. <link rel="stylesheet" href="../../../lib/godoc/jquery.treeview.css">
  10. <script type="text/javascript">window.initFuncs = [];</script>
  11. </head>
  12. <body>
  13. <div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
  14. ...
  15. </div><!-- #lowframe -->
  16. <div id="topbar" class="wide"><div class="container">
  17. <div class="top-heading" id="heading-wide"><a href="http://localhost:6060/">The Go Programming Language</a></div>
  18. <div class="top-heading" id="heading-narrow"><a href="http://localhost:6060/">Go</a></div>
  19. <a href="index.html#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
  20. <form method="GET" action="http://localhost:6060/search">
  21. <div id="menu">
  22. <a href="http://localhost:6060/doc/">Documents</a>
  23. <a href="http://localhost:6060/pkg/">Packages</a>
  24. <a href="http://localhost:6060/project/">The Project</a>
  25. <a href="http://localhost:6060/help/">Help</a>
  26. <a href="http://localhost:6060/blog/">Blog</a>
  27. <input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
  28. </div>
  29. </form>
  30. </div></div>
  31. <div id="page" class="wide">
  32. <div class="container">
  33. <h1>Package rpc</h1>
  34. <div id="nav"></div>
  35. <!--
  36. Copyright 2009 The Go Authors. All rights reserved.
  37. Use of this source code is governed by a BSD-style
  38. license that can be found in the LICENSE file.
  39. -->
  40. <!--
  41. Note: Static (i.e., not template-generated) href and id
  42. attributes start with "pkg-" to make it impossible for
  43. them to conflict with generated attributes (some of which
  44. correspond to Go identifiers).
  45. -->
  46. <script type='text/javascript'>
  47. document.ANALYSIS_DATA = null;
  48. document.CALLGRAPH = null;
  49. </script>
  50. <div id="short-nav">
  51. <dl>
  52. <dd><code>import "net/rpc"</code></dd>
  53. </dl>
  54. <dl>
  55. <dd><a href="index.html#pkg-overview" class="overviewLink">Overview</a></dd>
  56. <dd><a href="index.html#pkg-index" class="indexLink">Index</a></dd>
  57. <dd><a href="index.html#pkg-subdirectories">Subdirectories</a></dd>
  58. </dl>
  59. </div>
  60. <!-- The package's Name is printed as title by the top-level template -->
  61. <div id="pkg-overview" class="toggleVisible">
  62. <div class="collapsed">
  63. <h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
  64. </div>
  65. <div class="expanded">
  66. <h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
  67. <p>
  68. Package rpc provides access to the exported methods of an object across a
  69. network or other I/O connection. A server registers an object, making it visible
  70. as a service with the name of the type of the object. After registration, exported
  71. methods of the object will be accessible remotely. A server may register multiple
  72. objects (services) of different types but it is an error to register multiple
  73. objects of the same type.
  74. </p>
  75. <p>
  76. Only methods that satisfy these criteria will be made available for remote access;
  77. other methods will be ignored:
  78. </p>
  79. <pre>- the method&#39;s type is exported.
  80. - the method is exported.
  81. - the method has two arguments, both exported (or builtin) types.
  82. - the method&#39;s second argument is a pointer.
  83. - the method has return type error.
  84. </pre>
  85. <p>
  86. In effect, the method must look schematically like
  87. </p>
  88. <pre>func (t *T) MethodName(argType T1, replyType *T2) error
  89. </pre>
  90. <p>
  91. where T, T1 and T2 can be marshaled by encoding/gob.
  92. These requirements apply even if a different codec is used.
  93. (In the future, these requirements may soften for custom codecs.)
  94. </p>
  95. <p>
  96. The method&#39;s first argument represents the arguments provided by the caller; the
  97. second argument represents the result parameters to be returned to the caller.
  98. The method&#39;s return value, if non-nil, is passed back as a string that the client
  99. sees as if created by errors.New. If an error is returned, the reply parameter
  100. will not be sent back to the client.
  101. </p>
  102. <p>
  103. The server may handle requests on a single connection by calling ServeConn. More
  104. typically it will create a network listener and call Accept or, for an HTTP
  105. listener, HandleHTTP and http.Serve.
  106. </p>
  107. <p>
  108. A client wishing to use the service establishes a connection and then invokes
  109. NewClient on the connection. The convenience function Dial (DialHTTP) performs
  110. both steps for a raw network connection (an HTTP connection). The resulting
  111. Client object has two methods, Call and Go, that specify the service and method to
  112. call, a pointer containing the arguments, and a pointer to receive the result
  113. parameters.
  114. </p>
  115. <p>
  116. The Call method waits for the remote call to complete while the Go method
  117. launches the call asynchronously and signals completion using the Call
  118. structure&#39;s Done channel.
  119. </p>
  120. <p>
  121. Unless an explicit codec is set up, package encoding/gob is used to
  122. transport the data.
  123. </p>
  124. <p>
  125. Here is a simple example. A server wishes to export an object of type Arith:
  126. </p>
  127. <pre>package server
  128. type Args struct {
  129. A, B int
  130. }
  131. type Quotient struct {
  132. Quo, Rem int
  133. }
  134. type Arith int
  135. func (t *Arith) Multiply(args *Args, reply *int) error {
  136. *reply = args.A * args.B
  137. return nil
  138. }
  139. func (t *Arith) Divide(args *Args, quo *Quotient) error {
  140. if args.B == 0 {
  141. return errors.New(&#34;divide by zero&#34;)
  142. }
  143. quo.Quo = args.A / args.B
  144. quo.Rem = args.A % args.B
  145. return nil
  146. }
  147. </pre>
  148. <p>
  149. The server calls (for HTTP service):
  150. </p>
  151. <pre>arith := new(Arith)
  152. rpc.Register(arith)
  153. rpc.HandleHTTP()
  154. l, e := net.Listen(&#34;tcp&#34;, &#34;:1234&#34;)
  155. if e != nil {
  156. log.Fatal(&#34;listen error:&#34;, e)
  157. }
  158. go http.Serve(l, nil)
  159. </pre>
  160. <p>
  161. At this point, clients can see a service &#34;Arith&#34; with methods &#34;Arith.Multiply&#34; and
  162. &#34;Arith.Divide&#34;. To invoke one, a client first dials the server:
  163. </p>
  164. <pre>client, err := rpc.DialHTTP(&#34;tcp&#34;, serverAddress + &#34;:1234&#34;)
  165. if err != nil {
  166. log.Fatal(&#34;dialing:&#34;, err)
  167. }
  168. </pre>
  169. <p>
  170. Then it can make a remote call:
  171. </p>
  172. <pre>// Synchronous call
  173. args := &amp;server.Args{7,8}
  174. var reply int
  175. err = client.Call(&#34;Arith.Multiply&#34;, args, &amp;reply)
  176. if err != nil {
  177. log.Fatal(&#34;arith error:&#34;, err)
  178. }
  179. fmt.Printf(&#34;Arith: %d*%d=%d&#34;, args.A, args.B, reply)
  180. </pre>
  181. <p>
  182. or
  183. </p>
  184. <pre>// Asynchronous call
  185. quotient := new(Quotient)
  186. divCall := client.Go(&#34;Arith.Divide&#34;, args, quotient, nil)
  187. replyCall := &lt;-divCall.Done // will be equal to divCall
  188. // check errors, print, etc.
  189. </pre>
  190. <p>
  191. A server implementation will often provide a simple, type-safe wrapper for the
  192. client.
  193. </p>
  194. </div>
  195. </div>
  196. <div id="pkg-index" class="toggleVisible">
  197. <div class="collapsed">
  198. <h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
  199. </div>
  200. <div class="expanded">
  201. <h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
  202. <!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
  203. <div id="manual-nav">
  204. <dl>
  205. <dd><a href="index.html#pkg-constants">Constants</a></dd>
  206. <dd><a href="index.html#pkg-variables">Variables</a></dd>
  207. <dd><a href="index.html#Accept">func Accept(lis net.Listener)</a></dd>
  208. <dd><a href="index.html#HandleHTTP">func HandleHTTP()</a></dd>
  209. <dd><a href="index.html#Register">func Register(rcvr interface{}) error</a></dd>
  210. <dd><a href="index.html#RegisterName">func RegisterName(name string, rcvr interface{}) error</a></dd>
  211. <dd><a href="index.html#ServeCodec">func ServeCodec(codec ServerCodec)</a></dd>
  212. <dd><a href="index.html#ServeConn">func ServeConn(conn io.ReadWriteCloser)</a></dd>
  213. <dd><a href="index.html#ServeRequest">func ServeRequest(codec ServerCodec) error</a></dd>
  214. <dd><a href="index.html#Call">type Call</a></dd>
  215. <dd><a href="index.html#Client">type Client</a></dd>
  216. <dd>&nbsp; &nbsp; <a href="index.html#Dial">func Dial(network, address string) (*Client, error)</a></dd>
  217. <dd>&nbsp; &nbsp; <a href="index.html#DialHTTP">func DialHTTP(network, address string) (*Client, error)</a></dd>
  218. <dd>&nbsp; &nbsp; <a href="index.html#DialHTTPPath">func DialHTTPPath(network, address, path string) (*Client, error)</a></dd>
  219. <dd>&nbsp; &nbsp; <a href="index.html#NewClient">func NewClient(conn io.ReadWriteCloser) *Client</a></dd>
  220. <dd>&nbsp; &nbsp; <a href="index.html#NewClientWithCodec">func NewClientWithCodec(codec ClientCodec) *Client</a></dd>
  221. <dd>&nbsp; &nbsp; <a href="index.html#Client.Call">func (client *Client) Call(serviceMethod string, args interface{}, reply interface{}) error</a></dd>
  222. <dd>&nbsp; &nbsp; <a href="index.html#Client.Close">func (client *Client) Close() error</a></dd>
  223. <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>
  224. <dd><a href="index.html#ClientCodec">type ClientCodec</a></dd>
  225. <dd><a href="index.html#Request">type Request</a></dd>
  226. <dd><a href="index.html#Response">type Response</a></dd>
  227. <dd><a href="index.html#Server">type Server</a></dd>
  228. <dd>&nbsp; &nbsp; <a href="index.html#NewServer">func NewServer() *Server</a></dd>
  229. <dd>&nbsp; &nbsp; <a href="index.html#Server.Accept">func (server *Server) Accept(lis net.Listener)</a></dd>
  230. <dd>&nbsp; &nbsp; <a href="index.html#Server.HandleHTTP">func (server *Server) HandleHTTP(rpcPath, debugPath string)</a></dd>
  231. <dd>&nbsp; &nbsp; <a href="index.html#Server.Register">func (server *Server) Register(rcvr interface{}) error</a></dd>
  232. <dd>&nbsp; &nbsp; <a href="index.html#Server.RegisterName">func (server *Server) RegisterName(name string, rcvr interface{}) error</a></dd>
  233. <dd>&nbsp; &nbsp; <a href="index.html#Server.ServeCodec">func (server *Server) ServeCodec(codec ServerCodec)</a></dd>
  234. <dd>&nbsp; &nbsp; <a href="index.html#Server.ServeConn">func (server *Server) ServeConn(conn io.ReadWriteCloser)</a></dd>
  235. <dd>&nbsp; &nbsp; <a href="index.html#Server.ServeHTTP">func (server *Server) ServeHTTP(w http.ResponseWriter, req *http.Request)</a></dd>
  236. <dd>&nbsp; &nbsp; <a href="index.html#Server.ServeRequest">func (server *Server) ServeRequest(codec ServerCodec) error</a></dd>
  237. <dd><a href="index.html#ServerCodec">type ServerCodec</a></dd>
  238. <dd><a href="index.html#ServerError">type ServerError</a></dd>
  239. <dd>&nbsp; &nbsp; <a href="index.html#ServerError.Error">func (e ServerError) Error() string</a></dd>
  240. </dl>
  241. </div><!-- #manual-nav -->
  242. <h4>Package files</h4>
  243. <p>
  244. <span style="font-size:90%">
  245. <a href="http://localhost:6060/src/net/rpc/client.go">client.go</a>
  246. <a href="http://localhost:6060/src/net/rpc/debug.go">debug.go</a>
  247. <a href="http://localhost:6060/src/net/rpc/server.go">server.go</a>
  248. </span>
  249. </p>
  250. </div><!-- .expanded -->
  251. </div><!-- #pkg-index -->
  252. <div id="pkg-callgraph" class="toggle" style="display: none">
  253. <div class="collapsed">
  254. <h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
  255. </div> <!-- .expanded -->
  256. <div class="expanded">
  257. <h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
  258. <p>
  259. In the call graph viewer below, each node
  260. is a function belonging to this package
  261. and its children are the functions it
  262. calls&mdash;perhaps dynamically.
  263. </p>
  264. <p>
  265. The root nodes are the entry points of the
  266. package: functions that may be called from
  267. outside the package.
  268. There may be non-exported or anonymous
  269. functions among them if they are called
  270. dynamically from another package.
  271. </p>
  272. <p>
  273. Click a node to visit that function's source code.
  274. From there you can visit its callers by
  275. clicking its declaring <code>func</code>
  276. token.
  277. </p>
  278. <p>
  279. Functions may be omitted if they were
  280. determined to be unreachable in the
  281. particular programs or tests that were
  282. analyzed.
  283. </p>
  284. <!-- Zero means show all package entry points. -->
  285. <ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
  286. </div>
  287. </div> <!-- #pkg-callgraph -->
  288. <h2 id="pkg-constants">Constants</h2>
  289. <pre>const (
  290. <span class="comment">// Defaults used by HandleHTTP</span>
  291. <span id="DefaultRPCPath">DefaultRPCPath</span> = &#34;/_goRPC_&#34;
  292. <span id="DefaultDebugPath">DefaultDebugPath</span> = &#34;/debug/rpc&#34;
  293. )</pre>
  294. <h2 id="pkg-variables">Variables</h2>
  295. <pre>var <span id="DefaultServer">DefaultServer</span> = <a href="index.html#NewServer">NewServer</a>()</pre>
  296. <p>
  297. DefaultServer is the default instance of *Server.
  298. </p>
  299. <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>
  300. <h2 id="Accept">func <a href="http://localhost:6060/src/net/rpc/server.go?s=20231:20260#L667">Accept</a></h2>
  301. <pre>func Accept(lis <a href="../index.html">net</a>.<a href="../index.html#Listener">Listener</a>)</pre>
  302. <p>
  303. Accept accepts connections on the listener and serves requests
  304. to DefaultServer for each incoming connection.
  305. Accept blocks; the caller typically invokes it in a go statement.
  306. </p>
  307. <h2 id="HandleHTTP">func <a href="http://localhost:6060/src/net/rpc/server.go?s=21491:21508#L700">HandleHTTP</a></h2>
  308. <pre>func HandleHTTP()</pre>
  309. <p>
  310. HandleHTTP registers an HTTP handler for RPC messages to DefaultServer
  311. on DefaultRPCPath and a debugging handler on DefaultDebugPath.
  312. It is still necessary to invoke http.Serve(), typically in a go statement.
  313. </p>
  314. <h2 id="Register">func <a href="http://localhost:6060/src/net/rpc/server.go?s=18260:18297#L619">Register</a></h2>
  315. <pre>func Register(rcvr interface{}) <a href="../../builtin/index.html#error">error</a></pre>
  316. <p>
  317. Register publishes the receiver&#39;s methods in the DefaultServer.
  318. </p>
  319. <h2 id="RegisterName">func <a href="http://localhost:6060/src/net/rpc/server.go?s=18456:18510#L623">RegisterName</a></h2>
  320. <pre>func RegisterName(name <a href="../../builtin/index.html#string">string</a>, rcvr interface{}) <a href="../../builtin/index.html#error">error</a></pre>
  321. <p>
  322. RegisterName is like Register but uses the provided name for the type
  323. instead of the receiver&#39;s concrete type.
  324. </p>
  325. <h2 id="ServeCodec">func <a href="http://localhost:6060/src/net/rpc/server.go?s=19757:19791#L654">ServeCodec</a></h2>
  326. <pre>func ServeCodec(codec <a href="index.html#ServerCodec">ServerCodec</a>)</pre>
  327. <p>
  328. ServeCodec is like ServeConn but uses the specified codec to
  329. decode requests and encode responses.
  330. </p>
  331. <h2 id="ServeConn">func <a href="http://localhost:6060/src/net/rpc/server.go?s=19576:19615#L648">ServeConn</a></h2>
  332. <pre>func ServeConn(conn <a href="../../io/index.html">io</a>.<a href="../../io/index.html#ReadWriteCloser">ReadWriteCloser</a>)</pre>
  333. <p>
  334. ServeConn runs the DefaultServer on a single connection.
  335. ServeConn blocks, serving the connection until the client hangs up.
  336. The caller typically invokes ServeConn in a go statement.
  337. ServeConn uses the gob wire format (see package gob) on the
  338. connection. To use an alternate codec, use ServeCodec.
  339. </p>
  340. <h2 id="ServeRequest">func <a href="http://localhost:6060/src/net/rpc/server.go?s=19956:19998#L660">ServeRequest</a></h2>
  341. <pre>func ServeRequest(codec <a href="index.html#ServerCodec">ServerCodec</a>) <a href="../../builtin/index.html#error">error</a></pre>
  342. <p>
  343. ServeRequest is like ServeCodec but synchronously serves a single request.
  344. It does not close the codec upon completion.
  345. </p>
  346. <h2 id="Call">type <a href="http://localhost:6060/src/net/rpc/client.go?s=540:900#L19">Call</a></h2>
  347. <pre>type Call struct {
  348. ServiceMethod <a href="../../builtin/index.html#string">string</a> <span class="comment">// The name of the service and method to call.</span>
  349. Args interface{} <span class="comment">// The argument to the function (*struct).</span>
  350. Reply interface{} <span class="comment">// The reply from the function (*struct).</span>
  351. Error <a href="../../builtin/index.html#error">error</a> <span class="comment">// After completion, the error status.</span>
  352. Done chan *<a href="index.html#Call">Call</a> <span class="comment">// Strobes when call is complete.</span>
  353. }</pre>
  354. <p>
  355. Call represents an active RPC.
  356. </p>
  357. <h2 id="Client">type <a href="http://localhost:6060/src/net/rpc/client.go?s=1084:1360#L31">Client</a></h2>
  358. <pre>type Client struct {
  359. <span class="comment">// contains filtered or unexported fields</span>
  360. }</pre>
  361. <p>
  362. Client represents an RPC Client.
  363. There may be multiple outstanding Calls associated
  364. with a single Client, and a Client may be used by
  365. multiple goroutines simultaneously.
  366. </p>
  367. <h3 id="Dial">func <a href="http://localhost:6060/src/net/rpc/client.go?s=7222:7273#L259">Dial</a></h3>
  368. <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>
  369. <p>
  370. Dial connects to an RPC server at the specified network address.
  371. </p>
  372. <h3 id="DialHTTP">func <a href="http://localhost:6060/src/net/rpc/client.go?s=6275:6330#L226">DialHTTP</a></h3>
  373. <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>
  374. <p>
  375. DialHTTP connects to an HTTP RPC server at the specified network address
  376. listening on the default HTTP RPC path.
  377. </p>
  378. <h3 id="DialHTTPPath">func <a href="http://localhost:6060/src/net/rpc/client.go?s=6484:6549#L232">DialHTTPPath</a></h3>
  379. <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>
  380. <p>
  381. DialHTTPPath connects to an HTTP RPC server
  382. at the specified network address and path.
  383. </p>
  384. <h3 id="NewClient">func <a href="http://localhost:6060/src/net/rpc/client.go?s=5073:5120#L178">NewClient</a></h3>
  385. <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>
  386. <p>
  387. NewClient returns a new Client to handle requests to the
  388. set of services at the other end of the connection.
  389. It adds a buffer to the write side of the connection so
  390. the header and payload are sent as a unit.
  391. </p>
  392. <h3 id="NewClientWithCodec">func <a href="http://localhost:6060/src/net/rpc/client.go?s=5394:5444#L186">NewClientWithCodec</a></h3>
  393. <pre>func NewClientWithCodec(codec <a href="index.html#ClientCodec">ClientCodec</a>) *<a href="index.html#Client">Client</a></pre>
  394. <p>
  395. NewClientWithCodec is like NewClient but uses the specified
  396. codec to encode requests and decode responses.
  397. </p>
  398. <h3 id="Client.Call">func (*Client) <a href="http://localhost:6060/src/net/rpc/client.go?s=8636:8727#L304">Call</a></h3>
  399. <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>
  400. <p>
  401. Call invokes the named function, waits for it to complete, and returns its error status.
  402. </p>
  403. <h3 id="Client.Close">func (*Client) <a href="http://localhost:6060/src/net/rpc/client.go?s=7387:7422#L267">Close</a></h3>
  404. <pre>func (client *<a href="index.html#Client">Client</a>) Close() <a href="../../builtin/index.html#error">error</a></pre>
  405. <h3 id="Client.Go">func (*Client) <a href="http://localhost:6060/src/net/rpc/client.go?s=7910:8016#L282">Go</a></h3>
  406. <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>
  407. <p>
  408. Go invokes the function asynchronously. It returns the Call structure representing
  409. the invocation. The done channel will signal when the call is complete by returning
  410. the same Call object. If done is nil, Go will allocate a new channel.
  411. If non-nil, done must be buffered or Go will deliberately crash.
  412. </p>
  413. <h2 id="ClientCodec">type <a href="http://localhost:6060/src/net/rpc/client.go?s=1823:2059#L52">ClientCodec</a></h2>
  414. <pre>type ClientCodec interface {
  415. <span class="comment">// WriteRequest must be safe for concurrent use by multiple goroutines.</span>
  416. WriteRequest(*<a href="index.html#Request">Request</a>, interface{}) <a href="../../builtin/index.html#error">error</a>
  417. ReadResponseHeader(*<a href="index.html#Response">Response</a>) <a href="../../builtin/index.html#error">error</a>
  418. ReadResponseBody(interface{}) <a href="../../builtin/index.html#error">error</a>
  419. Close() <a href="../../builtin/index.html#error">error</a>
  420. }</pre>
  421. <p>
  422. A ClientCodec implements writing of RPC requests and
  423. reading of RPC responses for the client side of an RPC session.
  424. The client calls WriteRequest to write a request to the connection
  425. and calls ReadResponseHeader and ReadResponseBody in pairs
  426. to read responses. The client calls Close when finished with the
  427. connection. ReadResponseBody may be called with a nil
  428. argument to force the body of the response to be read and then
  429. discarded.
  430. </p>
  431. <h2 id="Request">type <a href="http://localhost:6060/src/net/rpc/server.go?s=5048:5234#L158">Request</a></h2>
  432. <pre>type Request struct {
  433. ServiceMethod <a href="../../builtin/index.html#string">string</a> <span class="comment">// format: &#34;Service.Method&#34;</span>
  434. Seq <a href="../../builtin/index.html#uint64">uint64</a> <span class="comment">// sequence number chosen by client</span>
  435. <span class="comment">// contains filtered or unexported fields</span>
  436. }</pre>
  437. <p>
  438. Request is a header written before every RPC call. It is used internally
  439. but documented here as an aid to debugging, such as when analyzing
  440. network traffic.
  441. </p>
  442. <h2 id="Response">type <a href="http://localhost:6060/src/net/rpc/server.go?s=5406:5635#L167">Response</a></h2>
  443. <pre>type Response struct {
  444. ServiceMethod <a href="../../builtin/index.html#string">string</a> <span class="comment">// echoes that of the Request</span>
  445. Seq <a href="../../builtin/index.html#uint64">uint64</a> <span class="comment">// echoes that of the request</span>
  446. Error <a href="../../builtin/index.html#string">string</a> <span class="comment">// error, if any.</span>
  447. <span class="comment">// contains filtered or unexported fields</span>
  448. }</pre>
  449. <p>
  450. Response is a header written before every RPC return. It is used internally
  451. but documented here as an aid to debugging, such as when analyzing
  452. network traffic.
  453. </p>
  454. <h2 id="Server">type <a href="http://localhost:6060/src/net/rpc/server.go?s=5673:5909#L175">Server</a></h2>
  455. <pre>type Server struct {
  456. <span class="comment">// contains filtered or unexported fields</span>
  457. }</pre>
  458. <p>
  459. Server represents an RPC Server.
  460. </p>
  461. <h3 id="NewServer">func <a href="http://localhost:6060/src/net/rpc/server.go?s=5946:5970#L185">NewServer</a></h3>
  462. <pre>func NewServer() *<a href="index.html#Server">Server</a></pre>
  463. <p>
  464. NewServer returns a new Server.
  465. </p>
  466. <h3 id="Server.Accept">func (*Server) <a href="http://localhost:6060/src/net/rpc/server.go?s=17995:18041#L607">Accept</a></h3>
  467. <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>
  468. <p>
  469. Accept accepts connections on the listener and serves requests
  470. for each incoming connection. Accept blocks until the listener
  471. returns a non-nil error. The caller typically invokes Accept in a
  472. go statement.
  473. </p>
  474. <h3 id="Server.HandleHTTP">func (*Server) <a href="http://localhost:6060/src/net/rpc/server.go?s=21135:21194#L692">HandleHTTP</a></h3>
  475. <pre>func (server *<a href="index.html#Server">Server</a>) HandleHTTP(rpcPath, debugPath <a href="../../builtin/index.html#string">string</a>)</pre>
  476. <p>
  477. HandleHTTP registers an HTTP handler for RPC messages on rpcPath,
  478. and a debugging handler on debugPath.
  479. It is still necessary to invoke http.Serve(), typically in a go statement.
  480. </p>
  481. <h3 id="Server.Register">func (*Server) <a href="http://localhost:6060/src/net/rpc/server.go?s=7103:7157#L218">Register</a></h3>
  482. <pre>func (server *<a href="index.html#Server">Server</a>) Register(rcvr interface{}) <a href="../../builtin/index.html#error">error</a></pre>
  483. <p>
  484. Register publishes in the server the set of methods of the
  485. receiver value that satisfy the following conditions:
  486. </p>
  487. <pre>- exported method of exported type
  488. - two arguments, both of exported type
  489. - the second argument is a pointer
  490. - one return value, of type error
  491. </pre>
  492. <p>
  493. It returns an error if the receiver is not an exported type or has
  494. no suitable methods. It also logs the error using package log.
  495. The client accesses each method using a string of the form &#34;Type.Method&#34;,
  496. where Type is the receiver&#39;s concrete type.
  497. </p>
  498. <h3 id="Server.RegisterName">func (*Server) <a href="http://localhost:6060/src/net/rpc/server.go?s=7321:7392#L224">RegisterName</a></h3>
  499. <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>
  500. <p>
  501. RegisterName is like Register but uses the provided name for the type
  502. instead of the receiver&#39;s concrete type.
  503. </p>
  504. <h3 id="Server.ServeCodec">func (*Server) <a href="http://localhost:6060/src/net/rpc/server.go?s=13822:13873#L449">ServeCodec</a></h3>
  505. <pre>func (server *<a href="index.html#Server">Server</a>) ServeCodec(codec <a href="index.html#ServerCodec">ServerCodec</a>)</pre>
  506. <p>
  507. ServeCodec is like ServeConn but uses the specified codec to
  508. decode requests and encode responses.
  509. </p>
  510. <h3 id="Server.ServeConn">func (*Server) <a href="http://localhost:6060/src/net/rpc/server.go?s=13479:13535#L436">ServeConn</a></h3>
  511. <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>
  512. <p>
  513. ServeConn runs the server on a single connection.
  514. ServeConn blocks, serving the connection until the client hangs up.
  515. The caller typically invokes ServeConn in a go statement.
  516. ServeConn uses the gob wire format (see package gob) on the
  517. connection. To use an alternate codec, use ServeCodec.
  518. </p>
  519. <h3 id="Server.ServeHTTP">func (*Server) <a href="http://localhost:6060/src/net/rpc/server.go?s=20463:20536#L673">ServeHTTP</a></h3>
  520. <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>
  521. <p>
  522. ServeHTTP implements an http.Handler that answers RPC requests.
  523. </p>
  524. <h3 id="Server.ServeRequest">func (*Server) <a href="http://localhost:6060/src/net/rpc/server.go?s=14536:14595#L474">ServeRequest</a></h3>
  525. <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>
  526. <p>
  527. ServeRequest is like ServeCodec but synchronously serves a single request.
  528. It does not close the codec upon completion.
  529. </p>
  530. <h2 id="ServerCodec">type <a href="http://localhost:6060/src/net/rpc/server.go?s=19024:19260#L634">ServerCodec</a></h2>
  531. <pre>type ServerCodec interface {
  532. ReadRequestHeader(*<a href="index.html#Request">Request</a>) <a href="../../builtin/index.html#error">error</a>
  533. ReadRequestBody(interface{}) <a href="../../builtin/index.html#error">error</a>
  534. <span class="comment">// WriteResponse must be safe for concurrent use by multiple goroutines.</span>
  535. WriteResponse(*<a href="index.html#Response">Response</a>, interface{}) <a href="../../builtin/index.html#error">error</a>
  536. Close() <a href="../../builtin/index.html#error">error</a>
  537. }</pre>
  538. <p>
  539. A ServerCodec implements reading of RPC requests and writing of
  540. RPC responses for the server side of an RPC session.
  541. The server calls ReadRequestHeader and ReadRequestBody in pairs
  542. to read requests from the connection, and it calls WriteResponse to
  543. write a response back. The server calls Close when finished with the
  544. connection. ReadRequestBody may be called with a nil
  545. argument to force the body of the request to be read and discarded.
  546. </p>
  547. <h2 id="ServerError">type <a href="http://localhost:6060/src/net/rpc/client.go?s=365:388#L10">ServerError</a></h2>
  548. <pre>type ServerError <a href="../../builtin/index.html#string">string</a></pre>
  549. <p>
  550. ServerError represents an error that has been returned from
  551. the remote side of the RPC connection.
  552. </p>
  553. <h3 id="ServerError.Error">func (ServerError) <a href="http://localhost:6060/src/net/rpc/client.go?s=390:425#L12">Error</a></h3>
  554. <pre>func (e <a href="index.html#ServerError">ServerError</a>) Error() <a href="../../builtin/index.html#string">string</a></pre>
  555. <h2 id="pkg-subdirectories">Subdirectories</h2>
  556. <div class="pkg-dir">
  557. <table>
  558. <tr>
  559. <th class="pkg-name">Name</th>
  560. <th class="pkg-synopsis">Synopsis</th>
  561. </tr>
  562. <tr>
  563. <td colspan="2"><a href="../index.html">..</a></td>
  564. </tr>
  565. <tr>
  566. <td class="pkg-name" style="padding-left: 0px;">
  567. <a href="jsonrpc/index.html">jsonrpc</a>
  568. </td>
  569. <td class="pkg-synopsis">
  570. Package jsonrpc implements a JSON-RPC ClientCodec and ServerCodec for the rpc package.
  571. </td>
  572. </tr>
  573. </table>
  574. </div>
  575. <div id="footer">
  576. Build version go1.6.<br>
  577. Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
  578. the content of this page is licensed under the
  579. Creative Commons Attribution 3.0 License,
  580. and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
  581. <a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
  582. <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
  583. </div>
  584. </div><!-- .container -->
  585. </div><!-- #page -->
  586. <!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
  587. <script type="text/javascript" src="../../../lib/godoc/jquery.js"></script>
  588. <script type="text/javascript" src="../../../lib/godoc/jquery.treeview.js"></script>
  589. <script type="text/javascript" src="../../../lib/godoc/jquery.treeview.edit.js"></script>
  590. <script type="text/javascript" src="../../../lib/godoc/godocs.js"></script>
  591. </body>
  592. </html>