server.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. /*
  5. Package rpc is a trimmed down version of net/rpc in the standard library.
  6. Original doc:
  7. Package rpc provides access to the exported methods of an object across a
  8. network or other I/O connection. A server registers an object, making it visible
  9. as a service with the name of the type of the object. After registration, exported
  10. methods of the object will be accessible remotely. A server may register multiple
  11. objects (services) of different types but it is an error to register multiple
  12. objects of the same type.
  13. Only methods that satisfy these criteria will be made available for remote access;
  14. other methods will be ignored:
  15. - the method's type is exported.
  16. - the method is exported.
  17. - the method has two arguments, both exported (or builtin) types.
  18. - the method's second argument is a pointer.
  19. - the method has return type error.
  20. In effect, the method must look schematically like
  21. func (t *T) MethodName(argType T1, replyType *T2) error
  22. where T1 and T2 can be marshaled by encoding/gob.
  23. These requirements apply even if a different codec is used.
  24. (In the future, these requirements may soften for custom codecs.)
  25. The method's first argument represents the arguments provided by the caller; the
  26. second argument represents the result parameters to be returned to the caller.
  27. The method's return value, if non-nil, is passed back as a string that the client
  28. sees as if created by errors.New. If an error is returned, the reply parameter
  29. will not be sent back to the client.
  30. The server may handle requests on a single connection by calling ServeConn. More
  31. typically it will create a network listener and call Accept or, for an HTTP
  32. listener, HandleHTTP and http.Serve.
  33. A client wishing to use the service establishes a connection and then invokes
  34. NewClient on the connection. The convenience function Dial (DialHTTP) performs
  35. both steps for a raw network connection (an HTTP connection). The resulting
  36. Client object has two methods, Call and Go, that specify the service and method to
  37. call, a pointer containing the arguments, and a pointer to receive the result
  38. parameters.
  39. The Call method waits for the remote call to complete while the Go method
  40. launches the call asynchronously and signals completion using the Call
  41. structure's Done channel.
  42. Unless an explicit codec is set up, package encoding/gob is used to
  43. transport the data.
  44. Here is a simple example. A server wishes to export an object of type Arith:
  45. package server
  46. import "errors"
  47. type Args struct {
  48. A, B int
  49. }
  50. type Quotient struct {
  51. Quo, Rem int
  52. }
  53. type Arith int
  54. func (t *Arith) Multiply(args *Args, reply *int) error {
  55. *reply = args.A * args.B
  56. return nil
  57. }
  58. func (t *Arith) Divide(args *Args, quo *Quotient) error {
  59. if args.B == 0 {
  60. return errors.New("divide by zero")
  61. }
  62. quo.Quo = args.A / args.B
  63. quo.Rem = args.A % args.B
  64. return nil
  65. }
  66. The server calls (for HTTP service):
  67. arith := new(Arith)
  68. rpc.Register(arith)
  69. rpc.HandleHTTP()
  70. l, e := net.Listen("tcp", ":1234")
  71. if e != nil {
  72. log.Fatal("listen error:", e)
  73. }
  74. go http.Serve(l, nil)
  75. At this point, clients can see a service "Arith" with methods "Arith.Multiply" and
  76. "Arith.Divide". To invoke one, a client first dials the server:
  77. client, err := rpc.DialHTTP("tcp", serverAddress + ":1234")
  78. if err != nil {
  79. log.Fatal("dialing:", err)
  80. }
  81. Then it can make a remote call:
  82. // Synchronous call
  83. args := &server.Args{7,8}
  84. var reply int
  85. err = client.Call("Arith.Multiply", args, &reply)
  86. if err != nil {
  87. log.Fatal("arith error:", err)
  88. }
  89. fmt.Printf("Arith: %d*%d=%d", args.A, args.B, reply)
  90. or
  91. // Asynchronous call
  92. quotient := new(Quotient)
  93. divCall := client.Go("Arith.Divide", args, quotient, nil)
  94. replyCall := <-divCall.Done // will be equal to divCall
  95. // check errors, print, etc.
  96. A server implementation will often provide a simple, type-safe wrapper for the
  97. client.
  98. The net/rpc package is frozen and is not accepting new features.
  99. */
  100. package rpc
  101. import (
  102. "bufio"
  103. "encoding/gob"
  104. "errors"
  105. "go/token"
  106. "io"
  107. "log"
  108. "net"
  109. "reflect"
  110. "strings"
  111. "sync"
  112. )
  113. // Precompute the reflect type for error. Can't use error directly
  114. // because Typeof takes an empty interface value. This is annoying.
  115. var typeOfError = reflect.TypeOf((*error)(nil)).Elem()
  116. type methodType struct {
  117. sync.Mutex // protects counters
  118. method reflect.Method
  119. ArgType reflect.Type
  120. ReplyType reflect.Type
  121. numCalls uint
  122. }
  123. type service struct {
  124. name string // name of service
  125. rcvr reflect.Value // receiver of methods for the service
  126. typ reflect.Type // type of the receiver
  127. method map[string]*methodType // registered methods
  128. }
  129. // Request is a header written before every RPC call. It is used internally
  130. // but documented here as an aid to debugging, such as when analyzing
  131. // network traffic.
  132. type Request struct {
  133. ServiceMethod string // format: "Service.Method"
  134. Seq uint64 // sequence number chosen by client
  135. next *Request // for free list in Server
  136. }
  137. // Response is a header written before every RPC return. It is used internally
  138. // but documented here as an aid to debugging, such as when analyzing
  139. // network traffic.
  140. type Response struct {
  141. ServiceMethod string // echoes that of the Request
  142. Seq uint64 // echoes that of the request
  143. Error string // error, if any.
  144. next *Response // for free list in Server
  145. }
  146. // Server represents an RPC Server.
  147. type Server struct {
  148. serviceMap sync.Map // map[string]*service
  149. reqLock sync.Mutex // protects freeReq
  150. freeReq *Request
  151. respLock sync.Mutex // protects freeResp
  152. freeResp *Response
  153. }
  154. // NewServer returns a new Server.
  155. func NewServer() *Server {
  156. return &Server{}
  157. }
  158. // DefaultServer is the default instance of *Server.
  159. var DefaultServer = NewServer()
  160. // Is this type exported or a builtin?
  161. func isExportedOrBuiltinType(t reflect.Type) bool {
  162. for t.Kind() == reflect.Ptr {
  163. t = t.Elem()
  164. }
  165. // PkgPath will be non-empty even for an exported type,
  166. // so we need to check the type name as well.
  167. return token.IsExported(t.Name()) || t.PkgPath() == ""
  168. }
  169. // Register publishes in the server the set of methods of the
  170. // receiver value that satisfy the following conditions:
  171. // - exported method of exported type
  172. // - two arguments, both of exported type
  173. // - the second argument is a pointer
  174. // - one return value, of type error
  175. //
  176. // It returns an error if the receiver is not an exported type or has
  177. // no suitable methods. It also logs the error using package log.
  178. // The client accesses each method using a string of the form "Type.Method",
  179. // where Type is the receiver's concrete type.
  180. func (server *Server) Register(rcvr any) error {
  181. return server.register(rcvr, "", false)
  182. }
  183. // RegisterName is like Register but uses the provided name for the type
  184. // instead of the receiver's concrete type.
  185. func (server *Server) RegisterName(name string, rcvr any) error {
  186. return server.register(rcvr, name, true)
  187. }
  188. func (server *Server) register(rcvr any, name string, useName bool) error {
  189. s := new(service)
  190. s.typ = reflect.TypeOf(rcvr)
  191. s.rcvr = reflect.ValueOf(rcvr)
  192. sname := reflect.Indirect(s.rcvr).Type().Name()
  193. if useName {
  194. sname = name
  195. }
  196. if sname == "" {
  197. s := "rpc.Register: no service name for type " + s.typ.String()
  198. log.Print(s)
  199. return errors.New(s)
  200. }
  201. if !token.IsExported(sname) && !useName {
  202. s := "rpc.Register: type " + sname + " is not exported"
  203. log.Print(s)
  204. return errors.New(s)
  205. }
  206. s.name = sname
  207. // Install the methods
  208. s.method = suitableMethods(s.typ, true)
  209. if len(s.method) == 0 {
  210. str := ""
  211. // To help the user, see if a pointer receiver would work.
  212. method := suitableMethods(reflect.PtrTo(s.typ), false)
  213. if len(method) != 0 {
  214. str = "rpc.Register: type " + sname + " has no exported methods of suitable type (hint: pass a pointer to value of that type)"
  215. } else {
  216. str = "rpc.Register: type " + sname + " has no exported methods of suitable type"
  217. }
  218. log.Print(str)
  219. return errors.New(str)
  220. }
  221. if _, dup := server.serviceMap.LoadOrStore(sname, s); dup {
  222. return errors.New("rpc: service already defined: " + sname)
  223. }
  224. return nil
  225. }
  226. // suitableMethods returns suitable Rpc methods of typ, it will report
  227. // error using log if reportErr is true.
  228. func suitableMethods(typ reflect.Type, reportErr bool) map[string]*methodType {
  229. methods := make(map[string]*methodType)
  230. for m := 0; m < typ.NumMethod(); m++ {
  231. method := typ.Method(m)
  232. mtype := method.Type
  233. mname := method.Name
  234. // Method must be exported.
  235. if method.PkgPath != "" {
  236. continue
  237. }
  238. // Method needs three ins: receiver, *args, *reply.
  239. if mtype.NumIn() != 3 {
  240. if reportErr {
  241. log.Printf("rpc.Register: method %q has %d input parameters; needs exactly three\n", mname, mtype.NumIn())
  242. }
  243. continue
  244. }
  245. // First arg need not be a pointer.
  246. argType := mtype.In(1)
  247. if !isExportedOrBuiltinType(argType) {
  248. if reportErr {
  249. log.Printf("rpc.Register: argument type of method %q is not exported: %q\n", mname, argType)
  250. }
  251. continue
  252. }
  253. // Second arg must be a pointer.
  254. replyType := mtype.In(2)
  255. if replyType.Kind() != reflect.Ptr {
  256. if reportErr {
  257. log.Printf("rpc.Register: reply type of method %q is not a pointer: %q\n", mname, replyType)
  258. }
  259. continue
  260. }
  261. // Reply type must be exported.
  262. if !isExportedOrBuiltinType(replyType) {
  263. if reportErr {
  264. log.Printf("rpc.Register: reply type of method %q is not exported: %q\n", mname, replyType)
  265. }
  266. continue
  267. }
  268. // Method needs one out.
  269. if mtype.NumOut() != 1 {
  270. if reportErr {
  271. log.Printf("rpc.Register: method %q has %d output parameters; needs exactly one\n", mname, mtype.NumOut())
  272. }
  273. continue
  274. }
  275. // The return type of the method must be error.
  276. if returnType := mtype.Out(0); returnType != typeOfError {
  277. if reportErr {
  278. log.Printf("rpc.Register: return type of method %q is %q, must be error\n", mname, returnType)
  279. }
  280. continue
  281. }
  282. methods[mname] = &methodType{method: method, ArgType: argType, ReplyType: replyType}
  283. }
  284. return methods
  285. }
  286. // A value sent as a placeholder for the server's response value when the server
  287. // receives an invalid request. It is never decoded by the client since the Response
  288. // contains an error when it is used.
  289. var invalidRequest = struct{}{}
  290. func (server *Server) sendResponse(sending *sync.Mutex, req *Request, reply any, codec ServerCodec, errmsg string) {
  291. resp := server.getResponse()
  292. // Encode the response header
  293. resp.ServiceMethod = req.ServiceMethod
  294. if errmsg != "" {
  295. resp.Error = errmsg
  296. reply = invalidRequest
  297. }
  298. resp.Seq = req.Seq
  299. sending.Lock()
  300. err := codec.WriteResponse(resp, reply)
  301. if debugLog && err != nil {
  302. log.Println("rpc: writing response:", err)
  303. }
  304. sending.Unlock()
  305. server.freeResponse(resp)
  306. }
  307. func (m *methodType) NumCalls() (n uint) {
  308. m.Lock()
  309. n = m.numCalls
  310. m.Unlock()
  311. return n
  312. }
  313. func (s *service) call(server *Server, sending *sync.Mutex, wg *sync.WaitGroup, mtype *methodType, req *Request, argv, replyv reflect.Value, codec ServerCodec) {
  314. if wg != nil {
  315. defer wg.Done()
  316. }
  317. mtype.Lock()
  318. mtype.numCalls++
  319. mtype.Unlock()
  320. function := mtype.method.Func
  321. // Invoke the method, providing a new value for the reply.
  322. returnValues := function.Call([]reflect.Value{s.rcvr, argv, replyv})
  323. // The return value for the method is an error.
  324. errInter := returnValues[0].Interface()
  325. errmsg := ""
  326. if errInter != nil {
  327. errmsg = errInter.(error).Error()
  328. }
  329. server.sendResponse(sending, req, replyv.Interface(), codec, errmsg)
  330. server.freeRequest(req)
  331. }
  332. type gobServerCodec struct {
  333. rwc io.ReadWriteCloser
  334. dec *gob.Decoder
  335. enc *gob.Encoder
  336. encBuf *bufio.Writer
  337. closed bool
  338. }
  339. func (c *gobServerCodec) ReadRequestHeader(r *Request) error {
  340. return c.dec.Decode(r)
  341. }
  342. func (c *gobServerCodec) ReadRequestBody(body any) error {
  343. return c.dec.Decode(body)
  344. }
  345. func (c *gobServerCodec) WriteResponse(r *Response, body any) (err error) {
  346. if err = c.enc.Encode(r); err != nil {
  347. if c.encBuf.Flush() == nil {
  348. // Gob couldn't encode the header. Should not happen, so if it does,
  349. // shut down the connection to signal that the connection is broken.
  350. log.Println("rpc: gob error encoding response:", err)
  351. c.Close()
  352. }
  353. return
  354. }
  355. if err = c.enc.Encode(body); err != nil {
  356. if c.encBuf.Flush() == nil {
  357. // Was a gob problem encoding the body but the header has been written.
  358. // Shut down the connection to signal that the connection is broken.
  359. log.Println("rpc: gob error encoding body:", err)
  360. c.Close()
  361. }
  362. return
  363. }
  364. return c.encBuf.Flush()
  365. }
  366. func (c *gobServerCodec) Close() error {
  367. if c.closed {
  368. // Only call c.rwc.Close once; otherwise the semantics are undefined.
  369. return nil
  370. }
  371. c.closed = true
  372. return c.rwc.Close()
  373. }
  374. // ServeConn runs the server on a single connection.
  375. // ServeConn blocks, serving the connection until the client hangs up.
  376. // The caller typically invokes ServeConn in a go statement.
  377. // ServeConn uses the gob wire format (see package gob) on the
  378. // connection. To use an alternate codec, use ServeCodec.
  379. // See NewClient's comment for information about concurrent access.
  380. func (server *Server) ServeConn(conn io.ReadWriteCloser) {
  381. buf := bufio.NewWriter(conn)
  382. srv := &gobServerCodec{
  383. rwc: conn,
  384. dec: gob.NewDecoder(conn),
  385. enc: gob.NewEncoder(buf),
  386. encBuf: buf,
  387. }
  388. server.ServeCodec(srv)
  389. }
  390. // ServeCodec is like ServeConn but uses the specified codec to
  391. // decode requests and encode responses.
  392. func (server *Server) ServeCodec(codec ServerCodec) {
  393. sending := new(sync.Mutex)
  394. wg := new(sync.WaitGroup)
  395. for {
  396. service, mtype, req, argv, replyv, keepReading, err := server.readRequest(codec)
  397. if err != nil {
  398. if debugLog && err != io.EOF {
  399. log.Println("rpc:", err)
  400. }
  401. if !keepReading {
  402. break
  403. }
  404. // send a response if we actually managed to read a header.
  405. if req != nil {
  406. server.sendResponse(sending, req, invalidRequest, codec, err.Error())
  407. server.freeRequest(req)
  408. }
  409. continue
  410. }
  411. wg.Add(1)
  412. go service.call(server, sending, wg, mtype, req, argv, replyv, codec)
  413. }
  414. // We've seen that there are no more requests.
  415. // Wait for responses to be sent before closing codec.
  416. wg.Wait()
  417. codec.Close()
  418. }
  419. // ServeRequest is like ServeCodec but synchronously serves a single request.
  420. // It does not close the codec upon completion.
  421. func (server *Server) ServeRequest(codec ServerCodec) error {
  422. sending := new(sync.Mutex)
  423. service, mtype, req, argv, replyv, keepReading, err := server.readRequest(codec)
  424. if err != nil {
  425. if !keepReading {
  426. return err
  427. }
  428. // send a response if we actually managed to read a header.
  429. if req != nil {
  430. server.sendResponse(sending, req, invalidRequest, codec, err.Error())
  431. server.freeRequest(req)
  432. }
  433. return err
  434. }
  435. service.call(server, sending, nil, mtype, req, argv, replyv, codec)
  436. return nil
  437. }
  438. func (server *Server) getRequest() *Request {
  439. server.reqLock.Lock()
  440. req := server.freeReq
  441. if req == nil {
  442. req = new(Request)
  443. } else {
  444. server.freeReq = req.next
  445. *req = Request{}
  446. }
  447. server.reqLock.Unlock()
  448. return req
  449. }
  450. func (server *Server) freeRequest(req *Request) {
  451. server.reqLock.Lock()
  452. req.next = server.freeReq
  453. server.freeReq = req
  454. server.reqLock.Unlock()
  455. }
  456. func (server *Server) getResponse() *Response {
  457. server.respLock.Lock()
  458. resp := server.freeResp
  459. if resp == nil {
  460. resp = new(Response)
  461. } else {
  462. server.freeResp = resp.next
  463. *resp = Response{}
  464. }
  465. server.respLock.Unlock()
  466. return resp
  467. }
  468. func (server *Server) freeResponse(resp *Response) {
  469. server.respLock.Lock()
  470. resp.next = server.freeResp
  471. server.freeResp = resp
  472. server.respLock.Unlock()
  473. }
  474. func (server *Server) readRequest(codec ServerCodec) (service *service, mtype *methodType, req *Request, argv, replyv reflect.Value, keepReading bool, err error) {
  475. service, mtype, req, keepReading, err = server.readRequestHeader(codec)
  476. if err != nil {
  477. if !keepReading {
  478. return
  479. }
  480. // discard body
  481. codec.ReadRequestBody(nil)
  482. return
  483. }
  484. // Decode the argument value.
  485. argIsValue := false // if true, need to indirect before calling.
  486. if mtype.ArgType.Kind() == reflect.Ptr {
  487. argv = reflect.New(mtype.ArgType.Elem())
  488. } else {
  489. argv = reflect.New(mtype.ArgType)
  490. argIsValue = true
  491. }
  492. // argv guaranteed to be a pointer now.
  493. if err = codec.ReadRequestBody(argv.Interface()); err != nil {
  494. return
  495. }
  496. if argIsValue {
  497. argv = argv.Elem()
  498. }
  499. replyv = reflect.New(mtype.ReplyType.Elem())
  500. switch mtype.ReplyType.Elem().Kind() {
  501. case reflect.Map:
  502. replyv.Elem().Set(reflect.MakeMap(mtype.ReplyType.Elem()))
  503. case reflect.Slice:
  504. replyv.Elem().Set(reflect.MakeSlice(mtype.ReplyType.Elem(), 0, 0))
  505. }
  506. return
  507. }
  508. func (server *Server) readRequestHeader(codec ServerCodec) (svc *service, mtype *methodType, req *Request, keepReading bool, err error) {
  509. // Grab the request header.
  510. req = server.getRequest()
  511. err = codec.ReadRequestHeader(req)
  512. if err != nil {
  513. req = nil
  514. if err == io.EOF || err == io.ErrUnexpectedEOF {
  515. return
  516. }
  517. err = errors.New("rpc: server cannot decode request: " + err.Error())
  518. return
  519. }
  520. // We read the header successfully. If we see an error now,
  521. // we can still recover and move on to the next request.
  522. keepReading = true
  523. dot := strings.LastIndex(req.ServiceMethod, ".")
  524. if dot < 0 {
  525. err = errors.New("rpc: service/method request ill-formed: " + req.ServiceMethod)
  526. return
  527. }
  528. serviceName := req.ServiceMethod[:dot]
  529. methodName := req.ServiceMethod[dot+1:]
  530. // Look up the request.
  531. svci, ok := server.serviceMap.Load(serviceName)
  532. if !ok {
  533. err = errors.New("rpc: can't find service " + req.ServiceMethod)
  534. return
  535. }
  536. svc = svci.(*service)
  537. mtype = svc.method[methodName]
  538. if mtype == nil {
  539. err = errors.New("rpc: can't find method " + req.ServiceMethod)
  540. }
  541. return
  542. }
  543. // Accept accepts connections on the listener and serves requests
  544. // for each incoming connection. Accept blocks until the listener
  545. // returns a non-nil error. The caller typically invokes Accept in a
  546. // go statement.
  547. func (server *Server) Accept(lis net.Listener) {
  548. for {
  549. conn, err := lis.Accept()
  550. if err != nil {
  551. log.Print("rpc.Serve: accept:", err.Error())
  552. return
  553. }
  554. go server.ServeConn(conn)
  555. }
  556. }
  557. // Register publishes the receiver's methods in the DefaultServer.
  558. func Register(rcvr any) error { return DefaultServer.Register(rcvr) }
  559. // RegisterName is like Register but uses the provided name for the type
  560. // instead of the receiver's concrete type.
  561. func RegisterName(name string, rcvr any) error {
  562. return DefaultServer.RegisterName(name, rcvr)
  563. }
  564. // A ServerCodec implements reading of RPC requests and writing of
  565. // RPC responses for the server side of an RPC session.
  566. // The server calls ReadRequestHeader and ReadRequestBody in pairs
  567. // to read requests from the connection, and it calls WriteResponse to
  568. // write a response back. The server calls Close when finished with the
  569. // connection. ReadRequestBody may be called with a nil
  570. // argument to force the body of the request to be read and discarded.
  571. // See NewClient's comment for information about concurrent access.
  572. type ServerCodec interface {
  573. ReadRequestHeader(*Request) error
  574. ReadRequestBody(any) error
  575. WriteResponse(*Response, any) error
  576. // Close can be called multiple times and must be idempotent.
  577. Close() error
  578. }
  579. // ServeConn runs the DefaultServer on a single connection.
  580. // ServeConn blocks, serving the connection until the client hangs up.
  581. // The caller typically invokes ServeConn in a go statement.
  582. // ServeConn uses the gob wire format (see package gob) on the
  583. // connection. To use an alternate codec, use ServeCodec.
  584. // See NewClient's comment for information about concurrent access.
  585. func ServeConn(conn io.ReadWriteCloser) {
  586. DefaultServer.ServeConn(conn)
  587. }
  588. // ServeCodec is like ServeConn but uses the specified codec to
  589. // decode requests and encode responses.
  590. func ServeCodec(codec ServerCodec) {
  591. DefaultServer.ServeCodec(codec)
  592. }
  593. // ServeRequest is like ServeCodec but synchronously serves a single request.
  594. // It does not close the codec upon completion.
  595. func ServeRequest(codec ServerCodec) error {
  596. return DefaultServer.ServeRequest(codec)
  597. }
  598. // Accept accepts connections on the listener and serves requests
  599. // to DefaultServer for each incoming connection.
  600. // Accept blocks; the caller typically invokes it in a go statement.
  601. func Accept(lis net.Listener) { DefaultServer.Accept(lis) }