-
Notifications
You must be signed in to change notification settings - Fork 5
/
stream.go
73 lines (60 loc) · 1.68 KB
/
stream.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package srpc
import (
"context"
)
// Stream is a handle to an on-going bi-directional or one-directional stream RPC handle.
type Stream interface {
// Context is canceled when the Stream is no longer valid.
Context() context.Context
// MsgSend sends the message to the remote.
MsgSend(msg Message) error
// MsgRecv receives an incoming message from the remote.
// Parses the message into the object at msg.
MsgRecv(msg Message) error
// CloseSend signals to the remote that we will no longer send any messages.
CloseSend() error
// Close closes the stream for reading and writing.
Close() error
}
// StreamRecv is a stream that can receive typed messages.
//
// T is the response type.
type StreamRecv[T any] interface {
Stream
Recv() (T, error)
RecvTo(T) error
}
// StreamSend is a stream that can send typed messages.
//
// T is the outgoing type.
type StreamSend[T any] interface {
Stream
Send(T) error
}
// StreamSendAndClose is a stream that can send typed messages, closing after.
//
// T is the outgoing type.
type StreamSendAndClose[T any] interface {
StreamSend[T]
SendAndClose(T) error
}
// streamWithClose is a Stream with a wrapped Close function.
type streamWithClose struct {
Stream
closeFn func() error
}
// NewStreamWithClose wraps a Stream with a close function to call when Close is called.
func NewStreamWithClose(strm Stream, close func() error) Stream {
return &streamWithClose{Stream: strm, closeFn: close}
}
// Close closes the stream for reading and writing.
func (s *streamWithClose) Close() error {
err := s.Stream.Close()
err2 := s.closeFn()
if err != nil {
return err
}
return err2
}
// _ is a type assertion
var _ Stream = (*streamWithClose)(nil)