1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef COMMUNICATIONNETSTACK_WEBSOCKET_EXEC_H 17 #define COMMUNICATIONNETSTACK_WEBSOCKET_EXEC_H 18 19 #include "close_context.h" 20 #include "connect_context.h" 21 #include "send_context.h" 22 23 namespace OHOS::NetStack { 24 class WebSocketExec final { 25 public: 26 /* async work execute */ 27 static bool ExecConnect(ConnectContext *context); 28 29 static bool ExecSend(SendContext *context); 30 31 static bool ExecClose(CloseContext *context); 32 33 /* async work callback */ 34 static napi_value ConnectCallback(ConnectContext *context); 35 36 static napi_value SendCallback(SendContext *context); 37 38 static napi_value CloseCallback(CloseContext *context); 39 40 static int LwsCallback(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 41 42 private: 43 static bool ParseUrl(ConnectContext *context, 44 char *prefix, 45 size_t prefixLen, 46 char *address, 47 size_t addressLen, 48 char *path, 49 size_t pathLen, 50 int *port); 51 52 static void RunService(EventManager *manager); 53 54 static int RaiseError(EventManager *manager); 55 56 static int HttpDummy(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 57 58 static int 59 LwsCallbackClientAppendHandshakeHeader(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 60 61 static int LwsCallbackWsPeerInitiatedClose(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 62 63 static int LwsCallbackClientWritable(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 64 65 static int 66 LwsCallbackClientConnectionError(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 67 68 static int LwsCallbackClientReceive(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 69 70 static int 71 LwsCallbackClientFilterPreEstablish(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 72 73 static int LwsCallbackClientEstablished(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 74 75 static int LwsCallbackClientClosed(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 76 77 static int LwsCallbackWsiDestroy(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 78 79 static int LwsCallbackProtocolDestroy(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 80 81 static void OnOpen(EventManager *manager, uint32_t status, const std::string &message); 82 83 static void OnError(EventManager *manager, int32_t code); 84 85 static void OnMessage(EventManager *manager, void *data, size_t length, bool isBinary); 86 87 static void OnClose(EventManager *manager, lws_close_status closeStatus, const std::string &closeReason); 88 }; 89 } // namespace OHOS::NetStack 90 #endif /* COMMUNICATIONNETSTACK_WEBSOCKET_EXEC_H */ 91