-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
48 lines (38 loc) · 893 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
package fastcall
import "net"
// Server listens for new connections
type Server struct {
lsnr net.Listener
}
// Serve creates a listener and accepts connections
func Serve(addr string) (s *Server, err error) {
lsnr, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
return &Server{lsnr: lsnr}, nil
}
// Close stops accepting connections
func (s *Server) Close() (err error) {
if err := s.lsnr.Close(); err != nil {
return err
}
return nil
}
// Accept returns a channel of connections
func (s *Server) Accept() (c *Conn, err error) {
conn, err := s.lsnr.Accept()
if err != nil {
return nil, err
}
return New(conn), nil
}
// AcceptBuf is similar to Accept except writes in the connection are
// buffered
func (s *Server) AcceptBuf() (c *Conn, err error) {
conn, err := s.lsnr.Accept()
if err != nil {
return nil, err
}
return NewBuf(conn), nil
}