| 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_server.h" | 5 #include "net/tools/quic/quic_server.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <features.h> | 8 #include <features.h> |
| 9 #include <netinet/in.h> | 9 #include <netinet/in.h> |
| 10 #include <string.h> | 10 #include <string.h> |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 } | 100 } |
| 101 | 101 |
| 102 QuicEpollClock clock(&epoll_server_); | 102 QuicEpollClock clock(&epoll_server_); |
| 103 | 103 |
| 104 scoped_ptr<CryptoHandshakeMessage> scfg(crypto_config_.AddDefaultConfig( | 104 scoped_ptr<CryptoHandshakeMessage> scfg(crypto_config_.AddDefaultConfig( |
| 105 QuicRandom::GetInstance(), &clock, crypto_config_options_)); | 105 QuicRandom::GetInstance(), &clock, crypto_config_options_)); |
| 106 } | 106 } |
| 107 | 107 |
| 108 QuicServer::~QuicServer() {} | 108 QuicServer::~QuicServer() {} |
| 109 | 109 |
| 110 bool QuicServer::Listen(const IPEndPoint& address) { | 110 bool QuicServer::CreateUDPSocketAndListen(const IPEndPoint& address) { |
| 111 port_ = address.port(); | 111 fd_ = QuicSocketUtils::CreateUDPSocket(address, &overflow_supported_); |
| 112 int address_family = address.GetSockAddrFamily(); | |
| 113 fd_ = socket(address_family, SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP); | |
| 114 if (fd_ < 0) { | 112 if (fd_ < 0) { |
| 115 LOG(ERROR) << "CreateSocket() failed: " << strerror(errno); | 113 LOG(ERROR) << "CreateSocket() failed: " << strerror(errno); |
| 116 return false; | 114 return false; |
| 117 } | 115 } |
| 118 | 116 |
| 119 // Enable the socket option that allows the local address to be | |
| 120 // returned if the socket is bound to more than one address. | |
| 121 int rc = QuicSocketUtils::SetGetAddressInfo(fd_, address_family); | |
| 122 | |
| 123 if (rc < 0) { | |
| 124 LOG(ERROR) << "IP detection not supported" << strerror(errno); | |
| 125 return false; | |
| 126 } | |
| 127 | |
| 128 int get_overflow = 1; | |
| 129 rc = setsockopt(fd_, SOL_SOCKET, SO_RXQ_OVFL, &get_overflow, | |
| 130 sizeof(get_overflow)); | |
| 131 | |
| 132 if (rc < 0) { | |
| 133 DLOG(WARNING) << "Socket overflow detection not supported"; | |
| 134 } else { | |
| 135 overflow_supported_ = true; | |
| 136 } | |
| 137 | |
| 138 // These send and receive buffer sizes are sized for a single connection, | |
| 139 // because the default usage of QuicServer is as a test server with one or | |
| 140 // two clients. Adjust higher for use with many clients. | |
| 141 if (!QuicSocketUtils::SetReceiveBufferSize(fd_, | |
| 142 kDefaultSocketReceiveBuffer)) { | |
| 143 return false; | |
| 144 } | |
| 145 | |
| 146 if (!QuicSocketUtils::SetSendBufferSize(fd_, kDefaultSocketReceiveBuffer)) { | |
| 147 return false; | |
| 148 } | |
| 149 | |
| 150 sockaddr_storage raw_addr; | 117 sockaddr_storage raw_addr; |
| 151 socklen_t raw_addr_len = sizeof(raw_addr); | 118 socklen_t raw_addr_len = sizeof(raw_addr); |
| 152 CHECK(address.ToSockAddr(reinterpret_cast<sockaddr*>(&raw_addr), | 119 CHECK(address.ToSockAddr(reinterpret_cast<sockaddr*>(&raw_addr), |
| 153 &raw_addr_len)); | 120 &raw_addr_len)); |
| 154 rc = | 121 int rc = |
| 155 bind(fd_, reinterpret_cast<const sockaddr*>(&raw_addr), sizeof(raw_addr)); | 122 bind(fd_, reinterpret_cast<const sockaddr*>(&raw_addr), sizeof(raw_addr)); |
| 156 if (rc < 0) { | 123 if (rc < 0) { |
| 157 LOG(ERROR) << "Bind failed: " << strerror(errno); | 124 LOG(ERROR) << "Bind failed: " << strerror(errno); |
| 158 return false; | 125 return false; |
| 159 } | 126 } |
| 160 | 127 |
| 161 DVLOG(1) << "Listening on " << address.ToString(); | 128 DVLOG(1) << "Listening on " << address.ToString(); |
| 162 if (port_ == 0) { | 129 if (port_ == 0) { |
| 163 SockaddrStorage storage; | 130 SockaddrStorage storage; |
| 164 IPEndPoint server_address; | 131 IPEndPoint server_address; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 dispatcher_->OnCanWrite(); | 190 dispatcher_->OnCanWrite(); |
| 224 if (dispatcher_->HasPendingWrites()) { | 191 if (dispatcher_->HasPendingWrites()) { |
| 225 event->out_ready_mask |= EPOLLOUT; | 192 event->out_ready_mask |= EPOLLOUT; |
| 226 } | 193 } |
| 227 } | 194 } |
| 228 if (event->in_events & EPOLLERR) { | 195 if (event->in_events & EPOLLERR) { |
| 229 } | 196 } |
| 230 } | 197 } |
| 231 | 198 |
| 232 } // namespace net | 199 } // namespace net |
| OLD | NEW |