nblayer.c 26 KB

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