ping and pong
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m53s

This commit is contained in:
matst80
2024-11-09 20:09:39 +01:00
parent 84cfc142e1
commit 198f24ede0

View File

@@ -162,6 +162,8 @@ const (
RemoteGrainChanged = uint16(4) RemoteGrainChanged = uint16(4)
AckChange = uint16(5) AckChange = uint16(5)
AckError = uint16(6) AckError = uint16(6)
Ping = uint16(7)
Pong = uint16(8)
) )
func (p *SyncedPool) handleConnection(conn net.Conn) { func (p *SyncedPool) handleConnection(conn net.Conn) {
@@ -180,6 +182,13 @@ func (p *SyncedPool) handleConnection(conn net.Conn) {
// return // return
// } // }
switch packet.MessageType { switch packet.MessageType {
case Ping:
err = SendPacket(conn, Pong, func(w io.Writer) error {
return nil
})
if err != nil {
log.Printf("Error sending pong: %v\n", err)
}
case RemoteNegotiate: case RemoteNegotiate:
negotiationCount.Inc() negotiationCount.Inc()
data := make([]byte, packet.DataLength) data := make([]byte, packet.DataLength)
@@ -318,6 +327,20 @@ func (p *SyncedPool) AddRemoteWithConnection(address string, connection net.Conn
return p.addRemoteHost(address, remote) return p.addRemoteHost(address, remote)
} }
func DoPing(connection net.Conn) error {
SendPacket(connection, Ping, func(w io.Writer) error {
return nil
})
t, _, err := ReceivePacket(connection)
if err != nil {
return err
}
if t != Pong {
return fmt.Errorf("unexpected message type %d", t)
}
return nil
}
func (p *SyncedPool) addRemoteHost(address string, remote RemoteHost) error { func (p *SyncedPool) addRemoteHost(address string, remote RemoteHost) error {
for _, r := range p.remotes { for _, r := range p.remotes {
if r.Host == address { if r.Host == address {
@@ -326,6 +349,11 @@ func (p *SyncedPool) addRemoteHost(address string, remote RemoteHost) error {
} }
} }
err := DoPing(remote.connection)
if err != nil {
log.Printf("Error pinging remote %s: %v\n", address, err)
}
p.remotes = append(p.remotes, remote) p.remotes = append(p.remotes, remote)
connectedRemotes.Set(float64(len(p.remotes))) connectedRemotes.Set(float64(len(p.remotes)))
log.Printf("Added remote %s\n", remote.Host) log.Printf("Added remote %s\n", remote.Host)