1 /*
2  * Copyright (c) 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_TCP_SERVER_COMMON_CONTEXT_H
17 #define COMMUNICATIONNETSTACK_TCP_SERVER_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 TcpServerCommonContext : public BaseContext {
30 public:
31     DISALLOW_COPY_AND_MOVE(TcpServerCommonContext);
32 
33     TcpServerCommonContext() = delete;
34 
35     explicit TcpServerCommonContext(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     NetAddress localAddress_;
49     int32_t errorNumber_ = 0;
50     int32_t clientId_ = 0;
51 
52 private:
53     bool CheckParamsType(napi_value *params, size_t paramsCount);
54 };
55 
56 typedef TcpServerCommonContext TcpServerGetStateContext;
57 typedef TcpServerCommonContext TcpServerGetLocalAddressContext;
58 typedef TcpServerCommonContext TcpConnectionGetLocalAddressContext;
59 typedef TcpServerCommonContext TcpServerGetRemoteAddressContext;
60 typedef TcpServerCommonContext TcpServerCloseContext;
61 } // namespace OHOS::NetStack::Socket
62 
63 #endif /* COMMUNICATIONNETSTACK_TCP_SERVER_COMMON_CONTEXT_H */
64