-
Notifications
You must be signed in to change notification settings - Fork 176
/
reverb.go
72 lines (67 loc) · 1.38 KB
/
reverb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// ex8.8 is a reverb server that disconnects inactive clients.
package main
import (
"bufio"
"fmt"
"io"
"log"
"net"
"strings"
"sync"
"time"
)
func echo(c net.Conn, shout string, delay time.Duration, wg *sync.WaitGroup) {
defer wg.Done()
fmt.Fprintln(c, "\t", strings.ToUpper(shout))
time.Sleep(delay)
fmt.Fprintln(c, "\t", shout)
time.Sleep(delay)
fmt.Fprintln(c, "\t", strings.ToLower(shout))
}
func scan(r io.Reader, lines chan<- string) {
s := bufio.NewScanner(r)
for s.Scan() {
lines <- s.Text()
}
// scan will most likely try to read from the connection after it's closed
// by handleConn. I don't know how to avoid this. Go seems to shun async io
// in favour of goroutines, so it probably isn't worth avoiding.
if s.Err() != nil {
log.Print("scan: ", s.Err())
}
}
func handleConn(c net.Conn) {
wg := &sync.WaitGroup{}
defer func() {
wg.Wait()
c.Close()
}()
lines := make(chan string)
go scan(c, lines)
timeout := 2 * time.Second
timer := time.NewTimer(2 * time.Second)
for {
select {
case line := <-lines:
timer.Reset(timeout)
wg.Add(1)
go echo(c, line, 1*time.Second, wg)
case <-timer.C:
return
}
}
}
func main() {
l, err := net.Listen("tcp", "localhost:8000")
if err != nil {
log.Fatal(err)
}
for {
conn, err := l.Accept()
if err != nil {
log.Print(err) // e.g., connection aborted
continue
}
go handleConn(conn)
}
}