Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef TCP_SOCKET_MONITOR_H |
| 18 | #define TCP_SOCKET_MONITOR_H |
| 19 | |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame^] | 20 | #include <chrono> |
| 21 | #include <condition_variable> |
| 22 | #include <mutex> |
| 23 | |
| 24 | #include <android-base/thread_annotations.h> |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 25 | #include "utils/String16.h" |
| 26 | |
| 27 | namespace android { |
| 28 | namespace net { |
| 29 | |
| 30 | class DumpWriter; |
| 31 | |
| 32 | class TcpSocketMonitor { |
| 33 | public: |
| 34 | static const String16 DUMP_KEYWORD; |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame^] | 35 | static const std::chrono::milliseconds kDefaultPollingInterval; |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 36 | |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame^] | 37 | TcpSocketMonitor(); |
| 38 | ~TcpSocketMonitor(); |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 39 | |
| 40 | void dump(DumpWriter& dw); |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame^] | 41 | void setPollingInterval(std::chrono::milliseconds duration); |
| 42 | void resumePolling(); |
| 43 | void suspendPolling(); |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 44 | |
| 45 | private: |
Hugo Benichi | a9e3c5d | 2018-01-18 10:33:22 +0900 | [diff] [blame^] | 46 | void poll(); |
| 47 | void waitForNextPoll(); |
| 48 | bool isRunning(); |
| 49 | |
| 50 | // Lock guarding all reads and writes to member variables. |
| 51 | std::mutex mLock; |
| 52 | // Used by the polling thread for sleeping between poll operations. |
| 53 | std::condition_variable mCv; |
| 54 | // The duration of a sleep between polls. Can be updated by the instance owner for dynamically |
| 55 | // adjusting the polling rate. |
| 56 | std::chrono::milliseconds mNextSleepDurationMs GUARDED_BY(mLock); |
| 57 | // The time of the last successful poll operation. |
| 58 | std::chrono::time_point<std::chrono::steady_clock> mLastPoll GUARDED_BY(mLock); |
| 59 | // True if the polling thread should sleep until notified. |
| 60 | bool mIsSuspended GUARDED_BY(mLock); |
| 61 | // True while the polling thread should poll. |
| 62 | bool mIsRunning GUARDED_BY(mLock); |
| 63 | std::thread mPollingThread; |
Hugo Benichi | 7b314e1 | 2018-01-15 21:54:00 +0900 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | } // namespace net |
| 67 | } // namespace android |
| 68 | |
| 69 | #endif /* TCP_SOCKET_MONITOR_H */ |