forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acceptor.go
104 lines (86 loc) · 2.55 KB
/
acceptor.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
100
101
102
103
104
package quickfix
import (
"fmt"
"github.com/quickfixgo/quickfix/config"
"net"
"strconv"
)
//Acceptor accepts connections from FIX clients and manages the associated sessions.
type Acceptor struct {
app Application
settings *Settings
logFactory LogFactory
storeFactory MessageStoreFactory
globalLog Log
qualifiedSessionIDs map[SessionID]SessionID
quitChan chan bool
}
//Start accepting connections.
func (a *Acceptor) Start() (e error) {
port, err := a.settings.GlobalSettings().IntSetting(config.SocketAcceptPort)
if err != nil {
return fmt.Errorf("error fetching required SocketAcceptPort: %v", err)
}
server, err := net.Listen("tcp", ":"+strconv.Itoa(port))
if server == nil {
return err
}
connections := a.listenForConnections(server)
a.quitChan = make(chan bool)
go func() {
for cxn := range connections {
go handleAcceptorConnection(cxn, a.qualifiedSessionIDs, a.globalLog, a.quitChan)
}
}()
return
}
//Stop logs out existing sessions, close their connections, and stop accepting new connections.
func (a *Acceptor) Stop() {
defer func() {
_ = recover() // suppress sending on closed channel error
}()
close(a.quitChan)
}
//NewAcceptor creates and initializes a new Acceptor.
func NewAcceptor(app Application, storeFactory MessageStoreFactory, settings *Settings, logFactory LogFactory) (*Acceptor, error) {
a := new(Acceptor)
a.app = app
a.storeFactory = storeFactory
a.settings = settings
a.logFactory = logFactory
a.qualifiedSessionIDs = make(map[SessionID]SessionID)
var err error
a.globalLog, err = logFactory.Create()
if err != nil {
return a, err
}
for sessionID, sessionSettings := range settings.SessionSettings() {
//unqualified sessionIDs must be unique
unqualifiedSessionID := SessionID{
BeginString: sessionID.BeginString,
TargetCompID: sessionID.TargetCompID,
SenderCompID: sessionID.SenderCompID}
if _, dup := a.qualifiedSessionIDs[unqualifiedSessionID]; dup {
return nil, fmt.Errorf("duplicate SessionID %v", unqualifiedSessionID)
}
a.qualifiedSessionIDs[unqualifiedSessionID] = sessionID
if err = createSession(sessionID, storeFactory, sessionSettings, logFactory, app); err != nil {
return nil, err
}
}
return a, nil
}
func (a *Acceptor) listenForConnections(listener net.Listener) (ch chan net.Conn) {
ch = make(chan net.Conn)
go func() {
for {
netConn, err := listener.Accept()
if netConn == nil {
a.globalLog.OnEventf("Couldn't Accept: %v", err.Error())
continue
}
ch <- netConn
}
}()
return ch
}