77#include <ngx_core.h>
88#include <ngx_http.h>
99
10+ typedef struct ngx_stream_zone_hash_s ngx_stream_zone_hash_t ;
11+ typedef struct ngx_stream_zone_node_s ngx_stream_zone_node_t ;
12+ typedef struct ngx_stream_zone_conf_s ngx_stream_zone_conf_t ;
1013
11- static ngx_int_t ngx_stream_zone_init_process (ngx_cycle_t * cycle );
12- static void ngx_stream_zone_exit_process (ngx_cycle_t * cycle );
13- static void * ngx_stream_zone_create_conf (ngx_cycle_t * cf );
14- static char * ngx_stream_zone (ngx_conf_t * cf , ngx_command_t * cmd , void * conf );
14+ static ngx_int_t
15+ ngx_stream_zone_init_process (ngx_cycle_t * cycle );
16+ static void
17+ ngx_stream_zone_exit_process (ngx_cycle_t * cycle );
18+ static void *
19+ ngx_stream_zone_create_conf (ngx_cycle_t * cf );
20+ static char *
21+ ngx_stream_zone_init_conf (ngx_cycle_t * cycle , void * conf );
22+ static char *
23+ ngx_stream_zone (ngx_conf_t * cf , ngx_command_t * cmd , void * conf );
24+ static char *
25+ ngx_stream_zone_shm_init (ngx_shm_t * shm , ngx_stream_zone_conf_t * szcf );
1526
1627
1728#define NAME_LEN 1024
1829
1930static ngx_str_t stream_zone_key = ngx_string ("stream_zone" );
2031
21- typedef struct {
32+ struct ngx_stream_zone_node_s {
2233 u_char name [NAME_LEN ];
2334 ngx_int_t slot ; /* process slot */
2435 ngx_int_t idx ;
2536 ngx_int_t next ; /* idx of stream node */
26- } ngx_stream_zone_node_t ;
37+ };
2738
28- typedef struct {
39+ struct ngx_stream_zone_hash_s {
2940 ngx_shmtx_t mutex ;
3041 ngx_shmtx_sh_t lock ;
3142 ngx_int_t node ; /* idx of stream node */
32- } ngx_stream_zone_hash_t ;
43+ };
3344
34- typedef struct {
45+ struct ngx_stream_zone_conf_s {
3546 ngx_int_t nbuckets ;
3647 ngx_int_t nstreams ;
3748
49+ ngx_pool_t * pool ;
50+
3851 ngx_shmtx_t * mutex ;
3952 ngx_shmtx_sh_t * lock ;
4053 ngx_stream_zone_hash_t * hash ; /* hash in shm */
4154 ngx_stream_zone_node_t * stream_node ;/* node in shm */
4255 ngx_int_t * free_node ; /* free node chain */
4356 ngx_int_t * alloc ; /* node number in use*/
44- } ngx_stream_zone_conf_t ;
57+ };
4558
4659
4760static ngx_command_t ngx_stream_zone_commands [] = {
@@ -60,7 +73,7 @@ static ngx_command_t ngx_stream_zone_commands[] = {
6073static ngx_core_module_t ngx_stream_zone_module_ctx = {
6174 ngx_string ("rtmp_stream_zone" ),
6275 ngx_stream_zone_create_conf , /* create conf */
63- NULL /* init conf */
76+ ngx_stream_zone_init_conf /* init conf */
6477};
6578
6679
@@ -143,10 +156,38 @@ ngx_stream_zone_create_conf(ngx_cycle_t *cf)
143156
144157 conf -> nbuckets = NGX_CONF_UNSET ;
145158 conf -> nstreams = NGX_CONF_UNSET ;
159+ conf -> pool = ngx_create_pool (4096 , cf -> log );
146160
147161 return conf ;
148162}
149163
164+ static char *
165+ ngx_stream_zone_init_conf (ngx_cycle_t * cycle , void * conf )
166+ {
167+ size_t len ;
168+ ngx_shm_t shm ;
169+ ngx_stream_zone_conf_t * szcf = conf ;
170+
171+ ngx_conf_init_value (szcf -> nbuckets , 512 );
172+ ngx_conf_init_value (szcf -> nstreams , 40960 );
173+
174+ /* create shm zone */
175+ len = sizeof (ngx_shmtx_t ) + sizeof (ngx_shmtx_sh_t )
176+ + sizeof (ngx_stream_zone_hash_t ) * szcf -> nbuckets
177+ + sizeof (ngx_stream_zone_node_t ) * szcf -> nstreams
178+ + sizeof (ngx_int_t ) + sizeof (ngx_int_t );
179+
180+ shm .size = len ;
181+ shm .name = stream_zone_key ;
182+ shm .log = cycle -> log ;
183+
184+ if (ngx_shm_alloc (& shm ) != NGX_OK ) {
185+ return NGX_CONF_ERROR ;
186+ }
187+
188+ return ngx_stream_zone_shm_init (& shm , szcf );
189+ }
190+
150191static void
151192ngx_stream_zone_clear (ngx_cycle_t * cycle )
152193{
@@ -208,8 +249,7 @@ ngx_stream_zone_exit_process(ngx_cycle_t *cycle)
208249}
209250
210251static char *
211- ngx_stream_zone_shm_init (ngx_conf_t * cf , ngx_shm_t * shm ,
212- ngx_stream_zone_conf_t * szcf )
252+ ngx_stream_zone_shm_init (ngx_shm_t * shm , ngx_stream_zone_conf_t * szcf )
213253{
214254 u_char * p ;
215255 ngx_int_t i , next ;
@@ -239,12 +279,12 @@ ngx_stream_zone_shm_init(ngx_conf_t *cf, ngx_shm_t *shm,
239279 p = NULL ;
240280
241281#else
242- p = ngx_pnalloc (cf -> pool , cf -> cycle -> lock_file .len
282+ p = ngx_pnalloc (szcf -> pool , cycle -> lock_file .len
243283 + stream_zone_key .len );
244284 if (p == NULL ) {
245285 return NGX_CONF_ERROR ;
246286 }
247- * ngx_sprintf (p , "%V%V" , & cf -> cycle -> lock_file , & stream_zone_key ) = 0 ;
287+ * ngx_sprintf (p , "%V%V" , & cycle -> lock_file , & stream_zone_key ) = 0 ;
248288
249289#endif
250290
@@ -258,12 +298,12 @@ ngx_stream_zone_shm_init(ngx_conf_t *cf, ngx_shm_t *shm,
258298 p = NULL ;
259299
260300#else
261- p = ngx_pnalloc (cf -> pool , cf -> cycle -> lock_file .len + stream_zone_key .len
301+ p = ngx_pnalloc (szcf -> pool , cycle -> lock_file .len + stream_zone_key .len
262302 + NGX_INT32_LEN );
263303 if (p == NULL ) {
264304 return NGX_CONF_ERROR ;
265305 }
266- * ngx_sprintf (p , "%V%V%d" , & cf -> cycle -> lock_file ,
306+ * ngx_sprintf (p , "%V%V%d" , & cycle -> lock_file ,
267307 & stream_zone_key , i ) = 0 ;
268308
269309#endif
@@ -300,8 +340,6 @@ ngx_stream_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
300340{
301341 ngx_uint_t i ;
302342 ngx_str_t * value ;
303- size_t len ;
304- ngx_shm_t shm ;
305343 ngx_stream_zone_conf_t * szcf = conf ;
306344
307345 value = cf -> args -> elts ;
@@ -337,21 +375,7 @@ ngx_stream_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
337375 return NGX_CONF_ERROR ;
338376 }
339377
340- /* create shm zone */
341- len = sizeof (ngx_shmtx_t ) + sizeof (ngx_shmtx_sh_t )
342- + sizeof (ngx_stream_zone_hash_t ) * szcf -> nbuckets
343- + sizeof (ngx_stream_zone_node_t ) * szcf -> nstreams
344- + sizeof (ngx_int_t ) + sizeof (ngx_int_t );
345-
346- shm .size = len ;
347- shm .name = stream_zone_key ;
348- shm .log = cf -> cycle -> log ;
349-
350- if (ngx_shm_alloc (& shm ) != NGX_OK ) {
351- return NGX_CONF_ERROR ;
352- }
353-
354- return ngx_stream_zone_shm_init (cf , & shm , szcf );
378+ return NGX_CONF_OK ;
355379}
356380
357381
0 commit comments