Documentation
¶
Overview ¶
Package pulsenet provides a high-performance, event-driven networking loop built on top of the ENet protocol. It is designed for real-time applications that require low-latency messaging, reliable and unreliable packet delivery, and tight control over peer-to-peer communication.
This package exposes a consistent and testable interface for sending and receiving packets over the network using ENet semantics. It is suitable for multiplayer games, simulations, and distributed systems where predictable performance is critical.
Usage:
server, err := pulsenet.NewNetworkLoop("0.0.0.0", 12345, 16) if err != nil { log.Fatal(err) } defer server.Destroy() server.RegisterEventHandler(myHandler) for { server.Tick() }
AGPLv3 Licensed. Commercial licenses available only if you lost a bet.
mocks_enet.go
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EventType ¶
type EventType int
EventType describes the type of a networking event.
Possible values: - EventPacketReceived: A packet was received from a peer. - EventConnected: A peer connected. - EventDisconnected: A peer disconnected.
type NetEvent ¶
type NetEvent struct { Type EventType PeerID uint32 Opcode uint16 // Only valid for EventPacketReceived Payload []byte // Only valid for EventPacketReceived }
NetEvent is the internal event representation used by PulseNet. It is delivered to the NetEventHandler you register.
type NetEventHandler ¶
type NetEventHandler interface {
HandleEvent(NetEvent)
}
NetEventHandler is implemented by any object that wants to receive networking events. This is the only integration point for receiving packets or connection events.
type NetworkLoop ¶
type NetworkLoop interface { Tick() int SendTo(peerID uint32, channelID uint8, flags PacketFlag, data []byte) Broadcast(channelID uint8, flags PacketFlag, data []byte) RegisterEventHandler(handler NetEventHandler) Destroy() }
NetworkLoop defines the high-level interface for PulseNet's networking system. It provides methods to send messages, receive events, and manage the lifecycle of the networking loop.
Users should call Tick() periodically (e.g. every frame or tick) to process events and flush messages. Always call Destroy() before shutdown to clean up internal resources.
func NewClientLoop ¶ added in v1.0.1
func NewClientLoop(hostname string, port uint16) (NetworkLoop, error)
NewClientLoop creates a NetworkLoop and connects it to a remote server using ENet. It automatically initializes the ENet library if needed and starts the internal event loop. You must call Destroy() when done to clean up resources.
Example:
loop, err := pulsenet.NewClientLoop("127.0.0.1", 12345) if err != nil { log.Fatal(err) } defer loop.Destroy()
Note: If you run both server and client in the same process, ENet will only be initialized once. But it's your responsibility to avoid calling Destroy on both sides if you plan to reuse ENet across instances.
func NewNetworkLoop ¶
func NewNetworkLoop(addr string, port uint16, maxPeers uint64) (NetworkLoop, error)
NewNetworkLoop initializes ENet, creates a host, and returns a running network loop.
addr: Address to bind to (e.g. "0.0.0.0"). port: Port to bind to. maxPeers: Maximum number of simultaneous connections.
The returned loop must have Destroy() called on it to clean up the host and ENet state.
Returns an error if the host cannot be created.
type PacketFlag ¶
type PacketFlag uint32
PacketFlag wraps ENet's packet flags and allows the user to specify reliability, sequencing, and other behavior when sending messages.
const ( FlagReliable PacketFlag = PacketFlag(enet.PacketFlagReliable) FlagUnsequenced PacketFlag = PacketFlag(enet.PacketFlagUnsequenced) FlagNoAllocate PacketFlag = PacketFlag(enet.PacketFlagNoAllocate) FlagUnreliableFragment PacketFlag = PacketFlag(enet.PacketFlagUnreliableFragment) FlagSent PacketFlag = PacketFlag(enet.PacketFlagSent) )
Source Files
¶
- doc.go
- enet_abstractions.go
- enet_init.go
- enet_mocks.go
- enet_wrappers.go
- eventloop.go
- events.go
- handlers.go
- netloop.go
- packets.go
- send.go
- tick.go