test-server-echogen.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * libwebsockets-test-server - libwebsockets test implementation
  3. *
  4. * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
  5. *
  6. * This file is made available under the Creative Commons CC0 1.0
  7. * Universal Public Domain Dedication.
  8. *
  9. * The person who associated a work with this deed has dedicated
  10. * the work to the public domain by waiving all of his or her rights
  11. * to the work worldwide under copyright law, including all related
  12. * and neighboring rights, to the extent allowed by law. You can copy,
  13. * modify, distribute and perform the work, even for commercial purposes,
  14. * all without asking permission.
  15. *
  16. * The test apps are intended to be adapted for use in your code, which
  17. * may be proprietary. So unlike the library itself, they are licensed
  18. * Public Domain.
  19. */
  20. #include "test-server.h"
  21. /* echogen protocol
  22. *
  23. * if you connect to him using his protocol, he'll send you a file chopped
  24. * up in various frame sizes repeated until he reaches a limit.
  25. */
  26. #define TOTAL 993840
  27. int
  28. callback_lws_echogen(struct lws *wsi, enum lws_callback_reasons reason,
  29. void *user, void *in, size_t len)
  30. {
  31. unsigned char buf[LWS_PRE + 8192];
  32. struct per_session_data__echogen *pss =
  33. (struct per_session_data__echogen *)user;
  34. unsigned char *p = &buf[LWS_PRE];
  35. int n, m;
  36. switch (reason) {
  37. case LWS_CALLBACK_ESTABLISHED:
  38. pss->total = TOTAL;
  39. pss->fragsize = 2048;
  40. pss->total_rx = 0;
  41. sprintf((char *)buf, "%s/test.html", resource_path);
  42. pss->fd = open((char *)buf, LWS_O_RDONLY);
  43. if (pss->fd < 0) {
  44. lwsl_err("Failed to open %s\n", buf);
  45. return -1;
  46. }
  47. pss->wr = LWS_WRITE_TEXT | LWS_WRITE_NO_FIN;
  48. lws_callback_on_writable(wsi);
  49. break;
  50. case LWS_CALLBACK_CLOSED:
  51. if (pss->fd >= 0)
  52. close(pss->fd);
  53. break;
  54. case LWS_CALLBACK_SERVER_WRITEABLE:
  55. // pss->fragsize += 16;
  56. // if (pss->fragsize >= 4096)
  57. // pss->fragsize = 32;
  58. lwsl_err("%s: cb writeable, total left %ld\n", __func__, (long)pss->total);
  59. m = pss->fragsize;
  60. if ((size_t)m >= pss->total) {
  61. m = (int)pss->total;
  62. pss->wr = LWS_WRITE_CONTINUATION; /* ie, FIN */
  63. }
  64. n = read(pss->fd, p, m);
  65. if (n < 0) {
  66. lwsl_err("failed read\n");
  67. return -1;
  68. }
  69. if (n < m) {
  70. lseek(pss->fd, 0, SEEK_SET);
  71. m = read(pss->fd, p + n, m - n);
  72. if (m < 0)
  73. return -1;
  74. } else
  75. m = 0;
  76. pss->total -= n + m;
  77. m = lws_write(wsi, p, n + m, pss->wr);
  78. if (m < n) {
  79. lwsl_err("ERROR %d writing to di socket\n", n);
  80. return -1;
  81. }
  82. if (!pss->total) {
  83. lwsl_err("Completed OK\n");
  84. break;
  85. }
  86. pss->wr = LWS_WRITE_CONTINUATION | LWS_WRITE_NO_FIN;
  87. lws_callback_on_writable(wsi);
  88. break;
  89. case LWS_CALLBACK_RECEIVE:
  90. pss->total_rx += len;
  91. lwsl_err("rx %ld\n", (long)pss->total_rx);
  92. if (pss->total_rx == TOTAL) {
  93. lws_close_reason(wsi, LWS_CLOSE_STATUS_NORMAL,
  94. (unsigned char *)"done", 4);
  95. return -1;
  96. }
  97. break;
  98. case LWS_CALLBACK_WS_PEER_INITIATED_CLOSE:
  99. lwsl_notice("LWS_CALLBACK_WS_PEER_INITIATED_CLOSE: len %lu\n",
  100. (unsigned long)len);
  101. for (n = 0; n < (int)len; n++)
  102. lwsl_notice(" %d: 0x%02X\n", n,
  103. ((unsigned char *)in)[n]);
  104. break;
  105. default:
  106. break;
  107. }
  108. return 0;
  109. }