Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/echo_clt.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "test.h"
20 #include <pjlib.h>
21
22 #if INCLUDE_ECHO_CLIENT
23
24 enum { BUF_SIZE = 512 };
25
26 struct client
27 {
28 int sock_type;
29 const char *server;
30 int port;
31 };
32
33 static pj_atomic_t *totalBytes;
34 static pj_atomic_t *timeout_counter;
35 static pj_atomic_t *invalid_counter;
36
37 #define MSEC_PRINT_DURATION 1000
38
39 static int wait_socket(pj_sock_t sock, unsigned msec_timeout)
40 {
41 pj_fd_set_t fdset;
42 pj_time_val timeout;
43
44 timeout.sec = 0;
45 timeout.msec = msec_timeout;
46 pj_time_val_normalize(&timeout);
47
48 PJ_FD_ZERO(&fdset);
49 PJ_FD_SET(sock, &fdset);
50
51 return pj_sock_select(FD_SETSIZE, &fdset, NULL, NULL, &timeout);
52 }
53
54 static int echo_client_thread(void *arg)
55 {
56 pj_sock_t sock;
57 char send_buf[BUF_SIZE];
58 char recv_buf[BUF_SIZE];
59 char addr[PJ_INET_ADDRSTRLEN];
60 pj_sockaddr_in addr;
61 pj_str_t s;
62 pj_status_t rc;
63 pj_uint32_t buffer_id;
64 pj_uint32_t buffer_counter;
65 struct client *client = arg;
66 pj_status_t last_recv_err = PJ_SUCCESS, last_send_err = PJ_SUCCESS;
67 unsigned counter = 0;
68
69 rc = app_socket(pj_AF_INET(), client->sock_type, 0, -1, &sock);
70 if (rc != PJ_SUCCESS) {
71 app_perror("...unable to create socket", rc);
72 return -10;
73 }
74
75 rc = pj_sockaddr_in_init( &addr, pj_cstr(&s, client->server),
76 (pj_uint16_t)client->port);
77 if (rc != PJ_SUCCESS) {
78 app_perror("...unable to resolve server", rc);
79 return -15;
80 }
81
82 rc = pj_sock_connect(sock, &addr, sizeof(addr));
83 if (rc != PJ_SUCCESS) {
84 app_perror("...connect() error", rc);
85 pj_sock_close(sock);
86 return -20;
87 }
88
89 PJ_LOG(3,("", "...socket connected to %s:%d",
90 pj_inet_ntop2(pj_AF_INET(), &addr.sin_addr,
91 addr, sizeof(addr)),
92 pj_ntohs(addr.sin_port)));
93
94 pj_memset(send_buf, 'A', BUF_SIZE);
95 send_buf[BUF_SIZE-1]='\0';
96
97 /* Give other thread chance to initialize themselves! */
98 pj_thread_sleep(200);
99
100 //PJ_LOG(3,("", "...thread %p running", pj_thread_this()));
101
102 buffer_id = (pj_uint32_t) pj_thread_this();
103 buffer_counter = 0;
104
105 *(pj_uint32_t*)send_buf = buffer_id;
106
107 for (;;) {
108 int rc;
109 pj_ssize_t bytes;
110
111 ++counter;
112
113 //while (wait_socket(sock,0) > 0)
114 // ;
115
116 /* Send a packet. */
117 bytes = BUF_SIZE;
118 *(pj_uint32_t*)(send_buf+4) = ++buffer_counter;
119 rc = pj_sock_send(sock, send_buf, &bytes, 0);
120 if (rc != PJ_SUCCESS || bytes != BUF_SIZE) {
121 if (rc != last_send_err) {
122 app_perror("...send() error", rc);
123 PJ_LOG(3,("", "...ignoring subsequent error.."));
124 last_send_err = rc;
125 pj_thread_sleep(100);
126 }
127 continue;
128 }
129
130 rc = wait_socket(sock, 500);
131 if (rc == 0) {
132 PJ_LOG(3,("", "...timeout"));
133 bytes = 0;
134 pj_atomic_inc(timeout_counter);
135 } else if (rc < 0) {
136 rc = pj_get_netos_error();
137 app_perror("...select() error", rc);
138 break;
139 } else {
140 /* Receive back the original packet. */
141 bytes = 0;
142 do {
143 pj_ssize_t received = BUF_SIZE - bytes;
144 rc = pj_sock_recv(sock, recv_buf+bytes, &received, 0);
145 if (rc != PJ_SUCCESS || received == 0) {
146 if (rc != last_recv_err) {
147 app_perror("...recv() error", rc);
148 PJ_LOG(3,("", "...ignoring subsequent error.."));
149 last_recv_err = rc;
150 pj_thread_sleep(100);
151 }
152 bytes = 0;
153 received = 0;
154 break;
155 }
156 bytes += received;
157 } while (bytes != BUF_SIZE && bytes != 0);
158 }
159
160 if (bytes == 0)
161 continue;
162
163 if (pj_memcmp(send_buf, recv_buf, BUF_SIZE) != 0) {
164 recv_buf[BUF_SIZE-1] = '\0';
165 PJ_LOG(3,("", "...error: buffer %u has changed!\n"
166 "send_buf=%s\n"
167 "recv_buf=%s\n",
168 counter, send_buf, recv_buf));
169 pj_atomic_inc(invalid_counter);
170 }
171
172 /* Accumulate total received. */
173 pj_atomic_add(totalBytes, bytes);
174 }
175
176 pj_sock_close(sock);
177 return 0;
178 }
179
180 int echo_client(int sock_type, const char *server, int port)
181 {
182 pj_pool_t *pool;
183 pj_thread_t *thread[ECHO_CLIENT_MAX_THREADS];
184 pj_status_t rc;
185 struct client client;
186 int i;
187 pj_atomic_value_t last_received;
188 pj_timestamp last_report;
189
190 client.sock_type = sock_type;
191 client.server = server;
192 client.port = port;
193
194 pool = pj_pool_create( mem, NULL, 4000, 4000, NULL );
195
196 rc = pj_atomic_create(pool, 0, &totalBytes);
197 if (rc != PJ_SUCCESS) {
198 PJ_LOG(3,("", "...error: unable to create atomic variable", rc));
199 return -30;
200 }
201 rc = pj_atomic_create(pool, 0, &invalid_counter);
202 rc = pj_atomic_create(pool, 0, &timeout_counter);
203
204 PJ_LOG(3,("", "Echo client started"));
205 PJ_LOG(3,("", " Destination: %s:%d",
206 ECHO_SERVER_ADDRESS, ECHO_SERVER_START_PORT));
207 PJ_LOG(3,("", " Press Ctrl-C to exit"));
208
209 for (i=0; i<ECHO_CLIENT_MAX_THREADS; ++i) {
210 rc = pj_thread_create( pool, NULL, &echo_client_thread, &client,
211 PJ_THREAD_DEFAULT_STACK_SIZE, 0,
212 &thread[i]);
213 if (rc != PJ_SUCCESS) {
214 app_perror("...error: unable to create thread", rc);
215 return -10;
216 }
217 }
218
219 last_received = 0;
220 pj_get_timestamp(&last_report);
221
222 for (;;) {
223 pj_timestamp now;
224 unsigned long received, cur_received;
225 unsigned msec;
226 pj_highprec_t bw;
227 pj_time_val elapsed;
228 unsigned bw32;
229 pj_uint32_t timeout, invalid;
230
231 pj_thread_sleep(1000);
232
233 pj_get_timestamp(&now);
234 elapsed = pj_elapsed_time(&last_report, &now);
235 msec = PJ_TIME_VAL_MSEC(elapsed);
236
237 received = pj_atomic_get(totalBytes);
238 cur_received = received - last_received;
239
240 bw = cur_received;
241 pj_highprec_mul(bw, 1000);
242 pj_highprec_div(bw, msec);
243
244 bw32 = (unsigned)bw;
245
246 last_report = now;
247 last_received = received;
248
249 timeout = pj_atomic_get(timeout_counter);
250 invalid = pj_atomic_get(invalid_counter);
251
252 PJ_LOG(3,("",
253 "...%d threads, total bandwidth: %d KB/s, "
254 "timeout=%d, invalid=%d",
255 ECHO_CLIENT_MAX_THREADS, bw32/1000,
256 timeout, invalid));
257 }
258
259 for (i=0; i<ECHO_CLIENT_MAX_THREADS; ++i) {
260 pj_thread_join( thread[i] );
261 }
262
263 pj_pool_release(pool);
264 return 0;
265 }
266
267
268 #else
269 int dummy_echo_client;
270 #endif /* INCLUDE_ECHO_CLIENT */