OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/tools/quic/quic_epoll_connection_helper.h" | 5 #include "net/tools/quic/quic_epoll_connection_helper.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <sys/socket.h> | 8 #include <sys/socket.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "net/base/ip_endpoint.h" | 12 #include "net/base/ip_endpoint.h" |
13 #include "net/quic/crypto/quic_random.h" | 13 #include "net/quic/crypto/quic_random.h" |
14 #include "net/tools/epoll_server/epoll_server.h" | 14 #include "net/tools/epoll_server/epoll_server.h" |
15 #include "net/tools/quic/quic_socket_utils.h" | 15 #include "net/tools/quic/quic_socket_utils.h" |
16 | 16 |
17 namespace net { | 17 namespace net { |
18 | 18 |
19 namespace { | |
20 | |
21 class QuicEpollAlarm : public QuicAlarm { | |
22 public: | |
23 QuicEpollAlarm(EpollServer* epoll_server, | |
24 QuicArenaScopedPtr<Delegate> delegate) | |
25 : QuicAlarm(std::move(delegate)), | |
26 epoll_server_(epoll_server), | |
27 epoll_alarm_impl_(this) {} | |
28 | |
29 protected: | |
30 void SetImpl() override { | |
31 DCHECK(deadline().IsInitialized()); | |
32 epoll_server_->RegisterAlarm( | |
33 deadline().Subtract(QuicTime::Zero()).ToMicroseconds(), | |
34 &epoll_alarm_impl_); | |
35 } | |
36 | |
37 void CancelImpl() override { | |
38 DCHECK(!deadline().IsInitialized()); | |
39 epoll_alarm_impl_.UnregisterIfRegistered(); | |
40 } | |
41 | |
42 private: | |
43 class EpollAlarmImpl : public EpollAlarm { | |
44 public: | |
45 explicit EpollAlarmImpl(QuicEpollAlarm* alarm) : alarm_(alarm) {} | |
46 | |
47 int64_t OnAlarm() override { | |
48 EpollAlarm::OnAlarm(); | |
49 alarm_->Fire(); | |
50 // Fire will take care of registering the alarm, if needed. | |
51 return 0; | |
52 } | |
53 | |
54 private: | |
55 QuicEpollAlarm* alarm_; | |
56 }; | |
57 | |
58 EpollServer* epoll_server_; | |
59 EpollAlarmImpl epoll_alarm_impl_; | |
60 }; | |
61 | |
62 } // namespace | |
63 | |
64 QuicEpollConnectionHelper::QuicEpollConnectionHelper(EpollServer* epoll_server, | 19 QuicEpollConnectionHelper::QuicEpollConnectionHelper(EpollServer* epoll_server, |
65 QuicAllocator type) | 20 QuicAllocator type) |
66 : epoll_server_(epoll_server), | 21 : clock_(epoll_server), |
67 clock_(epoll_server), | |
68 random_generator_(QuicRandom::GetInstance()), | 22 random_generator_(QuicRandom::GetInstance()), |
69 allocator_type_(type) {} | 23 allocator_type_(type) {} |
70 | 24 |
71 QuicEpollConnectionHelper::~QuicEpollConnectionHelper() {} | 25 QuicEpollConnectionHelper::~QuicEpollConnectionHelper() {} |
72 | 26 |
73 const QuicClock* QuicEpollConnectionHelper::GetClock() const { | 27 const QuicClock* QuicEpollConnectionHelper::GetClock() const { |
74 return &clock_; | 28 return &clock_; |
75 } | 29 } |
76 | 30 |
77 QuicRandom* QuicEpollConnectionHelper::GetRandomGenerator() { | 31 QuicRandom* QuicEpollConnectionHelper::GetRandomGenerator() { |
78 return random_generator_; | 32 return random_generator_; |
79 } | 33 } |
80 | 34 |
81 QuicAlarm* QuicEpollConnectionHelper::CreateAlarm( | |
82 QuicAlarm::Delegate* delegate) { | |
83 return new QuicEpollAlarm(epoll_server_, | |
84 QuicArenaScopedPtr<QuicAlarm::Delegate>(delegate)); | |
85 } | |
86 | |
87 QuicArenaScopedPtr<QuicAlarm> QuicEpollConnectionHelper::CreateAlarm( | |
88 QuicArenaScopedPtr<QuicAlarm::Delegate> delegate, | |
89 QuicConnectionArena* arena) { | |
90 if (arena != nullptr) { | |
91 return arena->New<QuicEpollAlarm>(epoll_server_, std::move(delegate)); | |
92 } else { | |
93 return QuicArenaScopedPtr<QuicAlarm>( | |
94 new QuicEpollAlarm(epoll_server_, std::move(delegate))); | |
95 } | |
96 } | |
97 | |
98 QuicBufferAllocator* QuicEpollConnectionHelper::GetBufferAllocator() { | 35 QuicBufferAllocator* QuicEpollConnectionHelper::GetBufferAllocator() { |
99 if (allocator_type_ == QuicAllocator::BUFFER_POOL) { | 36 if (allocator_type_ == QuicAllocator::BUFFER_POOL) { |
100 return &buffer_allocator_; | 37 return &buffer_allocator_; |
101 } else { | 38 } else { |
102 DCHECK(allocator_type_ == QuicAllocator::SIMPLE); | 39 DCHECK(allocator_type_ == QuicAllocator::SIMPLE); |
103 return &simple_buffer_allocator_; | 40 return &simple_buffer_allocator_; |
104 } | 41 } |
105 } | 42 } |
106 | 43 |
107 } // namespace net | 44 } // namespace net |
OLD | NEW |