| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/base/tcp_client_socket_libevent.h" | 5 #include "net/base/tcp_client_socket_libevent.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <netdb.h> | 9 #include <netdb.h> |
| 10 #include <sys/socket.h> | 10 #include <sys/socket.h> |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 char c; | 165 char c; |
| 166 int rv = recv(socket_, &c, 1, MSG_PEEK); | 166 int rv = recv(socket_, &c, 1, MSG_PEEK); |
| 167 if (rv >= 0) | 167 if (rv >= 0) |
| 168 return false; | 168 return false; |
| 169 if (errno != EAGAIN && errno != EWOULDBLOCK) | 169 if (errno != EAGAIN && errno != EWOULDBLOCK) |
| 170 return false; | 170 return false; |
| 171 | 171 |
| 172 return true; | 172 return true; |
| 173 } | 173 } |
| 174 | 174 |
| 175 int TCPClientSocketLibevent::Read(char* buf, | 175 int TCPClientSocketLibevent::Read(IOBuffer* buf, |
| 176 int buf_len, | 176 int buf_len, |
| 177 CompletionCallback* callback) { | 177 CompletionCallback* callback) { |
| 178 DCHECK_NE(kInvalidSocket, socket_); | 178 DCHECK_NE(kInvalidSocket, socket_); |
| 179 DCHECK(!waiting_connect_); | 179 DCHECK(!waiting_connect_); |
| 180 DCHECK(!read_callback_); | 180 DCHECK(!read_callback_); |
| 181 // Synchronous operation not supported | 181 // Synchronous operation not supported |
| 182 DCHECK(callback); | 182 DCHECK(callback); |
| 183 DCHECK_GT(buf_len, 0); | 183 DCHECK_GT(buf_len, 0); |
| 184 | 184 |
| 185 TRACE_EVENT_BEGIN("socket.read", this, ""); | 185 TRACE_EVENT_BEGIN("socket.read", this, ""); |
| 186 int nread = read(socket_, buf, buf_len); | 186 int nread = read(socket_, buf->data(), buf_len); |
| 187 if (nread >= 0) { | 187 if (nread >= 0) { |
| 188 TRACE_EVENT_END("socket.read", this, StringPrintf("%d bytes", nread)); | 188 TRACE_EVENT_END("socket.read", this, StringPrintf("%d bytes", nread)); |
| 189 return nread; | 189 return nread; |
| 190 } | 190 } |
| 191 if (errno != EAGAIN && errno != EWOULDBLOCK) { | 191 if (errno != EAGAIN && errno != EWOULDBLOCK) { |
| 192 DLOG(INFO) << "read failed, errno " << errno; | 192 DLOG(INFO) << "read failed, errno " << errno; |
| 193 return MapPosixError(errno); | 193 return MapPosixError(errno); |
| 194 } | 194 } |
| 195 | 195 |
| 196 if (!MessageLoopForIO::current()->WatchFileDescriptor( | 196 if (!MessageLoopForIO::current()->WatchFileDescriptor( |
| 197 socket_, true, MessageLoopForIO::WATCH_READ, | 197 socket_, true, MessageLoopForIO::WATCH_READ, |
| 198 &socket_watcher_, this)) { | 198 &socket_watcher_, this)) { |
| 199 DLOG(INFO) << "WatchFileDescriptor failed on read, errno " << errno; | 199 DLOG(INFO) << "WatchFileDescriptor failed on read, errno " << errno; |
| 200 return MapPosixError(errno); | 200 return MapPosixError(errno); |
| 201 } | 201 } |
| 202 | 202 |
| 203 read_buf_ = buf; | 203 read_buf_ = buf; |
| 204 read_buf_len_ = buf_len; | 204 read_buf_len_ = buf_len; |
| 205 read_callback_ = callback; | 205 read_callback_ = callback; |
| 206 return ERR_IO_PENDING; | 206 return ERR_IO_PENDING; |
| 207 } | 207 } |
| 208 | 208 |
| 209 int TCPClientSocketLibevent::Write(const char* buf, | 209 int TCPClientSocketLibevent::Write(IOBuffer* buf, |
| 210 int buf_len, | 210 int buf_len, |
| 211 CompletionCallback* callback) { | 211 CompletionCallback* callback) { |
| 212 DCHECK_NE(kInvalidSocket, socket_); | 212 DCHECK_NE(kInvalidSocket, socket_); |
| 213 DCHECK(!waiting_connect_); | 213 DCHECK(!waiting_connect_); |
| 214 DCHECK(!write_callback_); | 214 DCHECK(!write_callback_); |
| 215 // Synchronous operation not supported | 215 // Synchronous operation not supported |
| 216 DCHECK(callback); | 216 DCHECK(callback); |
| 217 DCHECK_GT(buf_len, 0); | 217 DCHECK_GT(buf_len, 0); |
| 218 | 218 |
| 219 TRACE_EVENT_BEGIN("socket.write", this, ""); | 219 TRACE_EVENT_BEGIN("socket.write", this, ""); |
| 220 int nwrite = write(socket_, buf, buf_len); | 220 int nwrite = write(socket_, buf->data(), buf_len); |
| 221 if (nwrite >= 0) { | 221 if (nwrite >= 0) { |
| 222 TRACE_EVENT_END("socket.write", this, StringPrintf("%d bytes", nwrite)); | 222 TRACE_EVENT_END("socket.write", this, StringPrintf("%d bytes", nwrite)); |
| 223 return nwrite; | 223 return nwrite; |
| 224 } | 224 } |
| 225 if (errno != EAGAIN && errno != EWOULDBLOCK) | 225 if (errno != EAGAIN && errno != EWOULDBLOCK) |
| 226 return MapPosixError(errno); | 226 return MapPosixError(errno); |
| 227 | 227 |
| 228 if (!MessageLoopForIO::current()->WatchFileDescriptor( | 228 if (!MessageLoopForIO::current()->WatchFileDescriptor( |
| 229 socket_, true, MessageLoopForIO::WATCH_WRITE, | 229 socket_, true, MessageLoopForIO::WATCH_WRITE, |
| 230 &socket_watcher_, this)) { | 230 &socket_watcher_, this)) { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 waiting_connect_ = false; | 302 waiting_connect_ = false; |
| 303 } | 303 } |
| 304 | 304 |
| 305 if (result != ERR_IO_PENDING) { | 305 if (result != ERR_IO_PENDING) { |
| 306 DoReadCallback(result); | 306 DoReadCallback(result); |
| 307 } | 307 } |
| 308 } | 308 } |
| 309 | 309 |
| 310 void TCPClientSocketLibevent::DidCompleteRead() { | 310 void TCPClientSocketLibevent::DidCompleteRead() { |
| 311 int bytes_transferred; | 311 int bytes_transferred; |
| 312 bytes_transferred = read(socket_, read_buf_, read_buf_len_); | 312 bytes_transferred = read(socket_, read_buf_->data(), read_buf_len_); |
| 313 | 313 |
| 314 int result; | 314 int result; |
| 315 if (bytes_transferred >= 0) { | 315 if (bytes_transferred >= 0) { |
| 316 TRACE_EVENT_END("socket.read", this, | 316 TRACE_EVENT_END("socket.read", this, |
| 317 StringPrintf("%d bytes", bytes_transferred)); | 317 StringPrintf("%d bytes", bytes_transferred)); |
| 318 result = bytes_transferred; | 318 result = bytes_transferred; |
| 319 } else { | 319 } else { |
| 320 result = MapPosixError(errno); | 320 result = MapPosixError(errno); |
| 321 } | 321 } |
| 322 | 322 |
| 323 if (result != ERR_IO_PENDING) { | 323 if (result != ERR_IO_PENDING) { |
| 324 read_buf_ = NULL; | 324 read_buf_ = NULL; |
| 325 read_buf_len_ = 0; | 325 read_buf_len_ = 0; |
| 326 socket_watcher_.StopWatchingFileDescriptor(); | 326 socket_watcher_.StopWatchingFileDescriptor(); |
| 327 DoReadCallback(result); | 327 DoReadCallback(result); |
| 328 } | 328 } |
| 329 } | 329 } |
| 330 | 330 |
| 331 void TCPClientSocketLibevent::DidCompleteWrite() { | 331 void TCPClientSocketLibevent::DidCompleteWrite() { |
| 332 int bytes_transferred; | 332 int bytes_transferred; |
| 333 bytes_transferred = write(socket_, write_buf_, write_buf_len_); | 333 bytes_transferred = write(socket_, write_buf_->data(), write_buf_len_); |
| 334 | 334 |
| 335 int result; | 335 int result; |
| 336 if (bytes_transferred >= 0) { | 336 if (bytes_transferred >= 0) { |
| 337 result = bytes_transferred; | 337 result = bytes_transferred; |
| 338 TRACE_EVENT_END("socket.write", this, | 338 TRACE_EVENT_END("socket.write", this, |
| 339 StringPrintf("%d bytes", bytes_transferred)); | 339 StringPrintf("%d bytes", bytes_transferred)); |
| 340 } else { | 340 } else { |
| 341 result = MapPosixError(errno); | 341 result = MapPosixError(errno); |
| 342 } | 342 } |
| 343 | 343 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 364 DidCompleteWrite(); | 364 DidCompleteWrite(); |
| 365 } | 365 } |
| 366 } | 366 } |
| 367 | 367 |
| 368 int TCPClientSocketLibevent::GetPeerName(struct sockaddr *name, | 368 int TCPClientSocketLibevent::GetPeerName(struct sockaddr *name, |
| 369 socklen_t *namelen) { | 369 socklen_t *namelen) { |
| 370 return ::getpeername(socket_, name, namelen); | 370 return ::getpeername(socket_, name, namelen); |
| 371 } | 371 } |
| 372 | 372 |
| 373 } // namespace net | 373 } // namespace net |
| OLD | NEW |