// listen for gui client to connect to our socket func accept(listener *net.UnixListener, ch chan []byte) { for { // we are going to eat the serial data until // we get a socket connection so we don't block the channel select { case <-ch: log.Println("eating serial data") default: } // set timeout to fall through so we can check the channel for // serial data listener.SetDeadline(time.Now().Add(100 * time.Millisecond)) conn, err := listener.AcceptUnix() if nil != err { if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() { continue } log.Println(err) } else { // we have connection, call handle, we only handle one connection // so no goroutine here handleSocket(conn, ch) } } }
func listen(sock *net.UnixListener, logChan chan []byte) { defer sock.Close() defer wg.Done() // Timeout after 2 seconds sock.SetDeadline(time.Now().Add((2 * time.Second))) for run { client, err := sock.Accept() if err != nil { ne, ok := err.(net.Error) if !ok || !ne.Temporary() { // Non-temporary (fatal) error log.Printf("Error accepting client:\n%v", err) break } } else { wg.Add(1) go handle(client, logChan) } } }