optimize ast_recvchar/ast_recvtext (bug #4591)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@5989 65c4cc65-6c06-0410-ace0-fbb531ad65f3
1.2-netsec
Kevin P. Fleming 21 years ago
parent dfe71e885e
commit 7ec4d28296

@ -1625,65 +1625,40 @@ int ast_indicate(struct ast_channel *chan, int condition)
int ast_recvchar(struct ast_channel *chan, int timeout) int ast_recvchar(struct ast_channel *chan, int timeout)
{ {
int res,ourto,c; int c;
struct ast_frame *f; char *buf = ast_recvtext(chan, timeout);
if (buf == NULL)
ourto = timeout; return -1; /* error or timeout */
for(;;) c = *(unsigned char *)buf;
{ free(buf);
if (ast_check_hangup(chan)) return -1; return c;
res = ast_waitfor(chan,ourto);
if (res <= 0) /* if timeout */
{
return 0;
}
ourto = res;
f = ast_read(chan);
if (f == NULL) return -1; /* if hangup */
if ((f->frametype == AST_FRAME_CONTROL) &&
(f->subclass == AST_CONTROL_HANGUP)) return -1; /* if hangup */
if (f->frametype == AST_FRAME_TEXT) /* if a text frame */
{
c = *((char *)f->data); /* get the data */
ast_frfree(f);
return(c);
}
ast_frfree(f);
}
} }
char *ast_recvtext(struct ast_channel *chan, int timeout) char *ast_recvtext(struct ast_channel *chan, int timeout)
{ {
int res,ourto; int res, done = 0;
struct ast_frame *f; char *buf = NULL;
char *buf;
ourto = timeout; while (!done) {
for(;;) { struct ast_frame *f;
if (ast_check_hangup(chan)) return NULL; if (ast_check_hangup(chan))
res = ast_waitfor(chan,ourto); break;
if (res <= 0) { res = ast_waitfor(chan, timeout);
/* if timeout */ if (res <= 0) /* timeout or error */
return NULL; break;
} timeout = res; /* update timeout */
ourto = res;
f = ast_read(chan); f = ast_read(chan);
if (f == NULL) return NULL; /* no frame */ if (f == NULL)
if ((f->frametype == AST_FRAME_CONTROL) && break; /* no frame */
(f->subclass == AST_CONTROL_HANGUP)) return NULL; /* if hangup */ if (f->frametype == AST_FRAME_CONTROL && f->subclass == AST_CONTROL_HANGUP)
if (f->frametype == AST_FRAME_TEXT) { done = 1; /* force a break */
/* if a text frame */ else if (f->frametype == AST_FRAME_TEXT) { /* what we want */
buf = (char *)malloc(strlen((char *)f->data)); buf = strdup((char *)f->data); /* dup and break */
if (buf) { done = 1;
strcpy(buf, (char *)f->data);
ast_frfree(f);
return(buf);
} else {
return NULL;
}
} }
ast_frfree(f); ast_frfree(f);
} }
return buf;
} }
int ast_sendtext(struct ast_channel *chan, char *text) int ast_sendtext(struct ast_channel *chan, char *text)

Loading…
Cancel
Save