packets

package
v0.0.0-...-78158b1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2015 License: MIT Imports: 9 Imported by: 2

Documentation

Overview

Package packets contains the necessary magic to create, parse, serialize and register Minecraft packets.

Index

Constants

View Source
const (
	NumStates     int = 4
	NumDirections     = 3
)

Variables

View Source
var (
	ErrNegativeLength = errors.New("kyubu: packet has negative length")
	ErrUnknownPacket  = errors.New("kyubu: packet has unknown id")
	ErrLostSync       = errors.New("kyubu: lost sync on packets")
	ErrNotCompressed  = errors.New("kyubu: packet not compressed correctly")
)
View Source
var Endianness = binary.BigEndian

Packets is a multidimensional array/map used to store packet factories. [4] = how many states there are [3] = how many directions there are [b] = packet ID byte

Functions

func PutUvarint

func PutUvarint(buf []byte, x uint64) int

PutUvarint encodes a uint64 into buf and returns the number of bytes written. If the buffer is too small, PutUvarint will panic.

func PutVarint

func PutVarint(buf []byte, x int64) int

PutVarint encodes an int64 into buf and returns the number of bytes written. If the buffer is too small, PutVarint will panic.

func ReadBool

func ReadBool(r io.Reader) (bool, error)

func ReadString

func ReadString(r io.Reader) (string, error)

func ReadUvarint

func ReadUvarint(r io.Reader) (uint64, error)

ReadUvarint reads an encoded unsigned integer from r and returns it as a uint64.

func ReadVarint

func ReadVarint(r io.Reader) (int64, error)

ReadVarint reads an encoded signed integer from r and returns it as an int64.

func Register

func Register(state State, dir PacketDirection, id byte, f PacketFunc)

XXX: Catch panics in case of out of bounds? Is that even possible?

func Uvarint

func Uvarint(buf []byte) (uint64, int)

Uvarint decodes a uint64 from buf and returns that value and the number of bytes read (> 0). If an error occurred, the value is 0 and the number of bytes n is <= 0 meaning:

	n == 0: buf too small
	n  < 0: value larger than 64 bits (overflow)
             and -n is the number of bytes read

func Varint

func Varint(buf []byte) (int64, int)

Varint decodes an int64 from buf and returns that value and the number of bytes read (> 0). If an error occurred, the value is 0 and the number of bytes n is <= 0 with the following meaning:

	n == 0: buf too small
	n  < 0: value larger than 64 bits (overflow)
             and -n is the number of bytes read

func WriteBool

func WriteBool(w io.Writer, b bool) error

func WriteString

func WriteString(w io.Writer, s string) error

func WriteVarint

func WriteVarint(w io.Writer, v VarInt) error

Types

type Angle

type Angle uint8

(1) Rotation angle in steps of 1/256 of a full turn Unsigned to get 0-255, rather than -128 to 127

type Chat

type Chat format.AnyComponent

type Packet

type Packet interface {
	Id() byte
	Encode(io.Writer) error
	Decode(io.Reader) error
}

type PacketDirection

type PacketDirection int

PacketDirection lets us define whether packets are purely C->S or S->C. This will change what the parser reads, depending on its direction.

const (
	ServerBound PacketDirection = iota
	ClientBound
	// Anomalous shouldn't come up usually. If it does, something is broken,
	// and you'll probably want to panic() out.
	Anomalous
)

ServerBound and ClientBound let us designate a parser's direction. This defines what kind of packets it'll parse/serialize.

func (*PacketDirection) Flip

func (p *PacketDirection) Flip() PacketDirection

type PacketFunc

type PacketFunc func() Packet

type Parser

type Parser struct {
	State     State
	Direction PacketDirection
	// contains filtered or unexported fields
}

func NewParser

func NewParser(c net.Conn, state State, dir PacketDirection) *Parser

func (*Parser) EnableEncryption

func (p *Parser) EnableEncryption(key []byte) error

func (*Parser) Recv

func (p *Parser) Recv() (Packet, error)

Recv returns the next packet parsed out of the stream.

func (*Parser) Send

func (p *Parser) Send(packet Packet) error

Send encodes (and compresses, if required) a packet, and sends it.

func (*Parser) SetCompression

func (p *Parser) SetCompression(threshold int)

type Position

type Position uint64

(8) x (-33554432 to 33554431), y (-2048 to 2047), z (-33554432 to 33554431) x as a 26-bit integer, followed by y as a 12-bit integer, followed by z as a 26-bit integer Code lifted from thinkofdeath/steven:protocol/handshaking.go

func NewPosition

func NewPosition(x, y, z int) Position

func (Position) String

func (p Position) String() string

func (Position) X

func (p Position) X() int

func (Position) Y

func (p Position) Y() int

func (Position) Z

func (p Position) Z() int

type State

type State int
const (
	Handshake State = iota
	Status
	Login
	Play
)

type UUID

type UUID [16]byte

type VarInt

type VarInt int32

(>= 1, <= 5) -2147483648 to 2147483647

type VarLong

type VarLong int64

(>= 1, <= 10) -9223372036854775808 to 9223372036854775807

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL