This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
newServer.go
95 lines (84 loc) · 2.26 KB
/
newServer.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
package webwire
import (
"errors"
"fmt"
"net/url"
"sync"
"github.com/qbeon/webwire-go/message"
)
// NewServer creates a new webwire server instance
func NewServer(
implementation ServerImplementation,
opts ServerOptions,
transport Transport,
) (instance Server, err error) {
if implementation == nil {
return nil, errors.New("missing server implementation")
}
if transport == nil {
return nil, errors.New("missing transport layer implementation")
}
if err := opts.Prepare(); err != nil {
return nil, err
}
sessionsEnabled := false
if opts.Sessions == Enabled {
sessionsEnabled = true
}
// Prepare the configuration push message for the webwire accept handshake
configMsg, err := message.NewAcceptConfMessage(
message.ServerConfiguration{
MajorProtocolVersion: 2,
MinorProtocolVersion: 0,
ReadTimeout: opts.ReadTimeout,
MessageBufferSize: opts.MessageBufferSize,
SubProtocolName: opts.SubProtocolName,
},
)
if err != nil {
return nil, fmt.Errorf(
"couldn't initialize server configuration-push message: %s",
err,
)
}
// Initialize the webwire server
srv := &server{
transport: transport,
impl: implementation,
sessionManager: opts.SessionManager,
sessionKeyGen: opts.SessionKeyGenerator,
sessionInfoParser: opts.SessionInfoParser,
addr: url.URL{},
options: opts,
configMsg: configMsg,
shutdown: false,
shutdownRdy: make(chan bool),
currentOps: 0,
opsLock: &sync.Mutex{},
connections: make([]*connection, 0),
connectionsLock: &sync.Mutex{},
sessionsEnabled: sessionsEnabled,
messagePool: message.NewSyncPool(opts.MessageBufferSize, 1024),
warnLog: opts.WarnLog,
errorLog: opts.ErrorLog,
}
srv.sessionRegistry = newSessionRegistry(
opts.MaxSessionConnections,
func(sessionKey string) {
if err := srv.sessionManager.OnSessionClosed(
sessionKey,
); err != nil {
srv.errorLog.Printf("session registry ")
}
},
)
// Initialize the transport layer
if err := transport.Initialize(
opts,
srv.isShuttingDown,
srv.handleConnection,
); err != nil {
return nil, fmt.Errorf("couldn't initialize transport layer: %s", err)
}
return srv, nil
}