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