Documentation ¶
Overview ¶
Package net pack network connection for Minecraft.
Index ¶
- Constants
- Variables
- type Conn
- type Dialer
- type Listener
- type MCDialer
- type RCONClientConn
- type RCONConn
- func (r *RCONConn) AcceptCmd() (string, error)
- func (r *RCONConn) AcceptLogin(password string) error
- func (r *RCONConn) Cmd(cmd string) error
- func (r *RCONConn) ReadPacket() (RequestID, Type int32, Payload string, err error)
- func (r *RCONConn) Resp() (resp string, err error)
- func (r *RCONConn) RespCmd(resp string) error
- func (r *RCONConn) WritePacket(RequestID, Type int32, Payload string) error
- type RCONListener
- type RCONServerConn
- type ReadWriter
- type Reader
- type Writer
Examples ¶
Constants ¶
const DefaultPort = 25565
const MaxRCONPackageSize = 4096
Variables ¶
var DefaultDialer = Dialer{}
Functions ¶
This section is empty.
Types ¶
type Conn ¶
Conn is a minecraft Connection
func DialMC ¶
DialMC create a Minecraft connection Lookup SRV records only if port doesn't exist or equals to 0.
func DialMCTimeout ¶
DialMCTimeout acts like DialMC but takes a timeout.
func WrapConn ¶
WrapConn warp a net.Conn to MC-Conn Helps you modify the connection process (e.g. using DialContext).
func (*Conn) ReadPacket ¶
ReadPacket read a Packet from Conn.
func (*Conn) SetThreshold ¶
SetThreshold set threshold to Conn. The data packet with length equal or longer then threshold will be compressed when sending.
type Dialer ¶
Dialer implements MCDialer interface.
It can be easily convert from net.Dialer.
dialer := net.Dialer{} mcDialer := (*Dialer)(&dialer)
type Listener ¶
A Listener is a minecraft Listener
type MCDialer ¶
type MCDialer interface { // The DialMCContext dial TCP connection to a minecraft server, and warp the net.Conn by calling [WrapConn]. DialMCContext(ctx context.Context, addr string) (*Conn, error) }
MCDialer provide DialMCContext method, can be used to dial a minecraft server. Dialer is its default implementation, and support SRV lookup.
Typically, if you want to use built-in proxies or custom dialer, you can hook go-mc/bot package by implement this interface. When implementing a custom MCDialer, SRV lookup is optional.
type RCONClientConn ¶
type RCONClientConn interface { Cmd(cmd string) error Resp() (resp string, err error) Close() error }
func DialRCON ¶
func DialRCON(addr string, password string) (client RCONClientConn, err error)
DialRCON connect to a RCON server and return the connection after login. We promise the returned RCONClientConn is an RCONConn, so you can convert them by type assertions if you need call the ReadPacket() or WritePacket() methods.
Example ¶
conn, err := DialRCON("localhost:25575", "CORRECT_PASSWORD") if err != nil { panic(err) } defer conn.Close() err = conn.Cmd("TEST COMMAND") if err != nil { panic(err) } for { // Server may send the result in more(or less) than one packet. // See: https://wiki.vg/RCON#Fragmentation resp, err := conn.Resp() if err != nil { fmt.Print(err) } fmt.Printf("Server response: %q", resp) break }
Output:
type RCONListener ¶
func ListenRCON ¶
func ListenRCON(addr string) (*RCONListener, error)
ListenRCON announces on the local network address, accepting RCON clients.
Example ¶
l, err := ListenRCON("localhost:25575") if err != nil { panic(err) } defer l.Close() for { conn, err := l.Accept() if err != nil { fmt.Printf("Accept connection error: %v", err) } go func(conn RCONServerConn) { err = conn.AcceptLogin("CORRECT_PASSWORD") if err != nil { fmt.Printf("Login fail: %v", err) } defer conn.Close() // The client is login, we are accepting its command for { cmd, err := conn.AcceptCmd() if err != nil { fmt.Printf("Read command fail: %v", err) break } resp := handleCommand(cmd) // Return the result of command. // It's allowed to call RespCmd multiple times for one command. err = conn.RespCmd(resp) if err != nil { fmt.Printf("Response command fail: %v", err) break } } }(conn) }
Output:
func (*RCONListener) Accept ¶
func (r *RCONListener) Accept() (RCONServerConn, error)
Accept RCON connection for client. We promise the returned RCONServerConn is an RCONConn, so you can convert them by type assertions if you need call the ReadPacket() or WritePacket() methods.
type RCONServerConn ¶
type RCONServerConn interface { AcceptLogin(password string) error AcceptCmd() (cmd string, err error) RespCmd(resp string) error Close() error }
RCONServerConn is the connection in the server side.