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 "build/build_config.h" | |
16 | |
17 #if defined(OS_WIN) | |
18 #include <winsock2.h> | |
19 #endif | |
20 #include <string> | 9 #include <string> |
21 #if defined(OS_WIN) | |
22 #include "base/win/object_watcher.h" | |
23 #elif defined(OS_POSIX) | |
24 #include "base/message_loop.h" | |
25 #endif | |
26 | 10 |
27 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
28 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
29 #include "net/base/listen_socket.h" | 13 #include "build/build_config.h" |
| 14 #include "net/base/default_listen_socket.h" |
30 #include "net/base/net_export.h" | 15 #include "net/base/net_export.h" |
31 | 16 |
32 #if defined(OS_POSIX) | |
33 typedef int SOCKET; | |
34 #endif | |
35 | |
36 namespace net { | 17 namespace net { |
37 | 18 |
38 // Implements a TCP socket interface. Note that this is ref counted. | 19 // Implements a TCP socket. Note that this is ref counted. |
39 class NET_EXPORT TCPListenSocket : public ListenSocket, | 20 class NET_EXPORT TCPListenSocket : public DefaultListenSocket { |
40 #if defined(OS_WIN) | |
41 public base::win::ObjectWatcher::Delegate { | |
42 #elif defined(OS_POSIX) | |
43 public MessageLoopForIO::Watcher { | |
44 #endif | |
45 public: | 21 public: |
46 // Listen on port for the specified IP address. Use 127.0.0.1 to only | 22 // Listen on port for the specified IP address. Use 127.0.0.1 to only |
47 // accept local connections. | 23 // accept local connections. |
48 static TCPListenSocket* CreateAndListen(std::string ip, int port, | 24 // Note that the deletion of the returned instance is under the |
| 25 // responsibility of the caller. |
| 26 static TCPListenSocket* CreateAndListen(const std::string& ip, int port, |
49 ListenSocketDelegate* del); | 27 ListenSocketDelegate* del); |
50 | 28 |
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: | 29 protected: |
58 enum WaitState { | |
59 NOT_WAITING = 0, | |
60 WAITING_ACCEPT = 1, | |
61 WAITING_READ = 2 | |
62 }; | |
63 | |
64 static const SOCKET kInvalidSocket; | |
65 static const int kSocketError; | |
66 | |
67 TCPListenSocket(SOCKET s, ListenSocketDelegate* del); | 30 TCPListenSocket(SOCKET s, ListenSocketDelegate* del); |
68 virtual ~TCPListenSocket(); | 31 virtual ~TCPListenSocket(); |
69 static SOCKET CreateAndBind(std::string ip, int port); | |
70 // if valid, returned SOCKET is non-blocking | |
71 static SOCKET Accept(SOCKET s); | |
72 | 32 |
73 // Implements ListenSocket::SendInternal. | 33 static SOCKET CreateAndBind(const std::string& ip, int port); |
74 virtual void SendInternal(const char* bytes, int len) OVERRIDE; | |
75 | 34 |
76 virtual void Listen(); | 35 // Implements DefaultListenSocket::AcceptInternal. |
77 virtual void Accept(); | 36 virtual void AcceptInternal() OVERRIDE; |
78 virtual void Read(); | |
79 virtual void Close(); | |
80 virtual void CloseSocket(SOCKET s); | |
81 | |
82 // Pass any value in case of Windows, because in Windows | |
83 // we are not using state. | |
84 void WatchSocket(WaitState state); | |
85 void UnwatchSocket(); | |
86 | |
87 #if defined(OS_WIN) | |
88 // ObjectWatcher delegate | |
89 virtual void OnObjectSignaled(HANDLE object); | |
90 base::win::ObjectWatcher watcher_; | |
91 HANDLE socket_event_; | |
92 #elif defined(OS_POSIX) | |
93 // Called by MessagePumpLibevent when the socket is ready to do I/O | |
94 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; | |
95 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; | |
96 WaitState wait_state_; | |
97 // The socket's libevent wrapper | |
98 MessageLoopForIO::FileDescriptorWatcher watcher_; | |
99 #endif | |
100 | |
101 SOCKET socket_; | |
102 | 37 |
103 private: | 38 private: |
104 bool reads_paused_; | |
105 bool has_pending_reads_; | |
106 | |
107 DISALLOW_COPY_AND_ASSIGN(TCPListenSocket); | 39 DISALLOW_COPY_AND_ASSIGN(TCPListenSocket); |
108 }; | 40 }; |
109 | 41 |
110 } // namespace net | 42 } // namespace net |
111 | 43 |
112 #endif // NET_BASE_TCP_LISTEN_SOCKET_H_ | 44 #endif // NET_BASE_TCP_LISTEN_SOCKET_H_ |
OLD | NEW |