| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "test.h" |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#if INCLUDE_SELECT_TEST |
| 35 |
|
| 36 |
#include <pj/sock.h> |
| 37 |
#include <pj/sock_select.h> |
| 38 |
#include <pj/log.h> |
| 39 |
#include <pj/string.h> |
| 40 |
#include <pj/assert.h> |
| 41 |
#include <pj/os.h> |
| 42 |
#include <pj/errno.h> |
| 43 |
|
| 44 |
enum |
| 45 |
{ |
| 46 |
READ_FDS, |
| 47 |
WRITE_FDS, |
| 48 |
EXCEPT_FDS |
| 49 |
}; |
| 50 |
|
| 51 |
#define THIS_FILE "select_test" |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
static int do_select( pj_sock_t sock1, pj_sock_t sock2, |
| 60 |
int setcount[]) |
| 61 |
{ |
| 62 |
pj_fd_set_t fds[3]; |
| 63 |
pj_time_val timeout; |
| 64 |
int i, n, nfds; |
| 65 |
|
| 66 |
for (i=0; i<3; ++i) { |
| 67 |
PJ_FD_ZERO(&fds[i]); |
| 68 |
PJ_FD_SET(sock1, &fds[i]); |
| 69 |
PJ_FD_SET(sock2, &fds[i]); |
| 70 |
setcount[i] = 0; |
| 71 |
} |
| 72 |
|
| 73 |
timeout.sec = 5; |
| 74 |
timeout.msec = 0; |
| 75 |
|
| 76 |
nfds = (int)(sock1 > sock2 ? sock1 : sock2) + 1; |
| 77 |
n = pj_sock_select(nfds, &fds[0], &fds[1], &fds[2], |
| 78 |
&timeout); |
| 79 |
if (n < 0) |
| 80 |
return n; |
| 81 |
if (n == 0) |
| 82 |
return 0; |
| 83 |
|
| 84 |
for (i=0; i<3; ++i) { |
| 85 |
if (PJ_FD_ISSET(sock1, &fds[i])) |
| 86 |
setcount[i]++; |
| 87 |
if (PJ_FD_ISSET(sock2, &fds[i])) |
| 88 |
setcount[i]++; |
| 89 |
} |
| 90 |
|
| 91 |
return n; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
int select_test() |
| 100 |
{ |
| 101 |
pj_sock_t udp1=PJ_INVALID_SOCKET, udp2=PJ_INVALID_SOCKET; |
| 102 |
pj_sockaddr_in udp_addr; |
| 103 |
int status; |
| 104 |
int setcount[3]; |
| 105 |
pj_str_t s; |
| 106 |
const char data[] = "hello"; |
| 107 |
const int datalen = 5; |
| 108 |
pj_ssize_t sent, received; |
| 109 |
char buf[10]; |
| 110 |
pj_status_t rc; |
| 111 |
int addrlen; |
| 112 |
|
| 113 |
PJ_LOG(3, (THIS_FILE, "...Testing simple UDP select()")); |
| 114 |
|
| 115 |
|
| 116 |
rc = pj_sock_socket( pj_AF_INET(), pj_SOCK_DGRAM(), 0, &udp1); |
| 117 |
if (rc != PJ_SUCCESS) { |
| 118 |
app_perror("...error: unable to create socket", rc); |
| 119 |
status=-10; goto on_return; |
| 120 |
} |
| 121 |
rc = pj_sock_socket( pj_AF_INET(), pj_SOCK_DGRAM(), 0, &udp2); |
| 122 |
if (rc != PJ_SUCCESS) { |
| 123 |
app_perror("...error: unable to create socket", rc); |
| 124 |
status=-20; goto on_return; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
pj_bzero(&udp_addr, sizeof(udp_addr)); |
| 129 |
udp_addr.sin_family = pj_AF_INET(); |
| 130 |
udp_addr.sin_port = 0; |
| 131 |
udp_addr.sin_addr = pj_inet_addr(pj_cstr(&s, "127.0.0.1")); |
| 132 |
|
| 133 |
rc = pj_sock_bind(udp2, &udp_addr, sizeof(udp_addr)); |
| 134 |
if (rc != PJ_SUCCESS) { |
| 135 |
app_perror("...error: unable to bind socket", rc); |
| 136 |
status=-30; goto on_return; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
addrlen = sizeof(udp_addr); |
| 141 |
rc = pj_sock_getsockname(udp2, &udp_addr, &addrlen); |
| 142 |
if (rc != PJ_SUCCESS) { |
| 143 |
app_perror("...error: unable to get socket name", rc); |
| 144 |
status=-35; goto on_return; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
sent = datalen; |
| 149 |
rc = pj_sock_sendto(udp1, data, &sent, 0, &udp_addr, sizeof(udp_addr)); |
| 150 |
if (rc != PJ_SUCCESS || sent != datalen) { |
| 151 |
app_perror("...error: sendto() error", rc); |
| 152 |
status=-40; goto on_return; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
pj_thread_sleep(10); |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
status = do_select(udp1, udp2, setcount); |
| 164 |
if (status < 0) { |
| 165 |
char errbuf[128]; |
| 166 |
pj_strerror(pj_get_netos_error(), errbuf, sizeof(errbuf)); |
| 167 |
PJ_LOG(1,(THIS_FILE, "...error: %s", errbuf)); |
| 168 |
status=-50; goto on_return; |
| 169 |
} |
| 170 |
if (status == 0) { |
| 171 |
status=-60; goto on_return; |
| 172 |
} |
| 173 |
|
| 174 |
if (setcount[READ_FDS] != 1) { |
| 175 |
status=-70; goto on_return; |
| 176 |
} |
| 177 |
if (setcount[WRITE_FDS] != 0) { |
| 178 |
if (setcount[WRITE_FDS] == 2) { |
| 179 |
PJ_LOG(3,(THIS_FILE, "...info: system reports writable sockets")); |
| 180 |
} else { |
| 181 |
status=-80; goto on_return; |
| 182 |
} |
| 183 |
} else { |
| 184 |
PJ_LOG(3,(THIS_FILE, |
| 185 |
"...info: system doesn't report writable sockets")); |
| 186 |
} |
| 187 |
if (setcount[EXCEPT_FDS] != 0) { |
| 188 |
status=-90; goto on_return; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
received = sizeof(buf); |
| 193 |
rc = pj_sock_recv(udp2, buf, &received, 0); |
| 194 |
if (rc != PJ_SUCCESS || received != 5) { |
| 195 |
status=-100; goto on_return; |
| 196 |
} |
| 197 |
|
| 198 |
status = 0; |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
setcount[0] = setcount[1] = setcount[2] = 0; |
| 205 |
status = do_select(udp1, udp2, setcount); |
| 206 |
if (status != 0 && status != setcount[WRITE_FDS]) { |
| 207 |
PJ_LOG(3,(THIS_FILE, "...error: expecting timeout but got %d sks set", |
| 208 |
status)); |
| 209 |
PJ_LOG(3,(THIS_FILE, " rdset: %d, wrset: %d, exset: %d", |
| 210 |
setcount[0], setcount[1], setcount[2])); |
| 211 |
status = -110; goto on_return; |
| 212 |
} |
| 213 |
if (setcount[READ_FDS] != 0) { |
| 214 |
PJ_LOG(3,(THIS_FILE, "...error: readable socket not expected")); |
| 215 |
status = -120; goto on_return; |
| 216 |
} |
| 217 |
|
| 218 |
status = 0; |
| 219 |
|
| 220 |
on_return: |
| 221 |
if (udp1 != PJ_INVALID_SOCKET) |
| 222 |
pj_sock_close(udp1); |
| 223 |
if (udp2 != PJ_INVALID_SOCKET) |
| 224 |
pj_sock_close(udp2); |
| 225 |
return status; |
| 226 |
} |
| 227 |
|
| 228 |
#else |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
int dummy_select_test; |
| 233 |
#endif |
| 234 |
|
| 235 |
|