OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/quic/quic_server.h" | 5 #include "net/quic/quic_server.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "net/base/ip_endpoint.h" | 9 #include "net/base/ip_endpoint.h" |
10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
11 #include "net/quic/crypto/crypto_handshake.h" | 11 #include "net/quic/crypto/crypto_handshake.h" |
12 #include "net/quic/crypto/quic_random.h" | 12 #include "net/quic/crypto/quic_random.h" |
13 #include "net/quic/quic_crypto_stream.h" | 13 #include "net/quic/quic_crypto_stream.h" |
14 #include "net/quic/quic_data_reader.h" | 14 #include "net/quic/quic_data_reader.h" |
15 #include "net/quic/quic_dispatcher.h" | 15 #include "net/quic/quic_per_connection_packet_writer.h" |
16 #include "net/quic/quic_protocol.h" | 16 #include "net/quic/quic_protocol.h" |
17 #include "net/quic/quic_server_packet_writer.h" | 17 #include "net/quic/quic_server_packet_writer.h" |
| 18 #include "net/tools/quic/quic_dispatcher.h" |
18 #include "net/udp/udp_server_socket.h" | 19 #include "net/udp/udp_server_socket.h" |
19 | 20 |
20 namespace net { | 21 namespace net { |
21 | 22 |
| 23 using tools::QuicDispatcher; |
| 24 |
22 namespace { | 25 namespace { |
23 | 26 |
24 const char kSourceAddressTokenSecret[] = "secret"; | 27 const char kSourceAddressTokenSecret[] = "secret"; |
25 | 28 |
26 // Allocate some extra space so we can send an error if the client goes over | 29 // Allocate some extra space so we can send an error if the client goes over |
27 // the limit. | 30 // the limit. |
28 const int kReadBufferSize = 2 * kMaxPacketSize; | 31 const int kReadBufferSize = 2 * kMaxPacketSize; |
29 | 32 |
| 33 // A packet writer factory which wraps a shared QuicServerPacketWriter |
| 34 // inside of a QuicPerConnectionPacketWriter. Instead of checking that |
| 35 // the shared_writer is the expected writer, this could instead cast |
| 36 // from QuicPacketWriter to QuicServerPacketWriter. |
| 37 class CustomPacketWriterFactory : public QuicDispatcher::PacketWriterFactory { |
| 38 public: |
| 39 ~CustomPacketWriterFactory() override {} |
| 40 |
| 41 QuicPacketWriter* Create(QuicPacketWriter* writer, |
| 42 QuicConnection* connection) override { |
| 43 if (writer == nullptr) { |
| 44 LOG(DFATAL) << "shared_writer not initialized"; |
| 45 return nullptr; |
| 46 } |
| 47 if (writer != shared_writer_) { |
| 48 LOG(DFATAL) << "writer mismatch"; |
| 49 return nullptr; |
| 50 } |
| 51 return new QuicPerConnectionPacketWriter(shared_writer_, connection); |
| 52 } |
| 53 |
| 54 void set_shared_writer(QuicServerPacketWriter* shared_writer) { |
| 55 shared_writer_ = shared_writer; |
| 56 } |
| 57 |
| 58 private: |
| 59 QuicServerPacketWriter* shared_writer_; // Not owned. |
| 60 }; |
| 61 |
30 } // namespace | 62 } // namespace |
31 | 63 |
32 QuicServer::QuicServer(const QuicConfig& config, | 64 QuicServer::QuicServer(const QuicConfig& config, |
33 const QuicVersionVector& supported_versions) | 65 const QuicVersionVector& supported_versions) |
34 : helper_(base::MessageLoop::current()->message_loop_proxy().get(), | 66 : helper_(base::MessageLoop::current()->message_loop_proxy().get(), |
35 &clock_, | 67 &clock_, |
36 QuicRandom::GetInstance()), | 68 QuicRandom::GetInstance()), |
37 config_(config), | 69 config_(config), |
38 crypto_config_(kSourceAddressTokenSecret, QuicRandom::GetInstance()), | 70 crypto_config_(kSourceAddressTokenSecret, QuicRandom::GetInstance()), |
39 supported_versions_(supported_versions), | 71 supported_versions_(supported_versions), |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 rc = socket->GetLocalAddress(&server_address_); | 136 rc = socket->GetLocalAddress(&server_address_); |
105 if (rc < 0) { | 137 if (rc < 0) { |
106 LOG(ERROR) << "GetLocalAddress() failed: " << ErrorToString(rc); | 138 LOG(ERROR) << "GetLocalAddress() failed: " << ErrorToString(rc); |
107 return rc; | 139 return rc; |
108 } | 140 } |
109 | 141 |
110 DVLOG(1) << "Listening on " << server_address_.ToString(); | 142 DVLOG(1) << "Listening on " << server_address_.ToString(); |
111 | 143 |
112 socket_.swap(socket); | 144 socket_.swap(socket); |
113 | 145 |
| 146 CustomPacketWriterFactory* factory = new CustomPacketWriterFactory(); |
114 dispatcher_.reset( | 147 dispatcher_.reset( |
115 new QuicDispatcher(config_, | 148 new QuicDispatcher(config_, |
116 crypto_config_, | 149 crypto_config_, |
117 supported_versions_, | 150 supported_versions_, |
118 new QuicDispatcher::DefaultPacketWriterFactory(), | 151 factory, |
119 &helper_)); | 152 &helper_)); |
120 QuicServerPacketWriter* writer = new QuicServerPacketWriter( | 153 QuicServerPacketWriter* writer = new QuicServerPacketWriter( |
121 socket_.get(), | 154 socket_.get(), |
122 dispatcher_.get()); | 155 dispatcher_.get()); |
123 dispatcher_->Initialize(writer); | 156 factory->set_shared_writer(writer); |
| 157 dispatcher_->InitializeWithWriter(writer); |
124 | 158 |
125 StartReading(); | 159 StartReading(); |
126 | 160 |
127 return OK; | 161 return OK; |
128 } | 162 } |
129 | 163 |
130 void QuicServer::Shutdown() { | 164 void QuicServer::Shutdown() { |
131 // Before we shut down the epoll server, give all active sessions a chance to | 165 // Before we shut down the epoll server, give all active sessions a chance to |
132 // notify clients that they're closing. | 166 // notify clients that they're closing. |
133 dispatcher_->Shutdown(); | 167 dispatcher_->Shutdown(); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 return; | 212 return; |
179 } | 213 } |
180 | 214 |
181 QuicEncryptedPacket packet(read_buffer_->data(), result, false); | 215 QuicEncryptedPacket packet(read_buffer_->data(), result, false); |
182 dispatcher_->ProcessPacket(server_address_, client_address_, packet); | 216 dispatcher_->ProcessPacket(server_address_, client_address_, packet); |
183 | 217 |
184 StartReading(); | 218 StartReading(); |
185 } | 219 } |
186 | 220 |
187 } // namespace net | 221 } // namespace net |
OLD | NEW |