Modified: freeswitch/trunk/libs/esl/src/esl.c (16720 => 16721)
--- freeswitch/trunk/libs/esl/src/esl.c 2010-02-22 17:10:01 UTC (rev 16720)
+++ freeswitch/trunk/libs/esl/src/esl.c 2010-02-22 18:35:21 UTC (rev 16721)
@@ -378,8 +378,13 @@
return s;
}
-static void sock_setup(esl_handle_t *handle)
+static int sock_setup(esl_handle_t *handle)
{
+
+ if (handle->sock == ESL_SOCK_INVALID) {
+ return ESL_FAIL;
+ }
+
#ifdef WIN32
BOOL bOptVal = TRUE;
int bOptLen = sizeof(BOOL);
@@ -388,28 +393,33 @@
int x = 1;
setsockopt(handle->sock, IPPROTO_TCP, TCP_NODELAY, &x, sizeof(x));
#endif
+
+ return ESL_SUCCESS;
}
ESL_DECLARE(esl_status_t) esl_attach_handle(esl_handle_t *handle, esl_socket_t socket, struct sockaddr_in *addr)
{
+
+ if (!handle || socket == ESL_SOCK_INVALID) {
+ return ESL_FAIL;
+ }
+
handle->sock = socket;
+
if (addr) {
handle->addr = *addr;
}
- if (handle->sock == ESL_SOCK_INVALID) {
+ if (sock_setup(handle) != ESL_SUCCESS) {
return ESL_FAIL;
}
-
-
+
if (!handle->mutex) {
esl_mutex_create(&handle->mutex);
}
handle->connected = 1;
- sock_setup(handle);
-
esl_send_recv(handle, "connect\n\n");
@@ -439,13 +449,23 @@
snprintf(event_buf, sizeof(event_buf), "sendevent %s\n", esl_event_name(event->event_id));
- send(handle->sock, event_buf, strlen(event_buf), 0);
- send(handle->sock, txt, strlen(txt), 0);
- send(handle->sock, "\n\n", 2, 0);
+ if (send(handle->sock, event_buf, strlen(event_buf), 0)) goto fail;
+ if (send(handle->sock, txt, strlen(txt), 0)) goto fail;
+ if (send(handle->sock, "\n\n", 2, 0)) goto fail;
+
+ free(txt);
+ return ESL_SUCCESS;
+
+
+ fail:
+
+ handle->connected = 0;
+
free(txt);
- return ESL_SUCCESS;
+ return ESL_FAIL;
+
}
ESL_DECLARE(esl_status_t) esl_execute(esl_handle_t *handle, const char *app, const char *arg, const char *uuid)
@@ -457,9 +477,9 @@
const char *bl_buf = "async: true\n";
char send_buf[1292] = "";
- if (!handle->connected) {
- return ESL_FAIL;
- }
+ if (!handle || !handle->connected || handle->sock == ESL_SOCK_INVALID) {
+ return ESL_FAIL;
+ }
if (uuid) {
snprintf(cmd_buf, sizeof(cmd_buf), "sendmsg %s", uuid);
@@ -484,9 +504,9 @@
{
char send_buf[1024] = "";
- if (!handle->connected) {
- return ESL_FAIL;
- }
+ if (!handle || !handle->connected || handle->sock == ESL_SOCK_INVALID) {
+ return ESL_FAIL;
+ }
snprintf(send_buf, sizeof(send_buf), "filter %s %s\n\n", header, value);
@@ -499,9 +519,9 @@
char send_buf[1024] = "";
const char *type = "plain";
- if (!handle->connected) {
- return ESL_FAIL;
- }
+ if (!handle || !handle->connected || handle->sock == ESL_SOCK_INVALID) {
+ return ESL_FAIL;
+ }
if (etype == ESL_EVENT_TYPE_XML) {
type = "xml";
@@ -677,7 +697,7 @@
fail:
- esl_disconnect(handle);
+ handle->connected = 0;
return ESL_FAIL;
}
@@ -687,10 +707,17 @@
esl_mutex_t *mutex = handle->mutex;
esl_status_t status = ESL_FAIL;
+ if (handle->destroyed) {
+ return ESL_FAIL;
+ }
+
if (mutex) {
esl_mutex_lock(mutex);
}
+ handle->destroyed = 1;
+ handle->connected = 0;
+
esl_event_safe_destroy(&handle->race_event);
esl_event_safe_destroy(&handle->last_event);
esl_event_safe_destroy(&handle->last_sr_event);
@@ -703,14 +730,13 @@
status = ESL_SUCCESS;
}
- handle->connected = 0;
-
if (mutex) {
esl_mutex_unlock(mutex);
+ esl_mutex_lock(mutex);
+ esl_mutex_unlock(mutex);
esl_mutex_destroy(&mutex);
}
-
return status;
}
@@ -721,6 +747,10 @@
int max, activity;
esl_status_t status = ESL_SUCCESS;
+ if (!handle || !handle->connected || handle->sock == ESL_SOCK_INVALID) {
+ return ESL_FAIL;
+ }
+
if (check_q) {
esl_mutex_lock(handle->mutex);
if (handle->race_event) {
@@ -730,10 +760,6 @@
esl_mutex_unlock(handle->mutex);
}
- if (!handle || !handle->connected || handle->sock == -1) {
- return ESL_FAIL;
- }
-
tv.tv_usec = ms * 1000;
@@ -754,6 +780,7 @@
max = handle->sock + 1;
if ((activity = select(max, &rfds, NULL, &efds, &tv)) < 0) {
+ handle->connected = 0;
return ESL_FAIL;
}
@@ -761,7 +788,28 @@
return ESL_BREAK;
}
- if (activity && FD_ISSET(handle->sock, &rfds)) {
+ tv.tv_usec = 0;
+
+ FD_ZERO(&rfds);
+ FD_ZERO(&efds);
+
+#ifdef WIN32
+#pragma warning( push )
+#pragma warning( disable : 4127 )
+ FD_SET(handle->sock, &rfds);
+ FD_SET(handle->sock, &efds);
+#pragma warning( pop )
+#else
+ FD_SET(handle->sock, &rfds);
+ FD_SET(handle->sock, &efds);
+#endif
+
+ activity = select(max, &rfds, NULL, &efds, &tv);
+
+ if (activity < 0) {
+ handle->connected = 0;
+ status = ESL_FAIL;
+ } else if (activity > 0 && FD_ISSET(handle->sock, &rfds)) {
if (esl_recv_event(handle, check_q, save_event)) {
status = ESL_FAIL;
}
@@ -789,11 +837,19 @@
esl_ssize_t len;
int zc = 0;
- if (!handle->connected) {
+
+ if (!handle || !handle->connected || handle->sock == ESL_SOCK_INVALID) {
return ESL_FAIL;
}
-
+
esl_mutex_lock(handle->mutex);
+
+ if (!handle->connected || handle->sock == ESL_SOCK_INVALID) {
+ handle->connected = 0;
+ esl_mutex_unlock(handle->mutex);
+ return ESL_FAIL;
+ }
+
esl_event_safe_destroy(&handle->last_event);
esl_event_safe_destroy(&handle->last_ievent);
@@ -822,7 +878,7 @@
rrval = recv(handle->sock, c, 1, 0);
if (rrval == 0) {
if (++zc >= 100) {
- esl_disconnect(handle);
+ handle->connected = 0;
esl_mutex_unlock(handle->mutex);
return ESL_DISCONNECTED;
}
@@ -987,7 +1043,8 @@
fail:
- esl_disconnect(handle);
+ handle->connected = 0;
+
return ESL_FAIL;
}
@@ -996,19 +1053,22 @@
{
const char *e = cmd + strlen(cmd) -1;
- if (!handle->connected) {
+
+ if (!handle || !handle->connected || handle->sock == ESL_SOCK_INVALID) {
return ESL_FAIL;
}
esl_log(ESL_LOG_DEBUG, "SEND\n%s\n", cmd);
if (send(handle->sock, cmd, strlen(cmd), 0) != (int)strlen(cmd)) {
+ handle->connected = 0;
strerror_r(handle->errnum, handle->err, sizeof(handle->err));
return ESL_FAIL;
}
if (!(*e == '\n' && *(e-1) == '\n')) {
if (send(handle->sock, "\n\n", 2, 0) != 2) {
+ handle->connected = 0;
strerror_r(handle->errnum, handle->err, sizeof(handle->err));
return ESL_FAIL;
}
@@ -1024,13 +1084,19 @@
const char *hval;
esl_status_t status;
- if (!handle->connected) {
+ if (!handle || !handle->connected || handle->sock == ESL_SOCK_INVALID) {
+ return ESL_FAIL;
+ }
+
+ esl_mutex_lock(handle->mutex);
+
+
+ if (!handle->connected || handle->sock == ESL_SOCK_INVALID) {
+ handle->connected = 0;
+ esl_mutex_unlock(handle->mutex);
return ESL_FAIL;
}
-
- esl_mutex_lock(handle->mutex);
-
esl_event_safe_destroy(&handle->last_event);
esl_event_safe_destroy(&handle->last_sr_event);
@@ -1063,6 +1129,13 @@
esl_mutex_unlock(handle->mutex);
esl_mutex_lock(handle->mutex);
+
+ if (!handle->connected || handle->sock == ESL_SOCK_INVALID) {
+ handle->connected = 0;
+ esl_mutex_unlock(handle->mutex);
+ return ESL_FAIL;
+ }
+
goto recv;
}