Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddHandler ¶
func AddHandler[T any](s *PacketSystem, id VarInt, handler func(packet *T))
AddHandler adds a new packet handling function to the packet system for packets that have the provided id. The handler function will be called with the packet data whenever one is received
func MarshalPacket ¶
func MarshalPacket(p *PacketBuffer, packet Packet) error
func UnMarshalPacket ¶
func UnMarshalPacket(p *PacketBuffer, out any) error
Types ¶
type Connection ¶
type Connection struct { Lock *sync.RWMutex Open bool ReadBuffer *PacketBuffer WriteBuffer *PacketBuffer *websocket.Conn }
Connection structure for storing the current connection state as well as the reference to the websocket connection and write lock for preventing concurrently packet writes
func (*Connection) Send ¶
func (conn *Connection) Send(packet Packet)
Send function for sending packets to the client. Will only send if the connection is open. Acquires write locks before sending packet
type PacketBuffer ¶
func NewPacketBuffer ¶
func NewPacketBuffer() *PacketBuffer
func (*PacketBuffer) ReadByteArray ¶
func (p *PacketBuffer) ReadByteArray(length VarInt) ([]byte, error)
func (*PacketBuffer) ReadString ¶
func (p *PacketBuffer) ReadString() (string, error)
func (*PacketBuffer) WriteString ¶
func (p *PacketBuffer) WriteString(value string) error
func (*PacketBuffer) WriteVarInt ¶
func (p *PacketBuffer) WriteVarInt(value VarInt) error
type PacketDecoder ¶
type PacketDecoder func(c *Connection)
PacketDecoder structure of a function which decodes a packet
type PacketSystem ¶
type PacketSystem struct { Handlers map[VarInt]PacketDecoder ErrorHandler ErrorHandler }
PacketSystem a structure for storing a list of handlers for different packet id values
func NewPacketSystem ¶
func NewPacketSystem() *PacketSystem
NewPacketSystem creates a new packet system and returns a handle to the newly created packet system
func (*PacketSystem) DecodePacket ¶
func (s *PacketSystem) DecodePacket(c *Connection) error
DecodePacket handles decoding of any packets received by the packet system. Uses the connection and the ReadJSON function to take the incoming data and then calls the handler function. This function will return an error if it failed to decode the packet
func (*PacketSystem) SetErrorHandler ¶
func (s *PacketSystem) SetErrorHandler(handler ErrorHandler)
func (*PacketSystem) UpgradeAndListen ¶
func (s *PacketSystem) UpgradeAndListen(w http.ResponseWriter, r *http.Request, callback func(conn *Connection, err error))
UpgradeAndListen upgrades the provided http connection to a websocket connection and listens for packet data in a loop. Calls the provided callback function before starting the loop