Web.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/Profiler.h"
  24. #include "../Core/CoreEvents.h"
  25. #include "../IO/Log.h"
  26. #include "../Web/Web.h"
  27. #include "../Web/WebRequest.h"
  28. #include "../Web/WebSocket.h"
  29. #ifndef EMSCRIPTEN
  30. #include "../Web/WebInternalConfig.h"
  31. #include <asio/io_service.hpp>
  32. #include <curl/curl.h>
  33. #endif
  34. #include "../DebugNew.h"
  35. namespace Atomic
  36. {
  37. struct WebPrivate
  38. {
  39. #ifndef EMSCRIPTEN
  40. asio::io_service service;
  41. CURLM *curlm;
  42. #endif
  43. };
  44. Web::Web(Context* context) :
  45. Object(context),
  46. d(new WebPrivate())
  47. {
  48. #ifndef EMSCRIPTEN
  49. d->curlm = curl_multi_init();
  50. #endif
  51. SubscribeToEvent(E_UPDATE, ATOMIC_HANDLER(Web, internalUpdate));
  52. }
  53. Web::~Web()
  54. {
  55. UnsubscribeFromEvent(E_UPDATE);
  56. #ifndef EMSCRIPTEN
  57. curl_multi_cleanup(d->curlm);
  58. #endif
  59. delete d;
  60. }
  61. void Web::internalUpdate(StringHash eventType, VariantMap& eventData)
  62. {
  63. #ifndef EMSCRIPTEN
  64. int runningHandles;
  65. curl_multi_perform(d->curlm, &runningHandles);
  66. CURLMsg *msg;
  67. int msgsLeft;
  68. while ((msg = curl_multi_info_read(d->curlm, &msgsLeft)))
  69. {
  70. if (msg->msg != CURLMSG_DONE)
  71. {
  72. continue;
  73. }
  74. WebRequest *wr;
  75. curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &wr);
  76. if (wr != NULL)
  77. {
  78. WebRequest::internalNotify(wr, int(msg->data.result));
  79. }
  80. }
  81. d->service.reset();
  82. d->service.poll();
  83. #endif
  84. }
  85. #ifndef EMSCRIPTEN
  86. void Web::setup(WebRequest& webRequest)
  87. {
  88. webRequest.setup(&d->service, d->curlm);
  89. }
  90. void Web::setup(WebSocket& webSocket)
  91. {
  92. webSocket.setup(&d->service);
  93. }
  94. #endif
  95. SharedPtr<WebRequest> Web::MakeWebRequest(const String& verb, const String& url, double requestContentSize)
  96. {
  97. ATOMIC_PROFILE(MakeWebRequest);
  98. // The initialization of the request will take time, can not know at this point if it has an error or not
  99. SharedPtr<WebRequest> webRequest(new WebRequest(context_, verb, url, requestContentSize));
  100. return webRequest;
  101. }
  102. SharedPtr<WebSocket> Web::MakeWebSocket(const String& url)
  103. {
  104. ATOMIC_PROFILE(MakeWebSocket);
  105. // The initialization of the WebSocket will take time, can not know at this point if it has an error or not
  106. SharedPtr<WebSocket> webSocket(new WebSocket(context_, url));
  107. return webSocket;
  108. }
  109. }