| 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 "prmem.h" |
| 8 |
#include "prprf.h" |
| 9 |
#include "prlog.h" |
| 10 |
#include "prerror.h" |
| 11 |
#include "prnetdb.h" |
| 12 |
#include "prthread.h" |
| 13 |
|
| 14 |
#include "plerror.h" |
| 15 |
#include "plgetopt.h" |
| 16 |
#include "prwin.h" |
| 17 |
|
| 18 |
#include <stdlib.h> |
| 19 |
#include <string.h> |
| 20 |
|
| 21 |
/* |
| 22 |
** Testing layering of I/O |
| 23 |
** |
| 24 |
** The layered server |
| 25 |
** A thread that acts as a server. It creates a TCP listener with a dummy |
| 26 |
** layer pushed on top. Then listens for incoming connections. Each connection |
| 27 |
** request for connection will be layered as well, accept one request, echo |
| 28 |
** it back and close. |
| 29 |
** |
| 30 |
** The layered client |
| 31 |
** Pretty much what you'd expect. |
| 32 |
*/ |
| 33 |
|
| 34 |
static PRFileDesc* logFile; |
| 35 |
static PRDescIdentity identity; |
| 36 |
static PRNetAddr server_address; |
| 37 |
|
| 38 |
static PRIOMethods myMethods; |
| 39 |
|
| 40 |
typedef enum { rcv_get_debit, rcv_send_credit, rcv_data } RcvState; |
| 41 |
typedef enum { xmt_send_debit, xmt_recv_credit, xmt_data } XmtState; |
| 42 |
|
| 43 |
struct PRFilePrivate { |
| 44 |
RcvState rcvstate; |
| 45 |
XmtState xmtstate; |
| 46 |
PRInt32 rcvreq, rcvinprogress; |
| 47 |
PRInt32 xmtreq, xmtinprogress; |
| 48 |
}; |
| 49 |
|
| 50 |
typedef enum Verbosity { silent, quiet, chatty, noisy } Verbosity; |
| 51 |
|
| 52 |
static PRIntn minor_iterations = 5; |
| 53 |
static PRIntn major_iterations = 1; |
| 54 |
static Verbosity verbosity = quiet; |
| 55 |
|
| 56 |
#ifdef DEBUG |
| 57 |
# define PORT_INC_DO +100 |
| 58 |
#else |
| 59 |
# define PORT_INC_DO |
| 60 |
#endif |
| 61 |
#ifdef IS_64 |
| 62 |
# define PORT_INC_3264 +200 |
| 63 |
#else |
| 64 |
# define PORT_INC_3264 |
| 65 |
#endif |
| 66 |
|
| 67 |
static PRUint16 default_port = 12273 PORT_INC_DO PORT_INC_3264; |
| 68 |
|
| 69 |
static PRFileDesc* PushLayer(PRFileDesc* stack) { |
| 70 |
PRStatus rv; |
| 71 |
PRFileDesc* layer = PR_CreateIOLayerStub(identity, &myMethods); |
| 72 |
layer->secret = PR_NEWZAP(PRFilePrivate); |
| 73 |
rv = PR_PushIOLayer(stack, PR_GetLayersIdentity(stack), layer); |
| 74 |
PR_ASSERT(PR_SUCCESS == rv); |
| 75 |
if (verbosity > quiet) { |
| 76 |
PR_fprintf(logFile, "Pushed layer(0x%x) onto stack(0x%x)\n", layer, stack); |
| 77 |
} |
| 78 |
return stack; |
| 79 |
} /* PushLayer */ |
| 80 |
|
| 81 |
static PRFileDesc* PopLayer(PRFileDesc* stack) { |
| 82 |
PRFileDesc* popped = PR_PopIOLayer(stack, identity); |
| 83 |
if (verbosity > quiet) { |
| 84 |
PR_fprintf(logFile, "Popped layer(0x%x) from stack(0x%x)\n", popped, stack); |
| 85 |
} |
| 86 |
PR_DELETE(popped->secret); |
| 87 |
popped->dtor(popped); |
| 88 |
return stack; |
| 89 |
} /* PopLayer */ |
| 90 |
|
| 91 |
static void PR_CALLBACK Client(void* arg) { |
| 92 |
PRStatus rv; |
| 93 |
PRIntn mits; |
| 94 |
PRInt32 ready; |
| 95 |
PRUint8 buffer[100]; |
| 96 |
PRPollDesc polldesc; |
| 97 |
PRIntn empty_flags = 0; |
| 98 |
PRIntn bytes_read, bytes_sent; |
| 99 |
PRFileDesc* stack = (PRFileDesc*)arg; |
| 100 |
|
| 101 |
/* Initialize the buffer so that Purify won't complain */ |
| 102 |
memset(buffer, 0, sizeof(buffer)); |
| 103 |
|
| 104 |
rv = PR_Connect(stack, &server_address, PR_INTERVAL_NO_TIMEOUT); |
| 105 |
if ((PR_FAILURE == rv) && (PR_IN_PROGRESS_ERROR == PR_GetError())) { |
| 106 |
if (verbosity > quiet) { |
| 107 |
PR_fprintf(logFile, "Client connect 'in progress'\n"); |
| 108 |
} |
| 109 |
do { |
| 110 |
polldesc.fd = stack; |
| 111 |
polldesc.out_flags = 0; |
| 112 |
polldesc.in_flags = PR_POLL_WRITE | PR_POLL_EXCEPT; |
| 113 |
ready = PR_Poll(&polldesc, 1, PR_INTERVAL_NO_TIMEOUT); |
| 114 |
if ((1 != ready) /* if not 1, then we're dead */ |
| 115 |
|| (0 == (polldesc.in_flags & polldesc.out_flags))) { |
| 116 |
PR_NOT_REACHED("Whoa!"); |
| 117 |
break; |
| 118 |
} |
| 119 |
if (verbosity > quiet) |
| 120 |
PR_fprintf(logFile, "Client connect 'in progress' [0x%x]\n", |
| 121 |
polldesc.out_flags); |
| 122 |
rv = PR_GetConnectStatus(&polldesc); |
| 123 |
if ((PR_FAILURE == rv) && (PR_IN_PROGRESS_ERROR != PR_GetError())) { |
| 124 |
break; |
| 125 |
} |
| 126 |
} while (PR_FAILURE == rv); |
| 127 |
} |
| 128 |
PR_ASSERT(PR_SUCCESS == rv); |
| 129 |
if (verbosity > chatty) { |
| 130 |
PR_fprintf(logFile, "Client created connection\n"); |
| 131 |
} |
| 132 |
|
| 133 |
for (mits = 0; mits < minor_iterations; ++mits) { |
| 134 |
bytes_sent = 0; |
| 135 |
if (verbosity > quiet) { |
| 136 |
PR_fprintf(logFile, "Client sending %d bytes\n", sizeof(buffer)); |
| 137 |
} |
| 138 |
do { |
| 139 |
if (verbosity > chatty) |
| 140 |
PR_fprintf(logFile, "Client sending %d bytes\n", |
| 141 |
sizeof(buffer) - bytes_sent); |
| 142 |
ready = PR_Send(stack, buffer + bytes_sent, sizeof(buffer) - bytes_sent, |
| 143 |
empty_flags, PR_INTERVAL_NO_TIMEOUT); |
| 144 |
if (verbosity > chatty) { |
| 145 |
PR_fprintf(logFile, "Client send status [%d]\n", ready); |
| 146 |
} |
| 147 |
if (0 < ready) { |
| 148 |
bytes_sent += ready; |
| 149 |
} else if ((-1 == ready) && (PR_WOULD_BLOCK_ERROR == PR_GetError())) { |
| 150 |
polldesc.fd = stack; |
| 151 |
polldesc.out_flags = 0; |
| 152 |
polldesc.in_flags = PR_POLL_WRITE; |
| 153 |
ready = PR_Poll(&polldesc, 1, PR_INTERVAL_NO_TIMEOUT); |
| 154 |
if ((1 != ready) /* if not 1, then we're dead */ |
| 155 |
|| (0 == (polldesc.in_flags & polldesc.out_flags))) { |
| 156 |
PR_NOT_REACHED("Whoa!"); |
| 157 |
break; |
| 158 |
} |
| 159 |
} else { |
| 160 |
break; |
| 161 |
} |
| 162 |
} while (bytes_sent < sizeof(buffer)); |
| 163 |
PR_ASSERT(sizeof(buffer) == bytes_sent); |
| 164 |
|
| 165 |
bytes_read = 0; |
| 166 |
do { |
| 167 |
if (verbosity > chatty) |
| 168 |
PR_fprintf(logFile, "Client receiving %d bytes\n", |
| 169 |
bytes_sent - bytes_read); |
| 170 |
ready = PR_Recv(stack, buffer + bytes_read, bytes_sent - bytes_read, |
| 171 |
empty_flags, PR_INTERVAL_NO_TIMEOUT); |
| 172 |
if (verbosity > chatty) |
| 173 |
PR_fprintf(logFile, "Client receive status [%d]\n", ready); |
| 174 |
if (0 < ready) { |
| 175 |
bytes_read += ready; |
| 176 |
} else if ((-1 == ready) && (PR_WOULD_BLOCK_ERROR == PR_GetError())) { |
| 177 |
polldesc.fd = stack; |
| 178 |
polldesc.out_flags = 0; |
| 179 |
polldesc.in_flags = PR_POLL_READ; |
| 180 |
ready = PR_Poll(&polldesc, 1, PR_INTERVAL_NO_TIMEOUT); |
| 181 |
if ((1 != ready) /* if not 1, then we're dead */ |
| 182 |
|| (0 == (polldesc.in_flags & polldesc.out_flags))) { |
| 183 |
PR_NOT_REACHED("Whoa!"); |
| 184 |
break; |
| 185 |
} |
| 186 |
} else { |
| 187 |
break; |
| 188 |
} |
| 189 |
} while (bytes_read < bytes_sent); |
| 190 |
if (verbosity > chatty) { |
| 191 |
PR_fprintf(logFile, "Client received %d bytes\n", bytes_read); |
| 192 |
} |
| 193 |
PR_ASSERT(bytes_read == bytes_sent); |
| 194 |
} |
| 195 |
|
| 196 |
if (verbosity > quiet) { |
| 197 |
PR_fprintf(logFile, "Client shutting down stack\n"); |
| 198 |
} |
| 199 |
|
| 200 |
rv = PR_Shutdown(stack, PR_SHUTDOWN_BOTH); |
| 201 |
PR_ASSERT(PR_SUCCESS == rv); |
| 202 |
} /* Client */ |
| 203 |
|
| 204 |
static void PR_CALLBACK Server(void* arg) { |
| 205 |
PRStatus rv; |
| 206 |
PRInt32 ready; |
| 207 |
PRUint8 buffer[100]; |
| 208 |
PRFileDesc* service; |
| 209 |
PRUintn empty_flags = 0; |
| 210 |
struct PRPollDesc polldesc; |
| 211 |
PRIntn bytes_read, bytes_sent; |
| 212 |
PRFileDesc* stack = (PRFileDesc*)arg; |
| 213 |
PRNetAddr client_address; |
| 214 |
|
| 215 |
do { |
| 216 |
if (verbosity > chatty) { |
| 217 |
PR_fprintf(logFile, "Server accepting connection\n"); |
| 218 |
} |
| 219 |
service = PR_Accept(stack, &client_address, PR_INTERVAL_NO_TIMEOUT); |
| 220 |
if (verbosity > chatty) { |
| 221 |
PR_fprintf(logFile, "Server accept status [0x%p]\n", service); |
| 222 |
} |
| 223 |
if ((NULL == service) && (PR_WOULD_BLOCK_ERROR == PR_GetError())) { |
| 224 |
polldesc.fd = stack; |
| 225 |
polldesc.out_flags = 0; |
| 226 |
polldesc.in_flags = PR_POLL_READ | PR_POLL_EXCEPT; |
| 227 |
ready = PR_Poll(&polldesc, 1, PR_INTERVAL_NO_TIMEOUT); |
| 228 |
if ((1 != ready) /* if not 1, then we're dead */ |
| 229 |
|| (0 == (polldesc.in_flags & polldesc.out_flags))) { |
| 230 |
PR_NOT_REACHED("Whoa!"); |
| 231 |
break; |
| 232 |
} |
| 233 |
} |
| 234 |
} while (NULL == service); |
| 235 |
PR_ASSERT(NULL != service); |
| 236 |
|
| 237 |
if (verbosity > quiet) { |
| 238 |
PR_fprintf(logFile, "Server accepting connection\n"); |
| 239 |
} |
| 240 |
|
| 241 |
do { |
| 242 |
bytes_read = 0; |
| 243 |
do { |
| 244 |
if (verbosity > chatty) |
| 245 |
PR_fprintf(logFile, "Server receiving %d bytes\n", |
| 246 |
sizeof(buffer) - bytes_read); |
| 247 |
ready = PR_Recv(service, buffer + bytes_read, sizeof(buffer) - bytes_read, |
| 248 |
empty_flags, PR_INTERVAL_NO_TIMEOUT); |
| 249 |
if (verbosity > chatty) { |
| 250 |
PR_fprintf(logFile, "Server receive status [%d]\n", ready); |
| 251 |
} |
| 252 |
if (0 < ready) { |
| 253 |
bytes_read += ready; |
| 254 |
} else if ((-1 == ready) && (PR_WOULD_BLOCK_ERROR == PR_GetError())) { |
| 255 |
polldesc.fd = service; |
| 256 |
polldesc.out_flags = 0; |
| 257 |
polldesc.in_flags = PR_POLL_READ; |
| 258 |
ready = PR_Poll(&polldesc, 1, PR_INTERVAL_NO_TIMEOUT); |
| 259 |
if ((1 != ready) /* if not 1, then we're dead */ |
| 260 |
|| (0 == (polldesc.in_flags & polldesc.out_flags))) { |
| 261 |
PR_NOT_REACHED("Whoa!"); |
| 262 |
break; |
| 263 |
} |
| 264 |
} else { |
| 265 |
break; |
| 266 |
} |
| 267 |
} while (bytes_read < sizeof(buffer)); |
| 268 |
|
| 269 |
if (0 != bytes_read) { |
| 270 |
if (verbosity > chatty) { |
| 271 |
PR_fprintf(logFile, "Server received %d bytes\n", bytes_read); |
| 272 |
} |
| 273 |
PR_ASSERT(bytes_read > 0); |
| 274 |
|
| 275 |
bytes_sent = 0; |
| 276 |
do { |
| 277 |
ready = PR_Send(service, buffer + bytes_sent, bytes_read - bytes_sent, |
| 278 |
empty_flags, PR_INTERVAL_NO_TIMEOUT); |
| 279 |
if (0 < ready) { |
| 280 |
bytes_sent += ready; |
| 281 |
} else if ((-1 == ready) && (PR_WOULD_BLOCK_ERROR == PR_GetError())) { |
| 282 |
polldesc.fd = service; |
| 283 |
polldesc.out_flags = 0; |
| 284 |
polldesc.in_flags = PR_POLL_WRITE; |
| 285 |
ready = PR_Poll(&polldesc, 1, PR_INTERVAL_NO_TIMEOUT); |
| 286 |
if ((1 != ready) /* if not 1, then we're dead */ |
| 287 |
|| (0 == (polldesc.in_flags & polldesc.out_flags))) { |
| 288 |
PR_NOT_REACHED("Whoa!"); |
| 289 |
break; |
| 290 |
} |
| 291 |
} else { |
| 292 |
break; |
| 293 |
} |
| 294 |
} while (bytes_sent < bytes_read); |
| 295 |
PR_ASSERT(bytes_read == bytes_sent); |
| 296 |
if (verbosity > chatty) { |
| 297 |
PR_fprintf(logFile, "Server sent %d bytes\n", bytes_sent); |
| 298 |
} |
| 299 |
} |
| 300 |
} while (0 != bytes_read); |
| 301 |
|
| 302 |
if (verbosity > quiet) { |
| 303 |
PR_fprintf(logFile, "Server shutting down stack\n"); |
| 304 |
} |
| 305 |
rv = PR_Shutdown(service, PR_SHUTDOWN_BOTH); |
| 306 |
PR_ASSERT(PR_SUCCESS == rv); |
| 307 |
rv = PR_Close(service); |
| 308 |
PR_ASSERT(PR_SUCCESS == rv); |
| 309 |
|
| 310 |
} /* Server */ |
| 311 |
|
| 312 |
static PRStatus PR_CALLBACK MyClose(PRFileDesc* fd) { |
| 313 |
PR_DELETE(fd->secret); /* manage my secret file object */ |
| 314 |
return (PR_GetDefaultIOMethods())->close(fd); /* let him do all the work */ |
| 315 |
} /* MyClose */ |
| 316 |
|
| 317 |
static PRInt16 PR_CALLBACK MyPoll(PRFileDesc* fd, PRInt16 in_flags, |
| 318 |
PRInt16* out_flags) { |
| 319 |
PRInt16 my_flags, new_flags; |
| 320 |
PRFilePrivate* mine = (PRFilePrivate*)fd->secret; |
| 321 |
if (0 != (PR_POLL_READ & in_flags)) { |
| 322 |
/* client thinks he's reading */ |
| 323 |
switch (mine->rcvstate) { |
| 324 |
case rcv_send_credit: |
| 325 |
my_flags = (in_flags & ~PR_POLL_READ) | PR_POLL_WRITE; |
| 326 |
break; |
| 327 |
case rcv_data: |
| 328 |
case rcv_get_debit: |
| 329 |
my_flags = in_flags; |
| 330 |
default: |
| 331 |
break; |
| 332 |
} |
| 333 |
} else if (0 != (PR_POLL_WRITE & in_flags)) { |
| 334 |
/* client thinks he's writing */ |
| 335 |
switch (mine->xmtstate) { |
| 336 |
case xmt_recv_credit: |
| 337 |
my_flags = (in_flags & ~PR_POLL_WRITE) | PR_POLL_READ; |
| 338 |
break; |
| 339 |
case xmt_send_debit: |
| 340 |
case xmt_data: |
| 341 |
my_flags = in_flags; |
| 342 |
default: |
| 343 |
break; |
| 344 |
} |
| 345 |
} else { |
| 346 |
PR_NOT_REACHED("How'd I get here?"); |
| 347 |
} |
| 348 |
new_flags = (fd->lower->methods->poll)(fd->lower, my_flags, out_flags); |
| 349 |
if (verbosity > chatty) |
| 350 |
PR_fprintf(logFile, "Poll [i: 0x%x, m: 0x%x, o: 0x%x, n: 0x%x]\n", in_flags, |
| 351 |
my_flags, *out_flags, new_flags); |
| 352 |
return new_flags; |
| 353 |
} /* MyPoll */ |
| 354 |
|
| 355 |
static PRFileDesc* PR_CALLBACK MyAccept(PRFileDesc* fd, PRNetAddr* addr, |
| 356 |
PRIntervalTime timeout) { |
| 357 |
PRStatus rv; |
| 358 |
PRFileDesc *newfd, *layer = fd; |
| 359 |
PRFileDesc* newstack; |
| 360 |
PRFilePrivate* newsecret; |
| 361 |
|
| 362 |
PR_ASSERT(fd != NULL); |
| 363 |
PR_ASSERT(fd->lower != NULL); |
| 364 |
|
| 365 |
newstack = PR_NEW(PRFileDesc); |
| 366 |
if (NULL == newstack) { |
| 367 |
PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); |
| 368 |
return NULL; |
| 369 |
} |
| 370 |
newsecret = PR_NEW(PRFilePrivate); |
| 371 |
if (NULL == newsecret) { |
| 372 |
PR_DELETE(newstack); |
| 373 |
PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); |
| 374 |
return NULL; |
| 375 |
} |
| 376 |
*newstack = *fd; /* make a copy of the accepting layer */ |
| 377 |
*newsecret = *fd->secret; |
| 378 |
newstack->secret = newsecret; |
| 379 |
|
| 380 |
newfd = (fd->lower->methods->accept)(fd->lower, addr, timeout); |
| 381 |
if (NULL == newfd) { |
| 382 |
PR_DELETE(newsecret); |
| 383 |
PR_DELETE(newstack); |
| 384 |
return NULL; |
| 385 |
} |
| 386 |
|
| 387 |
/* this PR_PushIOLayer call cannot fail */ |
| 388 |
rv = PR_PushIOLayer(newfd, PR_TOP_IO_LAYER, newstack); |
| 389 |
PR_ASSERT(PR_SUCCESS == rv); |
| 390 |
return newfd; /* that's it */ |
| 391 |
} |
| 392 |
|
| 393 |
static PRInt32 PR_CALLBACK MyRecv(PRFileDesc* fd, void* buf, PRInt32 amount, |
| 394 |
PRIntn flags, PRIntervalTime timeout) { |
| 395 |
char* b; |
| 396 |
PRInt32 rv; |
| 397 |
PRFileDesc* lo = fd->lower; |
| 398 |
PRFilePrivate* mine = (PRFilePrivate*)fd->secret; |
| 399 |
|
| 400 |
do { |
| 401 |
switch (mine->rcvstate) { |
| 402 |
case rcv_get_debit: |
| 403 |
b = (char*)&mine->rcvreq; |
| 404 |
mine->rcvreq = amount; |
| 405 |
rv = lo->methods->recv(lo, b + mine->rcvinprogress, |
| 406 |
sizeof(mine->rcvreq) - mine->rcvinprogress, |
| 407 |
flags, timeout); |
| 408 |
if (0 == rv) { |
| 409 |
goto closed; |
| 410 |
} |
| 411 |
if ((-1 == rv) && (PR_WOULD_BLOCK_ERROR == PR_GetError())) { |
| 412 |
break; |
| 413 |
} |
| 414 |
mine->rcvinprogress += rv; /* accumulate the read */ |
| 415 |
if (mine->rcvinprogress < sizeof(mine->rcvreq)) { |
| 416 |
break; /* loop */ |
| 417 |
} |
| 418 |
mine->rcvstate = rcv_send_credit; |
| 419 |
mine->rcvinprogress = 0; |
| 420 |
case rcv_send_credit: |
| 421 |
b = (char*)&mine->rcvreq; |
| 422 |
rv = lo->methods->send(lo, b + mine->rcvinprogress, |
| 423 |
sizeof(mine->rcvreq) - mine->rcvinprogress, |
| 424 |
flags, timeout); |
| 425 |
if ((-1 == rv) && (PR_WOULD_BLOCK_ERROR == PR_GetError())) { |
| 426 |
break; |
| 427 |
} |
| 428 |
mine->rcvinprogress += rv; /* accumulate the read */ |
| 429 |
if (mine->rcvinprogress < sizeof(mine->rcvreq)) { |
| 430 |
break; /* loop */ |
| 431 |
} |
| 432 |
mine->rcvstate = rcv_data; |
| 433 |
mine->rcvinprogress = 0; |
| 434 |
case rcv_data: |
| 435 |
b = (char*)buf; |
| 436 |
rv = lo->methods->recv(lo, b + mine->rcvinprogress, |
| 437 |
mine->rcvreq - mine->rcvinprogress, flags, |
| 438 |
timeout); |
| 439 |
if (0 == rv) { |
| 440 |
goto closed; |
| 441 |
} |
| 442 |
if ((-1 == rv) && (PR_WOULD_BLOCK_ERROR == PR_GetError())) { |
| 443 |
break; |
| 444 |
} |
| 445 |
mine->rcvinprogress += rv; /* accumulate the read */ |
| 446 |
if (mine->rcvinprogress < amount) { |
| 447 |
break; /* loop */ |
| 448 |
} |
| 449 |
mine->rcvstate = rcv_get_debit; |
| 450 |
mine->rcvinprogress = 0; |
| 451 |
return mine->rcvreq; /* << -- that's it! */ |
| 452 |
default: |
| 453 |
break; |
| 454 |
} |
| 455 |
} while (-1 != rv); |
| 456 |
return rv; |
| 457 |
closed: |
| 458 |
mine->rcvinprogress = 0; |
| 459 |
mine->rcvstate = rcv_get_debit; |
| 460 |
return 0; |
| 461 |
} /* MyRecv */ |
| 462 |
|
| 463 |
static PRInt32 PR_CALLBACK MySend(PRFileDesc* fd, const void* buf, |
| 464 |
PRInt32 amount, PRIntn flags, |
| 465 |
PRIntervalTime timeout) { |
| 466 |
char* b; |
| 467 |
PRInt32 rv; |
| 468 |
PRFileDesc* lo = fd->lower; |
| 469 |
PRFilePrivate* mine = (PRFilePrivate*)fd->secret; |
| 470 |
|
| 471 |
do { |
| 472 |
switch (mine->xmtstate) { |
| 473 |
case xmt_send_debit: |
| 474 |
b = (char*)&mine->xmtreq; |
| 475 |
mine->xmtreq = amount; |
| 476 |
rv = lo->methods->send(lo, b - mine->xmtinprogress, |
| 477 |
sizeof(mine->xmtreq) - mine->xmtinprogress, |
| 478 |
flags, timeout); |
| 479 |
if ((-1 == rv) && (PR_WOULD_BLOCK_ERROR == PR_GetError())) { |
| 480 |
break; |
| 481 |
} |
| 482 |
mine->xmtinprogress += rv; |
| 483 |
if (mine->xmtinprogress < sizeof(mine->xmtreq)) { |
| 484 |
break; |
| 485 |
} |
| 486 |
mine->xmtstate = xmt_recv_credit; |
| 487 |
mine->xmtinprogress = 0; |
| 488 |
case xmt_recv_credit: |
| 489 |
b = (char*)&mine->xmtreq; |
| 490 |
rv = lo->methods->recv(lo, b + mine->xmtinprogress, |
| 491 |
sizeof(mine->xmtreq) - mine->xmtinprogress, |
| 492 |
flags, timeout); |
| 493 |
if ((-1 == rv) && (PR_WOULD_BLOCK_ERROR == PR_GetError())) { |
| 494 |
break; |
| 495 |
} |
| 496 |
mine->xmtinprogress += rv; |
| 497 |
if (mine->xmtinprogress < sizeof(mine->xmtreq)) { |
| 498 |
break; |
| 499 |
} |
| 500 |
mine->xmtstate = xmt_data; |
| 501 |
mine->xmtinprogress = 0; |
| 502 |
case xmt_data: |
| 503 |
b = (char*)buf; |
| 504 |
rv = lo->methods->send(lo, b + mine->xmtinprogress, |
| 505 |
mine->xmtreq - mine->xmtinprogress, flags, |
| 506 |
timeout); |
| 507 |
if ((-1 == rv) && (PR_WOULD_BLOCK_ERROR == PR_GetError())) { |
| 508 |
break; |
| 509 |
} |
| 510 |
mine->xmtinprogress += rv; |
| 511 |
if (mine->xmtinprogress < amount) { |
| 512 |
break; |
| 513 |
} |
| 514 |
mine->xmtstate = xmt_send_debit; |
| 515 |
mine->xmtinprogress = 0; |
| 516 |
return mine->xmtreq; /* <<-- That's the one! */ |
| 517 |
default: |
| 518 |
break; |
| 519 |
} |
| 520 |
} while (-1 != rv); |
| 521 |
return rv; |
| 522 |
} /* MySend */ |
| 523 |
|
| 524 |
static Verbosity ChangeVerbosity(Verbosity verbosity, PRIntn delta) { |
| 525 |
PRIntn verbage = (PRIntn)verbosity + delta; |
| 526 |
if (verbage < (PRIntn)silent) { |
| 527 |
verbage = (PRIntn)silent; |
| 528 |
} else if (verbage > (PRIntn)noisy) { |
| 529 |
verbage = (PRIntn)noisy; |
| 530 |
} |
| 531 |
return (Verbosity)verbage; |
| 532 |
} /* ChangeVerbosity */ |
| 533 |
|
| 534 |
int main(int argc, char** argv) { |
| 535 |
PRStatus rv; |
| 536 |
PLOptStatus os; |
| 537 |
PRFileDesc *client, *service; |
| 538 |
PRNetAddr any_address; |
| 539 |
const char* server_name = NULL; |
| 540 |
const PRIOMethods* stubMethods; |
| 541 |
PRThread *client_thread, *server_thread; |
| 542 |
PRThreadScope thread_scope = PR_LOCAL_THREAD; |
| 543 |
PRSocketOptionData socket_noblock, socket_nodelay; |
| 544 |
PLOptState* opt = PL_CreateOptState(argc, argv, "dqGC:c:p:"); |
| 545 |
while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { |
| 546 |
if (PL_OPT_BAD == os) { |
| 547 |
continue; |
| 548 |
} |
| 549 |
switch (opt->option) { |
| 550 |
case 0: |
| 551 |
server_name = opt->value; |
| 552 |
break; |
| 553 |
case 'd': /* debug mode */ |
| 554 |
if (verbosity < noisy) { |
| 555 |
verbosity = ChangeVerbosity(verbosity, 1); |
| 556 |
} |
| 557 |
break; |
| 558 |
case 'q': /* debug mode */ |
| 559 |
if (verbosity > silent) { |
| 560 |
verbosity = ChangeVerbosity(verbosity, -1); |
| 561 |
} |
| 562 |
break; |
| 563 |
case 'G': /* use global threads */ |
| 564 |
thread_scope = PR_GLOBAL_THREAD; |
| 565 |
break; |
| 566 |
case 'C': /* number of threads waiting */ |
| 567 |
major_iterations = atoi(opt->value); |
| 568 |
break; |
| 569 |
case 'c': /* number of client threads */ |
| 570 |
minor_iterations = atoi(opt->value); |
| 571 |
break; |
| 572 |
case 'p': /* default port */ |
| 573 |
default_port = atoi(opt->value); |
| 574 |
break; |
| 575 |
default: |
| 576 |
break; |
| 577 |
} |
| 578 |
} |
| 579 |
PL_DestroyOptState(opt); |
| 580 |
PR_STDIO_INIT(); |
| 581 |
|
| 582 |
logFile = PR_GetSpecialFD(PR_StandardError); |
| 583 |
identity = PR_GetUniqueIdentity("Dummy"); |
| 584 |
stubMethods = PR_GetDefaultIOMethods(); |
| 585 |
|
| 586 |
/* |
| 587 |
** The protocol we're going to implement is one where in order to initiate |
| 588 |
** a send, the sender must first solicit permission. Therefore, every |
| 589 |
** send is really a send - receive - send sequence. |
| 590 |
*/ |
| 591 |
myMethods = *stubMethods; /* first get the entire batch */ |
| 592 |
myMethods.accept = MyAccept; /* then override the ones we care about */ |
| 593 |
myMethods.recv = MyRecv; /* then override the ones we care about */ |
| 594 |
myMethods.send = MySend; /* then override the ones we care about */ |
| 595 |
myMethods.close = MyClose; /* then override the ones we care about */ |
| 596 |
myMethods.poll = MyPoll; /* then override the ones we care about */ |
| 597 |
|
| 598 |
if (NULL == server_name) |
| 599 |
rv = PR_InitializeNetAddr(PR_IpAddrLoopback, default_port, &server_address); |
| 600 |
else { |
| 601 |
rv = PR_StringToNetAddr(server_name, &server_address); |
| 602 |
PR_ASSERT(PR_SUCCESS == rv); |
| 603 |
rv = PR_InitializeNetAddr(PR_IpAddrNull, default_port, &server_address); |
| 604 |
} |
| 605 |
PR_ASSERT(PR_SUCCESS == rv); |
| 606 |
|
| 607 |
socket_noblock.value.non_blocking = PR_TRUE; |
| 608 |
socket_noblock.option = PR_SockOpt_Nonblocking; |
| 609 |
socket_nodelay.value.no_delay = PR_TRUE; |
| 610 |
socket_nodelay.option = PR_SockOpt_NoDelay; |
| 611 |
|
| 612 |
/* one type w/o layering */ |
| 613 |
|
| 614 |
while (major_iterations-- > 0) { |
| 615 |
if (verbosity > silent) { |
| 616 |
PR_fprintf(logFile, "Beginning non-layered test\n"); |
| 617 |
} |
| 618 |
|
| 619 |
client = PR_NewTCPSocket(); |
| 620 |
PR_ASSERT(NULL != client); |
| 621 |
service = PR_NewTCPSocket(); |
| 622 |
PR_ASSERT(NULL != service); |
| 623 |
|
| 624 |
rv = PR_SetSocketOption(client, &socket_noblock); |
| 625 |
PR_ASSERT(PR_SUCCESS == rv); |
| 626 |
rv = PR_SetSocketOption(service, &socket_noblock); |
| 627 |
PR_ASSERT(PR_SUCCESS == rv); |
| 628 |
rv = PR_SetSocketOption(client, &socket_nodelay); |
| 629 |
PR_ASSERT(PR_SUCCESS == rv); |
| 630 |
rv = PR_SetSocketOption(service, &socket_nodelay); |
| 631 |
PR_ASSERT(PR_SUCCESS == rv); |
| 632 |
|
| 633 |
rv = PR_InitializeNetAddr(PR_IpAddrAny, default_port, &any_address); |
| 634 |
PR_ASSERT(PR_SUCCESS == rv); |
| 635 |
rv = PR_Bind(service, &any_address); |
| 636 |
PR_ASSERT(PR_SUCCESS == rv); |
| 637 |
rv = PR_Listen(service, 10); |
| 638 |
PR_ASSERT(PR_SUCCESS == rv); |
| 639 |
|
| 640 |
server_thread = |
| 641 |
PR_CreateThread(PR_USER_THREAD, Server, service, PR_PRIORITY_HIGH, |
| 642 |
thread_scope, PR_JOINABLE_THREAD, 16 * 1024); |
| 643 |
PR_ASSERT(NULL != server_thread); |
| 644 |
|
| 645 |
client_thread = |
| 646 |
PR_CreateThread(PR_USER_THREAD, Client, client, PR_PRIORITY_NORMAL, |
| 647 |
thread_scope, PR_JOINABLE_THREAD, 16 * 1024); |
| 648 |
PR_ASSERT(NULL != client_thread); |
| 649 |
|
| 650 |
rv = PR_JoinThread(client_thread); |
| 651 |
PR_ASSERT(PR_SUCCESS == rv); |
| 652 |
rv = PR_JoinThread(server_thread); |
| 653 |
PR_ASSERT(PR_SUCCESS == rv); |
| 654 |
|
| 655 |
rv = PR_Close(client); |
| 656 |
PR_ASSERT(PR_SUCCESS == rv); |
| 657 |
rv = PR_Close(service); |
| 658 |
PR_ASSERT(PR_SUCCESS == rv); |
| 659 |
if (verbosity > silent) { |
| 660 |
PR_fprintf(logFile, "Ending non-layered test\n"); |
| 661 |
} |
| 662 |
|
| 663 |
/* with layering */ |
| 664 |
if (verbosity > silent) { |
| 665 |
PR_fprintf(logFile, "Beginning layered test\n"); |
| 666 |
} |
| 667 |
client = PR_NewTCPSocket(); |
| 668 |
PR_ASSERT(NULL != client); |
| 669 |
service = PR_NewTCPSocket(); |
| 670 |
PR_ASSERT(NULL != service); |
| 671 |
|
| 672 |
rv = PR_SetSocketOption(client, &socket_noblock); |
| 673 |
PR_ASSERT(PR_SUCCESS == rv); |
| 674 |
rv = PR_SetSocketOption(service, &socket_noblock); |
| 675 |
PR_ASSERT(PR_SUCCESS == rv); |
| 676 |
rv = PR_SetSocketOption(client, &socket_nodelay); |
| 677 |
PR_ASSERT(PR_SUCCESS == rv); |
| 678 |
rv = PR_SetSocketOption(service, &socket_nodelay); |
| 679 |
PR_ASSERT(PR_SUCCESS == rv); |
| 680 |
|
| 681 |
PushLayer(client); |
| 682 |
PushLayer(service); |
| 683 |
|
| 684 |
rv = PR_InitializeNetAddr(PR_IpAddrAny, default_port, &any_address); |
| 685 |
PR_ASSERT(PR_SUCCESS == rv); |
| 686 |
rv = PR_Bind(service, &any_address); |
| 687 |
PR_ASSERT(PR_SUCCESS == rv); |
| 688 |
rv = PR_Listen(service, 10); |
| 689 |
PR_ASSERT(PR_SUCCESS == rv); |
| 690 |
|
| 691 |
server_thread = |
| 692 |
PR_CreateThread(PR_USER_THREAD, Server, service, PR_PRIORITY_HIGH, |
| 693 |
thread_scope, PR_JOINABLE_THREAD, 16 * 1024); |
| 694 |
PR_ASSERT(NULL != server_thread); |
| 695 |
|
| 696 |
client_thread = |
| 697 |
PR_CreateThread(PR_USER_THREAD, Client, client, PR_PRIORITY_NORMAL, |
| 698 |
thread_scope, PR_JOINABLE_THREAD, 16 * 1024); |
| 699 |
PR_ASSERT(NULL != client_thread); |
| 700 |
|
| 701 |
rv = PR_JoinThread(client_thread); |
| 702 |
PR_ASSERT(PR_SUCCESS == rv); |
| 703 |
rv = PR_JoinThread(server_thread); |
| 704 |
PR_ASSERT(PR_SUCCESS == rv); |
| 705 |
|
| 706 |
rv = PR_Close(PopLayer(client)); |
| 707 |
PR_ASSERT(PR_SUCCESS == rv); |
| 708 |
rv = PR_Close(PopLayer(service)); |
| 709 |
PR_ASSERT(PR_SUCCESS == rv); |
| 710 |
if (verbosity > silent) { |
| 711 |
PR_fprintf(logFile, "Ending layered test\n"); |
| 712 |
} |
| 713 |
} |
| 714 |
return 0; |
| 715 |
} /* main */ |
| 716 |
|
| 717 |
/* nblayer.c */ |