| 1 |
package yarpc |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"encoding/binary" |
| 6 |
"errors" |
| 7 |
"io" |
| 8 |
|
| 9 |
"github.com/influxdata/yamux" |
| 10 |
"github.com/influxdata/yarpc/codes" |
| 11 |
"github.com/influxdata/yarpc/status" |
| 12 |
) |
| 13 |
|
| 14 |
type StreamHandler func(srv interface{}, stream ServerStream) error |
| 15 |
|
| 16 |
type StreamDesc struct { |
| 17 |
Index uint8 |
| 18 |
StreamName string |
| 19 |
Handler StreamHandler |
| 20 |
|
| 21 |
ServerStreams bool |
| 22 |
ClientStreams bool |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
type Stream interface { |
| 27 |
|
| 28 |
Context() context.Context |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
SendMsg(m interface{}) error |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
RecvMsg(m interface{}) error |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
type ClientStream interface { |
| 51 |
|
| 52 |
|
| 53 |
CloseSend() error |
| 54 |
Stream |
| 55 |
} |
| 56 |
|
| 57 |
func NewClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, api uint16) (ClientStream, error) { |
| 58 |
cn, err := cc.NewStream() |
| 59 |
if err != nil { |
| 60 |
return nil, err |
| 61 |
} |
| 62 |
|
| 63 |
var tmp [2]byte |
| 64 |
binary.BigEndian.PutUint16(tmp[:], api) |
| 65 |
_, err = cn.Write(tmp[:]) |
| 66 |
if err != nil { |
| 67 |
return nil, err |
| 68 |
} |
| 69 |
|
| 70 |
cs := &clientStream{ |
| 71 |
cn: cn, |
| 72 |
codec: cc.dopts.codec, |
| 73 |
p: &parser{r: cn}, |
| 74 |
desc: desc, |
| 75 |
ctx: ctx, |
| 76 |
closing: make(chan struct{}), |
| 77 |
} |
| 78 |
go func() { |
| 79 |
select { |
| 80 |
case <-ctx.Done(): |
| 81 |
cs.CloseSend() |
| 82 |
case <-cs.closing: |
| 83 |
} |
| 84 |
}() |
| 85 |
|
| 86 |
return cs, nil |
| 87 |
} |
| 88 |
|
| 89 |
type clientStream struct { |
| 90 |
cn *yamux.Stream |
| 91 |
codec Codec |
| 92 |
p *parser |
| 93 |
desc *StreamDesc |
| 94 |
|
| 95 |
ctx context.Context |
| 96 |
closing chan struct{} |
| 97 |
} |
| 98 |
|
| 99 |
func (c *clientStream) CloseSend() error { |
| 100 |
select { |
| 101 |
case <-c.closing: |
| 102 |
default: |
| 103 |
close(c.closing) |
| 104 |
} |
| 105 |
return c.cn.Close() |
| 106 |
} |
| 107 |
|
| 108 |
func (c *clientStream) Context() context.Context { |
| 109 |
return c.ctx |
| 110 |
} |
| 111 |
|
| 112 |
func (c *clientStream) SendMsg(m interface{}) error { |
| 113 |
select { |
| 114 |
case <-c.closing: |
| 115 |
return errors.New("stream closed") |
| 116 |
default: |
| 117 |
} |
| 118 |
out, err := encode(c.codec, m) |
| 119 |
if err != nil { |
| 120 |
return err |
| 121 |
} |
| 122 |
|
| 123 |
_, err = c.cn.Write(out) |
| 124 |
return err |
| 125 |
} |
| 126 |
|
| 127 |
func (c *clientStream) RecvMsg(m interface{}) error { |
| 128 |
select { |
| 129 |
case <-c.closing: |
| 130 |
return errors.New("stream closed") |
| 131 |
default: |
| 132 |
} |
| 133 |
err := decode(c.p, c.codec, c.cn, m) |
| 134 |
if err == nil { |
| 135 |
if !c.desc.ClientStreams || c.desc.ServerStreams { |
| 136 |
return nil |
| 137 |
} |
| 138 |
} |
| 139 |
return err |
| 140 |
} |
| 141 |
|
| 142 |
type ServerStream interface { |
| 143 |
Stream |
| 144 |
} |
| 145 |
|
| 146 |
type serverStream struct { |
| 147 |
cn *yamux.Stream |
| 148 |
codec Codec |
| 149 |
p *parser |
| 150 |
buf []byte |
| 151 |
} |
| 152 |
|
| 153 |
func (s *serverStream) Context() context.Context { |
| 154 |
panic("implement me") |
| 155 |
} |
| 156 |
|
| 157 |
func (s *serverStream) SendMsg(m interface{}) error { |
| 158 |
out, err := encode(s.codec, m) |
| 159 |
if err != nil { |
| 160 |
return err |
| 161 |
} |
| 162 |
|
| 163 |
_, err = s.cn.Write(out) |
| 164 |
if err != nil { |
| 165 |
|
| 166 |
return err |
| 167 |
} |
| 168 |
return nil |
| 169 |
} |
| 170 |
|
| 171 |
func (s *serverStream) RecvMsg(m interface{}) error { |
| 172 |
if err := decode(s.p, s.codec, s.cn, m); err != nil { |
| 173 |
if err == io.EOF { |
| 174 |
return err |
| 175 |
} |
| 176 |
if err == io.ErrUnexpectedEOF { |
| 177 |
err = status.Errorf(codes.Internal, io.ErrUnexpectedEOF.Error()) |
| 178 |
} |
| 179 |
|
| 180 |
return err |
| 181 |
} |
| 182 |
return nil |
| 183 |
} |