networking

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

networking provides all network abstractions : connection read and write, parsing some data types, errors, etc...

Index

Constants

View Source
const (
	MaximumUDPDatagramLength int = 65535
)

Variables

View Source
var (
	ErrConnectionNotEstablished     error = errors.New("connection hasn't been established yet. Call Connect method to establish connection")
	ErrConnectionAlreadyEstablished error = errors.New("connection has already been established. If you want to reopen a connection for this client, you have to call Disconnect first")
)

Usual networking errors common to clients

Functions

This section is empty.

Types

type Conn

type Conn interface {
	ExecuteRequest(Output) (Input, error)
	Close() error
}

Conn is common interface between TCP and UDP connections.

type DialTCPOptions

type DialTCPOptions struct {
	SkipSRVLookup bool
	DialTimeout   time.Duration
}

DialTCPOptions are the options for the DialTCP function. An empty struct (all fields set to false) is considered as the default behavior for the DialTCP function.

type DialUDPOptions

type DialUDPOptions struct {
	SkipSRVLookup                bool
	ForceUDPProtocolForSRVLookup bool
	DialTimeout                  time.Duration
}

DialUDPOptions are the options for the DialUDP function. An empty struct (all fields set to false) is considered as the default behavior for the DialUDP function.

type Input

type Input struct {
	// contains filtered or unexported fields
}

