-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
99 lines (92 loc) · 2.35 KB
/
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package smtpsrv
import (
"net"
"sync"
)
// Server accepts incoming SMTP connections and hands them off to Client
// instances for processing.
type Server struct {
// Receives new messages from clients
NewMessage <-chan *Message
newMessage chan *Message
finished chan bool
config *Config
listener net.Listener
// Used for synchronizing shutdown - unfortunately, this is all necessary;
// the list monitors which clients are active so that shutdown can be
// performed upon request and the mutex guards access to the list
waitGroup sync.WaitGroup
mutex sync.Mutex
clients []*Client
clientFinished chan *Client
}
// accept listens for new connections from clients. When one connects, a new
// Client instance is created, it is added to the list, and the wait group is
// incremented.
func (s *Server) accept() {
for {
conn, err := s.listener.Accept()
if err != nil {
break
} else {
c := NewClient(s.config, s.newMessage, s.clientFinished, conn)
s.waitGroup.Add(1)
s.mutex.Lock()
s.clients = append(s.clients, c)
s.mutex.Unlock()
}
}
s.finished <- true
}
// remove watches for clients that have signalled that they are done and
// removes them from the list of active clients. The wait group is also
// decremented.
func (s *Server) remove() {
for p := range s.clientFinished {
s.mutex.Lock()
for i, v := range s.clients {
if v == p {
s.clients = append(s.clients[:i], s.clients[i+1:]...)
s.waitGroup.Done()
break
}
}
s.mutex.Unlock()
}
}
// NewServer creates a new server with the specified configuration.
func NewServer(config *Config) (*Server, error) {
l, err := net.Listen("tcp", config.Addr)
if err != nil {
return nil, err
}
var (
newMessage = make(chan *Message)
s = &Server{
NewMessage: newMessage,
newMessage: newMessage,
finished: make(chan bool),
config: config,
listener: l,
clientFinished: make(chan *Client),
}
)
go s.accept()
go s.remove()
return s, nil
}
// Close shuts down the server and waits for all clients to disconnect. If
// the force parameter is true, clients will be immediately disconnected.
func (s *Server) Close(force bool) {
s.listener.Close()
<-s.finished
if force {
s.mutex.Lock()
for _, v := range s.clients {
v.Close()
}
s.mutex.Unlock()
}
s.waitGroup.Wait()
close(s.newMessage)
}