Преглед изворни кода

Add client wrappers

tags/1.0^2
9seconds пре 6 година
родитељ
комит
c68f2cd50f

+ 3
- 0
wrappers/packetack/client_abridged.go Прегледај датотеку

@@ -25,6 +25,7 @@ type wrapperClientAbridged struct {
25 25
 func (w *wrapperClientAbridged) Read(acks *conntypes.ConnectionAcks) (conntypes.Packet, error) {
26 26
 	buf := bytes.Buffer{}
27 27
 
28
+	buf.Grow(1)
28 29
 	if _, err := io.CopyN(&buf, w.parent, 1); err != nil {
29 30
 		return nil, fmt.Errorf("cannot read message length: %w", err)
30 31
 	}
@@ -37,6 +38,7 @@ func (w *wrapperClientAbridged) Read(acks *conntypes.ConnectionAcks) (conntypes.
37 38
 	}
38 39
 
39 40
 	if msgLength == clientAbridgedSmallPacketLength {
41
+		buf.Grow(3)
40 42
 		if _, err := io.CopyN(&buf, w.parent, 3); err != nil {
41 43
 			return nil, fmt.Errorf("cannot read correct message length: %w", err)
42 44
 		}
@@ -47,6 +49,7 @@ func (w *wrapperClientAbridged) Read(acks *conntypes.ConnectionAcks) (conntypes.
47 49
 	msgLength *= 4
48 50
 
49 51
 	buf.Reset()
52
+	buf.Grow(int(msgLength))
50 53
 	if _, err := io.CopyN(&buf, w.parent, int64(msgLength)); err != nil {
51 54
 		return nil, fmt.Errorf("cannot read message: %w", err)
52 55
 	}

+ 85
- 0
wrappers/packetack/client_intermediate.go Прегледај датотеку

@@ -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
+}

+ 54
- 0
wrappers/packetack/client_intermediate_secure.go Прегледај датотеку

@@ -0,0 +1,54 @@
1
+package packetack
2
+
3
+import (
4
+	"bytes"
5
+	"encoding/binary"
6
+	"fmt"
7
+	"math/rand"
8
+
9
+	"github.com/9seconds/mtg/conntypes"
10
+)
11
+
12
+type wrapperClientIntermediateSecure struct {
13
+	wrapperClientIntermediate
14
+}
15
+
16
+func (w *wrapperClientIntermediateSecure) Read(acks *conntypes.ConnectionAcks) (conntypes.Packet, error) {
17
+	data, err := w.wrapperClientIntermediate.Read(acks)
18
+	if err != nil {
19
+		return nil, err
20
+	}
21
+	length := len(data) - (len(data) % 4)
22
+
23
+	return data[:length], nil
24
+}
25
+
26
+func (w *wrapperClientIntermediateSecure) Write(packet conntypes.Packet, acks *conntypes.ConnectionAcks) error {
27
+	if acks.Simple {
28
+		if _, err := w.parent.Write(packet); err != nil {
29
+			return fmt.Errorf("cannot send simpleacked packet: %w", err)
30
+		}
31
+		return nil
32
+	}
33
+
34
+	buf := bytes.Buffer{}
35
+	paddingLength := rand.Intn(4)
36
+	buf.Grow(4 + len(packet) + paddingLength)
37
+
38
+	binary.Write(&buf, binary.LittleEndian, uint32(len(packet)+paddingLength))
39
+	buf.Write(packet)
40
+	buf.Write(make([]byte, paddingLength))
41
+
42
+	if _, err := w.parent.Write(buf.Bytes()); err != nil {
43
+		return fmt.Errorf("cannot send packet: %w", err)
44
+	}
45
+	return nil
46
+}
47
+
48
+func NewClientIntermediateSecure(parent conntypes.StreamReadWriteCloser) conntypes.PacketAckFullReadWriteCloser {
49
+	return &wrapperClientIntermediateSecure{
50
+		wrapperClientIntermediate: wrapperClientIntermediate{
51
+			parent: parent,
52
+		},
53
+	}
54
+}

Loading…
Откажи
Сачувај