From ce831b31b6f0ceb253f22e8b8f3f7c7224ddc55d Mon Sep 17 00:00:00 2001 From: Kleissner Date: Mon, 1 Mar 2021 01:37:39 +0100 Subject: [PATCH] Connection status field --- Connection.go | 12 ++++++++++++ Network.go | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Connection.go b/Connection.go index c626e44..e17e4b3 100644 --- a/Connection.go +++ b/Connection.go @@ -24,8 +24,16 @@ type Connection struct { LastPacketOut time.Time // Last time an outgoing packet was attempted to send. LastPingOut time.Time // Last ping out. Expires time.Time // Inactive connections only: Expiry date. If it does not become active by that date, it will be considered expired and removed. + Status int // 0 = Active established connection, 1 = Inactive, 2 = Removed } +// Connection status +const ( + ConnectionActive = iota + ConnectionInactive + ConnectionRemoved +) + // Equal checks if the connection was established other the same network adapter using the same IP address. Port is intentionally not checked. func (c *Connection) Equal(other *Connection) bool { return c.Address.IP.Equal(other.Address.IP) && c.Network.address.IP.Equal(other.Network.address.IP) @@ -69,6 +77,7 @@ func (peer *PeerInfo) registerConnection(incoming *Connection) (result *Connecti } // elevate by adding to active and mark as latest active + connection.Status = ConnectionActive peer.connectionLatest = connection peer.connectionActive = append(peer.connectionActive, connection) @@ -96,6 +105,7 @@ func (peer *PeerInfo) invalidateActiveConnection(input *Connection) { defer peer.Unlock() // Change the status to inactive and start the expiration. If the connection does not become valid by that date, it will be removed. + input.Status = ConnectionInactive input.Expires = time.Now().Add(connectionRemove * time.Second) // remove from connectionLatest if selected so it won't be used by standard send function @@ -125,6 +135,8 @@ func (peer *PeerInfo) removeInactiveConnection(input *Connection) { peer.Lock() defer peer.Unlock() + input.Status = ConnectionRemoved + for n, connection := range peer.connectionInactive { if connection == input { diff --git a/Network.go b/Network.go index 1a3278d..c79e98c 100644 --- a/Network.go +++ b/Network.go @@ -129,7 +129,7 @@ func packetWorker(packets <-chan networkWire) { continue } - connection := &Connection{Network: packet.network, Address: packet.sender} + connection := &Connection{Network: packet.network, Address: packet.sender, Status: ConnectionActive} peer := PeerlistLookup(senderPublicKey) if peer != nil {