-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
54 lines (49 loc) · 1.23 KB
/
main.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
package main
import (
"flag"
"fmt"
"github.com/go-gevent/gevent"
"log"
"strings"
"time"
"github.com/go-gevent/gevent/ringbuffer"
)
func main() {
var port int
var loops int
var udp bool
var trace bool
var reuseport bool
flag.IntVar(&port, "port", 5007, "server port")
flag.BoolVar(&udp, "udp", false, "listen on udp")
flag.BoolVar(&reuseport, "reuseport", false, "reuseport (SO_REUSEPORT)")
flag.BoolVar(&trace, "trace", false, "print packets to console")
flag.IntVar(&loops, "loops", 1, "num loops")
flag.Parse()
var events gevent.Events
events.NumLoops = loops
events.Serving = func(srv gevent.Server) (action gevent.Action) {
log.Printf("echo server started on port %d (loops: %d)", port, srv.NumLoops)
if reuseport {
log.Printf("reuseport")
}
return
}
events.Data = func(c *gevent.Conn, in *ringbuffer.RingBuffer) (out []byte, action gevent.Action) {
first, end := in.PeekAll()
if trace {
log.Printf("%s", strings.TrimSpace(string(first)+string(end)))
}
out = first
if len(end) > 0 {
out = append(out, end...)
}
in.RetrieveAll()
return
}
scheme := "tcp"
if udp {
scheme = "udp"
}
log.Fatal(gevent.Serve(events, time.Second*10, fmt.Sprintf("%s://:%d?reuseport=%t", scheme, port, reuseport)))
}