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::Websocket { 24 class WebSocketExec final { 25 public: 26 static bool CreatConnectInfo(ConnectContext *context, lws_context *lwsContext, EventManager *manager); 27 /* async work execute */ 28 static bool ExecConnect(ConnectContext *context); 29 30 static bool ExecSend(SendContext *context); 31 32 static bool ExecClose(CloseContext *context); 33 34 /* async work callback */ 35 static napi_value ConnectCallback(ConnectContext *context); 36 37 static napi_value SendCallback(SendContext *context); 38 39 static napi_value CloseCallback(CloseContext *context); 40 41 static int LwsCallback(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 42 43 private: 44 static bool ParseUrl(ConnectContext *context, char *prefix, size_t prefixLen, char *address, size_t addressLen, 45 char *path, size_t pathLen, int *port); 46 47 static void RunService(EventManager *manager); 48 49 static int RaiseError(EventManager *manager); 50 51 static int HttpDummy(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 52 53 static int LwsCallbackClientAppendHandshakeHeader(lws *wsi, lws_callback_reasons reason, void *user, void *in, 54 size_t len); 55 56 static int LwsCallbackWsPeerInitiatedClose(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 57 58 static int LwsCallbackClientWritable(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 59 60 static int LwsCallbackClientConnectionError(lws *wsi, lws_callback_reasons reason, void *user, void *in, 61 size_t len); 62 63 static int LwsCallbackClientReceive(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 64 65 static int LwsCallbackClientFilterPreEstablish(lws *wsi, lws_callback_reasons reason, void *user, void *in, 66 size_t len); 67 68 static int LwsCallbackClientEstablished(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 69 70 static int LwsCallbackClientClosed(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 71 72 static int LwsCallbackWsiDestroy(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 73 74 static int LwsCallbackProtocolDestroy(lws *wsi, lws_callback_reasons reason, void *user, void *in, size_t len); 75 76 static void OnOpen(EventManager *manager, uint32_t status, const std::string &message); 77 78 static void OnError(EventManager *manager, int32_t code); 79 80 static void OnMessage(EventManager *manager, void *data, size_t length, bool isBinary, bool isFinal); 81 82 static void OnClose(EventManager *manager, lws_close_status closeStatus, const std::string &closeReason); 83 }; 84 } // namespace OHOS::NetStack::Websocket 85 #endif /* COMMUNICATIONNETSTACK_WEBSOCKET_EXEC_H */ 86