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