OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_SOCKET_H_ | 11 #ifndef NET_BASE_SOCKET_H_ |
12 #define NET_BASE_SOCKET_H_ | 12 #define NET_BASE_SOCKET_H_ |
13 | 13 |
14 #if defined(OS_WIN) | 14 #if defined(OS_WIN) |
15 #include <winsock2.h> | 15 #include <winsock2.h> |
| 16 #endif |
| 17 #include <string> |
| 18 #if defined(OS_WIN) |
16 #include "base/object_watcher.h" | 19 #include "base/object_watcher.h" |
17 #elif defined(OS_POSIX) | 20 #elif defined(OS_POSIX) |
18 #include "base/message_loop.h" | 21 #include "base/message_loop.h" |
19 #include "net/base/net_util.h" | 22 #include "net/base/net_util.h" |
20 #include "net/base/net_errors.h" | 23 #include "net/base/net_errors.h" |
21 #include "third_party/libevent/event.h" | |
22 #include "base/message_pump_libevent.h" | |
23 #endif | 24 #endif |
24 | 25 |
25 #include "base/basictypes.h" | 26 #include "base/basictypes.h" |
26 #include "base/ref_counted.h" | 27 #include "base/ref_counted.h" |
27 | 28 |
28 #if defined(OS_POSIX) | 29 #if defined(OS_POSIX) |
29 struct event; // From libevent | 30 struct event; // From libevent |
30 #define SOCKET int | 31 #define SOCKET int |
31 #endif | 32 #endif |
32 | 33 |
33 // Implements a raw socket interface | 34 // Implements a raw socket interface |
34 class ListenSocket : public base::RefCountedThreadSafe<ListenSocket>, | 35 class ListenSocket : public base::RefCountedThreadSafe<ListenSocket>, |
35 #if defined(OS_WIN) | 36 #if defined(OS_WIN) |
36 public base::ObjectWatcher::Delegate | 37 public base::ObjectWatcher::Delegate |
37 #elif defined(OS_POSIX) | 38 #elif defined(OS_POSIX) |
38 public base::MessagePumpLibevent::Watcher | 39 public MessageLoopForIO::Watcher |
39 #endif | 40 #endif |
40 { | 41 { |
41 public: | 42 public: |
42 // TODO(erikkay): this delegate should really be split into two parts | 43 // TODO(erikkay): this delegate should really be split into two parts |
43 // to split up the listener from the connected socket. Perhaps this class | 44 // to split up the listener from the connected socket. Perhaps this class |
44 // should be split up similarly. | 45 // should be split up similarly. |
45 class ListenSocketDelegate { | 46 class ListenSocketDelegate { |
46 public: | 47 public: |
47 // server is the original listening Socket, connection is the new | 48 // server is the original listening Socket, connection is the new |
48 // Socket that was created. Ownership of connection is transferred | 49 // Socket that was created. Ownership of connection is transferred |
(...skipping 24 matching lines...) Expand all Loading... |
73 virtual void Listen(); | 74 virtual void Listen(); |
74 virtual void Accept(); | 75 virtual void Accept(); |
75 virtual void Read(); | 76 virtual void Read(); |
76 virtual void Close(); | 77 virtual void Close(); |
77 virtual void CloseSocket(SOCKET s); | 78 virtual void CloseSocket(SOCKET s); |
78 | 79 |
79 enum WaitState { | 80 enum WaitState { |
80 NOT_WAITING = 0, | 81 NOT_WAITING = 0, |
81 WAITING_ACCEPT = 1, | 82 WAITING_ACCEPT = 1, |
82 WAITING_READ = 3, | 83 WAITING_READ = 3, |
83 WAITING_CLOSE = 4» | 84 WAITING_CLOSE = 4 |
84 }; | 85 }; |
85 // Pass any value in case of Windows, because in Windows | 86 // Pass any value in case of Windows, because in Windows |
86 // we are not using state. | 87 // we are not using state. |
87 void WatchSocket(WaitState state); | 88 void WatchSocket(WaitState state); |
88 void UnwatchSocket(); | 89 void UnwatchSocket(); |
89 | 90 |
90 #if defined(OS_WIN) | 91 #if defined(OS_WIN) |
91 // ObjectWatcher delegate | 92 // ObjectWatcher delegate |
92 virtual void OnObjectSignaled(HANDLE object); | 93 virtual void OnObjectSignaled(HANDLE object); |
93 base::ObjectWatcher watcher_; | 94 base::ObjectWatcher watcher_; |
94 HANDLE socket_event_; | 95 HANDLE socket_event_; |
95 #elif defined(OS_POSIX) | 96 #elif defined(OS_POSIX) |
96 WaitState wait_state_; | 97 WaitState wait_state_; |
97 // The socket's libevent wrapper | 98 // The socket's libevent wrapper |
98 scoped_ptr<event> event_; | 99 MessageLoopForIO::FileDescriptorWatcher watcher_; |
99 // Called by MessagePumpLibevent when the socket is ready to do I/O | 100 // Called by MessagePumpLibevent when the socket is ready to do I/O |
100 void OnSocketReady(short flags); | 101 void OnFileCanReadWithoutBlocking(int fd); |
| 102 void OnFileCanWriteWithoutBlocking(int fd); |
101 #endif | 103 #endif |
102 | 104 |
103 SOCKET socket_; | 105 SOCKET socket_; |
104 ListenSocketDelegate *socket_delegate_; | 106 ListenSocketDelegate *socket_delegate_; |
105 | 107 |
106 private: | 108 private: |
107 DISALLOW_EVIL_CONSTRUCTORS(ListenSocket); | 109 DISALLOW_EVIL_CONSTRUCTORS(ListenSocket); |
108 }; | 110 }; |
109 | 111 |
110 #endif // NET_BASE_SOCKET_H_ | 112 #endif // NET_BASE_SOCKET_H_ |
111 | |
OLD | NEW |