Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 844653c | 2008-12-23 17:27:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | #include "test.h" |
| 21 | |
| 22 | |
| 23 | /** |
| 24 | * \page page_pjlib_ioqueue_udp_test Test: I/O Queue (UDP) |
| 25 | * |
| 26 | * This file provides implementation to test the |
| 27 | * functionality of the I/O queue when UDP socket is used. |
| 28 | * |
| 29 | * |
| 30 | * This file is <b>pjlib-test/ioq_udp.c</b> |
| 31 | * |
| 32 | * \include pjlib-test/ioq_udp.c |
| 33 | */ |
| 34 | |
| 35 | |
| 36 | #if INCLUDE_UDP_IOQUEUE_TEST |
| 37 | |
| 38 | #include <pjlib.h> |
| 39 | |
| 40 | #include <pj/compat/socket.h> |
| 41 | |
| 42 | #define THIS_FILE "test_udp" |
| 43 | #define PORT 51233 |
Benny Prijono | 63ab356 | 2006-07-08 19:46:43 +0000 | [diff] [blame] | 44 | #define LOOP 2 |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 45 | ///#define LOOP 2 |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 46 | #define BUF_MIN_SIZE 32 |
| 47 | #define BUF_MAX_SIZE 2048 |
| 48 | #define SOCK_INACTIVE_MIN (1) |
| 49 | #define SOCK_INACTIVE_MAX (PJ_IOQUEUE_MAX_HANDLES - 2) |
| 50 | #define POOL_SIZE (2*BUF_MAX_SIZE + SOCK_INACTIVE_MAX*128 + 2048) |
| 51 | |
| 52 | #undef TRACE_ |
| 53 | #define TRACE_(msg) PJ_LOG(3,(THIS_FILE,"....." msg)) |
| 54 | |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 55 | #if 0 |
| 56 | # define TRACE__(args) PJ_LOG(3,args) |
| 57 | #else |
| 58 | # define TRACE__(args) |
| 59 | #endif |
| 60 | |
| 61 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 62 | static pj_ssize_t callback_read_size, |
| 63 | callback_write_size, |
| 64 | callback_accept_status, |
| 65 | callback_connect_status; |
| 66 | static pj_ioqueue_key_t *callback_read_key, |
| 67 | *callback_write_key, |
| 68 | *callback_accept_key, |
| 69 | *callback_connect_key; |
| 70 | static pj_ioqueue_op_key_t *callback_read_op, |
| 71 | *callback_write_op, |
| 72 | *callback_accept_op; |
| 73 | |
| 74 | static void on_ioqueue_read(pj_ioqueue_key_t *key, |
| 75 | pj_ioqueue_op_key_t *op_key, |
| 76 | pj_ssize_t bytes_read) |
| 77 | { |
| 78 | callback_read_key = key; |
| 79 | callback_read_op = op_key; |
| 80 | callback_read_size = bytes_read; |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 81 | TRACE__((THIS_FILE, " callback_read_key = %p, bytes=%d", |
| 82 | key, bytes_read)); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static void on_ioqueue_write(pj_ioqueue_key_t *key, |
| 86 | pj_ioqueue_op_key_t *op_key, |
| 87 | pj_ssize_t bytes_written) |
| 88 | { |
| 89 | callback_write_key = key; |
| 90 | callback_write_op = op_key; |
| 91 | callback_write_size = bytes_written; |
| 92 | } |
| 93 | |
| 94 | static void on_ioqueue_accept(pj_ioqueue_key_t *key, |
| 95 | pj_ioqueue_op_key_t *op_key, |
| 96 | pj_sock_t sock, int status) |
| 97 | { |
| 98 | PJ_UNUSED_ARG(sock); |
| 99 | callback_accept_key = key; |
| 100 | callback_accept_op = op_key; |
| 101 | callback_accept_status = status; |
| 102 | } |
| 103 | |
| 104 | static void on_ioqueue_connect(pj_ioqueue_key_t *key, int status) |
| 105 | { |
| 106 | callback_connect_key = key; |
| 107 | callback_connect_status = status; |
| 108 | } |
| 109 | |
| 110 | static pj_ioqueue_callback test_cb = |
| 111 | { |
| 112 | &on_ioqueue_read, |
| 113 | &on_ioqueue_write, |
| 114 | &on_ioqueue_accept, |
| 115 | &on_ioqueue_connect, |
| 116 | }; |
| 117 | |
| 118 | #ifdef PJ_WIN32 |
| 119 | # define S_ADDR S_un.S_addr |
| 120 | #else |
| 121 | # define S_ADDR s_addr |
| 122 | #endif |
| 123 | |
| 124 | /* |
| 125 | * compliance_test() |
| 126 | * To test that the basic IOQueue functionality works. It will just exchange |
| 127 | * data between two sockets. |
| 128 | */ |
Benny Prijono | e3f79fd | 2008-02-13 15:17:28 +0000 | [diff] [blame] | 129 | static int compliance_test(pj_bool_t allow_concur) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 130 | { |
| 131 | pj_sock_t ssock=-1, csock=-1; |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 132 | pj_sockaddr_in addr, dst_addr; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 133 | int addrlen; |
| 134 | pj_pool_t *pool = NULL; |
| 135 | char *send_buf, *recv_buf; |
| 136 | pj_ioqueue_t *ioque = NULL; |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 137 | pj_ioqueue_key_t *skey = NULL, *ckey = NULL; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 138 | pj_ioqueue_op_key_t read_op, write_op; |
| 139 | int bufsize = BUF_MIN_SIZE; |
| 140 | pj_ssize_t bytes, status = -1; |
| 141 | pj_str_t temp; |
| 142 | pj_bool_t send_pending, recv_pending; |
| 143 | pj_status_t rc; |
| 144 | |
| 145 | pj_set_os_error(PJ_SUCCESS); |
| 146 | |
| 147 | // Create pool. |
| 148 | pool = pj_pool_create(mem, NULL, POOL_SIZE, 4000, NULL); |
| 149 | |
| 150 | // Allocate buffers for send and receive. |
| 151 | send_buf = (char*)pj_pool_alloc(pool, bufsize); |
| 152 | recv_buf = (char*)pj_pool_alloc(pool, bufsize); |
| 153 | |
| 154 | // Allocate sockets for sending and receiving. |
| 155 | TRACE_("creating sockets..."); |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 156 | rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &ssock); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 157 | if (rc==PJ_SUCCESS) |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 158 | rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &csock); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 159 | else |
| 160 | csock = PJ_INVALID_SOCKET; |
| 161 | if (rc != PJ_SUCCESS) { |
| 162 | app_perror("...ERROR in pj_sock_socket()", rc); |
| 163 | status=-1; goto on_error; |
| 164 | } |
| 165 | |
| 166 | // Bind server socket. |
| 167 | TRACE_("bind socket..."); |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 168 | pj_bzero(&addr, sizeof(addr)); |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 169 | addr.sin_family = pj_AF_INET(); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 170 | addr.sin_port = pj_htons(PORT); |
| 171 | if (pj_sock_bind(ssock, &addr, sizeof(addr))) { |
| 172 | status=-10; goto on_error; |
| 173 | } |
| 174 | |
| 175 | // Create I/O Queue. |
| 176 | TRACE_("create ioqueue..."); |
| 177 | rc = pj_ioqueue_create(pool, PJ_IOQUEUE_MAX_HANDLES, &ioque); |
| 178 | if (rc != PJ_SUCCESS) { |
| 179 | status=-20; goto on_error; |
| 180 | } |
| 181 | |
Benny Prijono | e3f79fd | 2008-02-13 15:17:28 +0000 | [diff] [blame] | 182 | // Set concurrency |
| 183 | TRACE_("set concurrency..."); |
| 184 | rc = pj_ioqueue_set_default_concurrency(ioque, allow_concur); |
| 185 | if (rc != PJ_SUCCESS) { |
| 186 | status=-21; goto on_error; |
| 187 | } |
| 188 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 189 | // Register server and client socket. |
| 190 | // We put this after inactivity socket, hopefully this can represent the |
| 191 | // worst waiting time. |
| 192 | TRACE_("registering first sockets..."); |
| 193 | rc = pj_ioqueue_register_sock(pool, ioque, ssock, NULL, |
| 194 | &test_cb, &skey); |
| 195 | if (rc != PJ_SUCCESS) { |
| 196 | app_perror("...error(10): ioqueue_register error", rc); |
| 197 | status=-25; goto on_error; |
| 198 | } |
| 199 | TRACE_("registering second sockets..."); |
| 200 | rc = pj_ioqueue_register_sock( pool, ioque, csock, NULL, |
| 201 | &test_cb, &ckey); |
| 202 | if (rc != PJ_SUCCESS) { |
| 203 | app_perror("...error(11): ioqueue_register error", rc); |
| 204 | status=-26; goto on_error; |
| 205 | } |
| 206 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 207 | // Randomize send_buf. |
| 208 | pj_create_random_string(send_buf, bufsize); |
| 209 | |
| 210 | // Register reading from ioqueue. |
| 211 | TRACE_("start recvfrom..."); |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 212 | pj_bzero(&addr, sizeof(addr)); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 213 | addrlen = sizeof(addr); |
| 214 | bytes = bufsize; |
| 215 | rc = pj_ioqueue_recvfrom(skey, &read_op, recv_buf, &bytes, 0, |
| 216 | &addr, &addrlen); |
| 217 | if (rc != PJ_SUCCESS && rc != PJ_EPENDING) { |
| 218 | app_perror("...error: pj_ioqueue_recvfrom", rc); |
| 219 | status=-28; goto on_error; |
| 220 | } else if (rc == PJ_EPENDING) { |
| 221 | recv_pending = 1; |
| 222 | PJ_LOG(3, (THIS_FILE, |
| 223 | "......ok: recvfrom returned pending")); |
| 224 | } else { |
| 225 | PJ_LOG(3, (THIS_FILE, |
| 226 | "......error: recvfrom returned immediate ok!")); |
| 227 | status=-29; goto on_error; |
| 228 | } |
| 229 | |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 230 | // Set destination address to send the packet. |
| 231 | TRACE_("set destination address..."); |
| 232 | temp = pj_str("127.0.0.1"); |
| 233 | if ((rc=pj_sockaddr_in_init(&dst_addr, &temp, PORT)) != 0) { |
| 234 | app_perror("...error: unable to resolve 127.0.0.1", rc); |
| 235 | status=-290; goto on_error; |
| 236 | } |
| 237 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 238 | // Write must return the number of bytes. |
| 239 | TRACE_("start sendto..."); |
| 240 | bytes = bufsize; |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 241 | rc = pj_ioqueue_sendto(ckey, &write_op, send_buf, &bytes, 0, &dst_addr, |
| 242 | sizeof(dst_addr)); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 243 | if (rc != PJ_SUCCESS && rc != PJ_EPENDING) { |
| 244 | app_perror("...error: pj_ioqueue_sendto", rc); |
| 245 | status=-30; goto on_error; |
| 246 | } else if (rc == PJ_EPENDING) { |
| 247 | send_pending = 1; |
| 248 | PJ_LOG(3, (THIS_FILE, |
| 249 | "......ok: sendto returned pending")); |
| 250 | } else { |
| 251 | send_pending = 0; |
| 252 | PJ_LOG(3, (THIS_FILE, |
| 253 | "......ok: sendto returned immediate success")); |
| 254 | } |
| 255 | |
| 256 | // reset callback variables. |
| 257 | callback_read_size = callback_write_size = 0; |
| 258 | callback_accept_status = callback_connect_status = -2; |
| 259 | callback_read_key = callback_write_key = |
| 260 | callback_accept_key = callback_connect_key = NULL; |
| 261 | callback_read_op = callback_write_op = NULL; |
| 262 | |
| 263 | // Poll if pending. |
| 264 | while (send_pending || recv_pending) { |
| 265 | int rc; |
| 266 | pj_time_val timeout = { 5, 0 }; |
| 267 | |
| 268 | TRACE_("poll..."); |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 269 | #ifdef PJ_SYMBIAN |
| 270 | rc = pj_symbianos_poll(-1, 5000); |
| 271 | #else |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 272 | rc = pj_ioqueue_poll(ioque, &timeout); |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 273 | #endif |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 274 | |
| 275 | if (rc == 0) { |
| 276 | PJ_LOG(1,(THIS_FILE, "...ERROR: timed out...")); |
| 277 | status=-45; goto on_error; |
| 278 | } else if (rc < 0) { |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 279 | app_perror("...ERROR in ioqueue_poll()", -rc); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 280 | status=-50; goto on_error; |
| 281 | } |
| 282 | |
| 283 | if (callback_read_key != NULL) { |
| 284 | if (callback_read_size != bufsize) { |
| 285 | status=-61; goto on_error; |
| 286 | } |
| 287 | if (callback_read_key != skey) { |
| 288 | status=-65; goto on_error; |
| 289 | } |
| 290 | if (callback_read_op != &read_op) { |
| 291 | status=-66; goto on_error; |
| 292 | } |
| 293 | |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 294 | if (pj_memcmp(send_buf, recv_buf, bufsize) != 0) { |
| 295 | status=-67; goto on_error; |
| 296 | } |
| 297 | if (addrlen != sizeof(pj_sockaddr_in)) { |
| 298 | status=-68; goto on_error; |
| 299 | } |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 300 | if (addr.sin_family != pj_AF_INET()) { |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 301 | status=-69; goto on_error; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | |
| 305 | recv_pending = 0; |
| 306 | } |
| 307 | |
| 308 | if (callback_write_key != NULL) { |
| 309 | if (callback_write_size != bufsize) { |
| 310 | status=-73; goto on_error; |
| 311 | } |
| 312 | if (callback_write_key != ckey) { |
| 313 | status=-75; goto on_error; |
| 314 | } |
| 315 | if (callback_write_op != &write_op) { |
| 316 | status=-76; goto on_error; |
| 317 | } |
| 318 | |
| 319 | send_pending = 0; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | // Success |
| 324 | status = 0; |
| 325 | |
| 326 | on_error: |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 327 | if (skey) |
| 328 | pj_ioqueue_unregister(skey); |
| 329 | else if (ssock != -1) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 330 | pj_sock_close(ssock); |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 331 | |
| 332 | if (ckey) |
| 333 | pj_ioqueue_unregister(ckey); |
| 334 | else if (csock != -1) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 335 | pj_sock_close(csock); |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 336 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 337 | if (ioque != NULL) |
| 338 | pj_ioqueue_destroy(ioque); |
| 339 | pj_pool_release(pool); |
| 340 | return status; |
| 341 | |
| 342 | } |
| 343 | |
Benny Prijono | 8d317a0 | 2006-03-22 11:49:19 +0000 | [diff] [blame] | 344 | |
| 345 | static void on_read_complete(pj_ioqueue_key_t *key, |
| 346 | pj_ioqueue_op_key_t *op_key, |
| 347 | pj_ssize_t bytes_read) |
| 348 | { |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 349 | unsigned *p_packet_cnt = (unsigned*) pj_ioqueue_get_user_data(key); |
Benny Prijono | 8d317a0 | 2006-03-22 11:49:19 +0000 | [diff] [blame] | 350 | |
| 351 | PJ_UNUSED_ARG(op_key); |
| 352 | PJ_UNUSED_ARG(bytes_read); |
| 353 | |
| 354 | (*p_packet_cnt)++; |
| 355 | } |
| 356 | |
| 357 | /* |
| 358 | * unregister_test() |
| 359 | * Check if callback is still called after socket has been unregistered or |
| 360 | * closed. |
| 361 | */ |
Benny Prijono | e3f79fd | 2008-02-13 15:17:28 +0000 | [diff] [blame] | 362 | static int unregister_test(pj_bool_t allow_concur) |
Benny Prijono | 8d317a0 | 2006-03-22 11:49:19 +0000 | [diff] [blame] | 363 | { |
| 364 | enum { RPORT = 50000, SPORT = 50001 }; |
| 365 | pj_pool_t *pool; |
| 366 | pj_ioqueue_t *ioqueue; |
| 367 | pj_sock_t ssock; |
| 368 | pj_sock_t rsock; |
| 369 | int addrlen; |
| 370 | pj_sockaddr_in addr; |
| 371 | pj_ioqueue_key_t *key; |
| 372 | pj_ioqueue_op_key_t opkey; |
| 373 | pj_ioqueue_callback cb; |
| 374 | unsigned packet_cnt; |
| 375 | char sendbuf[10], recvbuf[10]; |
| 376 | pj_ssize_t bytes; |
| 377 | pj_time_val timeout; |
| 378 | pj_status_t status; |
| 379 | |
| 380 | pool = pj_pool_create(mem, "test", 4000, 4000, NULL); |
| 381 | if (!pool) { |
| 382 | app_perror("Unable to create pool", PJ_ENOMEM); |
| 383 | return -100; |
| 384 | } |
| 385 | |
| 386 | status = pj_ioqueue_create(pool, 16, &ioqueue); |
| 387 | if (status != PJ_SUCCESS) { |
| 388 | app_perror("Error creating ioqueue", status); |
| 389 | return -110; |
| 390 | } |
| 391 | |
Benny Prijono | e3f79fd | 2008-02-13 15:17:28 +0000 | [diff] [blame] | 392 | // Set concurrency |
| 393 | TRACE_("set concurrency..."); |
| 394 | status = pj_ioqueue_set_default_concurrency(ioqueue, allow_concur); |
| 395 | if (status != PJ_SUCCESS) { |
| 396 | return -112; |
| 397 | } |
| 398 | |
Benny Prijono | 8d317a0 | 2006-03-22 11:49:19 +0000 | [diff] [blame] | 399 | /* Create sender socket */ |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 400 | status = app_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, SPORT, &ssock); |
Benny Prijono | 8d317a0 | 2006-03-22 11:49:19 +0000 | [diff] [blame] | 401 | if (status != PJ_SUCCESS) { |
| 402 | app_perror("Error initializing socket", status); |
| 403 | return -120; |
| 404 | } |
| 405 | |
| 406 | /* Create receiver socket. */ |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 407 | status = app_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, RPORT, &rsock); |
Benny Prijono | 8d317a0 | 2006-03-22 11:49:19 +0000 | [diff] [blame] | 408 | if (status != PJ_SUCCESS) { |
| 409 | app_perror("Error initializing socket", status); |
| 410 | return -130; |
| 411 | } |
| 412 | |
| 413 | /* Register rsock to ioqueue. */ |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 414 | pj_bzero(&cb, sizeof(cb)); |
Benny Prijono | 8d317a0 | 2006-03-22 11:49:19 +0000 | [diff] [blame] | 415 | cb.on_read_complete = &on_read_complete; |
| 416 | packet_cnt = 0; |
| 417 | status = pj_ioqueue_register_sock(pool, ioqueue, rsock, &packet_cnt, |
| 418 | &cb, &key); |
| 419 | if (status != PJ_SUCCESS) { |
| 420 | app_perror("Error registering to ioqueue", status); |
| 421 | return -140; |
| 422 | } |
| 423 | |
| 424 | /* Init operation key. */ |
| 425 | pj_ioqueue_op_key_init(&opkey, sizeof(opkey)); |
| 426 | |
| 427 | /* Start reading. */ |
| 428 | bytes = sizeof(recvbuf); |
| 429 | status = pj_ioqueue_recv( key, &opkey, recvbuf, &bytes, 0); |
| 430 | if (status != PJ_EPENDING) { |
| 431 | app_perror("Expecting PJ_EPENDING, but got this", status); |
| 432 | return -150; |
| 433 | } |
| 434 | |
| 435 | /* Init destination address. */ |
| 436 | addrlen = sizeof(addr); |
| 437 | status = pj_sock_getsockname(rsock, &addr, &addrlen); |
| 438 | if (status != PJ_SUCCESS) { |
| 439 | app_perror("getsockname error", status); |
| 440 | return -160; |
| 441 | } |
| 442 | |
| 443 | /* Override address with 127.0.0.1, since getsockname will return |
| 444 | * zero in the address field. |
| 445 | */ |
| 446 | addr.sin_addr = pj_inet_addr2("127.0.0.1"); |
| 447 | |
| 448 | /* Init buffer to send */ |
| 449 | pj_ansi_strcpy(sendbuf, "Hello0123"); |
| 450 | |
| 451 | /* Send one packet. */ |
| 452 | bytes = sizeof(sendbuf); |
| 453 | status = pj_sock_sendto(ssock, sendbuf, &bytes, 0, |
| 454 | &addr, sizeof(addr)); |
| 455 | |
| 456 | if (status != PJ_SUCCESS) { |
| 457 | app_perror("sendto error", status); |
| 458 | return -170; |
| 459 | } |
| 460 | |
| 461 | /* Check if packet is received. */ |
| 462 | timeout.sec = 1; timeout.msec = 0; |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 463 | #ifdef PJ_SYMBIAN |
| 464 | pj_symbianos_poll(-1, 1000); |
| 465 | #else |
Benny Prijono | 8d317a0 | 2006-03-22 11:49:19 +0000 | [diff] [blame] | 466 | pj_ioqueue_poll(ioqueue, &timeout); |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 467 | #endif |
Benny Prijono | 8d317a0 | 2006-03-22 11:49:19 +0000 | [diff] [blame] | 468 | |
| 469 | if (packet_cnt != 1) { |
| 470 | return -180; |
| 471 | } |
| 472 | |
| 473 | /* Just to make sure things are settled.. */ |
| 474 | pj_thread_sleep(100); |
| 475 | |
| 476 | /* Start reading again. */ |
| 477 | bytes = sizeof(recvbuf); |
| 478 | status = pj_ioqueue_recv( key, &opkey, recvbuf, &bytes, 0); |
| 479 | if (status != PJ_EPENDING) { |
| 480 | app_perror("Expecting PJ_EPENDING, but got this", status); |
| 481 | return -190; |
| 482 | } |
| 483 | |
| 484 | /* Reset packet counter */ |
| 485 | packet_cnt = 0; |
| 486 | |
| 487 | /* Send one packet. */ |
| 488 | bytes = sizeof(sendbuf); |
| 489 | status = pj_sock_sendto(ssock, sendbuf, &bytes, 0, |
| 490 | &addr, sizeof(addr)); |
| 491 | |
| 492 | if (status != PJ_SUCCESS) { |
| 493 | app_perror("sendto error", status); |
| 494 | return -200; |
| 495 | } |
| 496 | |
| 497 | /* Now unregister and close socket. */ |
| 498 | pj_ioqueue_unregister(key); |
Benny Prijono | 8d317a0 | 2006-03-22 11:49:19 +0000 | [diff] [blame] | 499 | |
| 500 | /* Poll ioqueue. */ |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 501 | #ifdef PJ_SYMBIAN |
| 502 | pj_symbianos_poll(-1, 1000); |
| 503 | #else |
Benny Prijono | 8d317a0 | 2006-03-22 11:49:19 +0000 | [diff] [blame] | 504 | timeout.sec = 1; timeout.msec = 0; |
| 505 | pj_ioqueue_poll(ioqueue, &timeout); |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 506 | #endif |
Benny Prijono | 8d317a0 | 2006-03-22 11:49:19 +0000 | [diff] [blame] | 507 | |
| 508 | /* Must NOT receive any packets after socket is closed! */ |
| 509 | if (packet_cnt > 0) { |
| 510 | PJ_LOG(3,(THIS_FILE, "....errror: not expecting to receive packet " |
| 511 | "after socket has been closed")); |
| 512 | return -210; |
| 513 | } |
| 514 | |
| 515 | /* Success */ |
| 516 | pj_sock_close(ssock); |
| 517 | pj_ioqueue_destroy(ioqueue); |
| 518 | |
| 519 | pj_pool_release(pool); |
| 520 | |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 525 | /* |
| 526 | * Testing with many handles. |
| 527 | * This will just test registering PJ_IOQUEUE_MAX_HANDLES count |
| 528 | * of sockets to the ioqueue. |
| 529 | */ |
Benny Prijono | e3f79fd | 2008-02-13 15:17:28 +0000 | [diff] [blame] | 530 | static int many_handles_test(pj_bool_t allow_concur) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 531 | { |
| 532 | enum { MAX = PJ_IOQUEUE_MAX_HANDLES }; |
| 533 | pj_pool_t *pool; |
| 534 | pj_ioqueue_t *ioqueue; |
| 535 | pj_sock_t *sock; |
| 536 | pj_ioqueue_key_t **key; |
| 537 | pj_status_t rc; |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 538 | int count, i; /* must be signed */ |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 539 | |
| 540 | PJ_LOG(3,(THIS_FILE,"...testing with so many handles")); |
| 541 | |
| 542 | pool = pj_pool_create(mem, NULL, 4000, 4000, NULL); |
| 543 | if (!pool) |
| 544 | return PJ_ENOMEM; |
| 545 | |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 546 | key = (pj_ioqueue_key_t**) |
| 547 | pj_pool_alloc(pool, MAX*sizeof(pj_ioqueue_key_t*)); |
| 548 | sock = (pj_sock_t*) pj_pool_alloc(pool, MAX*sizeof(pj_sock_t)); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 549 | |
| 550 | /* Create IOQueue */ |
| 551 | rc = pj_ioqueue_create(pool, MAX, &ioqueue); |
| 552 | if (rc != PJ_SUCCESS || ioqueue == NULL) { |
| 553 | app_perror("...error in pj_ioqueue_create", rc); |
| 554 | return -10; |
| 555 | } |
| 556 | |
Benny Prijono | e3f79fd | 2008-02-13 15:17:28 +0000 | [diff] [blame] | 557 | // Set concurrency |
| 558 | rc = pj_ioqueue_set_default_concurrency(ioqueue, allow_concur); |
| 559 | if (rc != PJ_SUCCESS) { |
| 560 | return -11; |
| 561 | } |
| 562 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 563 | /* Register as many sockets. */ |
| 564 | for (count=0; count<MAX; ++count) { |
| 565 | sock[count] = PJ_INVALID_SOCKET; |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 566 | rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock[count]); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 567 | if (rc != PJ_SUCCESS || sock[count] == PJ_INVALID_SOCKET) { |
| 568 | PJ_LOG(3,(THIS_FILE, "....unable to create %d-th socket, rc=%d", |
| 569 | count, rc)); |
| 570 | break; |
| 571 | } |
| 572 | key[count] = NULL; |
| 573 | rc = pj_ioqueue_register_sock(pool, ioqueue, sock[count], |
| 574 | NULL, &test_cb, &key[count]); |
| 575 | if (rc != PJ_SUCCESS || key[count] == NULL) { |
| 576 | PJ_LOG(3,(THIS_FILE, "....unable to register %d-th socket, rc=%d", |
| 577 | count, rc)); |
| 578 | return -30; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | /* Test complete. */ |
| 583 | |
| 584 | /* Now deregister and close all handles. */ |
| 585 | |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 586 | /* NOTE for RTEMS: |
| 587 | * It seems that the order of close(sock) is pretty important here. |
| 588 | * If we close the sockets with the same order as when they were created, |
| 589 | * RTEMS doesn't seem to reuse the sockets, thus next socket created |
| 590 | * will have descriptor higher than the last socket created. |
| 591 | * If we close the sockets in the reverse order, then the descriptor will |
| 592 | * get reused. |
| 593 | * This used to cause problem with select ioqueue, since the ioqueue |
| 594 | * always gives FD_SETSIZE for the first select() argument. This ioqueue |
| 595 | * behavior can be changed with setting PJ_SELECT_NEEDS_NFDS macro. |
| 596 | */ |
| 597 | for (i=count-1; i>=0; --i) { |
| 598 | ///for (i=0; i<count; ++i) { |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 599 | rc = pj_ioqueue_unregister(key[i]); |
| 600 | if (rc != PJ_SUCCESS) { |
| 601 | app_perror("...error in pj_ioqueue_unregister", rc); |
| 602 | } |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | rc = pj_ioqueue_destroy(ioqueue); |
| 606 | if (rc != PJ_SUCCESS) { |
| 607 | app_perror("...error in pj_ioqueue_destroy", rc); |
| 608 | } |
| 609 | |
| 610 | pj_pool_release(pool); |
| 611 | |
| 612 | PJ_LOG(3,(THIS_FILE,"....many_handles_test() ok")); |
| 613 | |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | /* |
| 618 | * Multi-operation test. |
| 619 | */ |
| 620 | |
| 621 | /* |
| 622 | * Benchmarking IOQueue |
| 623 | */ |
Benny Prijono | e3f79fd | 2008-02-13 15:17:28 +0000 | [diff] [blame] | 624 | static int bench_test(pj_bool_t allow_concur, int bufsize, |
| 625 | int inactive_sock_count) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 626 | { |
| 627 | pj_sock_t ssock=-1, csock=-1; |
| 628 | pj_sockaddr_in addr; |
| 629 | pj_pool_t *pool = NULL; |
| 630 | pj_sock_t *inactive_sock=NULL; |
| 631 | pj_ioqueue_op_key_t *inactive_read_op; |
| 632 | char *send_buf, *recv_buf; |
| 633 | pj_ioqueue_t *ioque = NULL; |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 634 | pj_ioqueue_key_t *skey, *ckey, *keys[SOCK_INACTIVE_MAX+2]; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 635 | pj_timestamp t1, t2, t_elapsed; |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 636 | int rc=0, i; /* i must be signed */ |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 637 | pj_str_t temp; |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 638 | char errbuf[PJ_ERR_MSG_SIZE]; |
| 639 | |
| 640 | TRACE__((THIS_FILE, " bench test %d", inactive_sock_count)); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 641 | |
| 642 | // Create pool. |
| 643 | pool = pj_pool_create(mem, NULL, POOL_SIZE, 4000, NULL); |
| 644 | |
| 645 | // Allocate buffers for send and receive. |
| 646 | send_buf = (char*)pj_pool_alloc(pool, bufsize); |
| 647 | recv_buf = (char*)pj_pool_alloc(pool, bufsize); |
| 648 | |
| 649 | // Allocate sockets for sending and receiving. |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 650 | rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &ssock); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 651 | if (rc == PJ_SUCCESS) { |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 652 | rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &csock); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 653 | } else |
| 654 | csock = PJ_INVALID_SOCKET; |
| 655 | if (rc != PJ_SUCCESS) { |
| 656 | app_perror("...error: pj_sock_socket()", rc); |
| 657 | goto on_error; |
| 658 | } |
| 659 | |
| 660 | // Bind server socket. |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 661 | pj_bzero(&addr, sizeof(addr)); |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 662 | addr.sin_family = pj_AF_INET(); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 663 | addr.sin_port = pj_htons(PORT); |
| 664 | if (pj_sock_bind(ssock, &addr, sizeof(addr))) |
| 665 | goto on_error; |
| 666 | |
| 667 | pj_assert(inactive_sock_count+2 <= PJ_IOQUEUE_MAX_HANDLES); |
| 668 | |
| 669 | // Create I/O Queue. |
| 670 | rc = pj_ioqueue_create(pool, PJ_IOQUEUE_MAX_HANDLES, &ioque); |
| 671 | if (rc != PJ_SUCCESS) { |
| 672 | app_perror("...error: pj_ioqueue_create()", rc); |
| 673 | goto on_error; |
| 674 | } |
| 675 | |
Benny Prijono | e3f79fd | 2008-02-13 15:17:28 +0000 | [diff] [blame] | 676 | // Set concurrency |
| 677 | rc = pj_ioqueue_set_default_concurrency(ioque, allow_concur); |
| 678 | if (rc != PJ_SUCCESS) { |
| 679 | app_perror("...error: pj_ioqueue_set_default_concurrency()", rc); |
| 680 | goto on_error; |
| 681 | } |
| 682 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 683 | // Allocate inactive sockets, and bind them to some arbitrary address. |
| 684 | // Then register them to the I/O queue, and start a read operation. |
| 685 | inactive_sock = (pj_sock_t*)pj_pool_alloc(pool, |
| 686 | inactive_sock_count*sizeof(pj_sock_t)); |
| 687 | inactive_read_op = (pj_ioqueue_op_key_t*)pj_pool_alloc(pool, |
| 688 | inactive_sock_count*sizeof(pj_ioqueue_op_key_t)); |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 689 | pj_bzero(&addr, sizeof(addr)); |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 690 | addr.sin_family = pj_AF_INET(); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 691 | for (i=0; i<inactive_sock_count; ++i) { |
| 692 | pj_ssize_t bytes; |
| 693 | |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 694 | rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &inactive_sock[i]); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 695 | if (rc != PJ_SUCCESS || inactive_sock[i] < 0) { |
| 696 | app_perror("...error: pj_sock_socket()", rc); |
| 697 | goto on_error; |
| 698 | } |
| 699 | if ((rc=pj_sock_bind(inactive_sock[i], &addr, sizeof(addr))) != 0) { |
| 700 | pj_sock_close(inactive_sock[i]); |
| 701 | inactive_sock[i] = PJ_INVALID_SOCKET; |
| 702 | app_perror("...error: pj_sock_bind()", rc); |
| 703 | goto on_error; |
| 704 | } |
| 705 | rc = pj_ioqueue_register_sock(pool, ioque, inactive_sock[i], |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 706 | NULL, &test_cb, &keys[i]); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 707 | if (rc != PJ_SUCCESS) { |
| 708 | pj_sock_close(inactive_sock[i]); |
| 709 | inactive_sock[i] = PJ_INVALID_SOCKET; |
| 710 | app_perror("...error(1): pj_ioqueue_register_sock()", rc); |
| 711 | PJ_LOG(3,(THIS_FILE, "....i=%d", i)); |
| 712 | goto on_error; |
| 713 | } |
| 714 | bytes = bufsize; |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 715 | rc = pj_ioqueue_recv(keys[i], &inactive_read_op[i], recv_buf, &bytes, 0); |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 716 | if (rc != PJ_EPENDING) { |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 717 | pj_sock_close(inactive_sock[i]); |
| 718 | inactive_sock[i] = PJ_INVALID_SOCKET; |
| 719 | app_perror("...error: pj_ioqueue_read()", rc); |
| 720 | goto on_error; |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | // Register server and client socket. |
| 725 | // We put this after inactivity socket, hopefully this can represent the |
| 726 | // worst waiting time. |
| 727 | rc = pj_ioqueue_register_sock(pool, ioque, ssock, NULL, |
| 728 | &test_cb, &skey); |
| 729 | if (rc != PJ_SUCCESS) { |
| 730 | app_perror("...error(2): pj_ioqueue_register_sock()", rc); |
| 731 | goto on_error; |
| 732 | } |
| 733 | |
| 734 | rc = pj_ioqueue_register_sock(pool, ioque, csock, NULL, |
| 735 | &test_cb, &ckey); |
| 736 | if (rc != PJ_SUCCESS) { |
| 737 | app_perror("...error(3): pj_ioqueue_register_sock()", rc); |
| 738 | goto on_error; |
| 739 | } |
| 740 | |
| 741 | // Set destination address to send the packet. |
| 742 | pj_sockaddr_in_init(&addr, pj_cstr(&temp, "127.0.0.1"), PORT); |
| 743 | |
| 744 | // Test loop. |
| 745 | t_elapsed.u64 = 0; |
| 746 | for (i=0; i<LOOP; ++i) { |
| 747 | pj_ssize_t bytes; |
| 748 | pj_ioqueue_op_key_t read_op, write_op; |
| 749 | |
| 750 | // Randomize send buffer. |
| 751 | pj_create_random_string(send_buf, bufsize); |
| 752 | |
| 753 | // Start reading on the server side. |
| 754 | bytes = bufsize; |
| 755 | rc = pj_ioqueue_recv(skey, &read_op, recv_buf, &bytes, 0); |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 756 | if (rc != PJ_EPENDING) { |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 757 | app_perror("...error: pj_ioqueue_read()", rc); |
| 758 | break; |
| 759 | } |
| 760 | |
| 761 | // Starts send on the client side. |
| 762 | bytes = bufsize; |
| 763 | rc = pj_ioqueue_sendto(ckey, &write_op, send_buf, &bytes, 0, |
| 764 | &addr, sizeof(addr)); |
| 765 | if (rc != PJ_SUCCESS && rc != PJ_EPENDING) { |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 766 | app_perror("...error: pj_ioqueue_write()", rc); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 767 | break; |
| 768 | } |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 769 | if (rc == PJ_SUCCESS) { |
| 770 | if (bytes < 0) { |
| 771 | app_perror("...error: pj_ioqueue_sendto()", -bytes); |
| 772 | break; |
| 773 | } |
| 774 | } |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 775 | |
| 776 | // Begin time. |
| 777 | pj_get_timestamp(&t1); |
| 778 | |
| 779 | // Poll the queue until we've got completion event in the server side. |
| 780 | callback_read_key = NULL; |
| 781 | callback_read_size = 0; |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 782 | TRACE__((THIS_FILE, " waiting for key = %p", skey)); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 783 | do { |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 784 | pj_time_val timeout = { 1, 0 }; |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 785 | #ifdef PJ_SYMBIAN |
| 786 | rc = pj_symbianos_poll(-1, 1000); |
| 787 | #else |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 788 | rc = pj_ioqueue_poll(ioque, &timeout); |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 789 | #endif |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 790 | TRACE__((THIS_FILE, " poll rc=%d", rc)); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 791 | } while (rc >= 0 && callback_read_key != skey); |
| 792 | |
| 793 | // End time. |
| 794 | pj_get_timestamp(&t2); |
| 795 | t_elapsed.u64 += (t2.u64 - t1.u64); |
| 796 | |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 797 | if (rc < 0) { |
| 798 | app_perror(" error: pj_ioqueue_poll", -rc); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 799 | break; |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 800 | } |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 801 | |
| 802 | // Compare recv buffer with send buffer. |
| 803 | if (callback_read_size != bufsize || |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 804 | pj_memcmp(send_buf, recv_buf, bufsize)) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 805 | { |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 806 | rc = -10; |
| 807 | PJ_LOG(3,(THIS_FILE, " error: size/buffer mismatch")); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 808 | break; |
| 809 | } |
| 810 | |
| 811 | // Poll until all events are exhausted, before we start the next loop. |
| 812 | do { |
| 813 | pj_time_val timeout = { 0, 10 }; |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 814 | #ifdef PJ_SYMBIAN |
| 815 | rc = pj_symbianos_poll(-1, 100); |
| 816 | #else |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 817 | rc = pj_ioqueue_poll(ioque, &timeout); |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 818 | #endif |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 819 | } while (rc>0); |
| 820 | |
| 821 | rc = 0; |
| 822 | } |
| 823 | |
| 824 | // Print results |
| 825 | if (rc == 0) { |
| 826 | pj_timestamp tzero; |
| 827 | pj_uint32_t usec_delay; |
| 828 | |
| 829 | tzero.u32.hi = tzero.u32.lo = 0; |
| 830 | usec_delay = pj_elapsed_usec( &tzero, &t_elapsed); |
| 831 | |
| 832 | PJ_LOG(3, (THIS_FILE, "...%10d %15d % 9d", |
| 833 | bufsize, inactive_sock_count, usec_delay)); |
| 834 | |
| 835 | } else { |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 836 | PJ_LOG(2, (THIS_FILE, "...ERROR rc=%d (buf:%d, fds:%d)", |
| 837 | rc, bufsize, inactive_sock_count+2)); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | // Cleaning up. |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 841 | for (i=inactive_sock_count-1; i>=0; --i) { |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 842 | pj_ioqueue_unregister(keys[i]); |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 845 | pj_ioqueue_unregister(skey); |
| 846 | pj_ioqueue_unregister(ckey); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 847 | |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 848 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 849 | pj_ioqueue_destroy(ioque); |
| 850 | pj_pool_release( pool); |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 851 | return rc; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 852 | |
| 853 | on_error: |
| 854 | PJ_LOG(1,(THIS_FILE, "...ERROR: %s", |
| 855 | pj_strerror(pj_get_netos_error(), errbuf, sizeof(errbuf)))); |
| 856 | if (ssock) |
| 857 | pj_sock_close(ssock); |
| 858 | if (csock) |
| 859 | pj_sock_close(csock); |
| 860 | for (i=0; i<inactive_sock_count && inactive_sock && |
| 861 | inactive_sock[i]!=PJ_INVALID_SOCKET; ++i) |
| 862 | { |
| 863 | pj_sock_close(inactive_sock[i]); |
| 864 | } |
| 865 | if (ioque != NULL) |
| 866 | pj_ioqueue_destroy(ioque); |
| 867 | pj_pool_release( pool); |
| 868 | return -1; |
| 869 | } |
| 870 | |
Benny Prijono | e3f79fd | 2008-02-13 15:17:28 +0000 | [diff] [blame] | 871 | static int udp_ioqueue_test_imp(pj_bool_t allow_concur) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 872 | { |
| 873 | int status; |
| 874 | int bufsize, sock_count; |
| 875 | |
Benny Prijono | e3f79fd | 2008-02-13 15:17:28 +0000 | [diff] [blame] | 876 | PJ_LOG(3,(THIS_FILE, "..testing with concurency=%d", allow_concur)); |
| 877 | |
Benny Prijono | 63ab356 | 2006-07-08 19:46:43 +0000 | [diff] [blame] | 878 | //goto pass1; |
Benny Prijono | fe9c9b6 | 2006-07-06 20:43:07 +0000 | [diff] [blame] | 879 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 880 | PJ_LOG(3, (THIS_FILE, "...compliance test (%s)", pj_ioqueue_name())); |
Benny Prijono | e3f79fd | 2008-02-13 15:17:28 +0000 | [diff] [blame] | 881 | if ((status=compliance_test(allow_concur)) != 0) { |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 882 | return status; |
| 883 | } |
| 884 | PJ_LOG(3, (THIS_FILE, "....compliance test ok")); |
| 885 | |
Benny Prijono | 8d317a0 | 2006-03-22 11:49:19 +0000 | [diff] [blame] | 886 | |
| 887 | PJ_LOG(3, (THIS_FILE, "...unregister test (%s)", pj_ioqueue_name())); |
Benny Prijono | e3f79fd | 2008-02-13 15:17:28 +0000 | [diff] [blame] | 888 | if ((status=unregister_test(allow_concur)) != 0) { |
Benny Prijono | 8d317a0 | 2006-03-22 11:49:19 +0000 | [diff] [blame] | 889 | return status; |
| 890 | } |
| 891 | PJ_LOG(3, (THIS_FILE, "....unregister test ok")); |
| 892 | |
Benny Prijono | e3f79fd | 2008-02-13 15:17:28 +0000 | [diff] [blame] | 893 | if ((status=many_handles_test(allow_concur)) != 0) { |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 894 | return status; |
| 895 | } |
| 896 | |
Benny Prijono | 63ab356 | 2006-07-08 19:46:43 +0000 | [diff] [blame] | 897 | //return 0; |
| 898 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 899 | PJ_LOG(4, (THIS_FILE, "...benchmarking different buffer size:")); |
| 900 | PJ_LOG(4, (THIS_FILE, "... note: buf=bytes sent, fds=# of fds, " |
| 901 | "elapsed=in timer ticks")); |
| 902 | |
Benny Prijono | 5fb99e5 | 2007-01-23 04:17:56 +0000 | [diff] [blame] | 903 | //pass1: |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 904 | PJ_LOG(3, (THIS_FILE, "...Benchmarking poll times for %s:", pj_ioqueue_name())); |
| 905 | PJ_LOG(3, (THIS_FILE, "...=====================================")); |
| 906 | PJ_LOG(3, (THIS_FILE, "...Buf.size #inactive-socks Time/poll")); |
| 907 | PJ_LOG(3, (THIS_FILE, "... (bytes) (nanosec)")); |
| 908 | PJ_LOG(3, (THIS_FILE, "...=====================================")); |
| 909 | |
Benny Prijono | 63ab356 | 2006-07-08 19:46:43 +0000 | [diff] [blame] | 910 | //goto pass2; |
Benny Prijono | fe9c9b6 | 2006-07-06 20:43:07 +0000 | [diff] [blame] | 911 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 912 | for (bufsize=BUF_MIN_SIZE; bufsize <= BUF_MAX_SIZE; bufsize *= 2) { |
Benny Prijono | e3f79fd | 2008-02-13 15:17:28 +0000 | [diff] [blame] | 913 | if ((status=bench_test(allow_concur, bufsize, SOCK_INACTIVE_MIN)) != 0) |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 914 | return status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 915 | } |
Benny Prijono | 5fb99e5 | 2007-01-23 04:17:56 +0000 | [diff] [blame] | 916 | //pass2: |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 917 | bufsize = 512; |
| 918 | for (sock_count=SOCK_INACTIVE_MIN+2; |
| 919 | sock_count<=SOCK_INACTIVE_MAX+2; |
| 920 | sock_count *= 2) |
| 921 | { |
Benny Prijono | 63ab356 | 2006-07-08 19:46:43 +0000 | [diff] [blame] | 922 | //PJ_LOG(3,(THIS_FILE, "...testing with %d fds", sock_count)); |
Benny Prijono | e3f79fd | 2008-02-13 15:17:28 +0000 | [diff] [blame] | 923 | if ((status=bench_test(allow_concur, bufsize, sock_count-2)) != 0) |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 924 | return status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 925 | } |
| 926 | return 0; |
| 927 | } |
| 928 | |
Benny Prijono | e3f79fd | 2008-02-13 15:17:28 +0000 | [diff] [blame] | 929 | int udp_ioqueue_test() |
| 930 | { |
| 931 | int rc; |
| 932 | |
| 933 | rc = udp_ioqueue_test_imp(PJ_TRUE); |
| 934 | if (rc != 0) |
| 935 | return rc; |
| 936 | |
| 937 | rc = udp_ioqueue_test_imp(PJ_FALSE); |
| 938 | if (rc != 0) |
| 939 | return rc; |
| 940 | |
| 941 | return 0; |
| 942 | } |
| 943 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 944 | #else |
| 945 | /* To prevent warning about "translation unit is empty" |
| 946 | * when this test is disabled. |
| 947 | */ |
| 948 | int dummy_uiq_udp; |
| 949 | #endif /* INCLUDE_UDP_IOQUEUE_TEST */ |
| 950 | |
| 951 | |