test-server-dumb-increment.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. /* dumb_increment protocol */
  22. int
  23. callback_dumb_increment(struct lws *wsi, enum lws_callback_reasons reason,
  24. void *user, void *in, size_t len)
  25. {
  26. unsigned char buf[LWS_PRE + 512];
  27. struct per_session_data__dumb_increment *pss =
  28. (struct per_session_data__dumb_increment *)user;
  29. unsigned char *p = &buf[LWS_PRE];
  30. int n, m;
  31. switch (reason) {
  32. case LWS_CALLBACK_ESTABLISHED:
  33. pss->number = 0;
  34. break;
  35. case LWS_CALLBACK_SERVER_WRITEABLE:
  36. n = sprintf((char *)p, "%d", pss->number++);
  37. m = lws_write(wsi, p, n, LWS_WRITE_TEXT);
  38. if (m < n) {
  39. lwsl_err("ERROR %d writing to di socket\n", n);
  40. return -1;
  41. }
  42. if (close_testing && pss->number == 50) {
  43. lwsl_info("close tesing limit, closing\n");
  44. return -1;
  45. }
  46. break;
  47. case LWS_CALLBACK_RECEIVE:
  48. if (len < 6)
  49. break;
  50. if (strcmp((const char *)in, "reset\n") == 0)
  51. pss->number = 0;
  52. if (strcmp((const char *)in, "closeme\n") == 0) {
  53. lwsl_notice("dumb_inc: closing as requested\n");
  54. lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
  55. (unsigned char *)"seeya", 5);
  56. return -1;
  57. }
  58. break;
  59. /*
  60. * this just demonstrates how to use the protocol filter. If you won't
  61. * study and reject connections based on header content, you don't need
  62. * to handle this callback
  63. */
  64. case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
  65. dump_handshake_info(wsi);
  66. /* you could return non-zero here and kill the connection */
  67. break;
  68. /*
  69. * this just demonstrates how to handle
  70. * LWS_CALLBACK_WS_PEER_INITIATED_CLOSE and extract the peer's close
  71. * code and auxiliary data. You can just not handle it if you don't
  72. * have a use for this.
  73. */
  74. case LWS_CALLBACK_WS_PEER_INITIATED_CLOSE:
  75. lwsl_notice("LWS_CALLBACK_WS_PEER_INITIATED_CLOSE: len %lu\n",
  76. (unsigned long)len);
  77. for (n = 0; n < (int)len; n++)
  78. lwsl_notice(" %d: 0x%02X\n", n,
  79. ((unsigned char *)in)[n]);
  80. break;
  81. default:
  82. break;
  83. }
  84. return 0;
  85. }