From 89a9beddbb9267b107fec73a08b7ea5b3400cecd Mon Sep 17 00:00:00 2001 From: Benedetto Date: Mon, 28 Dec 2020 20:07:36 +0100 Subject: [PATCH] add on ack handler --- enetapi.go | 3 +++ host.go | 4 ++++ peer.go | 4 ++++ 3 files changed, 11 insertions(+) diff --git a/enetapi.go b/enetapi.go index 2e7ad99..f970823 100644 --- a/enetapi.go +++ b/enetapi.go @@ -9,11 +9,14 @@ type PeerEventHandler func(host Host, endpoint string, ret int) // chanid == 0xff unreliable data type DataEventHandler func(host Host, endpoint string, chanid uint8, payload []byte) +type AckEventHandler func(host Host, endpoint string, chanid uint8, payload []byte) + // push a signal to chan os.Signal will make host quit run loop type Host interface { SetConnectionHandler(PeerEventHandler) SetDisconnectionHandler(PeerEventHandler) SetDataHandler(DataEventHandler) + SetAckHandler(AckEventHandler) Connect(endpoint string) Disconnect(endpoint string) Write(endpoint string, chanid uint8, dat []byte, customHeader *EnetPacketHeader) diff --git a/host.go b/host.go index 7df190f..2d7ccac 100644 --- a/host.go +++ b/host.go @@ -34,6 +34,7 @@ type enet_host struct { notify_connected PeerEventHandler notify_disconnected PeerEventHandler notify_data DataEventHandler + notify_ack AckEventHandler } func NewHost(addr string) (Host, error) { @@ -349,6 +350,9 @@ func (host *enet_host) SetDisconnectionHandler(h PeerEventHandler) { func (host *enet_host) SetDataHandler(h DataEventHandler) { host.notify_data = h } +func (host *enet_host) SetAckHandler(h AckEventHandler) { + host.notify_ack = h +} func (host *enet_host) update_rcv_statis(rcvd int) { host.rcvd_bytes += rcvd host.last_recv_time = host.now diff --git a/peer.go b/peer.go index ad89c3a..83d39be 100644 --- a/peer.go +++ b/peer.go @@ -119,6 +119,10 @@ func (peer *enet_peer) when_enet_incoming_ack(header EnetPacketHeader, payload [ peer.update_throttle(rtt) debugf("peer in-ack %v\n", peer.remote_addr) + if peer.host.notify_ack != nil { + peer.host.notify_ack(peer.host, peer.endpoint, header.ChannelID, payload) + } + ch := peer.channel_from_id(header.ChannelID) ch.outgoing_ack(ack.SN) for i := ch.outgoing_slide(); i != nil; i = ch.outgoing_slide() {