|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+package packetack
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "bytes"
|
|
|
5
|
+ "encoding/binary"
|
|
|
6
|
+ "fmt"
|
|
|
7
|
+ "io"
|
|
|
8
|
+ "net"
|
|
|
9
|
+
|
|
|
10
|
+ "go.uber.org/zap"
|
|
|
11
|
+
|
|
|
12
|
+ "github.com/9seconds/mtg/conntypes"
|
|
|
13
|
+)
|
|
|
14
|
+
|
|
|
15
|
+const clientIntermediateQuickAckLength = 0x80000000
|
|
|
16
|
+
|
|
|
17
|
+type wrapperClientIntermediate struct {
|
|
|
18
|
+ parent conntypes.StreamReadWriteCloser
|
|
|
19
|
+}
|
|
|
20
|
+
|
|
|
21
|
+func (w *wrapperClientIntermediate) Read(acks *conntypes.ConnectionAcks) (conntypes.Packet, error) {
|
|
|
22
|
+ buf := bytes.Buffer{}
|
|
|
23
|
+
|
|
|
24
|
+ buf.Grow(4)
|
|
|
25
|
+ if _, err := io.CopyN(&buf, w.parent, 4); err != nil {
|
|
|
26
|
+ return nil, fmt.Errorf("cannot read message length: %w", err)
|
|
|
27
|
+ }
|
|
|
28
|
+ length := binary.LittleEndian.Uint32(buf.Bytes())
|
|
|
29
|
+
|
|
|
30
|
+ if length > clientIntermediateQuickAckLength {
|
|
|
31
|
+ acks.Quick = true
|
|
|
32
|
+ length -= clientIntermediateQuickAckLength
|
|
|
33
|
+ }
|
|
|
34
|
+
|
|
|
35
|
+ buf.Reset()
|
|
|
36
|
+ buf.Grow(int(length))
|
|
|
37
|
+ if _, err := io.CopyN(&buf, w.parent, int64(length)); err != nil {
|
|
|
38
|
+ return nil, fmt.Errorf("cannot read the message: %w", err)
|
|
|
39
|
+ }
|
|
|
40
|
+
|
|
|
41
|
+ return buf.Bytes(), nil
|
|
|
42
|
+}
|
|
|
43
|
+
|
|
|
44
|
+func (w *wrapperClientIntermediate) Write(packet conntypes.Packet, acks *conntypes.ConnectionAcks) error {
|
|
|
45
|
+ if acks.Simple {
|
|
|
46
|
+ if _, err := w.parent.Write(packet); err != nil {
|
|
|
47
|
+ return fmt.Errorf("cannot send simpleacked packet: %w", err)
|
|
|
48
|
+ }
|
|
|
49
|
+ return nil
|
|
|
50
|
+ }
|
|
|
51
|
+
|
|
|
52
|
+ length := [4]byte{}
|
|
|
53
|
+ binary.LittleEndian.PutUint32(length[:], uint32(len(packet)))
|
|
|
54
|
+
|
|
|
55
|
+ if _, err := w.parent.Write(append(length[:], packet...)); err != nil {
|
|
|
56
|
+ return fmt.Errorf("cannot send packet: %w", err)
|
|
|
57
|
+ }
|
|
|
58
|
+ return nil
|
|
|
59
|
+}
|
|
|
60
|
+
|
|
|
61
|
+func (w *wrapperClientIntermediate) Close() error {
|
|
|
62
|
+ return w.parent.Close()
|
|
|
63
|
+}
|
|
|
64
|
+
|
|
|
65
|
+func (w *wrapperClientIntermediate) Conn() net.Conn {
|
|
|
66
|
+ return w.parent.Conn()
|
|
|
67
|
+}
|
|
|
68
|
+
|
|
|
69
|
+func (w *wrapperClientIntermediate) LocalAddr() *net.TCPAddr {
|
|
|
70
|
+ return w.parent.LocalAddr()
|
|
|
71
|
+}
|
|
|
72
|
+
|
|
|
73
|
+func (w *wrapperClientIntermediate) RemoteAddr() *net.TCPAddr {
|
|
|
74
|
+ return w.parent.RemoteAddr()
|
|
|
75
|
+}
|
|
|
76
|
+
|
|
|
77
|
+func (w *wrapperClientIntermediate) Logger() *zap.SugaredLogger {
|
|
|
78
|
+ return w.parent.Logger().Named("client-intermediate")
|
|
|
79
|
+}
|
|
|
80
|
+
|
|
|
81
|
+func NewClientIntermediate(parent conntypes.StreamReadWriteCloser) conntypes.PacketAckFullReadWriteCloser {
|
|
|
82
|
+ return &wrapperClientIntermediate{
|
|
|
83
|
+ parent: parent,
|
|
|
84
|
+ }
|
|
|
85
|
+}
|