/
server.go
62 lines (48 loc) · 911 Bytes
/
server.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
package commandserver
import (
"log"
"net"
"net/textproto"
)
type Server struct {
address string
mux *ServeMux
}
func NewServer(address string) *Server {
return &Server{
address: address,
}
}
func (s *Server) ListenAndServe(mux *ServeMux) error {
s.mux = mux
ln, err := net.Listen("tcp", s.address)
if err != nil {
return err
}
for {
conn, err := ln.Accept()
if err != nil {
log.Printf("Error accepting connection: %s\n", err)
}
go s.serve(conn)
}
}
func (s *Server) serve(conn net.Conn) {
c := textproto.NewConn(conn)
// state controlled processing here
for {
cr, err := NewCommandRequest(c)
if err != nil {
log.Println(err)
continue
}
// start of a muxer
// s.handler[verb](c)
sr := &StatusResponse{}
if s.mux == nil {
panic("awp: no servemux specified")
}
s.mux.ServeAWP(cr, sr)
c.PrintfLine("%d %s\r\n", sr.Code, sr.Status)
}
}