windows-init.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
  22. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  23. #endif
  24. #include "core/private.h"
  25. void
  26. lws_plat_drop_app_privileges(const struct lws_context_creation_info *info)
  27. {
  28. }
  29. int
  30. lws_plat_context_early_init(void)
  31. {
  32. WORD wVersionRequested;
  33. WSADATA wsaData;
  34. int err;
  35. /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
  36. wVersionRequested = MAKEWORD(2, 2);
  37. err = WSAStartup(wVersionRequested, &wsaData);
  38. if (!err)
  39. return 0;
  40. /*
  41. * Tell the user that we could not find a usable
  42. * Winsock DLL
  43. */
  44. lwsl_err("WSAStartup failed with error: %d\n", err);
  45. return 1;
  46. }
  47. int
  48. lws_plat_init(struct lws_context *context,
  49. const struct lws_context_creation_info *info)
  50. {
  51. struct lws_context_per_thread *pt = &context->pt[0];
  52. int i, n = context->count_threads;
  53. for (i = 0; i < FD_HASHTABLE_MODULUS; i++) {
  54. context->fd_hashtable[i].wsi =
  55. lws_zalloc(sizeof(struct lws*) * context->max_fds,
  56. "win hashtable");
  57. if (!context->fd_hashtable[i].wsi)
  58. return -1;
  59. }
  60. while (n--) {
  61. pt->fds_count = 0;
  62. pt->events = WSACreateEvent(); /* the cancel event */
  63. pt++;
  64. }
  65. context->fd_random = 0;
  66. #ifdef LWS_WITH_PLUGINS
  67. if (info->plugin_dirs)
  68. lws_plat_plugins_init(context, info->plugin_dirs);
  69. #endif
  70. return 0;
  71. }
  72. void
  73. lws_plat_context_early_destroy(struct lws_context *context)
  74. {
  75. struct lws_context_per_thread *pt = &context->pt[0];
  76. int n = context->count_threads;
  77. while (n--) {
  78. WSACloseEvent(pt->events);
  79. pt++;
  80. }
  81. }
  82. void
  83. lws_plat_context_late_destroy(struct lws_context *context)
  84. {
  85. int n;
  86. for (n = 0; n < FD_HASHTABLE_MODULUS; n++) {
  87. if (context->fd_hashtable[n].wsi)
  88. lws_free(context->fd_hashtable[n].wsi);
  89. }
  90. WSACleanup();
  91. }