2
0

ops-raw-file.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010-2018 Andy Green <[email protected]>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation:
  9. * version 2.1 of the License.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. * MA 02110-1301 USA
  20. */
  21. #include <core/private.h>
  22. static int
  23. rops_handle_POLLIN_raw_file(struct lws_context_per_thread *pt, struct lws *wsi,
  24. struct lws_pollfd *pollfd)
  25. {
  26. int n;
  27. if (pollfd->revents & LWS_POLLOUT) {
  28. n = lws_callback_as_writeable(wsi);
  29. if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
  30. lwsl_info("failed at set pollfd\n");
  31. return LWS_HPI_RET_WSI_ALREADY_DIED;
  32. }
  33. if (n)
  34. return LWS_HPI_RET_PLEASE_CLOSE_ME;
  35. }
  36. if (pollfd->revents & LWS_POLLIN) {
  37. if (user_callback_handle_rxflow(wsi->protocol->callback,
  38. wsi, LWS_CALLBACK_RAW_RX_FILE,
  39. wsi->user_space, NULL, 0)) {
  40. lwsl_debug("raw rx callback closed it\n");
  41. return LWS_HPI_RET_PLEASE_CLOSE_ME;
  42. }
  43. }
  44. if (pollfd->revents & LWS_POLLHUP)
  45. return LWS_HPI_RET_PLEASE_CLOSE_ME;
  46. return LWS_HPI_RET_HANDLED;
  47. }
  48. #if !defined(LWS_NO_SERVER)
  49. static int
  50. rops_adoption_bind_raw_file(struct lws *wsi, int type, const char *vh_prot_name)
  51. {
  52. /* no socket or http: it can only be a raw file */
  53. if ((type & LWS_ADOPT_HTTP) || (type & LWS_ADOPT_SOCKET) ||
  54. (type & _LWS_ADOPT_FINISH))
  55. return 0; /* no match */
  56. lws_role_transition(wsi, 0, LRS_ESTABLISHED, &role_ops_raw_file);
  57. if (!vh_prot_name)
  58. wsi->protocol = &wsi->vhost->protocols[
  59. wsi->vhost->default_protocol_index];
  60. return 1; /* bound */
  61. }
  62. #endif
  63. struct lws_role_ops role_ops_raw_file = {
  64. /* role name */ "raw-file",
  65. /* alpn id */ NULL,
  66. /* check_upgrades */ NULL,
  67. /* init_context */ NULL,
  68. /* init_vhost */ NULL,
  69. /* destroy_vhost */ NULL,
  70. /* periodic_checks */ NULL,
  71. /* service_flag_pending */ NULL,
  72. /* handle_POLLIN */ rops_handle_POLLIN_raw_file,
  73. /* handle_POLLOUT */ NULL,
  74. /* perform_user_POLLOUT */ NULL,
  75. /* callback_on_writable */ NULL,
  76. /* tx_credit */ NULL,
  77. /* write_role_protocol */ NULL,
  78. /* encapsulation_parent */ NULL,
  79. /* alpn_negotiated */ NULL,
  80. /* close_via_role_protocol */ NULL,
  81. /* close_role */ NULL,
  82. /* close_kill_connection */ NULL,
  83. /* destroy_role */ NULL,
  84. #if !defined(LWS_NO_SERVER)
  85. /* adoption_bind */ rops_adoption_bind_raw_file,
  86. #else
  87. NULL,
  88. #endif
  89. /* client_bind */ NULL,
  90. /* writeable cb clnt, srv */ { LWS_CALLBACK_RAW_WRITEABLE_FILE, 0 },
  91. /* close cb clnt, srv */ { LWS_CALLBACK_RAW_CLOSE_FILE, 0 },
  92. /* protocol_bind cb c, srv */ { LWS_CALLBACK_RAW_FILE_BIND_PROTOCOL,
  93. LWS_CALLBACK_RAW_FILE_BIND_PROTOCOL },
  94. /* protocol_unbind cb c, srv */ { LWS_CALLBACK_RAW_FILE_DROP_PROTOCOL,
  95. LWS_CALLBACK_RAW_FILE_DROP_PROTOCOL },
  96. /* file_handle */ 1,
  97. };