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 // TCP/IP server that handles IO asynchronously in the specified MessageLoop. | |
6 // These objects are NOT thread safe. They use WSAEVENT handles to monitor | |
7 // activity in a given MessageLoop. This means that callbacks will | |
8 // happen in that loop's thread always and that all other methods (including | |
9 // constructors and destructors) should also be called from the same thread. | |
10 | |
11 #ifndef NET_BASE_TCP_LISTEN_SOCKET_H_ | 5 #ifndef NET_BASE_TCP_LISTEN_SOCKET_H_ |
12 #define NET_BASE_TCP_LISTEN_SOCKET_H_ | 6 #define NET_BASE_TCP_LISTEN_SOCKET_H_ |
13 #pragma once | 7 #pragma once |
14 | 8 |
15 #include <list> | |
16 | |
17 #include "build/build_config.h" | |
18 | |
19 #if defined(OS_WIN) | |
20 #include <winsock2.h> | |
21 #endif | |
22 #include <string> | 9 #include <string> |
23 #if defined(OS_WIN) | |
24 #include "base/win/object_watcher.h" | |
25 #elif defined(OS_POSIX) | |
26 #include "base/message_loop.h" | |
27 #endif | |
28 | 10 |
29 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
30 #include "base/compiler_specific.h" | |
31 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
32 #include "base/timer.h" | |
33 #include "net/base/backoff_entry.h" | |
34 #include "net/base/io_buffer.h" | |
35 #include "net/base/listen_socket.h" | |
36 #include "net/base/net_export.h" | 13 #include "net/base/net_export.h" |
37 | 14 #include "net/base/stream_listen_socket.h" |
38 #if defined(OS_POSIX) | |
39 typedef int SOCKET; | |
40 #endif | |
41 | 15 |
42 namespace net { | 16 namespace net { |
43 | 17 |
44 // Implements a TCP socket interface. Note that this is ref counted. | 18 // Implements a TCP socket. Note that this is ref counted. |
45 class NET_EXPORT TCPListenSocket : public ListenSocket, | 19 class NET_EXPORT TCPListenSocket : public StreamListenSocket { |
46 #if defined(OS_WIN) | |
47 public base::win::ObjectWatcher::Delegate { | |
48 #elif defined(OS_POSIX) | |
49 public MessageLoopForIO::Watcher { | |
50 #endif | |
51 public: | 20 public: |
52 // Listen on port for the specified IP address. Use 127.0.0.1 to only | 21 // Listen on port for the specified IP address. Use 127.0.0.1 to only |
53 // accept local connections. | 22 // accept local connections. |
54 static TCPListenSocket* CreateAndListen(const std::string& ip, int port, | 23 static scoped_refptr<TCPListenSocket> CreateAndListen( |
55 ListenSocketDelegate* del); | 24 const std::string& ip, int port, StreamListenSocket::Delegate* del); |
56 | 25 |
57 // NOTE: This is for unit test use only! | 26 virtual ~TCPListenSocket(); |
58 // Pause/Resume calling Read(). Note that ResumeReads() will also call | |
59 // Read() if there is anything to read. | |
60 void PauseReads(); | |
61 void ResumeReads(); | |
62 | 27 |
63 protected: | 28 protected: |
64 enum WaitState { | 29 TCPListenSocket(SOCKET s, StreamListenSocket::Delegate* del); |
65 NOT_WAITING = 0, | |
66 WAITING_ACCEPT = 1, | |
67 WAITING_READ = 2 | |
68 }; | |
69 | 30 |
70 static const SOCKET kInvalidSocket; | 31 static SOCKET CreateAndBind(const std::string& ip, int port); |
71 static const int kSocketError; | |
72 | 32 |
73 TCPListenSocket(SOCKET s, ListenSocketDelegate* del); | 33 // Implements StreamListenSocket::Accept. |
74 virtual ~TCPListenSocket(); | 34 virtual void Accept() OVERRIDE; |
75 static SOCKET CreateAndBind(const std::string& ip, int port); | |
76 // if valid, returned SOCKET is non-blocking | |
77 static SOCKET Accept(SOCKET s); | |
78 | |
79 // Implements ListenSocket::SendInternal. | |
80 virtual void SendInternal(const char* bytes, int len) OVERRIDE; | |
81 | |
82 virtual void Listen(); | |
83 virtual void Accept(); | |
84 virtual void Read(); | |
85 virtual void Close(); | |
86 virtual void CloseSocket(SOCKET s); | |
87 | |
88 // Pass any value in case of Windows, because in Windows | |
89 // we are not using state. | |
90 void WatchSocket(WaitState state); | |
91 void UnwatchSocket(); | |
92 | |
93 #if defined(OS_WIN) | |
94 // ObjectWatcher delegate | |
95 virtual void OnObjectSignaled(HANDLE object); | |
96 base::win::ObjectWatcher watcher_; | |
97 HANDLE socket_event_; | |
98 #elif defined(OS_POSIX) | |
99 // Called by MessagePumpLibevent when the socket is ready to do I/O | |
100 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; | |
101 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; | |
102 WaitState wait_state_; | |
103 // The socket's libevent wrapper | |
104 MessageLoopForIO::FileDescriptorWatcher watcher_; | |
105 #endif | |
106 | |
107 SOCKET socket_; | |
108 | 35 |
109 private: | 36 private: |
110 void SendData(); | |
111 | |
112 bool reads_paused_; | |
113 bool has_pending_reads_; | |
114 std::list<scoped_refptr<DrainableIOBuffer> > send_buffers_; | |
115 int send_pending_size_; | |
116 bool send_error_; | |
117 net::BackoffEntry send_backoff_; | |
118 | |
119 // Used to continue sending data asynchronously. | |
120 base::OneShotTimer<TCPListenSocket> send_timer_; | |
121 | |
122 DISALLOW_COPY_AND_ASSIGN(TCPListenSocket); | 37 DISALLOW_COPY_AND_ASSIGN(TCPListenSocket); |
123 }; | 38 }; |
124 | 39 |
125 } // namespace net | 40 } // namespace net |
126 | 41 |
127 #endif // NET_BASE_TCP_LISTEN_SOCKET_H_ | 42 #endif // NET_BASE_TCP_LISTEN_SOCKET_H_ |
OLD | NEW |