diff options
author | Liong Sauw Ming <ming@teluu.com> | 2010-08-27 06:46:29 +0000 |
---|---|---|
committer | Liong Sauw Ming <ming@teluu.com> | 2010-08-27 06:46:29 +0000 |
commit | a5c285d7fd046c862ef2ba11cd8bef71786db939 (patch) | |
tree | 13247c6735d6bb8f6a4b6d5253b39dd8a4b0e6a3 /pjlib/src/pj/activesock.c | |
parent | 41b2998504157f24d6b991d372e77addbed55030 (diff) |
Closed ticket #1107: iOS4 background feature
* pjlib:
* add support for activesock TCP to work in background mode.
* add feature in ioqueue to recreate closed UDP sockets.
* pjsip-apps:
* ipjsua: add support for iPhone OS 4 background mode
* ipjsystest: add support for iPhone OS 4 background mode
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@3299 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjlib/src/pj/activesock.c')
-rw-r--r-- | pjlib/src/pj/activesock.c | 81 |
1 files changed, 80 insertions, 1 deletions
diff --git a/pjlib/src/pj/activesock.c b/pjlib/src/pj/activesock.c index 6da4c7fa..0ba1a790 100644 --- a/pjlib/src/pj/activesock.c +++ b/pjlib/src/pj/activesock.c @@ -21,10 +21,16 @@ #include <pj/compat/socket.h> #include <pj/assert.h> #include <pj/errno.h> +#include <pj/log.h> #include <pj/pool.h> #include <pj/sock.h> #include <pj/string.h> +#if defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \ + PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0 +# include <CFNetwork/CFNetwork.h> +#endif + #define PJ_ACTIVESOCK_MAX_LOOP 50 @@ -71,7 +77,13 @@ struct pj_activesock_t unsigned async_count; unsigned max_loop; pj_activesock_cb cb; - +#if defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \ + PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0 + int bg_setting; + pj_sock_t sock; + CFReadStreamRef readStream; +#endif + struct send_data send_data; struct read_op *read_op; @@ -105,6 +117,51 @@ PJ_DEF(void) pj_activesock_cfg_default(pj_activesock_cfg *cfg) cfg->whole_data = PJ_TRUE; } +#if defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \ + PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0 +static void activesock_destroy_iphone_os_stream(pj_activesock_t *asock) +{ + if (asock->readStream) { + CFReadStreamClose(asock->readStream); + CFRelease(asock->readStream); + asock->readStream = NULL; + } +} + +static void activesock_create_iphone_os_stream(pj_activesock_t *asock) +{ + if (asock->bg_setting && asock->stream_oriented) { + activesock_destroy_iphone_os_stream(asock); + + CFStreamCreatePairWithSocket(kCFAllocatorDefault, asock->sock, + &asock->readStream, NULL); + + if (!asock->readStream || + CFReadStreamSetProperty(asock->readStream, + kCFStreamNetworkServiceType, + kCFStreamNetworkServiceTypeVoIP) + != TRUE || + CFReadStreamOpen(asock->readStream) != TRUE) + { + PJ_LOG(2,("", "Failed to configure TCP transport for VoIP " + "usage. Background mode will not be supported.")); + + activesock_destroy_iphone_os_stream(asock); + } + } +} + + +PJ_DEF(void) pj_activesock_set_iphone_os_bg(pj_activesock_t *asock, + int val) +{ + asock->bg_setting = val; + if (asock->bg_setting) + activesock_create_iphone_os_stream(asock); + else + activesock_destroy_iphone_os_stream(asock); +} +#endif PJ_DEF(pj_status_t) pj_activesock_create( pj_pool_t *pool, pj_sock_t sock, @@ -156,6 +213,13 @@ PJ_DEF(pj_status_t) pj_activesock_create( pj_pool_t *pool, pj_ioqueue_set_concurrency(asock->key, opt->concurrency); } +#if defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \ + PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0 + asock->sock = sock; + pj_activesock_set_iphone_os_bg(asock, + PJ_ACTIVESOCK_TCP_IPHONE_OS_BG); +#endif + *p_asock = asock; return PJ_SUCCESS; } @@ -215,6 +279,11 @@ PJ_DEF(pj_status_t) pj_activesock_close(pj_activesock_t *asock) { PJ_ASSERT_RETURN(asock, PJ_EINVAL); if (asock->key) { +#if defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \ + PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0 + activesock_destroy_iphone_os_stream(asock); +#endif + pj_ioqueue_unregister(asock->key); asock->key = NULL; } @@ -733,6 +802,10 @@ static void ioqueue_on_accept_complete(pj_ioqueue_key_t *key, if (!ret) return; +#if defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \ + PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0 + activesock_create_iphone_os_stream(asock); +#endif } else if (status==PJ_SUCCESS) { /* Application doesn't handle the new socket, we need to * close it to avoid resource leak. @@ -775,6 +848,12 @@ static void ioqueue_on_connect_complete(pj_ioqueue_key_t *key, /* We've been destroyed */ return; } + +#if defined(PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT) && \ + PJ_IPHONE_OS_HAS_MULTITASKING_SUPPORT!=0 + activesock_create_iphone_os_stream(asock); +#endif + } } #endif /* PJ_HAS_TCP */ |