]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
FS-7933 [mod_v8] Implement timeout property for Socket() class in javascript.
authorAndrey Volk <andywolk@gmail.com>
Tue, 27 Dec 2016 01:18:14 +0000 (04:18 +0300)
committerAndrey Volk <andywolk@gmail.com>
Tue, 27 Dec 2016 01:18:14 +0000 (04:18 +0300)
src/include/switch_apr.h
src/mod/languages/mod_v8/include/fssocket.hpp
src/mod/languages/mod_v8/src/fssocket.cpp
src/switch_apr.c

index 82a25f3b00882ca53bd7661f8da65d26d291af18..07f65063e6760d4d100540a4dc0344a19741d576 100644 (file)
@@ -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.
+ * <PRE>
+ *   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
+ * </PRE>
+ */
+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.
index 58f4bb5838cd70ab378173b969ef3c514165c2e8..0cb44b319e825372eeaa7fd625a0157511440d7f 100644 (file)
@@ -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 */
index f7af7a96e716df35055611e75f0867d4b925e10f..8339fd662440f146d2a45c50ee3e20666ed4fac3 100644 (file)
@@ -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}
 };
 
index daaa2716a12b7240b99aa4d869b38a975eb148dc..27140b90ea2e2d7e633c5679084642e50d7d83b1 100644 (file)
@@ -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);