Input represents a connection input (i.e. what's read from the connection). It wraps several helpers to read from this input.

func NewInput

func NewInput(reader io.Reader) Input

NewInput returns a well-formed input.

func (*Input) Read

func (in *Input) Read(buf []byte) (int, error)

Read is just a wrapper around internal reader to make Input implements io.Reader.

func (*Input) ReadBigEndianInt16

func (in *Input) ReadBigEndianInt16() (uint16, error)

ReadBigEndianInt16 tries to read a big endian 2-bytes int (short) from the input.

func (*Input) ReadBigEndianInt32

func (in *Input) ReadBigEndianInt32() (uint32, error)

ReadBigEndianInt32 tries to read a big endian 4-bytes int from the input.

func (*Input) ReadBigEndianInt64

func (in *Input) ReadBigEndianInt64() (uint64, error)

ReadBigEndianInt64 tries to read a big endian 8-bytes int (long) from the input.

func (*Input) ReadByte

func (in *Input) ReadByte() (byte, error)

ReadByte tries to read a single byte from the input. RedByte also implements io.ByteReader interface, which is useful to use binary.ReadUvarint on the Input itself (see method ReadUVarInt).

func (*Input) ReadBytes

func (in *Input) ReadBytes(n int) ([]byte, error)

ReadBytes tries to read a slice of bytes of size n from the input.

func (*Input) ReadLittleEndianInt16

func (in *Input) ReadLittleEndianInt16() (uint16, error)

ReadLittleEndianInt16 tries to read a little endian 2-bytes int (short) from the input.

func (*Input) ReadLittleEndianInt32

func (in *Input) ReadLittleEndianInt32() (uint32, error)

ReadLittleEndianInt32 tries to read a little endian 4-bytes int from the input.

func (*Input) ReadLittleEndianInt64

func (in *Input) ReadLittleEndianInt64() (uint64, error)

ReadLittleEndianInt64 tries to read a little endian 8-bytes int (long) from the input.

func (*Input) ReadNullTerminatedString

func (in *Input) ReadNullTerminatedString() (string, error)

ReadNullTerminatedString tries to read a null terminated string from the input.

func (*Input) ReadRaknetString added in v1.3.0

func (in *Input) ReadRaknetString() (string, error)

ReadRaknetString tries to read a raknet string from the input. It is a UTF-8 string prefixed with its size in bytes as a big endian unsigned short.

func (*Input) ReadString

func (in *Input) ReadString() (string, error)

ReadString tries to read a standard minecraft protocol string from the input. It is a UTF-8 string prefixed with its size in bytes as an unsigned varint.

func (*Input) ReadUVarInt

func (in *Input) ReadUVarInt() (uint64, error)

ReadUVarInt64 tries to read an unsigned varint from the input.

func (*Input) ReadVarInt

func (in *Input) ReadVarInt() (int64, error)

ReadVarInt64 tries to read a signed varint from the input.

type Output

type Output struct {
	// contains filtered or unexported fields
}

Output represents a connection output (i.e. what's written to the connection). It wraps several helpers to write to this output.

func MergeOutputs

func MergeOutputs(out1, out2 Output) Output

MergeOutputs merge buffers of two outputs, creating a new output and without modifying any of the merged output buffer.

func NewOutput

func NewOutput() Output

NewOutput returns a well-formed Output.

func (*Output) Bytes

func (out *Output) Bytes() []byte

Bytes returns the underlying buffer.

func (*Output) Write

func (out *Output) Write(buf []byte) (int, error)

Write is just a wrapper around WriteBytes to make Request implement io.Writer

func (*Output) WriteBigEndianInt16

func (out *Output) WriteBigEndianInt16(i uint16)

WriteBigEndianInt16 writes a big endian 2-bytes int (short) to the output.

func (*Output) WriteBigEndianInt32

func (out *Output) WriteBigEndianInt32(i uint32)

WriteBigEndianInt32 writes a big endian 4-bytes int to the output.

func (*Output) WriteBigEndianInt64

func (out *Output) WriteBigEndianInt64(i uint64)

WriteBigEndianInt64 writes a big endian 8-bytes int (long) to the output.

func (*Output) WriteByte

func (out *Output) WriteByte(b byte) error

WriteByte is equivalent to WriteSingleByte but implements io.ByteWriter interface

func (*Output) WriteBytes

func (out *Output) WriteBytes(b []byte)

WriteBytes writes a slice of bytes to the output.

func (*Output) WriteLittleEndianInt16

func (out *Output) WriteLittleEndianInt16(i uint16)

WriteLittleEndianInt16 writes a little endian 2-bytes int (short) to the output.

func (*Output) WriteLittleEndianInt32

func (out *Output) WriteLittleEndianInt32(i uint32)

WriteLittleEndianInt32 writes a little endian 4-bytes int to the output.

func (*Output) WriteLittleEndianInt64

func (out *Output) WriteLittleEndianInt64(i uint64)

WriteLittleEndianInt64 writes a little endian 8-bytes int (long) to the output.

func (*Output) WriteNullTerminatedString

func (out *Output) WriteNullTerminatedString(s string)

WriteNullTerminatedString writes a null terminated string the the output.

func (*Output) WriteRaknetString added in v1.3.0

func (out *Output) WriteRaknetString(s string)

WriteRaknetString writes a raknet string to the output. It is a UTF-8 string prefixed with its size in bytes as a big endian unsigned short.

func (*Output) WriteSingleByte

func (out *Output) WriteSingleByte(b byte)

WriteSingleByte writes a single byte to the output.

func (*Output) WriteString

func (out *Output) WriteString(s string)

WriteString writes a standard minecraft protocol string to the output. It is a UTF-8 string prefixed with its size in bytes as an unsigned varint.

func (*Output) WriteUVarInt

func (out *Output) WriteUVarInt(i uint64)

WriteUVarInt64 writes an unsigned varint to the output.

func (*Output) WriteVarInt

func (out *Output) WriteVarInt(i int64)

WriteVarInt64 writes a signed varint to the output.

type TCPConn

type TCPConn struct {
	// contains filtered or unexported fields
}

TCPConn is a tcp connection.

func DialTCP

func DialTCP(hostname string, port int, options DialTCPOptions) (*TCPConn, error)

DialTCP resolve TCP address and connects to the address using TCP.

func (TCPConn) Close

func (tcpc TCPConn) Close() error

Close closes the connection.

func (TCPConn) Send

func (tcpc TCPConn) Send(req Output) (Input, error)

Send sends output to the connection, waits for response and returns the connection input. For TCP connections, as they can be read in multiple time, the connection is simply passed as the reader of the response.

func (TCPConn) SetReadDeadline added in v1.2.0

func (tcpc TCPConn) SetReadDeadline(d time.Duration) error

SetReadDeadline sets the read deadline of the underlying connection.

type UDPConn

type UDPConn struct {
	// contains filtered or unexported fields
}

UDPConn is a udp connection.

func DialUDP

func DialUDP(hostname string, port int, options DialUDPOptions) (*UDPConn, error)

DialTCP resolve UDP address and connects to the address using UDP.

func (UDPConn) Close

func (udpc UDPConn) Close() error

Close closes the connection.

func (UDPConn) Send

func (udpc UDPConn) Send(out Output) (Input, error)

Send sends output to the connection, waits for response and returns the connection input. For UDP connections, as they cannot be read in multiple time, the connection is read a single time and loaded into a buffer of size MaximumUDPDatagramLength. UDP datagram length should not be over MaximumUDPDatagramLength, so the entire datagram should be loaded. A *bytes.Buffer is the passed as the reader for the response.

func (UDPConn) SetReadDeadline added in v1.2.0

func (udpc UDPConn) SetReadDeadline(d time.Duration) error

SetReadDeadline sets the read deadline of the underlying connection.

Jump to

Keyboard shortcuts

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