blob: ec39a13eca1be3d503ab2bc2e5dd54175afa1913 [file] [log] [blame]
Hugo Benichi7b314e12018-01-15 21:54:00 +09001/*
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 Benichia9e3c5d2018-01-18 10:33:22 +090020#include <chrono>
21#include <condition_variable>
22#include <mutex>
23
24#include <android-base/thread_annotations.h>
Hugo Benichi7b314e12018-01-15 21:54:00 +090025#include "utils/String16.h"
26
27namespace android {
28namespace net {
29
30class DumpWriter;
31
32class TcpSocketMonitor {
33 public:
34 static const String16 DUMP_KEYWORD;
Hugo Benichia9e3c5d2018-01-18 10:33:22 +090035 static const std::chrono::milliseconds kDefaultPollingInterval;
Hugo Benichi7b314e12018-01-15 21:54:00 +090036
Hugo Benichia9e3c5d2018-01-18 10:33:22 +090037 TcpSocketMonitor();
38 ~TcpSocketMonitor();
Hugo Benichi7b314e12018-01-15 21:54:00 +090039
40 void dump(DumpWriter& dw);
Hugo Benichia9e3c5d2018-01-18 10:33:22 +090041 void setPollingInterval(std::chrono::milliseconds duration);
42 void resumePolling();
43 void suspendPolling();
Hugo Benichi7b314e12018-01-15 21:54:00 +090044
45 private:
Hugo Benichia9e3c5d2018-01-18 10:33:22 +090046 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 Benichi7b314e12018-01-15 21:54:00 +090064};
65
66} // namespace net
67} // namespace android
68
69#endif /* TCP_SOCKET_MONITOR_H */