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/base/stream_listen_socket.h" | 5 #include "net/base/stream_listen_socket.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 // winsock2.h must be included first in order to ensure it is included before | 8 // winsock2.h must be included first in order to ensure it is included before |
9 // windows.h. | 9 // windows.h. |
10 #include <winsock2.h> | 10 #include <winsock2.h> |
11 #elif defined(OS_POSIX) | 11 #elif defined(OS_POSIX) |
12 #include <errno.h> | 12 #include <errno.h> |
13 #include <sys/types.h> | 13 #include <sys/types.h> |
14 #include <sys/socket.h> | 14 #include <sys/socket.h> |
15 #include <netinet/in.h> | 15 #include <netinet/in.h> |
16 #include <arpa/inet.h> | 16 #include <arpa/inet.h> |
17 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
18 #endif | 18 #endif |
19 | 19 |
20 #include "base/eintr_wrapper.h" | 20 #include "base/eintr_wrapper.h" |
21 #include "base/logging.h" | 21 #include "base/logging.h" |
22 #include "base/memory/ref_counted.h" | 22 #include "base/memory/ref_counted.h" |
23 #include "base/memory/scoped_ptr.h" | 23 #include "base/memory/scoped_ptr.h" |
24 #include "base/sys_byteorder.h" | 24 #include "base/sys_byteorder.h" |
25 #include "base/threading/platform_thread.h" | 25 #include "base/threading/platform_thread.h" |
26 #include "build/build_config.h" | 26 #include "build/build_config.h" |
| 27 #include "net/base/ip_endpoint.h" |
| 28 #include "net/base/net_errors.h" |
27 #include "net/base/net_util.h" | 29 #include "net/base/net_util.h" |
28 | 30 |
29 using std::string; | 31 using std::string; |
30 | 32 |
31 #if defined(OS_WIN) | 33 #if defined(OS_WIN) |
32 typedef int socklen_t; | 34 typedef int socklen_t; |
33 #include "net/base/winsock_init.h" | 35 #include "net/base/winsock_init.h" |
34 #endif // defined(OS_WIN) | 36 #endif // defined(OS_WIN) |
35 | 37 |
36 namespace net { | 38 namespace net { |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 bool append_linefeed) { | 110 bool append_linefeed) { |
109 SendInternal(bytes, len); | 111 SendInternal(bytes, len); |
110 if (append_linefeed) | 112 if (append_linefeed) |
111 SendInternal("\r\n", 2); | 113 SendInternal("\r\n", 2); |
112 } | 114 } |
113 | 115 |
114 void StreamListenSocket::Send(const string& str, bool append_linefeed) { | 116 void StreamListenSocket::Send(const string& str, bool append_linefeed) { |
115 Send(str.data(), static_cast<int>(str.length()), append_linefeed); | 117 Send(str.data(), static_cast<int>(str.length()), append_linefeed); |
116 } | 118 } |
117 | 119 |
| 120 int StreamListenSocket::GetLocalAddress(IPEndPoint* address) { |
| 121 SockaddrStorage storage; |
| 122 if (getsockname(socket_, storage.addr, &storage.addr_len)) { |
| 123 #if defined(OS_WIN) |
| 124 int err = WSAGetLastError(); |
| 125 #else |
| 126 int err = errno; |
| 127 #endif |
| 128 return MapSystemError(err); |
| 129 } |
| 130 if (!address->FromSockAddr(storage.addr, storage.addr_len)) |
| 131 return ERR_FAILED; |
| 132 return OK; |
| 133 } |
| 134 |
118 SocketDescriptor StreamListenSocket::AcceptSocket() { | 135 SocketDescriptor StreamListenSocket::AcceptSocket() { |
119 SocketDescriptor conn = HANDLE_EINTR(accept(socket_, NULL, NULL)); | 136 SocketDescriptor conn = HANDLE_EINTR(accept(socket_, NULL, NULL)); |
120 if (conn == kInvalidSocket) | 137 if (conn == kInvalidSocket) |
121 LOG(ERROR) << "Error accepting connection."; | 138 LOG(ERROR) << "Error accepting connection."; |
122 else | 139 else |
123 SetNonBlocking(conn); | 140 SetNonBlocking(conn); |
124 return conn; | 141 return conn; |
125 } | 142 } |
126 | 143 |
127 void StreamListenSocket::SendInternal(const char* bytes, int len) { | 144 void StreamListenSocket::SendInternal(const char* bytes, int len) { |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 } | 379 } |
363 | 380 |
364 if (!send_buffers_.empty()) { | 381 if (!send_buffers_.empty()) { |
365 DCHECK(!send_timer_.IsRunning()); | 382 DCHECK(!send_timer_.IsRunning()); |
366 send_timer_.Start(FROM_HERE, send_backoff_.GetTimeUntilRelease(), | 383 send_timer_.Start(FROM_HERE, send_backoff_.GetTimeUntilRelease(), |
367 this, &StreamListenSocket::SendData); | 384 this, &StreamListenSocket::SendData); |
368 } | 385 } |
369 } | 386 } |
370 | 387 |
371 } // namespace net | 388 } // namespace net |
OLD | NEW |