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);
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");
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_FAIL;
+
}
ESL_DECLARE(esl_status_t) esl_execute(esl_handle_t *handle, const char *app, const char *arg, const char *uuid)
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);
{
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);
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";
fail:
- esl_disconnect(handle);
+ handle->connected = 0;
return ESL_FAIL;
}
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);
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;
}
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) {
esl_mutex_unlock(handle->mutex);
}
- if (!handle || !handle->connected || handle->sock == -1) {
- return ESL_FAIL;
- }
-
tv.tv_usec = ms * 1000;
max = handle->sock + 1;
if ((activity = select(max, &rfds, NULL, &efds, &tv)) < 0) {
+ handle->connected = 0;
return ESL_FAIL;
}
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;
}
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);
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;
}
fail:
- esl_disconnect(handle);
+ handle->connected = 0;
+
return ESL_FAIL;
}
{
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;
}
const char *hval;
esl_status_t status;
- if (!handle->connected) {
- return ESL_FAIL;
- }
-
+ 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_sr_event);
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;
}