WSClient.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  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 "../../Core/WorkQueue.h"
  23. #include "../../IO/MemoryBuffer.h"
  24. #include "../../IO/Log.h"
  25. #include "../Network.h"
  26. #include "WSClient.h"
  27. #include "WSHandler.h"
  28. #include <libwebsockets.h>
  29. #include <string.h>
  30. #include <signal.h>
  31. static Urho3D::WSClient* WSClientInstance = nullptr;
  32. static struct lws_context *context;
  33. #if defined(WIN32)
  34. #define HAVE_STRUCT_TIMESPEC
  35. #if defined(pid_t)
  36. #undef pid_t
  37. #endif
  38. #endif
  39. #include <pthread.h>
  40. static struct lws *client_wsi;
  41. static int interrupted, port = 9292, ssl_connection = LCCSCF_USE_SSL;
  42. static const char *server_address = "127.0.0.1", *pro = "ws-server";
  43. static lws_sorted_usec_list_t sul;
  44. static const lws_retry_bo_t retry = {
  45. .secs_since_valid_ping = 3,
  46. .secs_since_valid_hangup = 10,
  47. };
  48. static void connect_cb(lws_sorted_usec_list_t *_sul)
  49. {
  50. struct lws_client_connect_info i;
  51. lwsl_notice("%s: connecting\n", __func__);
  52. memset(&i, 0, sizeof(i));
  53. i.context = context;
  54. i.port = port;
  55. i.address = server_address;
  56. i.path = "/";
  57. i.host = i.address;
  58. i.origin = i.address;
  59. // i.ssl_connection = ssl_connection;
  60. i.protocol = pro; // Server protocol name to connect to
  61. i.alpn = "http/1.1";
  62. i.local_protocol_name = "ws_client";
  63. i.pwsi = &client_wsi;
  64. // i.retry_and_idle_policy = &retry;
  65. if (!lws_client_connect_via_info(&i))
  66. lws_sul_schedule(context, 0, _sul, connect_cb, 5 * LWS_USEC_PER_SEC);
  67. }
  68. static int WSCallback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
  69. {
  70. URHO3D_LOGINFOF("Incoming client messsage reason %d", reason);
  71. switch (reason) {
  72. case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
  73. if (WSClientInstance) {
  74. WSClientInstance->SetState(Urho3D::WCS_CONNECTION_FAILED);
  75. }
  76. break;
  77. case LWS_CALLBACK_CLIENT_ESTABLISHED:
  78. if (WSClientInstance) {
  79. WSClientInstance->SetWSConnection(wsi);
  80. WSClientInstance->SetState(Urho3D::WCS_CONNECTED);
  81. }
  82. lwsl_user("%s: established\n", __func__);
  83. break;
  84. case LWS_CALLBACK_CLIENT_RECEIVE: {
  85. URHO3D_LOGINFOF("Received buffer of size %d", len);
  86. Urho3D::VectorBuffer b((unsigned char*)in, len);
  87. if (b.GetData()[0] == URHO3D_MESSAGE) {
  88. WSPacket packet(wsi, b);
  89. if (WSClientInstance) {
  90. WSClientInstance->AddIncomingPacket(packet);
  91. }
  92. } else {
  93. URHO3D_LOGINFOF("Received message that is not part of the engine %d", b.GetData()[0]);
  94. }
  95. lws_callback_on_writable(wsi);
  96. break;
  97. }
  98. case LWS_CALLBACK_CLIENT_WRITEABLE:
  99. if (WSClientInstance) {
  100. if(WSClientInstance->GetNumOutgoingPackets(wsi)) {
  101. auto packet = WSClientInstance->GetOutgoingPacket(wsi);
  102. if (packet) {
  103. unsigned char buf[LWS_PRE + packet->second_.GetSize()];
  104. memcpy(&buf[LWS_PRE], packet->second_.GetData(), packet->second_.GetSize());
  105. int retval = lws_write(wsi, &buf[LWS_PRE], packet->second_.GetSize(), LWS_WRITE_BINARY);
  106. if (retval < packet->second_.GetSize()) {
  107. URHO3D_LOGERRORF("Failed to write to WS, ret = %d", retval);
  108. break;
  109. }
  110. WSClientInstance->RemoveOutgoingPacket(wsi);
  111. }
  112. }
  113. lws_callback_on_writable(wsi);
  114. }
  115. break;
  116. case LWS_CALLBACK_CLIENT_CLOSED:
  117. if (WSClientInstance) {
  118. WSClientInstance->Disconnect();
  119. }
  120. URHO3D_LOGINFOF("LWS_CALLBACK_CLIENT_CLOSED");
  121. break;
  122. default:
  123. break;
  124. }
  125. return 0;
  126. }
  127. static const struct lws_protocols protocols[] = {
  128. {
  129. "ws_client",
  130. WSCallback,
  131. 0,
  132. 0,
  133. },
  134. { NULL, NULL, 0, 0 }
  135. };
  136. static void sigint_handler(int sig)
  137. {
  138. interrupted = 1;
  139. }
  140. using namespace Urho3D;
  141. static void RunService(const WorkItem* item, unsigned threadIndex) {
  142. int result = lws_service(context, 0);
  143. if (result < 0 && WSClientInstance) {
  144. WSClientInstance->Disconnect();
  145. }
  146. URHO3D_LOGINFOF("Running client service");
  147. }
  148. WSClient::WSClient(Context* context):
  149. Object(context),
  150. ws_(nullptr)
  151. {
  152. SetState(WCS_DISCONNECTED);
  153. WSClientInstance = this;
  154. }
  155. WSClient::~WSClient()
  156. {
  157. WSClientInstance = nullptr;
  158. }
  159. int WSClient::Connect()
  160. {
  161. struct lws_context_creation_info info;
  162. const char *p;
  163. int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
  164. /* for LLL_ verbosity above NOTICE to be built into lws,
  165. * lws must have been configured and built with
  166. * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
  167. /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
  168. /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
  169. /* | LLL_DEBUG */;
  170. signal(SIGINT, sigint_handler);
  171. lws_set_log_level(logs, OutputWSLog);
  172. lwsl_user("LWS minimal ws client PING\n");
  173. memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
  174. // info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
  175. info.port = CONTEXT_PORT_NO_LISTEN; /* we do not run any server */
  176. info.protocols = protocols;
  177. #if defined(LWS_WITH_MBEDTLS) || defined(USE_WOLFSSL)
  178. /*
  179. * OpenSSL uses the system trust store. mbedTLS has to be told which
  180. * CA to trust explicitly.
  181. */
  182. info.client_ssl_ca_filepath = "./libwebsockets.org.cer";
  183. #endif
  184. // info.fd_limit_per_thread = 1 + 1 + 1;
  185. context = lws_create_context(&info);
  186. if (!context) {
  187. lwsl_err("lws init failed\n");
  188. return 1;
  189. }
  190. lws_sul_schedule(context, 0, &sul, connect_cb, 50);
  191. SubscribeToEvent(E_WORKITEMCOMPLETED, URHO3D_HANDLER(WSClient, HandleWorkItemFinished));
  192. SetState(WCS_CONNECTING);
  193. return 0;
  194. }
  195. void WSClient::Update(float timestep)
  196. {
  197. if (currentState_ == WCS_CONNECTING && nextState_ == WCS_CONNECTED) {
  198. GetSubsystem<Network>()->OnServerConnected(GetWSConnection());
  199. }
  200. if (nextState_ == WCS_DISCONNECTED) {
  201. GetSubsystem<Network>()->OnServerDisconnected(GetWSConnection(), false);
  202. }
  203. if (nextState_ == WCS_CONNECTION_FAILED) {
  204. GetSubsystem<Network>()->OnServerDisconnected(GetWSConnection(), true);
  205. }
  206. if (context) {
  207. if (!serviceWorkItem_ && currentState_ != WCS_DISCONNECTED) {
  208. WorkQueue *workQueue = GetSubsystem<WorkQueue>();
  209. serviceWorkItem_ = workQueue->GetFreeItem();
  210. serviceWorkItem_->priority_ = M_MAX_INT;
  211. serviceWorkItem_->workFunction_ = RunService;
  212. serviceWorkItem_->aux_ = this;
  213. serviceWorkItem_->sendEvent_ = true;
  214. workQueue->AddWorkItem(serviceWorkItem_);
  215. }
  216. }
  217. while (GetNumIncomingPackets()) {
  218. auto packet = GetIncomingPacket();
  219. GetSubsystem<Network>()->HandleIncomingPacket(packet, false);
  220. RemoveIncomingPacket();
  221. }
  222. if (currentState_ != nextState_) {
  223. currentState_ = nextState_;
  224. }
  225. }
  226. void WSClient::Disconnect()
  227. {
  228. SetState(WCS_DISCONNECTED);
  229. if (context)
  230. {
  231. lws_context_destroy(context);
  232. context = nullptr;
  233. }
  234. }
  235. void WSClient::SetWSConnection(lws *ws)
  236. {
  237. ws_ = ws;
  238. }
  239. void WSClient::SetState(WSClientState state)
  240. {
  241. nextState_ = state;
  242. }
  243. void WSClient::HandleWorkItemFinished(StringHash eventType, VariantMap& eventData)
  244. {
  245. using namespace WorkItemCompleted;
  246. WorkItem *workItem = reinterpret_cast<WorkItem *>(eventData[P_ITEM].GetPtr());
  247. if (workItem->aux_ != this) {
  248. return;
  249. }
  250. if (workItem->workFunction_ == RunService) {
  251. serviceWorkItem_.Reset();
  252. }
  253. }