Skip to content

Commit

Permalink
improved closing session on error
Browse files Browse the repository at this point in the history
  • Loading branch information
arut committed Mar 13, 2012
1 parent 4602695 commit 8e32bae
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 73 deletions.
7 changes: 6 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
- Implement modules support.
~ Implement modules support.
Move AMF0 handlers to modules.
Move broadcast to module.

- remove macros hell from ngx_rtmp_send.c

- packet dropping

- l <-> cl

- closing session on send error
causes crash because of double-freeing stack

- shortcuts for big-endian copy

- implement loc confs (=fms apps)
Expand Down
7 changes: 4 additions & 3 deletions ngx_rtmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ typedef struct {


void ngx_rtmp_init_connection(ngx_connection_t *c);
void ngx_rtmp_close_session(ngx_rtmp_session_t *s);
/*void ngx_rtmp_close_session(ngx_rtmp_session_t *s);*/
void ngx_rtmp_close_connection(ngx_connection_t *c);
u_char * ngx_rtmp_log_error(ngx_log_t *log, u_char *buf, size_t len);


Expand All @@ -271,9 +272,9 @@ ngx_int_t ngx_rtmp_amf0_message_handler(ngx_rtmp_session_t *s,

/* Sending messages */
ngx_chain_t * ngx_rtmp_alloc_shared_buf(ngx_rtmp_session_t *s);
void ngx_rtmp_prepare_message(ngx_rtmp_header_t *h,
void ngx_rtmp_prepare_message(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
ngx_chain_t *out, uint8_t fmt);
void ngx_rtmp_send_message(ngx_rtmp_session_t *s, ngx_chain_t *out);
ngx_int_t ngx_rtmp_send_message(ngx_rtmp_session_t *s, ngx_chain_t *out);

#define NGX_RTMP_LIMIT_SOFT 0
#define NGX_RTMP_LIMIT_HARD 1
Expand Down
26 changes: 18 additions & 8 deletions ngx_rtmp_amf0.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,17 @@ ngx_rtmp_amf0_debug(const char* op, ngx_log_t *log, u_char *p, size_t n)

for(i = 0; i < n && i < NGX_RTMP_AMF0_DEBUG_SIZE; ++i) {
*hp++ = ' ';
*hp++ = hex[(*p & 0xf0) >> 4];
*hp++ = hex[*p & 0x0f];
*sp++ = (*p >= 0x20 && *p <= 0x7e) ?
*p : (u_char)'?';
++p;
if (p) {
*hp++ = hex[(*p & 0xf0) >> 4];
*hp++ = hex[*p & 0x0f];
*sp++ = (*p >= 0x20 && *p <= 0x7e) ?
*p : (u_char)'?';
++p;
} else {
*hp++ = 'X';
*hp++ = 'X';
*sp++ = '?';
}
}
*hp = *sp = '\0';

Expand All @@ -69,7 +75,7 @@ ngx_rtmp_amf0_get(ngx_rtmp_amf0_ctx_t *ctx, void *p, size_t n)

b = l->buf;

if (b->last > n + b->pos) {
if (b->last >= n + b->pos) {
if (p) {
p = ngx_cpymem(p, b->pos, n);
}
Expand Down Expand Up @@ -128,6 +134,7 @@ ngx_rtmp_amf0_put(ngx_rtmp_amf0_ctx_t *ctx, void *p, size_t n)
l = ln;
}

ctx->link = l;
b = l->buf;
b->pos = b->last = b->start;
}
Expand Down Expand Up @@ -156,6 +163,7 @@ ngx_rtmp_amf0_read_object(ngx_rtmp_amf0_ctx_t *ctx, ngx_rtmp_amf0_elt_t *elts,
uint16_t len;
size_t n, namelen, maxlen;
ngx_int_t rc;
u_char buf[2];

maxlen = 0;
for(n = 0; n < nelts; ++n) {
Expand All @@ -169,9 +177,11 @@ ngx_rtmp_amf0_read_object(ngx_rtmp_amf0_ctx_t *ctx, ngx_rtmp_amf0_elt_t *elts,
char name[maxlen + 1];

/* read key */
if (ngx_rtmp_amf0_get(ctx, &len, sizeof(len)) != NGX_OK)
if (ngx_rtmp_amf0_get(ctx, buf, 2) != NGX_OK)
return NGX_ERROR;

ngx_rtmp_amf0_reverse_copy(&len, buf, 2);

if (!len)
break;

Expand Down Expand Up @@ -241,7 +251,7 @@ ngx_rtmp_amf0_read(ngx_rtmp_amf0_ctx_t *ctx, ngx_rtmp_amf0_elt_t *elts, size_t n
if (ngx_rtmp_amf0_get(ctx, buf, 8) != NGX_OK) {
return NGX_ERROR;
}
ngx_rtmp_amf0_reverse_copy(data, buf, 0);
ngx_rtmp_amf0_reverse_copy(data, buf, 8);
break;

case NGX_RTMP_AMF0_BOOLEAN:
Expand Down
9 changes: 5 additions & 4 deletions ngx_rtmp_broadcast_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ ngx_rtmp_broadcast_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
p += (l->buf->end - l->buf->last);
}

ngx_rtmp_prepare_message(h, out, 0/*fmt*/);
ngx_rtmp_prepare_message(s, h, out, 0/*fmt*/);

/* broadcast to all subscribers */
for(cctx = *ngx_rtmp_broadcast_get_head(s);
Expand All @@ -242,7 +242,9 @@ ngx_rtmp_broadcast_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
&& !ngx_strncmp(cctx->stream.data, ctx->stream.data,
ctx->stream.len))
{
ngx_rtmp_send_message(s, out);
if (ngx_rtmp_send_message(s, out) != NGX_OK) {
return NGX_ERROR;
}
}
}

Expand All @@ -261,7 +263,7 @@ ngx_rtmp_broadcast_connect(ngx_rtmp_session_t *s, double in_trans,

static ngx_rtmp_amf0_elt_t in_cmd[] = {
{ NGX_RTMP_AMF0_STRING, "app", app, sizeof(app) },
{ NGX_RTMP_AMF0_STRING, "pageUrl", url, sizeof(url) },
{ NGX_RTMP_AMF0_STRING, "tcUrl" , url, sizeof(url) },
};

static ngx_rtmp_amf0_elt_t out_inf[] = {
Expand All @@ -272,7 +274,6 @@ ngx_rtmp_broadcast_connect(ngx_rtmp_session_t *s, double in_trans,

static ngx_rtmp_amf0_elt_t in_elts[] = {
{ NGX_RTMP_AMF0_OBJECT, NULL, in_cmd, sizeof(in_cmd) },
{ NGX_RTMP_AMF0_NULL, NULL, NULL, 0 },
};

static ngx_rtmp_amf0_elt_t out_elts[] = {
Expand Down
Loading

0 comments on commit 8e32bae

Please sign in to comment.