enet

package module
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: MIT Imports: 6 Imported by: 0

README

go-enet

Enet-CSharp (github.com/nxrighthere/ENet-CSharp) bindings for Go using cgo.

Note

This is just a port of github.com/codecat/go-enet for ENet-CSharp which has independent enet implementation with a modified protocol.

Installation

Works out of the box!

  • Not Tested
  • Cross compile untested
$ go get github.com/TubbyStubby/go-enet-sharp

Usage

import "github.com/TubbyStubby/go-enet-sharp"

The API is mostly the same as the C API, except it's more object-oriented.

Example

Check upstream for examples and make changes where necessary.

Documentation

Index

Constants

View Source
const (
	// PacketFlagReliable packets must be received by the target peer and resend attempts
	// should be made until the packet is delivered
	PacketFlagReliable PacketFlags = C.ENET_PACKET_FLAG_RELIABLE

	// PacketFlagUnsequenced packets will not be sequenced with other packets not supported
	// for reliable packets
	PacketFlagUnsequenced = C.ENET_PACKET_FLAG_UNSEQUENCED

	// PacketFlagNoAllocate packets will not allocate data, and user must supply it instead
	PacketFlagNoAllocate = C.ENET_PACKET_FLAG_NO_ALLOCATE

	// PacketFlagUnreliableFragment packets will be fragmented using unreliable (instead of
	// reliable) sends if it exceeds the MTU
	PacketFlagUnreliableFragment = C.ENET_PACKET_FLAG_UNRELIABLE_FRAGMENTED

	// PacketFlagSent specifies whether the packet has been sent from all queues it has been
	// entered into
	PacketFlagSent = C.ENET_PACKET_FLAG_SENT
)

Variables

This section is empty.

Functions

func Deinitialize

func Deinitialize()

Deinitialize enet

func Initialize

func Initialize()

Initialize enet

func LinkedVersion

func LinkedVersion() string

LinkedVersion returns the linked version of enet currently being used. Returns MAJOR.MINOR.PATCH as a string.

func NewEvent added in v0.0.9

func NewEvent() *enetEvent

Types

type Address

type Address interface {
	SetHostAny()
	SetHost(ip string)
	SetPort(port uint16)

	String() string
	GetPort() uint16
}

Address specifies a portable internet address structure.

func NewAddress

func NewAddress(ip string, port uint16) Address

NewAddress creates a new address

func NewListenAddress

func NewListenAddress(port uint16) Address

NewListenAddress makes a new address ready for listening on ENET_HOST_ANY

type Event

type Event interface {
	GetType() EventType
	GetPeer() Peer
	GetChannelID() uint8
	GetData() uint32
	GetPacket() Packet
}

Event as returned by Host.Service()

type EventType

type EventType int

EventType is a type of event

const (
	// EventNone means that no event occurred within the specified time limit
	EventNone EventType = iota

	// EventConnect means that a connection request initiated by Host.Connect has completed
	// The peer field contains the peer which successfully connected
	EventConnect

	// EventDisconnect means that a peer has disconnected. This event is generated on a
	// successful completion of a disconnect initiated by Peer.Disconnect.
	// The peer field contains the peer which disconnected. The data field contains user supplied
	// data describing the disconnection, or 0, if none is available.
	EventDisconnect

	// EventReceive means that a packet has been received from a peer. The peer field
	// specifies the peer which sent the packet. The channelID field specifies the channel
	// number upon which the packet was received. The packet field contains the packet that
	// was received; this packet must be destroyed with Packet.Destroy after use.
	EventReceive

	// This means peer has disconnected due to timeout or the connection request initialized by
	// Host.Connect has timedout.
	EventDisconnectTimeout
)

type Host

type Host interface {
	Destroy()
	Service(timeout uint32) Event
	ServiceV2(event *enetEvent, timeout uint32) int

	Connect(addr Address, channelCount int, data uint32) (Peer, error)

	BroadcastBytes(data []byte, channel uint8, flags PacketFlags) error
	BroadcastPacket(packet Packet, channel uint8) error
	BroadcastString(str string, channel uint8, flags PacketFlags) error

	GetBytesSent() uint32
	GetBytesReceived() uint32
	GetPacketsSent() uint32
	GetPacketsReceived() uint32

	ResetBytesSent()
	ResetBytesReceived()
	ResetPacketsSent()
	ResetPacketsReceived()
}

Host for communicating with peers

func NewHost

func NewHost(addr Address, peerCount, channelLimit uint64, incomingBandwidth, outgoingBandwidth uint32, bufferLimit int) (Host, error)

NewHost creats a host for communicating to peers

type Packet

type Packet interface {
	Destroy()
	GetData() []byte
	GetFlags() PacketFlags
}

Packet may be sent to or received from a peer

func NewPacket

func NewPacket(data []byte, flags PacketFlags) (Packet, error)

NewPacket creates a new packet to send to peers

type PacketFlags

type PacketFlags uint32

PacketFlags are bit constants

type Peer

type Peer interface {
	GetAddress() Address

	Disconnect(data uint32)
	DisconnectNow(data uint32)
	DisconnectLater(data uint32)

	// Sets a timeout parameters for a peer. The timeout parameters control how and
	// when a peer will timeout from a failure to acknowledge reliable traffic.
	// Timeout values used in the semi-linear mechanism, where if a reliable packet
	// is not acknowledged within an average round-trip time plus a variance tolerance
	// until timeout reaches a set limit. If the timeout is thus at this limit and reliable
	// packets have been sent but not acknowledged within a certain minimum time period, the peer
	// will be disconnected. Alternatively, if reliable packets have been sent but not
	// acknowledged for a certain maximum time period, the peer will be disconnected regardless
	// of the current timeout limit value.
	SetTimeout(limit uint32, min uint32, max uint32)

	SendBytes(data []byte, channel uint8, flags PacketFlags) error
	SendString(str string, channel uint8, flags PacketFlags) error
	SendPacket(packet Packet, channel uint8) error

	// SetData sets an arbitrary value against a peer. This is useful to attach some
	// application-specific data for future use, such as an identifier.
	//
	// http://enet.bespin.org/structENetPeer.html#a1873959810db7ac7a02da90469ee384e
	//
	// Note that due to the way the enet library works, if using this you are
	// responsible for clearing this data when the peer is finished with.
	// SetData(nil) will free underlying memory and avoid any leaks.
	//
	// See http://enet.bespin.org/Tutorial.html#ManageHost for an example of this
	// in the underlying library.
	SetData(data []byte)

	// GetData returns an application-specific value that's been set
	// against this peer. This returns nil if no data has been set.
	//
	// http://enet.bespin.org/structENetPeer.html#a1873959810db7ac7a02da90469ee384e
	GetData() []byte

	PingInterval(intterval uint32)

	GetBytesSent() uint64
	GetBytesReceived() uint64
	GetPacketsSent() uint64
	GetPacketsLost() uint64
}

Peer is a peer which data packets may be sent or received from

Jump to

Keyboard shortcuts

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