freeswitch php esl,FreeSWITCH的ESL消息接口

FreeSWITCH的ESL消息接口

tags:FreeSWITCH ESL SOCKET 创建时间:2015-11-22 20:48:25

```c

ESL_DECLARE(esl_status_t) esl_send(esl_handle_t handle, const charcmd)

{

const char *e = cmd + strlen(cmd) -1;

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;

if (!(strerror_r(handle->errnum, handle->err, sizeof(handle->err))))

*(handle->err)=0;

return ESL_FAIL;

}

if (!(*e == '\n' && *(e-1) == '\n')) {

if (send(handle->sock, "\n\n", 2, 0) != 2) {

handle->connected = 0;

if (!(strerror_r(handle->errnum, handle->err, sizeof(handle->err))))

*(handle->err)=0;

return ESL_FAIL;

}

}

return ESL_SUCCESS;

}

ESL_DECLARE(esl_status_t) esl_send_recv_timed(esl_handle_t handle, const charcmd, uint32_t ms)

{

const char *hval;

esl_status_t status;

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_sr_event);

*handle->last_sr_reply = '\0';

if ((status = esl_send(handle, cmd))) {

esl_mutex_unlock(handle->mutex);

return status;

}

recv:

esl_event_safe_destroy(&handle->last_sr_event);

*handle->last_sr_reply = '\0';

status = esl_recv_event_timed(handle, ms, 0, &handle->last_sr_event);

if (handle->last_sr_event) {

char *ct = esl_event_get_header(handle->last_sr_event,"content-type");

if (strcasecmp(ct, "api/response") && strcasecmp(ct, "command/reply")) {

esl_event_t *ep;

for(ep = handle->race_event; ep && ep->next; ep = ep->next);

if (ep) {

ep->next = handle->last_sr_event;

} else {

handle->race_event = handle->last_sr_event;

}

handle->last_sr_event = NULL;

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;

}

if (handle->last_sr_event) {

hval = esl_event_get_header(handle->last_sr_event, "reply-text");

if (!esl_strlen_zero(hval)) {

strncpy(handle->last_sr_reply, hval, sizeof(handle->last_sr_reply));

}

}

}

esl_mutex_unlock(handle->mutex);

return status;

}

```

上海老李,QQ:1354608370,FreeSWITCH QQ群:

你可能感兴趣的:(freeswitch,php,esl)