diff --git a/src/include/switch_apr.h b/src/include/switch_apr.h index 82a25f3b00..07f65063e6 100644 --- a/src/include/switch_apr.h +++ b/src/include/switch_apr.h @@ -1233,6 +1233,19 @@ SWITCH_DECLARE(switch_status_t) switch_socket_recv(switch_socket_t *sock, char * */ SWITCH_DECLARE(switch_status_t) switch_socket_opt_set(switch_socket_t *sock, int32_t opt, int32_t on); +/** + * Query socket timeout for the specified socket + * @param sock The socket to query + * @param t Socket timeout returned from the query. + *
+ * t > 0 -- read and write calls return APR_TIMEUP if specified time + * elapsess with no data read or written + * t == 0 -- read and write calls never block + * t < 0 -- read and write calls block + *+ */ +SWITCH_DECLARE(switch_status_t) switch_socket_timeout_get(switch_socket_t *sock, switch_interval_time_t *t); + /** * Setup socket timeout for the specified socket * @param sock The socket to set up. diff --git a/src/mod/languages/mod_v8/include/fssocket.hpp b/src/mod/languages/mod_v8/include/fssocket.hpp index 58f4bb5838..0cb44b319e 100644 --- a/src/mod/languages/mod_v8/include/fssocket.hpp +++ b/src/mod/languages/mod_v8/include/fssocket.hpp @@ -67,6 +67,7 @@ public: JS_SOCKET_FUNCTION_DEF(ReadBytes); JS_SOCKET_FUNCTION_DEF(Read); JS_SOCKET_GET_PROPERTY_DEF(GetProperty); + JS_SOCKET_SET_PROPERTY_DEF(SetPropertyTimeOut); }; #endif /* FS_SOCKET_H */ diff --git a/src/mod/languages/mod_v8/src/fssocket.cpp b/src/mod/languages/mod_v8/src/fssocket.cpp index f7af7a96e7..8339fd6624 100644 --- a/src/mod/languages/mod_v8/src/fssocket.cpp +++ b/src/mod/languages/mod_v8/src/fssocket.cpp @@ -175,7 +175,7 @@ JS_SOCKET_FUNCTION_IMPL(ReadBytes) ret = switch_socket_recv(this->_socket, this->_read_buffer, &len); if (ret != SWITCH_STATUS_SUCCESS) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "switch_socket_send failed: %d.\n", ret); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "switch_socket_recv failed: %d.\n", ret); info.GetReturnValue().Set(false); return; } else { @@ -282,11 +282,27 @@ JS_SOCKET_GET_PROPERTY_IMPL(GetProperty) } else { info.GetReturnValue().Set(Integer::New(info.GetIsolate(), 0)); } + } else if (!strcmp(js_safe_str(*str), "timeout")) { + switch_interval_time_t timeout; + + switch_socket_timeout_get(this->_socket, &timeout); + + info.GetReturnValue().Set(Int32::New(info.GetIsolate(), (int32_t)timeout)); } else { info.GetIsolate()->ThrowException(String::NewFromUtf8(info.GetIsolate(), "Bad property")); } } +JS_SOCKET_SET_PROPERTY_IMPL(SetPropertyTimeOut) +{ + if (!this->_socket) { + info.GetIsolate()->ThrowException(String::NewFromUtf8(info.GetIsolate(), "Socket is not active")); + return; + } + + switch_socket_timeout_set(this->_socket, value->Int32Value()); +} + static const js_function_t socket_methods[] = { {"connect", FSSocket::Connect}, {"close", FSSocket::Close}, @@ -299,6 +315,7 @@ static const js_function_t socket_methods[] = { static const js_property_t socket_props[] = { {"address", FSSocket::GetProperty, JSBase::DefaultSetProperty}, {"port", FSSocket::GetProperty, JSBase::DefaultSetProperty}, + {"timeout", FSSocket::GetProperty, FSSocket::SetPropertyTimeOut}, {0} }; diff --git a/src/switch_apr.c b/src/switch_apr.c index daaa2716a1..27140b90ea 100644 --- a/src/switch_apr.c +++ b/src/switch_apr.c @@ -859,6 +859,11 @@ SWITCH_DECLARE(switch_status_t) switch_socket_opt_set(switch_socket_t *sock, int return apr_socket_opt_set(sock, opt, on); } +SWITCH_DECLARE(switch_status_t) switch_socket_timeout_get(switch_socket_t *sock, switch_interval_time_t *t) +{ + return apr_socket_timeout_get(sock, t); +} + SWITCH_DECLARE(switch_status_t) switch_socket_timeout_set(switch_socket_t *sock, switch_interval_time_t t) { return apr_socket_timeout_set(sock, t);