Created
September 3, 2011 03:25
-
-
Save davecheney/1190497 to your computer and use it in GitHub Desktop.
ea
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net" | |
"os" | |
) | |
type worker struct { | |
conn *net.UDPConn | |
pkts chan []byte | |
} | |
func (w *worker) run() { | |
for { | |
buf := make([]byte, 1500) | |
read, _, err := w.conn.ReadFromUDP(buf) | |
if err != nil { | |
log.Fatalf("could not ReadFromUDP: %s", err) | |
} | |
w.pkts <- buf[:read] | |
} | |
} | |
func listen(pkts chan []byte) os.Error { | |
c, err := net.ListenUDP("udp4", &net.UDPAddr { net.IPv4zero, 5353 }) | |
if err != nil { | |
return err | |
} | |
// BUG should be &worker | |
w := worker{ c, pkts } | |
go w.run() | |
return nil | |
} | |
func main() { | |
pkts := make(chan []byte, 16) | |
if err := listen(pkts); err != nil { | |
log.Fatal(err) | |
} | |
<- pkts // code will pause here | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment