-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.go
203 lines (166 loc) · 5.31 KB
/
connection.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package mcproto
import (
"bytes"
"errors"
"fmt"
enc "github.com/Raqbit/mcproto/encoding"
"github.com/Raqbit/mcproto/game"
"github.com/Raqbit/mcproto/packet"
"io"
"net"
"time"
)
var (
ErrConnectionState = errors.New("connection has invalid state for packet type")
ErrDirection = errors.New("packet being sent in wrong direction")
ErrLegacyServerPing = errors.New("not implemented: legacy server ping")
)
// Connection represents a connection
// to a Minecraft server or client.
type Connection interface {
ReadPacket() (packet.Packet, error)
WritePacket(packet.Packet) error
SetReadDeadline(time.Time) error
SetWriteDeadline(time.Time) error
Close() error
SetState(state game.ConnectionState)
}
type connection struct {
conn net.Conn
state game.ConnectionState
side Side
packets map[packet.Info]packet.Packet
writeBuf *bytes.Buffer
readBuf *bytes.Buffer
logger Logger
}
// ReadPacket reads a Minecraft protocol packet from the connection.
// ReadPacket will also try to parse the contents of this packet and return
// an error if the given packet ID is unknown or if the known packet format
// did not decode correctly.
func (c *connection) ReadPacket() (packet.Packet, error) {
var err error
// Read packet length
var length enc.VarInt
if err = length.Read(c.conn); err != nil {
return nil, fmt.Errorf("could not read packet length: %w", err)
}
// TODO: Handle legacy server ping (https://wiki.vg/Server_List_Ping#1.6)
if length == 0xFE {
return nil, ErrLegacyServerPing
}
// Catch invalid packet lengths
if length < 1 || length > packet.MaxPacketLength {
return nil, packet.ErrInvalidPacketLength
}
// Read complete packet into read buffer
if _, err = c.readBuf.ReadFrom(io.LimitReader(c.conn, int64(length))); err != nil {
return nil, fmt.Errorf("could not read packet from connection: %w", err)
}
// Read packet ID
var pID enc.VarInt
if err = pID.Read(c.readBuf); err != nil {
return nil, fmt.Errorf("could not decode packet ID: %w", err)
}
// Lookup packet type
pkt, err := c.getPacketTypeByID(int32(pID))
if err != nil {
return nil, err
}
c.logger.Debugf("[Recv] %s, %d: %s\n", c.state, pID, pkt.String())
// Decode packet
if err = pkt.Read(c.readBuf); err != nil {
return nil, fmt.Errorf("could not decode packet data: %w", err)
}
// Reset read buffer
c.readBuf.Reset()
return pkt, nil
}
// WritePacket writes a Minecraft protocol packet to the connection.
//
// If the provided deadline expires before a packet could be written,
// an error is returned. Providing a zero value for the deadline will
// make the read not time out.
func (c *connection) WritePacket(packetToWrite packet.Packet) error {
if packetToWrite.State() != c.state {
return ErrConnectionState
}
if packetToWrite.Direction() != c.side.WriteDirection() {
return ErrDirection
}
c.logger.Debugf("[Send] %s, %d: %s\n", c.state, packetToWrite.ID(), packetToWrite.String())
packetId := enc.VarInt(packetToWrite.ID())
// Write packet ID to buffer
if err := packetId.Write(c.writeBuf); err != nil {
return fmt.Errorf("unable to write packet id to buffer: %w", err)
}
// Write packet data to buffer
if err := packetToWrite.Write(c.writeBuf); err != nil {
return fmt.Errorf("could not encode packet data: %w", err)
}
// Get packet length
length := enc.VarInt(c.writeBuf.Len())
// Write packet length to connection
if err := length.Write(c.conn); err != nil {
return fmt.Errorf("could not write packet length: %w", err)
}
// Flush write buffer to connection
if _, err := c.writeBuf.WriteTo(c.conn); err != nil {
return fmt.Errorf("could not flush write buffer: %w", err)
}
// Reset write buffer
c.writeBuf.Reset()
return nil
}
// SetReadDeadline sets the deadline for future ReadPacket calls
// and any currently-blocked ReadPacket call.
// A zero value for t means ReadPacket will not time out.
func (c *connection) SetReadDeadline(t time.Time) error {
return c.conn.SetReadDeadline(t)
}
// SetWriteDeadline sets the deadline for future WritePacket calls
// and any currently-blocked WritePacket call.
// A zero value for t means WritePacket will not time out.
func (c *connection) SetWriteDeadline(t time.Time) error {
return c.conn.SetWriteDeadline(t)
}
// SetState switches the protocol to a different state,
// which changes the meaning of packet IDs.
func (c *connection) SetState(state game.ConnectionState) {
c.state = state
}
// Close closes the connection. After this has been called,
// the Connection should not be used anymore.
func (c *connection) Close() error {
return c.conn.Close()
}
func (c *connection) registerPacket(pkt packet.Packet) {
c.packets[packet.Info{
ID: pkt.ID(),
ConnectionState: pkt.State(),
Direction: pkt.Direction(),
}] = pkt
}
type UnknownPacketError struct {
id int32
direction packet.Direction
state game.ConnectionState
}
func (u UnknownPacketError) Error() string {
return fmt.Sprintf("unknown packet ID, direction: %s, state: %s, ID: %#x", u.direction, u.state, u.id)
}
func (c *connection) getPacketTypeByID(id int32) (packet.Packet, error) {
p, ok := c.packets[packet.Info{
ID: id,
Direction: c.side.ReadDirection(),
ConnectionState: c.state,
}]
if !ok {
return nil, UnknownPacketError{
id: id,
direction: c.side.WriteDirection(),
state: c.state,
}
}
return p, nil
}