raknet

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2019 License: MIT Imports: 14 Imported by: 0

README

go-raknet

go-raknet is a library that implements a basic version of the RakNet protocol, which is used for games such as Minecraft (Bedrock Edition) and Terraria (Mobile Edition). It implements Unreliable, Reliable and ReliableOrdered packets and sends user packets as ReliableOrdered.

go-raknet attempts to abstract away direct interaction with RakNet, and provides simple to use, idiomatic Go API used to listen for connections or connect to servers.

Getting started

Prerequisites

To use this library, Go must be installed. go-raknet does not depend on any other libraries than the standard Go library.

Usage

go-raknet can be used for both clients and servers, (and proxies, when combined) in a way very similar to the standard net.TCP* functions.

Basic RakNet server:

package main

import (
	"github.com/sandertv/raknet"
)

func main() {
	// We start a listener with in this case a specific protocol.
    listener, _ := raknet.Listen("0.0.0.0:19132", raknet.Protocol(raknet.OfficialProtocol))
    defer listener.Close()
    for {
        conn, _ := listener.Accept()
        
        b := make([]byte, conn.MaxPacketSize())
        _, _ = conn.Read(b)
        _, _ = conn.Write([]byte{1, 2, 3})
        
        conn.Close()
    }
}

Basic RakNet client:

package main

import (
	"github.com/sandertv/raknet"
)

func main() {
    conn, _ := raknet.Dial("mco.mineplex.com:19132")
    defer conn.Close()
    
    b := make([]byte, conn.MaxPacketSize())
    _, _ = conn.Write([]byte{1, 2, 3})
    _, _ = conn.Read(b)
}

For an example on how to apply these and other methods in order to create a proxy, see the examples/proxy folder.

Documentation

Documentation may be found here.

Documentation

Index

Constants

View Source
const (
	// MinecraftProtocol is the current default Minecraft RakNet protocol version. This is Minecraft specific.
	// For the default RakNet, use OfficialProtocol.
	// MinecraftProtocol is the default in go-raknet.
	MinecraftProtocol byte = 9
	// OfficialProtocol is the protocol version of the official open source RakNet library.
	OfficialProtocol byte = 6
)

Variables

This section is empty.

Functions

func ErrConnectionClosed

func ErrConnectionClosed(err error) bool

ErrConnectionClosed checks if the error passed was an error caused by reading from a Conn of which the connection was closed.

func ErrReadTimeout

func ErrReadTimeout(err error) bool

ErrReadTimeout checks if the error passed was an error caused by a timeout set when reading from the Conn.

func Ping

func Ping(address string, settings ...Setting) (response []byte, err error)

Ping sends a ping to an address and returns the response obtained. If successful, a non-nil response byte slice containing the data is returned. If the ping failed, an error is returned describing the failure. Note that the packet sent to the server may be lost due to the nature of UDP. If this is the case, an error is returned which implies a timeout occurred.

Types

type Conn

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

Conn represents a connection to a specific client. It is not a real connection, as UDP is connectionless, but rather a connection emulated using RakNet. Methods may be called on Conn from multiple goroutines simultaneously.

func Dial

func Dial(address string, settings ...Setting) (*Conn, error)

Dial attempts to dial a RakNet connection to the address passed. The address may be either an IP address or a hostname, combined with a port that is separated with ':'. Dial will attempt to dial a connection within 10 seconds. If not all packets are received after that, the connection will timeout and an error will be returned. Optionally, a variadic amount of settings may be passed into Dial to specify additional behaviour.

func (*Conn) Close

func (conn *Conn) Close() error

Close closes the connection. All blocking Read or Write actions are cancelled and will return an error.

func (*Conn) Latency

func (conn *Conn) Latency() int

Latency returns the last measured latency between both ends of the connection in milliseconds. The latency is updated every 4 seconds. The latency returned is the time it takes to send one packet from one end to the other end of the connection. It is not the round-trip time.

func (*Conn) LocalAddr

func (conn *Conn) LocalAddr() net.Addr

LocalAddr returns the local address of the connection, which is always the same as the listener's.

func (*Conn) Read

func (conn *Conn) Read(b []byte) (n int, err error)

Read reads from the connection into the byte slice passed. If successful, the amount of bytes read n is returned, and the error returned will be nil. Read blocks until a packet is received over the connection, or until the session is closed or the read times out, in which case an error is returned.

func (*Conn) RemoteAddr

func (conn *Conn) RemoteAddr() net.Addr

RemoteAddr returns the remote address of the connection, meaning the address this connection leads to.

func (*Conn) SetDeadline

func (conn *Conn) SetDeadline(t time.Time) error

SetDeadline sets the deadline of the connection for both Read and Write. SetDeadline is equivalent to calling both SetReadDeadline and SetWriteDeadline.

func (*Conn) SetReadDeadline

func (conn *Conn) SetReadDeadline(t time.Time) error

SetReadDeadline sets the read deadline of the connection. An error is returned only if the time passed is before time.Now(). Calling SetReadDeadline means the next Read call that exceeds the deadline will fail and return an error. Setting the read deadline to the default value of time.Time removes the deadline.

func (*Conn) SetWriteDeadline

func (conn *Conn) SetWriteDeadline(t time.Time) error

SetWriteDeadline has no behaviour. It is merely there to satisfy the net.Conn interface.

func (*Conn) Write

func (conn *Conn) Write(b []byte) (n int, err error)

Write writes a buffer b over the RakNet connection. The amount of bytes written n is always equal to the length of the bytes written if the write was successful. If not, an error is returned and n is 0. Write may be called simultaneously from multiple goroutines, but will write one by one.

type Listener

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

Listener implements a RakNet connection listener. It follows the same methods as those implemented by the TCPListener in the net package.

func Listen

func Listen(address string, settings ...Setting) (*Listener, error)

Listen listens on the address passed and returns a listener that may be used to accept connections. If not successful, an error is returned. The address follows the same rules as those defined in the net.TCPListen() function.

func (*Listener) Accept

func (listener *Listener) Accept() (*Conn, error)

Accept blocks until a connection can be accepted by the listener. If successful, Accept returns a connection that is ready to send and receive data. If not successful, a nil listener is returned and an error describing the problem.

func (*Listener) Close

func (listener *Listener) Close() error

Close closes the listener so that it may be cleaned up. It makes sure the goroutine handling incoming packets is able to be freed.

func (*Listener) HijackPong

func (listener *Listener) HijackPong(address string) error

HijackPong hijacks the pong response from a server at an address passed. The listener passed will continuously update its pong data by hijacking the pong data of the server at the address. The hijack will last until the listener is shut down. If the address passed could not be resolved, an error is returned. Calling HijackPong means that any current and future pong data set using listener.PongData is overwritten each update. A list of settings may be passed in to specify additional settings such as the protocol version for the ping/pong.

func (*Listener) PongData

func (listener *Listener) PongData(data []byte)

PongData sets the pong data that is used to respond with when a client sends a ping. It usually holds game specific data that is used to display in a server list. If a data slice is set with a size bigger than math.MaxInt16, the function panics.

type Setting added in v0.3.0

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

Setting is a container of a setting with a key and a value.

func Protocol added in v0.4.0

func Protocol(protocolVersion byte) Setting

Protocol returns a version setting using the protocol version passed. The setting may be passed into a raknet.Dial() or raknet.Listen() call.

Directories

Path Synopsis
examples
proxy command

Jump to

Keyboard shortcuts

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