rpc.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a BSD-style 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. cls bool
  40. h Handle
  41. }
  42. func newRPCCodec(conn io.ReadWriteCloser, h Handle) rpcCodec {
  43. bw := bufio.NewWriter(conn)
  44. br := bufio.NewReader(conn)
  45. return rpcCodec{
  46. rwc: conn,
  47. bw: bw,
  48. br: br,
  49. enc: NewEncoder(bw, h),
  50. dec: NewDecoder(br, h),
  51. h: h,
  52. }
  53. }
  54. func (c *rpcCodec) BufferedReader() *bufio.Reader {
  55. return c.br
  56. }
  57. func (c *rpcCodec) BufferedWriter() *bufio.Writer {
  58. return c.bw
  59. }
  60. func (c *rpcCodec) write(obj1, obj2 interface{}, writeObj2, doFlush bool) (err error) {
  61. if c.cls {
  62. return io.EOF
  63. }
  64. if err = c.enc.Encode(obj1); err != nil {
  65. return
  66. }
  67. t, tOk := c.h.(rpcEncodeTerminator)
  68. if tOk {
  69. c.bw.Write(t.rpcEncodeTerminate())
  70. }
  71. if writeObj2 {
  72. if err = c.enc.Encode(obj2); err != nil {
  73. return
  74. }
  75. if tOk {
  76. c.bw.Write(t.rpcEncodeTerminate())
  77. }
  78. }
  79. if doFlush {
  80. return c.bw.Flush()
  81. }
  82. return
  83. }
  84. func (c *rpcCodec) read(obj interface{}) (err error) {
  85. if c.cls {
  86. return io.EOF
  87. }
  88. //If nil is passed in, we should still attempt to read content to nowhere.
  89. if obj == nil {
  90. var obj2 interface{}
  91. return c.dec.Decode(&obj2)
  92. }
  93. return c.dec.Decode(obj)
  94. }
  95. func (c *rpcCodec) Close() error {
  96. if c.cls {
  97. return io.EOF
  98. }
  99. c.cls = true
  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