summaryrefslogtreecommitdiff
path: root/pjlib
diff options
context:
space:
mode:
authorNanang Izzuddin <nanang@teluu.com>2013-06-19 09:06:55 +0000
committerNanang Izzuddin <nanang@teluu.com>2013-06-19 09:06:55 +0000
commit1229122bc17bb86b6a10e043aa3505715b797347 (patch)
treeae8a72fe1f020f77c31514b05fbd98af2fdcd434 /pjlib
parentd7aa4332dae06ae890053dd13239ddabee46b86c (diff)
Close #1681:
- Added compile-time settings PJMEDIA_TRANSPORT_SO_RCVBUF_SIZE and PJMEDIA_TRANSPORT_SO_SNDBUF_SIZE. The default values are both 64 KB when PJMEDIA_HAS_VIDEO is set, otherwise just zero (socket buffer size uses OS default). The settings will be applied to media transport UDP and ICE. - Also added run-time settings so_sndbuf_size and so_rcvbuf_size into ICE stream transport, STUN socket, and TURN socket. Default values are all zero. git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@4538 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjlib')
-rw-r--r--pjlib/include/pj/sock.h21
-rw-r--r--pjlib/src/pj/sock_common.c63
2 files changed, 84 insertions, 0 deletions
diff --git a/pjlib/include/pj/sock.h b/pjlib/include/pj/sock.h
index 4011f21a..b7d1cb05 100644
--- a/pjlib/include/pj/sock.h
+++ b/pjlib/include/pj/sock.h
@@ -1307,6 +1307,27 @@ PJ_DECL(pj_status_t) pj_sock_setsockopt( pj_sock_t sockfd,
/**
+ * Helper function to set socket buffer size using #pj_sock_setsockopt()
+ * with capability to auto retry with lower buffer setting value until
+ * the highest possible value is successfully set.
+ *
+ * @param sockfd The socket descriptor.
+ * @param optname The option name, valid values are pj_SO_RCVBUF()
+ * and pj_SO_SNDBUF().
+ * @param auto_retry Option whether auto retry with lower value is
+ * enabled.
+ * @param buf_size On input, specify the prefered buffer size setting,
+ * on output, the buffer size setting applied.
+ *
+ * @return PJ_SUCCESS or the status code.
+ */
+PJ_DECL(pj_status_t) pj_sock_setsockopt_sobuf( pj_sock_t sockfd,
+ pj_uint16_t optname,
+ pj_bool_t auto_retry,
+ unsigned *buf_size);
+
+
+/**
* Receives data stream or message coming to the specified socket.
*
* @param sockfd The socket descriptor.
diff --git a/pjlib/src/pj/sock_common.c b/pjlib/src/pj/sock_common.c
index eb338e73..94fa52e4 100644
--- a/pjlib/src/pj/sock_common.c
+++ b/pjlib/src/pj/sock_common.c
@@ -1079,6 +1079,69 @@ PJ_DEF(pj_status_t) pj_sock_bind_random( pj_sock_t sockfd,
}
+/*
+ * Adjust socket send/receive buffer size.
+ */
+PJ_DEF(pj_status_t) pj_sock_setsockopt_sobuf( pj_sock_t sockfd,
+ pj_uint16_t optname,
+ pj_bool_t auto_retry,
+ unsigned *buf_size)
+{
+ pj_status_t status;
+ int try_size, cur_size, i, step, size_len;
+ enum { MAX_TRY = 20 };
+
+ PJ_CHECK_STACK();
+
+ PJ_ASSERT_RETURN(sockfd != PJ_INVALID_SOCKET &&
+ buf_size &&
+ *buf_size > 0 &&
+ (optname == pj_SO_RCVBUF() ||
+ optname == pj_SO_SNDBUF()),
+ PJ_EINVAL);
+
+ size_len = sizeof(cur_size);
+ status = pj_sock_getsockopt(sockfd, pj_SOL_SOCKET(), optname,
+ &cur_size, &size_len);
+ if (status != PJ_SUCCESS)
+ return status;
+
+ try_size = *buf_size;
+ step = (try_size - cur_size) / MAX_TRY;
+ if (step < 4096)
+ step = 4096;
+
+ for (i = 0; i < (MAX_TRY-1); ++i) {
+ if (try_size <= cur_size) {
+ /* Done, return current size */
+ *buf_size = cur_size;
+ break;
+ }
+
+ status = pj_sock_setsockopt(sockfd, pj_SOL_SOCKET(), optname,
+ &try_size, sizeof(try_size));
+ if (status == PJ_SUCCESS) {
+ status = pj_sock_getsockopt(sockfd, pj_SOL_SOCKET(), optname,
+ &cur_size, &size_len);
+ if (status != PJ_SUCCESS) {
+ /* Ops! No info about current size, just return last try size
+ * and quit.
+ */
+ *buf_size = try_size;
+ break;
+ }
+ }
+
+ if (!auto_retry)
+ break;
+
+ try_size -= step;
+ }
+
+ return status;
+}
+
+
/* Only need to implement these in DLL build */
#if defined(PJ_DLL)