| 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 <sys/socket.h> |
| 7 #include <errno.h> | 8 #include <errno.h> |
| 8 #include <sys/socket.h> | |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/stl_util.h" | |
| 12 #include "net/base/ip_endpoint.h" | |
| 13 #include "net/quic/crypto/quic_random.h" | 11 #include "net/quic/crypto/quic_random.h" |
| 14 #include "net/tools/epoll_server/epoll_server.h" | 12 #include "net/tools/epoll_server/epoll_server.h" |
| 15 #include "net/tools/quic/quic_socket_utils.h" | |
| 16 | 13 |
| 17 namespace net { | 14 namespace net { |
| 18 | 15 |
| 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, | 16 QuicEpollConnectionHelper::QuicEpollConnectionHelper(EpollServer* epoll_server, |
| 65 QuicAllocator type) | 17 QuicAllocator type) |
| 66 : epoll_server_(epoll_server), | 18 : clock_(epoll_server), |
| 67 clock_(epoll_server), | |
| 68 random_generator_(QuicRandom::GetInstance()), | 19 random_generator_(QuicRandom::GetInstance()), |
| 69 allocator_type_(type) {} | 20 allocator_type_(type) {} |
| 70 | 21 |
| 71 QuicEpollConnectionHelper::~QuicEpollConnectionHelper() {} | 22 QuicEpollConnectionHelper::~QuicEpollConnectionHelper() {} |
| 72 | 23 |
| 73 const QuicClock* QuicEpollConnectionHelper::GetClock() const { | 24 const QuicClock* QuicEpollConnectionHelper::GetClock() const { |
| 74 return &clock_; | 25 return &clock_; |
| 75 } | 26 } |
| 76 | 27 |
| 77 QuicRandom* QuicEpollConnectionHelper::GetRandomGenerator() { | 28 QuicRandom* QuicEpollConnectionHelper::GetRandomGenerator() { |
| 78 return random_generator_; | 29 return random_generator_; |
| 79 } | 30 } |
| 80 | 31 |
| 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() { | 32 QuicBufferAllocator* QuicEpollConnectionHelper::GetBufferAllocator() { |
| 99 if (allocator_type_ == QuicAllocator::BUFFER_POOL) { | 33 if (allocator_type_ == QuicAllocator::BUFFER_POOL) { |
| 100 return &buffer_allocator_; | 34 return &buffer_allocator_; |
| 101 } else { | 35 } else { |
| 102 DCHECK(allocator_type_ == QuicAllocator::SIMPLE); | 36 DCHECK(allocator_type_ == QuicAllocator::SIMPLE); |
| 103 return &simple_buffer_allocator_; | 37 return &simple_buffer_allocator_; |
| 104 } | 38 } |
| 105 } | 39 } |
| 106 | 40 |
| 107 } // namespace net | 41 } // namespace net |
| OLD | NEW |