-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
106 lines (90 loc) · 2.47 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
100
101
102
103
104
105
106
package sylph
import (
"fmt"
"github.com/google/uuid"
"github.com/tkmn0/sylph/internal/engine"
"github.com/tkmn0/sylph/internal/listener"
"github.com/tkmn0/sylph/internal/transport"
)
// Server handles base connections. (udp, dtls, sctp)
// A Server includes a listener.
// A Server handles a bundle of Transports.
// After a Client connected, OnTransport will be called.
type Server struct {
listener *listener.Listener
listenerConfig listener.ListenerConfig
transports []Transport
onTransportHandler func(transport Transport)
close chan bool
}
// NewServer creates a Server.
func NewServer() *Server {
return &Server{
listener: listener.NewListener(),
transports: []Transport{},
}
}
// obserbeClose obserbes and handles closing
func (s *Server) obserbeClose() {
<-s.close
s.close = nil
s.listener.Close()
}
// Run runs server with address, port, and TransportConfig.
// This will block process, call this with goroutine when necessary.
func (s *Server) Run(address string, port int, tc TransportConfig) {
s.close = make(chan bool)
go s.obserbeClose()
c := listener.ListenerConfig{
Address: address,
Port: port,
}
s.listener.Listen(c)
for {
conn := <-s.listener.Connection
id, err := s.createId()
if err != nil {
fmt.Println("id creation error")
return
}
sctp := transport.NewSctpTransport(id)
err = sctp.Init(conn, false, engine.EngineConfig{
HeartbeatRateMillisec: tc.HeartbeatRateMillisec,
TimeOutDurationMilliSec: tc.TimeOutDurationMilliSec,
})
if err != nil {
fmt.Println("sctp initialize error")
}
s.transports = append(s.transports, sctp)
if s.onTransportHandler != nil {
s.onTransportHandler(sctp)
}
go sctp.AcceptStreamLoop()
}
}
// createId creates id for Transport.
// This id will be used server side and client.
// The id in client side has suffix "-client" with server side id.
func (s *Server) createId() (string, error) {
uuidObj, err := uuid.NewRandom()
if err != nil {
return "", err
}
uuid := uuidObj.String()
for _, sctp := range s.transports {
if sctp.Id() == uuid {
return s.createId()
}
}
return uuid, nil
}
// OnTransport will be called when Client connected.
func (s *Server) OnTransport(handler func(t Transport)) {
s.onTransportHandler = handler
}
// Close closes server.
func (s *Server) Close() {
if s.close != nil {
s.close <- true
}
}