Connection status field

This commit is contained in:
Kleissner
2021-03-01 01:37:39 +01:00
parent dcc7464f46
commit ce831b31b6
2 changed files with 13 additions and 1 deletions

View File

@@ -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 {

View File

@@ -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 {