| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 |
|
| 6 |
#include "prio.h" |
| 7 |
#include "prprf.h" |
| 8 |
#include "prlog.h" |
| 9 |
#include "prnetdb.h" |
| 10 |
#include "prthread.h" |
| 11 |
|
| 12 |
#include "plerror.h" |
| 13 |
#include "plgetopt.h" |
| 14 |
#include "prwin.h" |
| 15 |
|
| 16 |
#include <stdlib.h> |
| 17 |
#include <string.h> |
| 18 |
|
| 19 |
/* |
| 20 |
** Testing layering of I/O |
| 21 |
** |
| 22 |
** The layered server |
| 23 |
** A thread that acts as a server. It creates a TCP listener with a dummy |
| 24 |
** layer pushed on top. Then listens for incoming connections. Each connection |
| 25 |
** request for connection will be layered as well, accept one request, echo |
| 26 |
** it back and close. |
| 27 |
** |
| 28 |
** The layered client |
| 29 |
** Pretty much what you'd expect. |
| 30 |
*/ |
| 31 |
|
| 32 |
static PRFileDesc* logFile; |
| 33 |
static PRDescIdentity identity; |
| 34 |
static PRNetAddr server_address; |
| 35 |
|
| 36 |
static PRIOMethods myMethods; |
| 37 |
|
| 38 |
typedef enum Verbosity { silent, quiet, chatty, noisy } Verbosity; |
| 39 |
|
| 40 |
static PRIntn minor_iterations = 5; |
| 41 |
static PRIntn major_iterations = 1; |
| 42 |
static Verbosity verbosity = quiet; |
| 43 |
|
| 44 |
#ifdef DEBUG |
| 45 |
# define PORT_INC_DO +100 |
| 46 |
#else |
| 47 |
# define PORT_INC_DO |
| 48 |
#endif |
| 49 |
#ifdef IS_64 |
| 50 |
# define PORT_INC_3264 +200 |
| 51 |
#else |
| 52 |
# define PORT_INC_3264 |
| 53 |
#endif |
| 54 |
|
| 55 |
static PRUint16 default_port = 12273 PORT_INC_DO PORT_INC_3264; |
| 56 |
|
| 57 |
static PRFileDesc* PushLayer(PRFileDesc* stack) { |
| 58 |
PRFileDesc* layer = PR_CreateIOLayerStub(identity, &myMethods); |
| 59 |
PRStatus rv = PR_PushIOLayer(stack, PR_GetLayersIdentity(stack), layer); |
| 60 |
if (verbosity > quiet) { |
| 61 |
PR_fprintf(logFile, "Pushed layer(0x%x) onto stack(0x%x)\n", layer, stack); |
| 62 |
} |
| 63 |
PR_ASSERT(PR_SUCCESS == rv); |
| 64 |
return stack; |
| 65 |
} /* PushLayer */ |
| 66 |
|
| 67 |
static PRFileDesc* PushNewLayers(PRFileDesc* stack) { |
| 68 |
PRDescIdentity tmp_identity; |
| 69 |
PRFileDesc* layer; |
| 70 |
PRStatus rv; |
| 71 |
|
| 72 |
/* push a dummy layer */ |
| 73 |
tmp_identity = PR_GetUniqueIdentity("Dummy 1"); |
| 74 |
layer = PR_CreateIOLayerStub(tmp_identity, PR_GetDefaultIOMethods()); |
| 75 |
rv = PR_PushIOLayer(stack, PR_GetLayersIdentity(stack), layer); |
| 76 |
if (verbosity > quiet) |
| 77 |
PR_fprintf(logFile, "Pushed layer(0x%x) onto stack(0x%x)\n", layer, stack); |
| 78 |
PR_ASSERT(PR_SUCCESS == rv); |
| 79 |
|
| 80 |
/* push a data processing layer */ |
| 81 |
layer = PR_CreateIOLayerStub(identity, &myMethods); |
| 82 |
rv = PR_PushIOLayer(stack, PR_GetLayersIdentity(stack), layer); |
| 83 |
if (verbosity > quiet) |
| 84 |
PR_fprintf(logFile, "Pushed layer(0x%x) onto stack(0x%x)\n", layer, stack); |
| 85 |
PR_ASSERT(PR_SUCCESS == rv); |
| 86 |
|
| 87 |
/* push another dummy layer */ |
| 88 |
tmp_identity = PR_GetUniqueIdentity("Dummy 2"); |
| 89 |
layer = PR_CreateIOLayerStub(tmp_identity, PR_GetDefaultIOMethods()); |
| 90 |
rv = PR_PushIOLayer(stack, PR_GetLayersIdentity(stack), layer); |
| 91 |
if (verbosity > quiet) |
| 92 |
PR_fprintf(logFile, "Pushed layer(0x%x) onto stack(0x%x)\n", layer, stack); |
| 93 |
PR_ASSERT(PR_SUCCESS == rv); |
| 94 |
return stack; |
| 95 |
} /* PushLayer */ |
| 96 |
|
| 97 |
#if 0 |
| 98 |
static PRFileDesc *PopLayer(PRFileDesc *stack) |
| 99 |
{ |
| 100 |
PRFileDesc *popped = PR_PopIOLayer(stack, identity); |
| 101 |
if (verbosity > quiet) { |
| 102 |
PR_fprintf(logFile, "Popped layer(0x%x) from stack(0x%x)\n", popped, stack); |
| 103 |
} |
| 104 |
popped->dtor(popped); |
| 105 |
|
| 106 |
return stack; |
| 107 |
} /* PopLayer */ |
| 108 |
#endif |
| 109 |
|
| 110 |
static void PR_CALLBACK Client(void* arg) { |
| 111 |
PRStatus rv; |
| 112 |
PRUint8 buffer[100]; |
| 113 |
PRIntn empty_flags = 0; |
| 114 |
PRIntn bytes_read, bytes_sent; |
| 115 |
PRFileDesc* stack = (PRFileDesc*)arg; |
| 116 |
|
| 117 |
/* Initialize the buffer so that Purify won't complain */ |
| 118 |
memset(buffer, 0, sizeof(buffer)); |
| 119 |
|
| 120 |
rv = PR_Connect(stack, &server_address, PR_INTERVAL_NO_TIMEOUT); |
| 121 |
PR_ASSERT(PR_SUCCESS == rv); |
| 122 |
while (minor_iterations-- > 0) { |
| 123 |
bytes_sent = PR_Send(stack, buffer, sizeof(buffer), empty_flags, |
| 124 |
PR_INTERVAL_NO_TIMEOUT); |
| 125 |
PR_ASSERT(sizeof(buffer) == bytes_sent); |
| 126 |
if (verbosity > chatty) { |
| 127 |
PR_fprintf(logFile, "Client sending %d bytes\n", bytes_sent); |
| 128 |
} |
| 129 |
bytes_read = |
| 130 |
PR_Recv(stack, buffer, bytes_sent, empty_flags, PR_INTERVAL_NO_TIMEOUT); |
| 131 |
if (verbosity > chatty) { |
| 132 |
PR_fprintf(logFile, "Client receiving %d bytes\n", bytes_read); |
| 133 |
} |
| 134 |
PR_ASSERT(bytes_read == bytes_sent); |
| 135 |
} |
| 136 |
|
| 137 |
if (verbosity > quiet) { |
| 138 |
PR_fprintf(logFile, "Client shutting down stack\n"); |
| 139 |
} |
| 140 |
|
| 141 |
rv = PR_Shutdown(stack, PR_SHUTDOWN_BOTH); |
| 142 |
PR_ASSERT(PR_SUCCESS == rv); |
| 143 |
} /* Client */ |
| 144 |
|
| 145 |
static void PR_CALLBACK Server(void* arg) { |
| 146 |
PRStatus rv; |
| 147 |
PRUint8 buffer[100]; |
| 148 |
PRFileDesc* service; |
| 149 |
PRUintn empty_flags = 0; |
| 150 |
PRIntn bytes_read, bytes_sent; |
| 151 |
PRFileDesc* stack = (PRFileDesc*)arg; |
| 152 |
PRNetAddr client_address; |
| 153 |
|
| 154 |
service = PR_Accept(stack, &client_address, PR_INTERVAL_NO_TIMEOUT); |
| 155 |
if (verbosity > quiet) { |
| 156 |
PR_fprintf(logFile, "Server accepting connection\n"); |
| 157 |
} |
| 158 |
|
| 159 |
do { |
| 160 |
bytes_read = PR_Recv(service, buffer, sizeof(buffer), empty_flags, |
| 161 |
PR_INTERVAL_NO_TIMEOUT); |
| 162 |
if (0 != bytes_read) { |
| 163 |
if (verbosity > chatty) { |
| 164 |
PR_fprintf(logFile, "Server receiving %d bytes\n", bytes_read); |
| 165 |
} |
| 166 |
PR_ASSERT(bytes_read > 0); |
| 167 |
bytes_sent = PR_Send(service, buffer, bytes_read, empty_flags, |
| 168 |
PR_INTERVAL_NO_TIMEOUT); |
| 169 |
if (verbosity > chatty) { |
| 170 |
PR_fprintf(logFile, "Server sending %d bytes\n", bytes_sent); |
| 171 |
} |
| 172 |
PR_ASSERT(bytes_read == bytes_sent); |
| 173 |
} |
| 174 |
|
| 175 |
} while (0 != bytes_read); |
| 176 |
|
| 177 |
if (verbosity > quiet) { |
| 178 |
PR_fprintf(logFile, "Server shutting down and closing stack\n"); |
| 179 |
} |
| 180 |
rv = PR_Shutdown(service, PR_SHUTDOWN_BOTH); |
| 181 |
PR_ASSERT(PR_SUCCESS == rv); |
| 182 |
rv = PR_Close(service); |
| 183 |
PR_ASSERT(PR_SUCCESS == rv); |
| 184 |
|
| 185 |
} /* Server */ |
| 186 |
|
| 187 |
static PRInt32 PR_CALLBACK MyRecv(PRFileDesc* fd, void* buf, PRInt32 amount, |
| 188 |
PRIntn flags, PRIntervalTime timeout) { |
| 189 |
char* b = (char*)buf; |
| 190 |
PRFileDesc* lo = fd->lower; |
| 191 |
PRInt32 rv, readin = 0, request = 0; |
| 192 |
rv = lo->methods->recv(lo, &request, sizeof(request), flags, timeout); |
| 193 |
if (verbosity > chatty) |
| 194 |
PR_fprintf(logFile, "MyRecv sending permission for %d bytes\n", request); |
| 195 |
if (0 < rv) { |
| 196 |
if (verbosity > chatty) |
| 197 |
PR_fprintf(logFile, "MyRecv received permission request for %d bytes\n", |
| 198 |
request); |
| 199 |
rv = lo->methods->send(lo, &request, sizeof(request), flags, timeout); |
| 200 |
if (0 < rv) { |
| 201 |
if (verbosity > chatty) |
| 202 |
PR_fprintf(logFile, "MyRecv sending permission for %d bytes\n", |
| 203 |
request); |
| 204 |
while (readin < request) { |
| 205 |
rv = lo->methods->recv(lo, b + readin, amount - readin, flags, timeout); |
| 206 |
if (rv <= 0) { |
| 207 |
break; |
| 208 |
} |
| 209 |
if (verbosity > chatty) |
| 210 |
PR_fprintf(logFile, "MyRecv received %d bytes\n", rv); |
| 211 |
readin += rv; |
| 212 |
} |
| 213 |
rv = readin; |
| 214 |
} |
| 215 |
} |
| 216 |
return rv; |
| 217 |
} /* MyRecv */ |
| 218 |
|
| 219 |
static PRInt32 PR_CALLBACK MySend(PRFileDesc* fd, const void* buf, |
| 220 |
PRInt32 amount, PRIntn flags, |
| 221 |
PRIntervalTime timeout) { |
| 222 |
PRFileDesc* lo = fd->lower; |
| 223 |
const char* b = (const char*)buf; |
| 224 |
PRInt32 rv, wroteout = 0, request; |
| 225 |
if (verbosity > chatty) |
| 226 |
PR_fprintf(logFile, "MySend asking permission to send %d bytes\n", amount); |
| 227 |
rv = lo->methods->send(lo, &amount, sizeof(amount), flags, timeout); |
| 228 |
if (0 < rv) { |
| 229 |
rv = lo->methods->recv(lo, &request, sizeof(request), flags, timeout); |
| 230 |
if (0 < rv) { |
| 231 |
PR_ASSERT(request == amount); |
| 232 |
if (verbosity > chatty) |
| 233 |
PR_fprintf(logFile, "MySend got permission to send %d bytes\n", |
| 234 |
request); |
| 235 |
while (wroteout < request) { |
| 236 |
rv = lo->methods->send(lo, b + wroteout, request - wroteout, flags, |
| 237 |
timeout); |
| 238 |
if (rv <= 0) { |
| 239 |
break; |
| 240 |
} |
| 241 |
if (verbosity > chatty) |
| 242 |
PR_fprintf(logFile, "MySend wrote %d bytes\n", rv); |
| 243 |
wroteout += rv; |
| 244 |
} |
| 245 |
rv = amount; |
| 246 |
} |
| 247 |
} |
| 248 |
return rv; |
| 249 |
} /* MySend */ |
| 250 |
|
| 251 |
static Verbosity ChangeVerbosity(Verbosity verbosity, PRIntn delta) { |
| 252 |
PRIntn verbage = (PRIntn)verbosity + delta; |
| 253 |
if (verbage < (PRIntn)silent) { |
| 254 |
verbage = (PRIntn)silent; |
| 255 |
} else if (verbage > (PRIntn)noisy) { |
| 256 |
verbage = (PRIntn)noisy; |
| 257 |
} |
| 258 |
return (Verbosity)verbage; |
| 259 |
} /* ChangeVerbosity */ |
| 260 |
|
| 261 |
int main(int argc, char** argv) { |
| 262 |
PRStatus rv; |
| 263 |
PRIntn mits; |
| 264 |
PLOptStatus os; |
| 265 |
PRFileDesc *client, *service; |
| 266 |
PRFileDesc *client_stack, *service_stack; |
| 267 |
PRNetAddr any_address; |
| 268 |
const char* server_name = NULL; |
| 269 |
const PRIOMethods* stubMethods; |
| 270 |
PRThread *client_thread, *server_thread; |
| 271 |
PRThreadScope thread_scope = PR_LOCAL_THREAD; |
| 272 |
PLOptState* opt = PL_CreateOptState(argc, argv, "dqGC:c:p:"); |
| 273 |
while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { |
| 274 |
if (PL_OPT_BAD == os) { |
| 275 |
continue; |
| 276 |
} |
| 277 |
switch (opt->option) { |
| 278 |
case 0: |
| 279 |
server_name = opt->value; |
| 280 |
break; |
| 281 |
case 'd': /* debug mode */ |
| 282 |
if (verbosity < noisy) { |
| 283 |
verbosity = ChangeVerbosity(verbosity, 1); |
| 284 |
} |
| 285 |
break; |
| 286 |
case 'q': /* debug mode */ |
| 287 |
if (verbosity > silent) { |
| 288 |
verbosity = ChangeVerbosity(verbosity, -1); |
| 289 |
} |
| 290 |
break; |
| 291 |
case 'G': /* use global threads */ |
| 292 |
thread_scope = PR_GLOBAL_THREAD; |
| 293 |
break; |
| 294 |
case 'C': /* number of threads waiting */ |
| 295 |
major_iterations = atoi(opt->value); |
| 296 |
break; |
| 297 |
case 'c': /* number of client threads */ |
| 298 |
minor_iterations = atoi(opt->value); |
| 299 |
break; |
| 300 |
case 'p': /* default port */ |
| 301 |
default_port = atoi(opt->value); |
| 302 |
break; |
| 303 |
default: |
| 304 |
break; |
| 305 |
} |
| 306 |
} |
| 307 |
PL_DestroyOptState(opt); |
| 308 |
PR_STDIO_INIT(); |
| 309 |
|
| 310 |
logFile = PR_GetSpecialFD(PR_StandardError); |
| 311 |
|
| 312 |
identity = PR_GetUniqueIdentity("Dummy"); |
| 313 |
stubMethods = PR_GetDefaultIOMethods(); |
| 314 |
|
| 315 |
/* |
| 316 |
** The protocol we're going to implement is one where in order to initiate |
| 317 |
** a send, the sender must first solicit permission. Therefore, every |
| 318 |
** send is really a send - receive - send sequence. |
| 319 |
*/ |
| 320 |
myMethods = *stubMethods; /* first get the entire batch */ |
| 321 |
myMethods.recv = MyRecv; /* then override the ones we care about */ |
| 322 |
myMethods.send = MySend; /* then override the ones we care about */ |
| 323 |
|
| 324 |
if (NULL == server_name) |
| 325 |
rv = PR_InitializeNetAddr(PR_IpAddrLoopback, default_port, &server_address); |
| 326 |
else { |
| 327 |
rv = PR_StringToNetAddr(server_name, &server_address); |
| 328 |
PR_ASSERT(PR_SUCCESS == rv); |
| 329 |
rv = PR_InitializeNetAddr(PR_IpAddrNull, default_port, &server_address); |
| 330 |
} |
| 331 |
PR_ASSERT(PR_SUCCESS == rv); |
| 332 |
|
| 333 |
/* one type w/o layering */ |
| 334 |
|
| 335 |
mits = minor_iterations; |
| 336 |
while (major_iterations-- > 0) { |
| 337 |
if (verbosity > silent) { |
| 338 |
PR_fprintf(logFile, "Beginning non-layered test\n"); |
| 339 |
} |
| 340 |
client = PR_NewTCPSocket(); |
| 341 |
PR_ASSERT(NULL != client); |
| 342 |
service = PR_NewTCPSocket(); |
| 343 |
PR_ASSERT(NULL != service); |
| 344 |
rv = PR_InitializeNetAddr(PR_IpAddrAny, default_port, &any_address); |
| 345 |
PR_ASSERT(PR_SUCCESS == rv); |
| 346 |
rv = PR_Bind(service, &any_address); |
| 347 |
PR_ASSERT(PR_SUCCESS == rv); |
| 348 |
rv = PR_Listen(service, 10); |
| 349 |
PR_ASSERT(PR_SUCCESS == rv); |
| 350 |
|
| 351 |
minor_iterations = mits; |
| 352 |
server_thread = |
| 353 |
PR_CreateThread(PR_USER_THREAD, Server, service, PR_PRIORITY_HIGH, |
| 354 |
thread_scope, PR_JOINABLE_THREAD, 16 * 1024); |
| 355 |
PR_ASSERT(NULL != server_thread); |
| 356 |
|
| 357 |
client_thread = |
| 358 |
PR_CreateThread(PR_USER_THREAD, Client, client, PR_PRIORITY_NORMAL, |
| 359 |
thread_scope, PR_JOINABLE_THREAD, 16 * 1024); |
| 360 |
PR_ASSERT(NULL != client_thread); |
| 361 |
|
| 362 |
rv = PR_JoinThread(client_thread); |
| 363 |
PR_ASSERT(PR_SUCCESS == rv); |
| 364 |
rv = PR_JoinThread(server_thread); |
| 365 |
PR_ASSERT(PR_SUCCESS == rv); |
| 366 |
|
| 367 |
rv = PR_Close(client); |
| 368 |
PR_ASSERT(PR_SUCCESS == rv); |
| 369 |
rv = PR_Close(service); |
| 370 |
PR_ASSERT(PR_SUCCESS == rv); |
| 371 |
if (verbosity > silent) { |
| 372 |
PR_fprintf(logFile, "Ending non-layered test\n"); |
| 373 |
} |
| 374 |
|
| 375 |
/* with layering */ |
| 376 |
if (verbosity > silent) { |
| 377 |
PR_fprintf(logFile, "Beginning layered test\n"); |
| 378 |
} |
| 379 |
client = PR_NewTCPSocket(); |
| 380 |
PR_ASSERT(NULL != client); |
| 381 |
PushLayer(client); |
| 382 |
service = PR_NewTCPSocket(); |
| 383 |
PR_ASSERT(NULL != service); |
| 384 |
PushLayer(service); |
| 385 |
rv = PR_InitializeNetAddr(PR_IpAddrAny, default_port, &any_address); |
| 386 |
PR_ASSERT(PR_SUCCESS == rv); |
| 387 |
rv = PR_Bind(service, &any_address); |
| 388 |
PR_ASSERT(PR_SUCCESS == rv); |
| 389 |
rv = PR_Listen(service, 10); |
| 390 |
PR_ASSERT(PR_SUCCESS == rv); |
| 391 |
|
| 392 |
minor_iterations = mits; |
| 393 |
server_thread = |
| 394 |
PR_CreateThread(PR_USER_THREAD, Server, service, PR_PRIORITY_HIGH, |
| 395 |
thread_scope, PR_JOINABLE_THREAD, 16 * 1024); |
| 396 |
PR_ASSERT(NULL != server_thread); |
| 397 |
|
| 398 |
client_thread = |
| 399 |
PR_CreateThread(PR_USER_THREAD, Client, client, PR_PRIORITY_NORMAL, |
| 400 |
thread_scope, PR_JOINABLE_THREAD, 16 * 1024); |
| 401 |
PR_ASSERT(NULL != client_thread); |
| 402 |
|
| 403 |
rv = PR_JoinThread(client_thread); |
| 404 |
PR_ASSERT(PR_SUCCESS == rv); |
| 405 |
rv = PR_JoinThread(server_thread); |
| 406 |
PR_ASSERT(PR_SUCCESS == rv); |
| 407 |
|
| 408 |
rv = PR_Close(client); |
| 409 |
PR_ASSERT(PR_SUCCESS == rv); |
| 410 |
rv = PR_Close(service); |
| 411 |
PR_ASSERT(PR_SUCCESS == rv); |
| 412 |
/* with layering, using new style stack */ |
| 413 |
if (verbosity > silent) |
| 414 |
PR_fprintf(logFile, "Beginning layered test with new style stack\n"); |
| 415 |
client = PR_NewTCPSocket(); |
| 416 |
PR_ASSERT(NULL != client); |
| 417 |
client_stack = PR_CreateIOLayer(client); |
| 418 |
PushNewLayers(client_stack); |
| 419 |
service = PR_NewTCPSocket(); |
| 420 |
PR_ASSERT(NULL != service); |
| 421 |
service_stack = PR_CreateIOLayer(service); |
| 422 |
PushNewLayers(service_stack); |
| 423 |
rv = PR_InitializeNetAddr(PR_IpAddrAny, default_port, &any_address); |
| 424 |
PR_ASSERT(PR_SUCCESS == rv); |
| 425 |
rv = PR_Bind(service, &any_address); |
| 426 |
PR_ASSERT(PR_SUCCESS == rv); |
| 427 |
rv = PR_Listen(service, 10); |
| 428 |
PR_ASSERT(PR_SUCCESS == rv); |
| 429 |
|
| 430 |
minor_iterations = mits; |
| 431 |
server_thread = |
| 432 |
PR_CreateThread(PR_USER_THREAD, Server, service_stack, PR_PRIORITY_HIGH, |
| 433 |
thread_scope, PR_JOINABLE_THREAD, 16 * 1024); |
| 434 |
PR_ASSERT(NULL != server_thread); |
| 435 |
|
| 436 |
client_thread = PR_CreateThread(PR_USER_THREAD, Client, client_stack, |
| 437 |
PR_PRIORITY_NORMAL, thread_scope, |
| 438 |
PR_JOINABLE_THREAD, 16 * 1024); |
| 439 |
PR_ASSERT(NULL != client_thread); |
| 440 |
|
| 441 |
rv = PR_JoinThread(client_thread); |
| 442 |
PR_ASSERT(PR_SUCCESS == rv); |
| 443 |
rv = PR_JoinThread(server_thread); |
| 444 |
PR_ASSERT(PR_SUCCESS == rv); |
| 445 |
|
| 446 |
rv = PR_Close(client_stack); |
| 447 |
PR_ASSERT(PR_SUCCESS == rv); |
| 448 |
rv = PR_Close(service_stack); |
| 449 |
PR_ASSERT(PR_SUCCESS == rv); |
| 450 |
if (verbosity > silent) { |
| 451 |
PR_fprintf(logFile, "Ending layered test\n"); |
| 452 |
} |
| 453 |
} |
| 454 |
return 0; |
| 455 |
} /* main */ |
| 456 |
|
| 457 |
/* layer.c */ |