Skip to content

Commit

Permalink
add on ack handler
Browse files Browse the repository at this point in the history
  • Loading branch information
bennesp committed Dec 28, 2020
1 parent db0d59b commit 89a9bed
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions enetapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 89a9bed

Please sign in to comment.