rpc.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. "errors"
  7. "io"
  8. "net/rpc"
  9. "sync"
  10. )
  11. // Rpc provides a rpc Server or Client Codec for rpc communication.
  12. type Rpc interface {
  13. ServerCodec(conn io.ReadWriteCloser, h Handle) rpc.ServerCodec
  14. ClientCodec(conn io.ReadWriteCloser, h Handle) rpc.ClientCodec
  15. }
  16. // RpcCodecBuffered allows access to the underlying bufio.Reader/Writer
  17. // used by the rpc connection. It accommodates use-cases where the connection
  18. // should be used by rpc and non-rpc functions, e.g. streaming a file after
  19. // sending an rpc response.
  20. type RpcCodecBuffered interface {
  21. BufferedReader() *bufio.Reader
  22. BufferedWriter() *bufio.Writer
  23. }
  24. // -------------------------------------
  25. // rpcCodec defines the struct members and common methods.
  26. type rpcCodec struct {
  27. rwc io.ReadWriteCloser
  28. dec *Decoder
  29. enc *Encoder
  30. bw *bufio.Writer
  31. br *bufio.Reader
  32. mu sync.Mutex
  33. h Handle
  34. cls bool
  35. clsmu sync.RWMutex
  36. }
  37. func newRPCCodec(conn io.ReadWriteCloser, h Handle) rpcCodec {
  38. bw := bufio.NewWriter(conn)
  39. br := bufio.NewReader(conn)
  40. // defensive: ensure that jsonH has TermWhitespace turned on.
  41. if jsonH, ok := h.(*JsonHandle); ok && !jsonH.TermWhitespace {
  42. panic(errors.New("rpc requires a JsonHandle with TermWhitespace set to true"))
  43. }
  44. return rpcCodec{
  45. rwc: conn,
  46. bw: bw,
  47. br: br,
  48. enc: NewEncoder(bw, h),
  49. dec: NewDecoder(br, h),
  50. h: h,
  51. }
  52. }
  53. func (c *rpcCodec) BufferedReader() *bufio.Reader {
  54. return c.br
  55. }
  56. func (c *rpcCodec) BufferedWriter() *bufio.Writer {
  57. return c.bw
  58. }
  59. func (c *rpcCodec) write(obj1, obj2 interface{}, writeObj2, doFlush bool) (err error) {
  60. if c.isClosed() {
  61. return io.EOF
  62. }
  63. if err = c.enc.Encode(obj1); err != nil {
  64. return
  65. }
  66. if writeObj2 {
  67. if err = c.enc.Encode(obj2); err != nil {
  68. return
  69. }
  70. }
  71. if doFlush {
  72. return c.bw.Flush()
  73. }
  74. return
  75. }
  76. func (c *rpcCodec) read(obj interface{}) (err error) {
  77. if c.isClosed() {
  78. return io.EOF
  79. }
  80. //If nil is passed in, we should still attempt to read content to nowhere.
  81. if obj == nil {
  82. var obj2 interface{}
  83. return c.dec.Decode(&obj2)
  84. }
  85. return c.dec.Decode(obj)
  86. }
  87. func (c *rpcCodec) isClosed() bool {
  88. c.clsmu.RLock()
  89. x := c.cls
  90. c.clsmu.RUnlock()
  91. return x
  92. }
  93. func (c *rpcCodec) Close() error {
  94. if c.isClosed() {
  95. return io.EOF
  96. }
  97. c.clsmu.Lock()
  98. c.cls = true
  99. c.clsmu.Unlock()
  100. return c.rwc.Close()
  101. }
  102. func (c *rpcCodec) ReadResponseBody(body interface{}) error {
  103. return c.read(body)
  104. }
  105. // -------------------------------------
  106. type goRpcCodec struct {
  107. rpcCodec
  108. }
  109. func (c *goRpcCodec) WriteRequest(r *rpc.Request, body interface{}) error {
  110. // Must protect for concurrent access as per API
  111. c.mu.Lock()
  112. defer c.mu.Unlock()
  113. return c.write(r, body, true, true)
  114. }
  115. func (c *goRpcCodec) WriteResponse(r *rpc.Response, body interface{}) error {
  116. c.mu.Lock()
  117. defer c.mu.Unlock()
  118. return c.write(r, body, true, true)
  119. }
  120. func (c *goRpcCodec) ReadResponseHeader(r *rpc.Response) error {
  121. return c.read(r)
  122. }
  123. func (c *goRpcCodec) ReadRequestHeader(r *rpc.Request) error {
  124. return c.read(r)
  125. }
  126. func (c *goRpcCodec) ReadRequestBody(body interface{}) error {
  127. return c.read(body)
  128. }
  129. // -------------------------------------
  130. // goRpc is the implementation of Rpc that uses the communication protocol
  131. // as defined in net/rpc package.
  132. type goRpc struct{}
  133. // GoRpc implements Rpc using the communication protocol defined in net/rpc package.
  134. // Its methods (ServerCodec and ClientCodec) return values that implement RpcCodecBuffered.
  135. var GoRpc goRpc
  136. func (x goRpc) ServerCodec(conn io.ReadWriteCloser, h Handle) rpc.ServerCodec {
  137. return &goRpcCodec{newRPCCodec(conn, h)}
  138. }
  139. func (x goRpc) ClientCodec(conn io.ReadWriteCloser, h Handle) rpc.ClientCodec {
  140. return &goRpcCodec{newRPCCodec(conn, h)}
  141. }
  142. var _ RpcCodecBuffered = (*rpcCodec)(nil) // ensure *rpcCodec implements RpcCodecBuffered