@@ -28,8 +28,7 @@ type Server struct {
28
28
packetConnConfigs []PacketConnConfig
29
29
listenerConfigs []ListenerConfig
30
30
allocationManagers []* allocation.Manager
31
-
32
- inboundMTU int
31
+ inboundMTU int
33
32
}
34
33
35
34
// NewServer creates the Pion TURN server
@@ -57,7 +56,6 @@ func NewServer(config ServerConfig) (*Server, error) {
57
56
channelBindTimeout :config .ChannelBindTimeout ,
58
57
packetConnConfigs :config .PacketConnConfigs ,
59
58
listenerConfigs :config .ListenerConfigs ,
60
- allocationManagers :make ([]* allocation.Manager ,len (config .PacketConnConfigs )+ len (config .ListenerConfigs )),
61
59
nonces :& sync.Map {},
62
60
inboundMTU :mtu ,
63
61
}
@@ -66,96 +64,48 @@ func NewServer(config ServerConfig) (*Server, error) {
66
64
s .channelBindTimeout = proto .DefaultLifetime
67
65
}
68
66
69
- for i := range s .packetConnConfigs {
70
- go func (i int ,p PacketConnConfig ) {
71
- permissionHandler := p .PermissionHandler
72
- if permissionHandler == nil {
73
- permissionHandler = DefaultPermissionHandler
74
- }
75
-
76
- allocationManager ,err := allocation .NewManager (allocation.ManagerConfig {
77
- AllocatePacketConn :p .RelayAddressGenerator .AllocatePacketConn ,
78
- AllocateConn :p .RelayAddressGenerator .AllocateConn ,
79
- PermissionHandler :permissionHandler ,
80
- LeveledLogger :s .log ,
81
- })
82
- if err != nil {
83
- s .log .Errorf ("exit read loop on error: %s" ,err .Error ())
84
- return
85
- }
86
- s .allocationManagers [i ]= allocationManager
87
- defer func () {
88
- if err := allocationManager .Close ();err != nil {
89
- s .log .Errorf ("Failed to close AllocationManager: %s" ,err .Error ())
90
- }
91
- }()
92
-
93
- s .readLoop (p .PacketConn ,allocationManager )
94
- }(i ,s .packetConnConfigs [i ])
95
- }
96
-
97
- for i ,listener := range s .listenerConfigs {
98
- go func (i int ,l ListenerConfig ) {
99
- permissionHandler := l .PermissionHandler
100
- if permissionHandler == nil {
101
- permissionHandler = DefaultPermissionHandler
102
- }
103
-
104
- allocationManager ,err := allocation .NewManager (allocation.ManagerConfig {
105
- AllocatePacketConn :l .RelayAddressGenerator .AllocatePacketConn ,
106
- AllocateConn :l .RelayAddressGenerator .AllocateConn ,
107
- PermissionHandler :permissionHandler ,
108
- LeveledLogger :s .log ,
109
- })
110
- if err != nil {
111
- s .log .Errorf ("exit read loop on error: %s" ,err .Error ())
112
- return
113
- }
114
- s .allocationManagers [i ]= allocationManager
115
- defer func () {
116
- if err := allocationManager .Close ();err != nil {
117
- s .log .Errorf ("Failed to close AllocationManager: %s" ,err .Error ())
118
- }
119
- }()
120
-
121
- for {
122
- conn ,err := l .Listener .Accept ()
123
- if err != nil {
124
- s .log .Debugf ("exit accept loop on error: %s" ,err .Error ())
125
- return
126
- }
127
-
128
- go s .readLoop (NewSTUNConn (conn ),allocationManager )
129
- }
130
- }(i + len (s .packetConnConfigs ),listener )
67
+ for _ ,cfg := range s .packetConnConfigs {
68
+ am ,err := s .createAllocationManager (cfg .RelayAddressGenerator ,cfg .PermissionHandler )
69
+ if err != nil {
70
+ return nil ,fmt .Errorf ("failed to create AllocationManager: %w" ,err )
71
+ }
72
+
73
+ go s .readPacketConn (cfg ,am )
74
+ }
75
+
76
+ for _ ,cfg := range s .listenerConfigs {
77
+ am ,err := s .createAllocationManager (cfg .RelayAddressGenerator ,cfg .PermissionHandler )
78
+ if err != nil {
79
+ return nil ,fmt .Errorf ("failed to create AllocationManager: %w" ,err )
80
+ }
81
+
82
+ go s .readListener (cfg ,am )
131
83
}
132
84
133
85
return s ,nil
134
86
}
135
87
136
88
// AllocationCount returns the number of active allocations. It can be used to drain the server before closing
137
89
func (s * Server )AllocationCount ()int {
138
- allocations := 0
139
- for _ ,manager := range s .allocationManagers {
140
- if manager != nil {
141
- allocations += manager .AllocationCount ()
142
- }
90
+ allocs := 0
91
+ for _ ,am := range s .allocationManagers {
92
+ allocs += am .AllocationCount ()
143
93
}
144
- return allocations
94
+ return allocs
145
95
}
146
96
147
97
// Close stops the TURN Server. It cleans up any associated state and closes all connections it is managing
148
98
func (s * Server )Close ()error {
149
99
var errors []error
150
100
151
- for _ ,p := range s .packetConnConfigs {
152
- if err := p .PacketConn .Close ();err != nil {
101
+ for _ ,cfg := range s .packetConnConfigs {
102
+ if err := cfg .PacketConn .Close ();err != nil {
153
103
errors = append (errors ,err )
154
104
}
155
105
}
156
106
157
- for _ ,l := range s .listenerConfigs {
158
- if err := l .Listener .Close ();err != nil {
107
+ for _ ,cfg := range s .listenerConfigs {
108
+ if err := cfg .Listener .Close ();err != nil {
159
109
errors = append (errors ,err )
160
110
}
161
111
}
@@ -166,12 +116,58 @@ func (s *Server) Close() error {
166
116
167
117
err := errFailedToClose
168
118
for _ ,e := range errors {
169
- err = fmt .Errorf ("%s; close error (%w) " ,err . Error () ,e )
119
+ err = fmt .Errorf ("%s; close error (%w) " ,err ,e )
170
120
}
171
121
172
122
return err
173
123
}
174
124
125
+ func (s * Server )readPacketConn (p PacketConnConfig ,am * allocation.Manager ) {
126
+ s .readLoop (p .PacketConn ,am )
127
+
128
+ if err := am .Close ();err != nil {
129
+ s .log .Errorf ("Failed to close AllocationManager: %s" ,err )
130
+ }
131
+ }
132
+
133
+ func (s * Server )readListener (l ListenerConfig ,am * allocation.Manager ) {
134
+ defer func () {
135
+ if err := am .Close ();err != nil {
136
+ s .log .Errorf ("Failed to close AllocationManager: %s" ,err )
137
+ }
138
+ }()
139
+
140
+ for {
141
+ conn ,err := l .Listener .Accept ()
142
+ if err != nil {
143
+ s .log .Debugf ("Failed to accept: %s" ,err )
144
+ return
145
+ }
146
+
147
+ go s .readLoop (NewSTUNConn (conn ),am )
148
+ }
149
+ }
150
+
151
+ func (s * Server )createAllocationManager (addrGenerator RelayAddressGenerator ,handler PermissionHandler ) (* allocation.Manager ,error ) {
152
+ if handler == nil {
153
+ handler = DefaultPermissionHandler
154
+ }
155
+
156
+ am ,err := allocation .NewManager (allocation.ManagerConfig {
157
+ AllocatePacketConn :addrGenerator .AllocatePacketConn ,
158
+ AllocateConn :addrGenerator .AllocateConn ,
159
+ PermissionHandler :handler ,
160
+ LeveledLogger :s .log ,
161
+ })
162
+ if err != nil {
163
+ return am ,err
164
+ }
165
+
166
+ s .allocationManagers = append (s .allocationManagers ,am )
167
+
168
+ return am ,err
169
+ }
170
+
175
171
func (s * Server )readLoop (p net.PacketConn ,allocationManager * allocation.Manager ) {
176
172
buf := make ([]byte ,s .inboundMTU )
177
173
for {