mc-router/mcproto/types.go

68 lines
1.3 KiB
Go
Raw Normal View History

2018-05-08 05:16:01 +02:00
package mcproto
import "fmt"
2018-05-08 05:16:01 +02:00
type Frame struct {
2019-04-17 01:58:13 +02:00
Length int
2018-05-08 05:16:01 +02:00
Payload []byte
}
type State int
const (
StateHandshaking = iota
)
var trimLimit = 64
func trimBytes(data []byte) ([]byte, string) {
if len(data) < trimLimit {
return data, ""
} else {
return data[:trimLimit], "..."
}
}
func (f *Frame) String() string {
trimmed, cont := trimBytes(f.Payload)
return fmt.Sprintf("Frame:[len=%d, payload=%#X%s]", f.Length, trimmed, cont)
}
2018-05-08 05:16:01 +02:00
type Packet struct {
2019-04-17 01:58:13 +02:00
Length int
2018-05-08 05:16:01 +02:00
PacketID int
// Data is either a byte slice of raw content or a parsed message
Data interface{}
2018-05-08 05:16:01 +02:00
}
func (p *Packet) String() string {
if dataBytes, ok := p.Data.([]byte); ok {
trimmed, cont := trimBytes(dataBytes)
return fmt.Sprintf("Frame:[len=%d, packetId=%d, data=%#X%s]", p.Length, p.PacketID, trimmed, cont)
} else {
return fmt.Sprintf("Frame:[len=%d, packetId=%d, data=%+v]", p.Length, p.PacketID, p.Data)
}
}
const (
PacketIdHandshake = 0x00
PacketIdLegacyServerListPing = 0xFE
)
2019-04-17 01:58:13 +02:00
2018-05-08 05:16:01 +02:00
type Handshake struct {
ProtocolVersion int
2019-04-17 01:58:13 +02:00
ServerAddress string
ServerPort uint16
NextState int
2018-05-08 05:16:01 +02:00
}
type LegacyServerListPing struct {
ProtocolVersion int
ServerAddress string
ServerPort uint16
}
2018-05-08 05:16:01 +02:00
type ByteReader interface {
ReadByte() (byte, error)
}