rpc.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a MIT license found in the LICENSE file.
  3. package codec
  4. import (
  5. "bufio"
  6. "io"
  7. "net/rpc"
  8. "sync"
  9. )
  10. // rpcEncodeTerminator allows a handler specify a []byte terminator to send after each Encode.
  11. //
  12. // Some codecs like json need to put a space after each encoded value, to serve as a
  13. // delimiter for things like numbers (else json codec will continue reading till EOF).
  14. type rpcEncodeTerminator interface {
  15. rpcEncodeTerminate() []byte
  16. }
  17. // Rpc provides a rpc Server or Client Codec for rpc communication.
  18. type Rpc interface {
  19. ServerCodec(conn io.ReadWriteCloser, h Handle) rpc.ServerCodec
  20. ClientCodec(conn io.ReadWriteCloser, h Handle) rpc.ClientCodec
  21. }
  22. // RpcCodecBuffered allows access to the underlying bufio.Reader/Writer
  23. // used by the rpc connection. It accomodates use-cases where the connection
  24. // should be used by rpc and non-rpc functions, e.g. streaming a file after
  25. // sending an rpc response.
  26. type RpcCodecBuffered interface {
  27. BufferedReader() *bufio.Reader
  28. BufferedWriter() *bufio.Writer
  29. }
  30. // -------------------------------------
  31. // rpcCodec defines the struct members and common methods.
  32. type rpcCodec struct {
  33. rwc io.ReadWriteCloser
  34. dec *Decoder
  35. enc *Encoder
  36. bw *bufio.Writer
  37. br *bufio.Reader
  38. mu sync.Mutex
  39. h Handle
  40. cls bool
  41. clsmu sync.RWMutex
  42. }
  43. func newRPCCodec(conn io.ReadWriteCloser, h Handle) rpcCodec {
  44. bw := bufio.NewWriter(conn)
  45. br := bufio.NewReader(conn)
  46. return rpcCodec{
  47. rwc: conn,
  48. bw: bw,
  49. br: br,
  50. enc: NewEncoder(bw, h),
  51. dec: NewDecoder(br, h),
  52. h: h,
  53. }
  54. }
  55. func (c *rpcCodec) BufferedReader() *bufio.Reader {
  56. return c.br
  57. }
  58. func (c *rpcCodec) BufferedWriter() *bufio.Writer {
  59. return c.bw
  60. }
  61. func (c *rpcCodec) write(obj1, obj2 interface{}, writeObj2, doFlush bool) (err error) {
  62. if c.isClosed() {
  63. return io.EOF
  64. }
  65. if err = c.enc.Encode(obj1); err != nil {
  66. return
  67. }
  68. t, tOk := c.h.(rpcEncodeTerminator)
  69. if tOk {
  70. c.bw.Write(t.rpcEncodeTerminate())
  71. }
  72. if writeObj2 {
  73. if err = c.enc.Encode(obj2); err != nil {
  74. return
  75. }
  76. if tOk {
  77. c.bw.Write(t.rpcEncodeTerminate())
  78. }
  79. }
  80. if doFlush {
  81. return c.bw.Flush()
  82. }
  83. return
  84. }
  85. func (c *rpcCodec) read(obj interface{}) (err error) {
  86. if c.isClosed() {
  87. return io.EOF
  88. }
  89. //If nil is passed in, we should still attempt to read content to nowhere.
  90. if obj == nil {
  91. var obj2 interface{}
  92. return c.dec.Decode(&obj2)
  93. }
  94. return c.dec.Decode(obj)
  95. }
  96. func (c *rpcCodec) isClosed() bool {
  97. c.clsmu.RLock()
  98. x := c.cls
  99. c.clsmu.RUnlock()
  100. return x
  101. }
  102. func (c *rpcCodec) Close() error {
  103. if c.isClosed() {
  104. return io.EOF
  105. }
  106. c.clsmu.Lock()
  107. c.cls = true
  108. c.clsmu.Unlock()
  109. return c.rwc.Close()
  110. }
  111. func (c *rpcCodec) ReadResponseBody(body interface{}) error {
  112. return c.read(body)
  113. }
  114. // -------------------------------------
  115. type goRpcCodec struct {
  116. rpcCodec
  117. }
  118. func (c *goRpcCodec) WriteRequest(r *rpc.Request, body interface{}) error {
  119. // Must protect for concurrent access as per API
  120. c.mu.Lock()
  121. defer c.mu.Unlock()
  122. return c.write(r, body, true, true)
  123. }
  124. func (c *goRpcCodec) WriteResponse(r *rpc.Response, body interface{}) error {
  125. c.mu.Lock()
  126. defer c.mu.Unlock()
  127. return c.write(r, body, true, true)
  128. }
  129. func (c *goRpcCodec) ReadResponseHeader(r *rpc.Response) error {
  130. return c.read(r)
  131. }
  132. func (c *goRpcCodec) ReadRequestHeader(r *rpc.Request) error {
  133. return c.read(r)
  134. }
  135. func (c *goRpcCodec) ReadRequestBody(body interface{}) error {
  136. return c.read(body)
  137. }
  138. // -------------------------------------
  139. // goRpc is the implementation of Rpc that uses the communication protocol
  140. // as defined in net/rpc package.
  141. type goRpc struct{}
  142. // GoRpc implements Rpc using the communication protocol defined in net/rpc package.
  143. // Its methods (ServerCodec and ClientCodec) return values that implement RpcCodecBuffered.
  144. var GoRpc goRpc
  145. func (x goRpc) ServerCodec(conn io.ReadWriteCloser, h Handle) rpc.ServerCodec {
  146. return &goRpcCodec{newRPCCodec(conn, h)}
  147. }
  148. func (x goRpc) ClientCodec(conn io.ReadWriteCloser, h Handle) rpc.ClientCodec {
  149. return &goRpcCodec{newRPCCodec(conn, h)}
  150. }
  151. var _ RpcCodecBuffered = (*rpcCodec)(nil) // ensure *rpcCodec implements RpcCodecBuffered