OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 } | 48 } |
49 return owned_socket.release(); | 49 return owned_socket.release(); |
50 } | 50 } |
51 | 51 |
52 AsyncTCPSocketBase::AsyncTCPSocketBase(AsyncSocket* socket, bool listen, | 52 AsyncTCPSocketBase::AsyncTCPSocketBase(AsyncSocket* socket, bool listen, |
53 size_t max_packet_size) | 53 size_t max_packet_size) |
54 : socket_(socket), | 54 : socket_(socket), |
55 listen_(listen), | 55 listen_(listen), |
56 insize_(max_packet_size), | 56 insize_(max_packet_size), |
57 inpos_(0), | 57 inpos_(0), |
58 outsize_(max_packet_size), | 58 max_outsize_(max_packet_size) { |
59 outpos_(0) { | |
60 if (!listen_) { | 59 if (!listen_) { |
61 // Listening sockets don't send/receive data, so they don't need buffers. | 60 // Listening sockets don't send/receive data, so they don't need buffers. |
62 inbuf_.reset(new char[insize_]); | 61 inbuf_.reset(new char[insize_]); |
63 outbuf_.reset(new char[outsize_]); | |
64 } | 62 } |
65 | 63 |
66 RTC_DCHECK(socket_.get() != NULL); | 64 RTC_DCHECK(socket_.get() != NULL); |
67 socket_->SignalConnectEvent.connect( | 65 socket_->SignalConnectEvent.connect( |
68 this, &AsyncTCPSocketBase::OnConnectEvent); | 66 this, &AsyncTCPSocketBase::OnConnectEvent); |
69 socket_->SignalReadEvent.connect(this, &AsyncTCPSocketBase::OnReadEvent); | 67 socket_->SignalReadEvent.connect(this, &AsyncTCPSocketBase::OnReadEvent); |
70 socket_->SignalWriteEvent.connect(this, &AsyncTCPSocketBase::OnWriteEvent); | 68 socket_->SignalWriteEvent.connect(this, &AsyncTCPSocketBase::OnWriteEvent); |
71 socket_->SignalCloseEvent.connect(this, &AsyncTCPSocketBase::OnCloseEvent); | 69 socket_->SignalCloseEvent.connect(this, &AsyncTCPSocketBase::OnCloseEvent); |
72 | 70 |
73 if (listen_) { | 71 if (listen_) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 const SocketAddress& remote_address = GetRemoteAddress(); | 129 const SocketAddress& remote_address = GetRemoteAddress(); |
132 if (addr == remote_address) | 130 if (addr == remote_address) |
133 return Send(pv, cb, options); | 131 return Send(pv, cb, options); |
134 // Remote address may be empty if there is a sudden network change. | 132 // Remote address may be empty if there is a sudden network change. |
135 RTC_DCHECK(remote_address.IsNil()); | 133 RTC_DCHECK(remote_address.IsNil()); |
136 socket_->SetError(ENOTCONN); | 134 socket_->SetError(ENOTCONN); |
137 return -1; | 135 return -1; |
138 } | 136 } |
139 | 137 |
140 int AsyncTCPSocketBase::SendRaw(const void * pv, size_t cb) { | 138 int AsyncTCPSocketBase::SendRaw(const void * pv, size_t cb) { |
141 if (outpos_ + cb > outsize_) { | 139 if (outbuf_.size() + cb > max_outsize_) { |
142 socket_->SetError(EMSGSIZE); | 140 socket_->SetError(EMSGSIZE); |
143 return -1; | 141 return -1; |
144 } | 142 } |
145 | 143 |
146 RTC_DCHECK(outbuf_.get()); | 144 RTC_DCHECK(!listen_); |
147 memcpy(outbuf_.get() + outpos_, pv, cb); | 145 outbuf_.AppendData(static_cast<const uint8_t*>(pv), cb); |
148 outpos_ += cb; | |
149 | 146 |
150 return FlushOutBuffer(); | 147 return FlushOutBuffer(); |
151 } | 148 } |
152 | 149 |
153 int AsyncTCPSocketBase::FlushOutBuffer() { | 150 int AsyncTCPSocketBase::FlushOutBuffer() { |
154 RTC_DCHECK(outbuf_.get()); | 151 RTC_DCHECK(!listen_); |
155 int res = socket_->Send(outbuf_.get(), outpos_); | 152 int res = socket_->Send(outbuf_.data(), outbuf_.size()); |
156 if (res <= 0) { | 153 if (res <= 0) { |
157 return res; | 154 return res; |
158 } | 155 } |
159 if (static_cast<size_t>(res) <= outpos_) { | 156 if (static_cast<size_t>(res) > outbuf_.size()) { |
160 outpos_ -= res; | |
161 } else { | |
162 RTC_NOTREACHED(); | 157 RTC_NOTREACHED(); |
163 return -1; | 158 return -1; |
164 } | 159 } |
165 if (outpos_ > 0) { | 160 size_t new_size = outbuf_.size() - res; |
166 memmove(outbuf_.get(), outbuf_.get() + res, outpos_); | 161 if (new_size > 0) { |
| 162 memmove(outbuf_.data(), outbuf_.data() + res, new_size); |
167 } | 163 } |
| 164 outbuf_.SetSize(new_size); |
168 return res; | 165 return res; |
169 } | 166 } |
170 | 167 |
171 void AsyncTCPSocketBase::AppendToOutBuffer(const void* pv, size_t cb) { | 168 void AsyncTCPSocketBase::AppendToOutBuffer(const void* pv, size_t cb) { |
172 RTC_DCHECK(outpos_ + cb < outsize_); | 169 RTC_DCHECK(outbuf_.size() + cb <= max_outsize_); |
173 RTC_DCHECK(outbuf_.get()); | 170 RTC_DCHECK(!listen_); |
174 memcpy(outbuf_.get() + outpos_, pv, cb); | 171 outbuf_.AppendData(static_cast<const uint8_t*>(pv), cb); |
175 outpos_ += cb; | |
176 } | 172 } |
177 | 173 |
178 void AsyncTCPSocketBase::OnConnectEvent(AsyncSocket* socket) { | 174 void AsyncTCPSocketBase::OnConnectEvent(AsyncSocket* socket) { |
179 SignalConnect(this); | 175 SignalConnect(this); |
180 } | 176 } |
181 | 177 |
182 void AsyncTCPSocketBase::OnReadEvent(AsyncSocket* socket) { | 178 void AsyncTCPSocketBase::OnReadEvent(AsyncSocket* socket) { |
183 RTC_DCHECK(socket_.get() == socket); | 179 RTC_DCHECK(socket_.get() == socket); |
184 | 180 |
185 if (listen_) { | 181 if (listen_) { |
(...skipping 29 matching lines...) Expand all Loading... |
215 LOG(LS_ERROR) << "input buffer overflow"; | 211 LOG(LS_ERROR) << "input buffer overflow"; |
216 RTC_NOTREACHED(); | 212 RTC_NOTREACHED(); |
217 inpos_ = 0; | 213 inpos_ = 0; |
218 } | 214 } |
219 } | 215 } |
220 } | 216 } |
221 | 217 |
222 void AsyncTCPSocketBase::OnWriteEvent(AsyncSocket* socket) { | 218 void AsyncTCPSocketBase::OnWriteEvent(AsyncSocket* socket) { |
223 RTC_DCHECK(socket_.get() == socket); | 219 RTC_DCHECK(socket_.get() == socket); |
224 | 220 |
225 if (outpos_ > 0) { | 221 if (outbuf_.size() > 0) { |
226 FlushOutBuffer(); | 222 FlushOutBuffer(); |
227 } | 223 } |
228 | 224 |
229 if (outpos_ == 0) { | 225 if (outbuf_.size() == 0) { |
230 SignalReadyToSend(this); | 226 SignalReadyToSend(this); |
231 } | 227 } |
232 } | 228 } |
233 | 229 |
234 void AsyncTCPSocketBase::OnCloseEvent(AsyncSocket* socket, int error) { | 230 void AsyncTCPSocketBase::OnCloseEvent(AsyncSocket* socket, int error) { |
235 SignalClose(this, error); | 231 SignalClose(this, error); |
236 } | 232 } |
237 | 233 |
238 // AsyncTCPSocket | 234 // AsyncTCPSocket |
239 // Binds and connects |socket| and creates AsyncTCPSocket for | 235 // Binds and connects |socket| and creates AsyncTCPSocket for |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 memmove(data, data + kPacketLenSize + pkt_len, *len); | 295 memmove(data, data + kPacketLenSize + pkt_len, *len); |
300 } | 296 } |
301 } | 297 } |
302 } | 298 } |
303 | 299 |
304 void AsyncTCPSocket::HandleIncomingConnection(AsyncSocket* socket) { | 300 void AsyncTCPSocket::HandleIncomingConnection(AsyncSocket* socket) { |
305 SignalNewConnection(this, new AsyncTCPSocket(socket, false)); | 301 SignalNewConnection(this, new AsyncTCPSocket(socket, false)); |
306 } | 302 } |
307 | 303 |
308 } // namespace rtc | 304 } // namespace rtc |
OLD | NEW |