Skip to content

Commit

Permalink
udpclient: close client on errors so that reconnection can fix any re…
Browse files Browse the repository at this point in the history
…ad/write desyncs
  • Loading branch information
JamesDunne committed Jun 12, 2021
1 parent a499a68 commit ced2f1b
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions udpclient/udpclient.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package udpclient

import (
"errors"
"fmt"
"log"
"net"
Expand Down Expand Up @@ -54,6 +55,13 @@ func (c *UDPClient) ReadTimeout(d time.Duration) (b []byte, err error) {
b = make([]byte, 1500)
n, _, err = c.c.ReadFromUDP(b)
if err != nil {
b = nil
if isTimeoutError(err) {
err = c.Close()
}
if errors.Is(err, net.ErrClosed) {
err = c.Close()
}
return
}

Expand Down Expand Up @@ -121,25 +129,25 @@ func (c *UDPClient) Disconnect() {
return
}

c.isConnected = false

// close the underlying connection:
err := c.Close()
if err != nil {
c.log("%s: close: %v\n", c.name, err)
}

c.log("%s: disconnected from server '%s'\n", c.name, c.addr)

c.c = nil
}

func (c *UDPClient) Close() error {
func (c *UDPClient) Close() (err error) {
if c.c == nil {
return nil
c.isConnected = false
return
}

return c.Close()
err = c.c.Close()
c.isConnected = false
c.c = nil
return
}

func isTimeoutError(err error) bool {
Expand Down

0 comments on commit ced2f1b

Please sign in to comment.