fix disconnect issue in event socket and fix a few small bugs

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@11234 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Anthony Minessale
2009-01-15 22:11:27 +00:00
parent 169ced2f93
commit 6f8e06f087
7 changed files with 147 additions and 82 deletions

View File

@@ -37,7 +37,6 @@
#endif
/* Written by Marc Espie, public domain */
#define ESL_CTYPE_NUM_CHARS 256
@@ -181,6 +180,48 @@ ESL_DECLARE(const char *)esl_stristr(const char *instr, const char *str)
#endif
#endif
int vasprintf(char **ret, const char *format, va_list ap);
ESL_DECLARE(int) esl_vasprintf(char **ret, const char *fmt, va_list ap)
{
#if !defined(WIN32) && !defined(__sun)
return vasprintf(ret, fmt, ap);
#else
char *buf;
int len;
size_t buflen;
va_list ap2;
char *tmp = NULL;
#ifdef _MSC_VER
#if _MSC_VER >= 1500
/* hack for incorrect assumption in msvc header files for code analysis */
__analysis_assume(tmp);
#endif
ap2 = ap;
#else
va_copy(ap2, ap);
#endif
len = vsnprintf(tmp, 0, fmt, ap2);
if (len > 0 && (buf = malloc((buflen = (size_t) (len + 1)))) != NULL) {
len = vsnprintf(buf, buflen, fmt, ap);
*ret = buf;
} else {
*ret = NULL;
len = -1;
}
va_end(ap2);
return len;
#endif
}
ESL_DECLARE(int) esl_snprintf(char *buffer, size_t count, const char *fmt, ...)
{
va_list ap;
@@ -236,9 +277,10 @@ static const char *cut_path(const char *in)
static void default_logger(const char *file, const char *func, int line, int level, const char *fmt, ...)
{
const char *fp;
char data[1024];
char *data;
va_list ap;
int ret;
if (level < 0 || level > 7) {
level = 7;
}
@@ -250,10 +292,12 @@ static void default_logger(const char *file, const char *func, int line, int lev
va_start(ap, fmt);
vsnprintf(data, sizeof(data), fmt, ap);
ret = esl_vasprintf(&data, fmt, ap);
fprintf(stderr, "[%s] %s:%d %s() %s", LEVEL_NAMES[level], file, line, func, data);
if (ret != -1) {
fprintf(stderr, "[%s] %s:%d %s() %s", LEVEL_NAMES[level], file, line, func, data);
free(data);
}
va_end(ap);
@@ -334,6 +378,14 @@ ESL_DECLARE(char *)esl_url_decode(char *s)
return s;
}
static void sock_setup(esl_handle_t *handle)
{
int x = 1;
setsockopt(handle->sock, IPPROTO_TCP, TCP_NODELAY, &x, sizeof(x));
}
ESL_DECLARE(esl_status_t) esl_attach_handle(esl_handle_t *handle, esl_socket_t socket, struct sockaddr_in addr)
{
handle->sock = socket;
@@ -350,9 +402,11 @@ ESL_DECLARE(esl_status_t) esl_attach_handle(esl_handle_t *handle, esl_socket_t s
handle->connected = 1;
sock_setup(handle);
esl_send_recv(handle, "connect\n\n");
if (handle->last_sr_event) {
handle->info_event = handle->last_sr_event;
handle->last_sr_event = NULL;
@@ -413,6 +467,39 @@ ESL_DECLARE(esl_status_t) esl_execute(esl_handle_t *handle, const char *app, con
return esl_send_recv(handle, send_buf);
}
ESL_DECLARE(esl_status_t) esl_filter(esl_handle_t *handle, const char *header, const char *value)
{
char send_buf[1024] = "";
if (!handle->connected) {
return ESL_FAIL;
}
snprintf(send_buf, sizeof(send_buf), "filter %s %s\n\n", header, value);
return esl_send_recv(handle, send_buf);
}
ESL_DECLARE(esl_status_t) esl_events(esl_handle_t *handle, esl_event_type_t etype, const char *value)
{
char send_buf[1024] = "";
const char *type = "plain";
if (!handle->connected) {
return ESL_FAIL;
}
if (etype == ESL_EVENT_TYPE_XML) {
type = "xml";
}
snprintf(send_buf, sizeof(send_buf), "event %s %s\n\n", type, value);
return esl_send_recv(handle, send_buf);
}
static int esl_socket_reuseaddr(esl_socket_t socket)
{
#ifdef WIN32
@@ -536,6 +623,8 @@ ESL_DECLARE(esl_status_t) esl_connect(esl_handle_t *handle, const char *host, es
goto fail;
}
sock_setup(handle);
handle->connected = 1;
if (esl_recv(handle)) {

View File

@@ -311,45 +311,6 @@ static esl_status_t esl_event_base_add_header(esl_event_t *event, esl_stack_t st
return ESL_SUCCESS;
}
int vasprintf(char **ret, const char *format, va_list ap);
static int esl_vasprintf(char **ret, const char *fmt, va_list ap)
{
#if !defined(WIN32) && !defined(__sun)
return vasprintf(ret, fmt, ap);
#else
char *buf;
int len;
size_t buflen;
va_list ap2;
char *tmp = NULL;
#ifdef _MSC_VER
#if _MSC_VER >= 1500
/* hack for incorrect assumption in msvc header files for code analysis */
__analysis_assume(tmp);
#endif
ap2 = ap;
#else
va_copy(ap2, ap);
#endif
len = vsnprintf(tmp, 0, fmt, ap2);
if (len > 0 && (buf = malloc((buflen = (size_t) (len + 1)))) != NULL) {
len = vsnprintf(buf, buflen, fmt, ap);
*ret = buf;
} else {
*ret = NULL;
len = -1;
}
va_end(ap2);
return len;
#endif
}
ESL_DECLARE(esl_status_t) esl_event_add_header(esl_event_t *event, esl_stack_t stack, const char *header_name, const char *fmt, ...)
{
int ret = 0;
@@ -496,6 +457,8 @@ ESL_DECLARE(esl_status_t) esl_event_serialize(esl_event_t *event, char **str, es
* the memory, allocate and only reallocate if we need more. This avoids an alloc, free CPU
* destroying loop.
*/
new_len = (strlen(hp->value) * 3) + 1;
if (encode_len < new_len) {
@@ -575,6 +538,7 @@ ESL_DECLARE(esl_status_t) esl_event_serialize(esl_event_t *event, char **str, es
snprintf(buf + len, dlen - len, "\n");
}
*str = buf;
return ESL_SUCCESS;

View File

@@ -42,6 +42,12 @@
typedef struct esl_event_header esl_event_header_t;
typedef struct esl_event esl_event_t;
typedef enum {
ESL_EVENT_TYPE_PLAIN,
ESL_EVENT_TYPE_XML
} esl_event_type_t;
#ifdef WIN32
#define ESL_SEQ_FWHITE FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY
#define ESL_SEQ_FRED FOREGROUND_RED | FOREGROUND_INTENSITY
@@ -164,6 +170,7 @@ typedef struct esl_event esl_event_t;
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
#include <netinet/tcp.h>
#include <sys/signal.h>
#include <unistd.h>
#include <ctype.h>
@@ -290,6 +297,9 @@ typedef enum {
#define ESL_LOG_EMERG ESL_PRE, ESL_LOG_LEVEL_EMERG
typedef void (*esl_logger_t)(const char *file, const char *func, int line, int level, const char *fmt, ...);
ESL_DECLARE(int) esl_vasprintf(char **ret, const char *fmt, va_list ap);
ESL_DECLARE_DATA extern esl_logger_t esl_log;
ESL_DECLARE(void) esl_global_set_logger(esl_logger_t logger);
@@ -320,6 +330,9 @@ ESL_DECLARE(esl_status_t) esl_send(esl_handle_t *handle, const char *cmd);
ESL_DECLARE(esl_status_t) esl_recv_event(esl_handle_t *handle, esl_event_t **save_event);
ESL_DECLARE(esl_status_t) esl_recv_event_timed(esl_handle_t *handle, uint32_t ms, esl_event_t **save_event);
ESL_DECLARE(esl_status_t) esl_send_recv(esl_handle_t *handle, const char *cmd);
ESL_DECLARE(esl_status_t) esl_filter(esl_handle_t *handle, const char *header, const char *value);
ESL_DECLARE(esl_status_t) esl_events(esl_handle_t *handle, esl_event_type_t etype, const char *value);
#define esl_recv(_h) esl_recv_event(_h, NULL)
#define esl_recv_timed(_h, _ms) esl_recv_event_timed(_h, _ms, NULL)