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