windows-init.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
  25. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  26. #endif
  27. #include "private-lib-core.h"
  28. int
  29. lws_plat_drop_app_privileges(struct lws_context *context, int actually_set)
  30. {
  31. return 0;
  32. }
  33. int
  34. lws_plat_context_early_init(void)
  35. {
  36. WORD wVersionRequested;
  37. WSADATA wsaData;
  38. int err;
  39. /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
  40. wVersionRequested = MAKEWORD(2, 2);
  41. err = WSAStartup(wVersionRequested, &wsaData);
  42. if (!err)
  43. return 0;
  44. /*
  45. * Tell the user that we could not find a usable
  46. * Winsock DLL
  47. */
  48. lwsl_err("WSAStartup failed with error: %d\n", err);
  49. return 1;
  50. }
  51. #if defined(LWS_WITH_PLUGINS)
  52. static int
  53. protocol_plugin_cb(struct lws_plugin *pin, void *each_user)
  54. {
  55. struct lws_context *context = (struct lws_context *)each_user;
  56. const lws_plugin_protocol_t *plpr =
  57. (const lws_plugin_protocol_t *)pin->hdr;
  58. context->plugin_protocol_count += plpr->count_protocols;
  59. context->plugin_extension_count += plpr->count_extensions;
  60. return 0;
  61. }
  62. #endif
  63. int
  64. lws_plat_init(struct lws_context *context,
  65. const struct lws_context_creation_info *info)
  66. {
  67. struct lws_context_per_thread *pt = &context->pt[0];
  68. int i, n = context->count_threads;
  69. for (i = 0; i < FD_HASHTABLE_MODULUS; i++) {
  70. context->fd_hashtable[i].wsi =
  71. lws_zalloc(sizeof(struct lws*) * context->max_fds,
  72. "win hashtable");
  73. if (!context->fd_hashtable[i].wsi)
  74. return -1;
  75. }
  76. while (n--) {
  77. int m;
  78. pt->fds_count = 0;
  79. for (m = 0; m < WSA_MAXIMUM_WAIT_EVENTS; m++)
  80. pt->events[m] = WSACreateEvent();
  81. InitializeCriticalSection(&pt->interrupt_lock);
  82. pt++;
  83. }
  84. context->fd_random = 0;
  85. #if defined(LWS_WITH_PLUGINS)
  86. if (info->plugin_dirs)
  87. lws_plat_plugins_init(&context->plugin_list, info->plugin_dirs,
  88. "lws_protocol_plugin",
  89. protocol_plugin_cb, context);
  90. #endif
  91. return 0;
  92. }
  93. void
  94. lws_plat_context_early_destroy(struct lws_context *context)
  95. {
  96. struct lws_context_per_thread *pt = &context->pt[0];
  97. int n = context->count_threads;
  98. while (n--) {
  99. int m;
  100. for (m = 0; m < WSA_MAXIMUM_WAIT_EVENTS; m++)
  101. WSACloseEvent(pt->events[m]);
  102. DeleteCriticalSection(&pt->interrupt_lock);
  103. pt++;
  104. }
  105. }
  106. void
  107. lws_plat_context_late_destroy(struct lws_context *context)
  108. {
  109. int n;
  110. #ifdef LWS_WITH_PLUGINS
  111. if (context->plugin_list)
  112. lws_plugins_destroy(&context->plugin_list);
  113. #endif
  114. for (n = 0; n < FD_HASHTABLE_MODULUS; n++) {
  115. if (context->fd_hashtable[n].wsi)
  116. lws_free(context->fd_hashtable[n].wsi);
  117. }
  118. WSACleanup();
  119. }