func new_packet(data []byte, flags Flag) *C.ENetPacket { c_packet := C.enet_packet_create(unsafe.Pointer(&data[0]), C.size_t(len(data)), C.enet_uint32(flags)) if c_packet == nil { panic("Allocation failure inside ENet") } return c_packet }
// enet_host_create func CreateHost(address *net.UDPAddr, peerCount uint, channelLimit uint, incomingBandwidth uint32, outgoingBandwith uint32) (*Host, error) { var c_host *C.ENetHost if address != nil { c_addr := conv_addr(address) c_host = C.enet_host_create(&c_addr, C.size_t(peerCount), C.size_t(channelLimit), C.enet_uint32(incomingBandwidth), C.enet_uint32(outgoingBandwith)) } else { c_host = C.enet_host_create(nil, C.size_t(peerCount), C.size_t(channelLimit), C.enet_uint32(incomingBandwidth), C.enet_uint32(outgoingBandwith)) } if c_host == nil { return nil, errors.New("ENet failed to create an ENetHost.") } C.enet_host_compress(c_host, nil) return &Host{c_host, make(map[*C.ENetPeer]*Peer)}, nil }
func (p *Peer) Send(payload []byte, flags PacketFlag, channel uint8) { if len(payload) == 0 { return } packet := C.enet_packet_create(unsafe.Pointer(&payload[0]), C.size_t(len(payload)), C.enet_uint32(flags)) p.out <- outgoingPacket{packet, channel} }
// enet_host_service func (host *Host) Service(timeout time.Duration) (*Event, error) { if timeout < 0 { return nil, errors.New("Timeout duration was negative") } var c_event C.ENetEvent ret := C.enet_host_service(host.host, &c_event, C.enet_uint32(timeout/time.Millisecond)) return host.ret_to_error(&c_event, ret) }
// enet_host_connect func (host *Host) Connect(address *net.UDPAddr, channelCount uint, data uint) (*Peer, error) { c_addr := conv_addr(address) c_peer := C.enet_host_connect(host.host, &c_addr, C.size_t(channelCount), C.enet_uint32(data)) if c_peer == nil { return nil, errors.New("No available peers for initiating an ENet connection.") } peer := &Peer{c_peer} host.peers[c_peer] = peer return peer, nil }
func (p *ENetPeer) Timeout(timeoutLimit, timeoutMinimum, timeoutMaximum int) { C.enet_peer_timeout((*C.ENetPeer)(p), C.enet_uint32(timeoutLimit), C.enet_uint32(timeoutMinimum), C.enet_uint32(timeoutMaximum)) }
// Currently this method requires that the data be written in byte form. // Ideally it would be nice to write arbitrary data such as one can do in the C ENet library. func NewPacket(data []byte, dataLength int, flags ENetPacketFlag) *ENetPacket { return (*ENetPacket)(C.enet_packet_create(unsafe.Pointer(&data[0]), C.size_t(dataLength), C.enet_uint32(flags))) }
func (h *ENetHost) Connect(a *ENetAddress, channelCount int, data int) *ENetPeer { return (*ENetPeer)(C.enet_host_connect((*C.ENetHost)(h), (*C.ENetAddress)(a), C.size_t(channelCount), C.enet_uint32(data))) }
func (p *ENetPeer) ThrottleConfigure(interval, acceleration, deceleration int) { C.enet_peer_throttle_configure((*C.ENetPeer)(p), C.enet_uint32(interval), C.enet_uint32(acceleration), C.enet_uint32(deceleration)) }
func (a *ENetAddress) SetHost(h uint) { a.host = C.enet_uint32(h) }
func NewHost(a *ENetAddress, peerCount, channelLimit int, incomingBandwidth, outgoingBandwidth int) *ENetHost { return (*ENetHost)(C.enet_host_create((*C.ENetAddress)(a), C.size_t(peerCount), C.size_t(channelLimit), C.enet_uint32(incomingBandwidth), C.enet_uint32(outgoingBandwidth))) }
func (p *ENetPeer) DisconnectLater(data int) { C.enet_peer_disconnect_later((*C.ENetPeer)(p), C.enet_uint32(data)) }
func (h *ENetHost) Service(event *ENetEvent, timeout int) int { return int(C.enet_host_service((*C.ENetHost)(h), (*C.ENetEvent)(event), C.enet_uint32(timeout))) }
func (p *ENetPeer) PingInterval(pingInterval int) { C.enet_peer_ping_interval((*C.ENetPeer)(p), C.enet_uint32(pingInterval)) }
// enet_peer_disconnect_later func (peer *Peer) DisconnectLater(data uint) { C.enet_peer_disconnect_later(peer.peer, C.enet_uint32(data)) }
// enet_peer_disconnect func (peer *Peer) Disconnect(data uint) { C.enet_peer_disconnect(peer.peer, C.enet_uint32(data)) }
func (h *ENetHost) BandwidthLimit(incomingBandwidth, outgoingBandwidth int) { C.enet_host_bandwidth_limit((*C.ENetHost)(h), C.enet_uint32(incomingBandwidth), C.enet_uint32(incomingBandwidth)) }
func (p *Peer) Disconnect(reason uint32) { delete(peers, p.cPeer) C.enet_peer_disconnect(p.cPeer, C.enet_uint32(reason)) }
func (p *ENetPeer) DisconnectNow(data int) { C.enet_peer_disconnect_now((*C.ENetPeer)(p), C.enet_uint32(data)) }