| 181 | === SIP transport keepalive while in background === #keepalive |
| 182 | |
| 183 | As the process is normally suspended when application is in the background, the worker thread that handles TCP keepalive timer is also suspended. So basically application needs to schedule periodic wakeup to allow the library send TCP keep-alive. Sample code: |
| 184 | {{{ |
| 185 | - (void)keepAlive { |
| 186 | /* Register this thread if not yet */ |
| 187 | if (!pj_thread_is_registered()) { |
| 188 | static pj_thread_desc thread_desc; |
| 189 | static pj_thread_t *thread; |
| 190 | pj_thread_register("mainthread", thread_desc, &thread); |
| 191 | } |
| 192 | |
| 193 | /* Simply sleep for 5s, give the time for library to send transport |
| 194 | * keepalive packet, and wait for server response if any. Don't sleep |
| 195 | * too short, to avoid too many wakeups, because when there is any |
| 196 | * response from server, app will be woken up again (see also #1482). |
| 197 | */ |
| 198 | pj_thread_sleep(5000); |
| 199 | } |
| 200 | |
| 201 | - (void)applicationDidEnterBackground:(UIApplication *)application |
| 202 | { |
| 203 | /* Send keep alive manually at the beginning of background */ |
| 204 | pjsip_endpt_send_raw*(...); |
| 205 | |
| 206 | /* iOS requires that the minimum keep alive interval is 600s */ |
| 207 | [application setKeepAliveTimeout:600 handler: ^{ |
| 208 | [self performSelectorOnMainThread:@selector(keepAlive) |
| 209 | withObject:nil waitUntilDone:YES]; |
| 210 | }]; |
| 211 | } |
| 212 | }}} |
| 213 | |
| 214 | Make sure that keepalive feature of SIP transport is not disabled, see [http://www.pjsip.org/docs/latest/pjsip/docs/html/group__PJSIP__CONFIG.htm#ga02217f4919a7c575d71eed407be63d04 PJSIP_TCP/TLS_KEEP_ALIVE_INTERVAL] docs, and the keepalive interval is set to less than 600s. |
| 215 | |
| 216 | Alternatively, configuring server to send keepalive ping packet, if possible, and client responds back by sending keepalive pong to the server, so we have two-way traffic. As there is no way to detect incoming ping from server, currently application can just always send pong packet whenever it becomes active (application will be woken up when receiving TCP packet), e.g: send pong packet in UIApplication::applicationDidBecomeActive(). |
| 217 | |