1 /*
2  * Copyright (c) 2022-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 WRAPPER_LISTENER_H
17 #define WRAPPER_LISTENER_H
18 
19 #include "ffrt.h"
20 #define NET_SYMBOL_VISIBLE __attribute__ ((visibility("default")))
21 
22 namespace OHOS {
23 namespace nmd {
24 class NET_SYMBOL_VISIBLE WrapperListener {
25 public:
26     using RecvFunc = std::function<void(int32_t)>;
27     WrapperListener(int32_t socketFd, RecvFunc recvFunc);
28     WrapperListener() = delete;
29     ~WrapperListener();
30 
31     /**
32      * Start listen event message
33      * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed
34      */
35     int32_t Start();
36 
37     /**
38      * Stop listen event message
39      * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed
40      */
41     int32_t Stop();
42 
43 private:
44     static constexpr int32_t BACK_LOG = 4;
45     static constexpr int32_t PIPE_SHUTDOWN = 0;
46     static constexpr int32_t PIPE_WAKEUP = 1;
47     static constexpr uint32_t PIPE_SIZE = 2;
48     int32_t socket_;
49     ffrt::mutex clientsLock_;
50     int32_t pipe_[PIPE_SIZE] = {0};
51     RecvFunc startReceiveFunc_;
52 
53     void Listen();
54     static void ListenThread(WrapperListener *listener);
55 };
56 } // namespace nmd
57 } // namespace OHOS
58 
59 #endif // WRAPPER_LISTENER_H
60