• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 WRAPPER_LISTENER_H
17 #define WRAPPER_LISTENER_H
18 
19 #include <thread>
20 
21 namespace OHOS {
22 namespace nmd {
23 class WrapperListener {
24 public:
25     using RecvFunc = std::function<void(int32_t)>;
26     WrapperListener(int32_t socketFd, RecvFunc recvFunc);
27     WrapperListener() = delete;
28     ~WrapperListener();
29 
30     /**
31      * Start listen event message
32      * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed
33      */
34     int32_t Start();
35 
36     /**
37      * Stop listen event message
38      * @return NETMANAGER_SUCCESS suceess or NETMANAGER_ERROR failed
39      */
40     int32_t Stop();
41 
42 private:
43     static constexpr int32_t BACK_LOG = 4;
44     static constexpr int32_t PIPE_SHUTDOWN = 0;
45     static constexpr int32_t PIPE_WAKEUP = 1;
46     static constexpr uint32_t PIPE_SIZE = 2;
47     int32_t socket_;
48     std::thread thread_;
49     std::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