1 /*
2  * Copyright (c) 2021-2023 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_COMMON_CONTEXT_H
17 #define COMMUNICATIONNETSTACK_COMMON_CONTEXT_H
18 
19 #include <cstddef>
20 
21 #include "base_context.h"
22 #include "napi/native_api.h"
23 #include "net_address.h"
24 #include "nocopyable.h"
25 #include "socket_remote_info.h"
26 #include "socket_state_base.h"
27 
28 namespace OHOS::NetStack::Socket {
29 class CommonContext : public BaseContext {
30 public:
31     DISALLOW_COPY_AND_MOVE(CommonContext);
32 
33     CommonContext() = delete;
34 
35     explicit CommonContext(napi_env env, EventManager *manager);
36 
37     void ParseParams(napi_value *params, size_t paramsCount) override;
38 
39     [[nodiscard]] int GetSocketFd() const;
40 
41     [[nodiscard]] int32_t GetErrorCode() const override;
42 
43     [[nodiscard]] std::string GetErrorMessage() const override;
44 
45     SocketStateBase state_;
46 
47     NetAddress address_;
48 
49     NetAddress localAddress_;
50 
51     int32_t errorNumber_ = 0;
52 
53 private:
54     bool CheckParamsType(napi_value *params, size_t paramsCount);
55 };
56 
57 typedef CommonContext GetStateContext;
58 
59 typedef CommonContext GetRemoteAddressContext;
60 
61 typedef CommonContext GetLocalAddressContext;
62 
63 typedef CommonContext GetSocketFdContext;
64 
65 class CloseContext final : public CommonContext {
66 public:
67     DISALLOW_COPY_AND_MOVE(CloseContext);
68 
69     CloseContext() = delete;
70 
71     explicit CloseContext(napi_env env, EventManager *manager);
72 
73     void SetSocketFd(int sock);
74 };
75 } // namespace OHOS::NetStack::Socket
76 
77 #endif /* COMMUNICATIONNETSTACK_COMMON_CONTEXT_H */
78