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. | 5 // Default (abstract) listen socket implementation that can be used by clients |
mmenke
2012/04/25 17:25:31
Default isn't really an accurate description, or a
Philippe
2012/04/30 06:55:21
Indeed.
| |
6 // These objects are NOT thread safe. They use WSAEVENT handles to monitor | 6 // to implement more specific classes like TCPListenSocket and UnixDomainSocket. |
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 | 7 |
11 #ifndef NET_BASE_TCP_LISTEN_SOCKET_H_ | 8 // Server that handles IO asynchronously in the specified MessageLoop. These |
12 #define NET_BASE_TCP_LISTEN_SOCKET_H_ | 9 // objects are NOT thread safe. They use WSAEVENT handles to monitor activity |
10 // in a given MessageLoop. This means that callbacks will happen in that loop's | |
11 // thread always and that all other methods (including constructors and | |
12 // destructors) should also be called from the same thread. | |
13 | |
14 #ifndef NET_BASE_DEFAULT_LISTEN_SOCKET_H_ | |
15 #define NET_BASE_DEFAULT_LISTEN_SOCKET_H_ | |
13 #pragma once | 16 #pragma once |
14 | 17 |
15 #include "build/build_config.h" | 18 #include "build/build_config.h" |
16 | 19 |
17 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
18 #include <winsock2.h> | 21 #include <winsock2.h> |
19 #endif | 22 #endif |
20 #include <string> | 23 #include <string> |
21 #if defined(OS_WIN) | 24 #if defined(OS_WIN) |
22 #include "base/win/object_watcher.h" | 25 #include "base/win/object_watcher.h" |
23 #elif defined(OS_POSIX) | 26 #elif defined(OS_POSIX) |
24 #include "base/message_loop.h" | 27 #include "base/message_loop.h" |
25 #endif | 28 #endif |
26 | 29 |
27 #include "base/basictypes.h" | 30 #include "base/basictypes.h" |
28 #include "base/compiler_specific.h" | 31 #include "base/compiler_specific.h" |
29 #include "net/base/listen_socket.h" | 32 #include "net/base/listen_socket.h" |
30 #include "net/base/net_export.h" | 33 #include "net/base/net_export.h" |
31 | 34 |
32 #if defined(OS_POSIX) | 35 #if defined(OS_POSIX) |
33 typedef int SOCKET; | 36 typedef int SOCKET; |
34 #endif | 37 #endif |
35 | 38 |
36 namespace net { | 39 namespace net { |
37 | 40 |
38 // Implements a TCP socket interface. Note that this is ref counted. | 41 class NET_EXPORT DefaultListenSocket |
mmenke
2012/04/25 17:25:31
Suggest you call this ListenSocketBase. It has tw
mmenke
2012/04/25 17:25:31
Are there any plans to make any other direct subcl
Philippe
2012/04/30 06:55:21
I liked StreamListenSocket.
Philippe
2012/04/30 06:55:21
I don't think so. I guess that your underlying sug
mmenke
2012/04/30 19:11:44
Yea, I was wondering if there was any need for it.
| |
39 class NET_EXPORT TCPListenSocket : public ListenSocket, | 42 : public ListenSocket, |
40 #if defined(OS_WIN) | 43 #if defined(OS_WIN) |
41 public base::win::ObjectWatcher::Delegate { | 44 public base::win::ObjectWatcher::Delegate { |
42 #elif defined(OS_POSIX) | 45 #elif defined(OS_POSIX) |
43 public MessageLoopForIO::Watcher { | 46 public MessageLoopForIO::Watcher { |
44 #endif | 47 #endif |
45 public: | |
46 // Listen on port for the specified IP address. Use 127.0.0.1 to only | |
47 // accept local connections. | |
48 static TCPListenSocket* CreateAndListen(std::string ip, int port, | |
49 ListenSocketDelegate* del); | |
50 | |
51 // NOTE: This is for unit test use only! | |
52 // Pause/Resume calling Read(). Note that ResumeReads() will also call | |
53 // Read() if there is anything to read. | |
54 void PauseReads(); | |
55 void ResumeReads(); | |
56 | |
57 protected: | 48 protected: |
mmenke
2012/04/25 17:25:31
nit: Please add a blank line above the "protected
Philippe
2012/04/30 06:55:21
Done.
| |
58 enum WaitState { | 49 enum WaitState { |
59 NOT_WAITING = 0, | 50 NOT_WAITING = 0, |
60 WAITING_ACCEPT = 1, | 51 WAITING_ACCEPT = 1, |
61 WAITING_READ = 2 | 52 WAITING_READ = 2 |
62 }; | 53 }; |
63 | 54 |
64 static const SOCKET kInvalidSocket; | 55 static const SOCKET kInvalidSocket; |
65 static const int kSocketError; | 56 static const int kSocketError; |
66 | 57 |
67 TCPListenSocket(SOCKET s, ListenSocketDelegate* del); | 58 DefaultListenSocket(SOCKET s, ListenSocketDelegate* del); |
68 virtual ~TCPListenSocket(); | 59 virtual ~DefaultListenSocket(); |
69 static SOCKET CreateAndBind(std::string ip, int port); | 60 |
70 // if valid, returned SOCKET is non-blocking | |
71 static SOCKET Accept(SOCKET s); | 61 static SOCKET Accept(SOCKET s); |
mmenke
2012/04/25 17:25:31
There doesn't seem to be any reason to make the cu
mmenke
2012/04/25 17:27:20
Just to clarify, as a non-static function, it woul
Philippe
2012/04/30 06:55:21
Good point.
| |
72 | 62 |
73 // Implements ListenSocket::SendInternal. | 63 // Implements ListenSocket::SendInternal. |
74 virtual void SendInternal(const char* bytes, int len) OVERRIDE; | 64 virtual void SendInternal(const char* bytes, int len) OVERRIDE; |
75 | 65 |
76 virtual void Listen(); | 66 virtual void Listen(); |
mmenke
2012/04/25 17:25:31
It looks to me like Listen, Read, Close, and Close
Philippe
2012/04/30 06:55:21
Done.
| |
77 virtual void Accept(); | 67 virtual void AcceptInternal() = 0; |
mmenke
2012/04/25 17:25:31
This naming seems a little weird. SendInternal is
Philippe
2012/04/30 06:55:21
Done.
| |
78 virtual void Read(); | 68 virtual void Read(); |
79 virtual void Close(); | 69 virtual void Close(); |
80 virtual void CloseSocket(SOCKET s); | 70 virtual void CloseSocket(SOCKET s); |
81 | 71 |
82 // Pass any value in case of Windows, because in Windows | 72 // Pass any value in case of Windows, because in Windows |
83 // we are not using state. | 73 // we are not using state. |
84 void WatchSocket(WaitState state); | 74 void WatchSocket(WaitState state); |
85 void UnwatchSocket(); | 75 void UnwatchSocket(); |
86 | 76 |
87 #if defined(OS_WIN) | 77 #if defined(OS_WIN) |
88 // ObjectWatcher delegate | 78 // ObjectWatcher delegate |
89 virtual void OnObjectSignaled(HANDLE object); | 79 virtual void OnObjectSignaled(HANDLE object); |
90 base::win::ObjectWatcher watcher_; | 80 base::win::ObjectWatcher watcher_; |
91 HANDLE socket_event_; | 81 HANDLE socket_event_; |
92 #elif defined(OS_POSIX) | 82 #elif defined(OS_POSIX) |
93 // Called by MessagePumpLibevent when the socket is ready to do I/O | 83 // Called by MessagePumpLibevent when the socket is ready to do I/O |
94 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; | 84 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; |
95 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; | 85 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; |
96 WaitState wait_state_; | 86 WaitState wait_state_; |
97 // The socket's libevent wrapper | 87 // The socket's libevent wrapper |
98 MessageLoopForIO::FileDescriptorWatcher watcher_; | 88 MessageLoopForIO::FileDescriptorWatcher watcher_; |
99 #endif | 89 #endif |
100 | 90 |
101 SOCKET socket_; | 91 const SOCKET socket_; |
102 | 92 |
103 private: | 93 private: |
94 // NOTE: This is for unit test use only! | |
95 // Pause/Resume calling Read(). Note that ResumeReads() will also call | |
96 // Read() if there is anything to read. | |
97 friend class TransportClientSocketTest; | |
mmenke
2012/04/25 17:25:31
nit: Please add a linebreak between the friend de
Philippe
2012/04/30 06:55:21
Done.
| |
98 void PauseReads(); | |
99 void ResumeReads(); | |
100 | |
104 bool reads_paused_; | 101 bool reads_paused_; |
105 bool has_pending_reads_; | 102 bool has_pending_reads_; |
106 | 103 |
107 DISALLOW_COPY_AND_ASSIGN(TCPListenSocket); | 104 DISALLOW_COPY_AND_ASSIGN(DefaultListenSocket); |
108 }; | 105 }; |
109 | 106 |
110 } // namespace net | 107 } // namespace net |
111 | 108 |
112 #endif // NET_BASE_TCP_LISTEN_SOCKET_H_ | 109 #endif // NET_BASE_DEFAULT_LISTEN_SOCKET_H_ |
OLD | NEW |