Browse Source

Correct success/error handling for multicast/broadcast sends

Jakob Borg 10 years ago
parent
commit
5ecb8bdd8a
2 changed files with 30 additions and 10 deletions
  1. 17 4
      lib/beacon/broadcast.go
  2. 13 6
      lib/beacon/multicast.go

+ 17 - 4
lib/beacon/broadcast.go

@@ -124,12 +124,14 @@ func (w *broadcastWriter) Serve() {
 			l.Debugln("addresses:", dsts)
 		}
 
+		success := 0
 		for _, ip := range dsts {
 			dst := &net.UDPAddr{IP: ip, Port: w.port}
 
 			w.conn.SetWriteDeadline(time.Now().Add(time.Second))
 			_, err := w.conn.WriteTo(bs, dst)
 			w.conn.SetWriteDeadline(time.Time{})
+
 			if err, ok := err.(net.Error); ok && err.Timeout() {
 				// Write timeouts should not happen. We treat it as a fatal
 				// error on the socket.
@@ -138,23 +140,34 @@ func (w *broadcastWriter) Serve() {
 				}
 				w.setError(err)
 				return
-			} else if err, ok := err.(net.Error); ok && err.Temporary() {
+			}
+
+			if err, ok := err.(net.Error); ok && err.Temporary() {
 				// A transient error. Lets hope for better luck in the future.
 				if debug {
 					l.Debugln(err)
 				}
 				continue
-			} else if err != nil {
+			}
+
+			if err != nil {
 				// Some other error that we don't expect. Bail and retry.
 				if debug {
 					l.Debugln(err)
 				}
 				w.setError(err)
 				return
-			} else if debug {
+			}
+
+			if debug {
 				l.Debugf("sent %d bytes to %s", len(bs), dst)
-				w.setError(nil)
 			}
+
+			success++
+		}
+
+		if success > 0 {
+			w.setError(nil)
 		}
 	}
 }

+ 13 - 6
lib/beacon/multicast.go

@@ -124,19 +124,26 @@ func (w *multicastWriter) Serve() {
 			return
 		}
 
-		var success int
-
+		success := 0
 		for _, intf := range intfs {
 			wcm.IfIndex = intf.Index
 			pconn.SetWriteDeadline(time.Now().Add(time.Second))
 			_, err = pconn.WriteTo(bs, wcm, gaddr)
 			pconn.SetWriteDeadline(time.Time{})
-			if err != nil && debug {
-				l.Debugln(err, "on write to", gaddr, intf.Name)
-			} else if debug {
+
+			if err != nil {
+				if debug {
+					l.Debugln(err, "on write to", gaddr, intf.Name)
+				}
+				w.setError(err)
+				continue
+			}
+
+			if debug {
 				l.Debugf("sent %d bytes to %v on %s", len(bs), gaddr, intf.Name)
-				success++
 			}
+
+			success++
 		}
 
 		if success > 0 {