| 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 // Default (abstract) listen socket implementation that can be used by clients |
| 6 // These objects are NOT thread safe. They use WSAEVENT handles to monitor | 6 // to implement more specific classes like TCPListenSocket and UnixDomainSocket. |
| 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_TCP_LISTEN_SOCKET_H_ | 8 // Server that handles IO asynchronously in the specified MessageLoop. These |
| 12 #define NET_BASE_TCP_LISTEN_SOCKET_H_ | 9 // objects are NOT thread safe. They use WSAEVENT handles to monitor activity |
| 10 // in a given MessageLoop. This means that callbacks will happen in that loop's |
| 11 // thread always and that all other methods (including constructors and |
| 12 // destructors) should also be called from the same thread. |
| 13 |
| 14 #ifndef NET_BASE_DEFAULT_LISTEN_SOCKET_H_ |
| 15 #define NET_BASE_DEFAULT_LISTEN_SOCKET_H_ |
| 13 #pragma once | 16 #pragma once |
| 14 | 17 |
| 15 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 16 | 19 |
| 17 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
| 18 #include <winsock2.h> | 21 #include <winsock2.h> |
| 19 #endif | 22 #endif |
| 20 #include <string> | 23 #include <string> |
| 21 #if defined(OS_WIN) | 24 #if defined(OS_WIN) |
| 22 #include "base/win/object_watcher.h" | 25 #include "base/win/object_watcher.h" |
| 23 #elif defined(OS_POSIX) | 26 #elif defined(OS_POSIX) |
| 24 #include "base/message_loop.h" | 27 #include "base/message_loop.h" |
| 25 #endif | 28 #endif |
| 26 | 29 |
| 27 #include "base/basictypes.h" | 30 #include "base/basictypes.h" |
| 28 #include "base/compiler_specific.h" | 31 #include "base/compiler_specific.h" |
| 29 #include "net/base/listen_socket.h" | 32 #include "net/base/listen_socket.h" |
| 30 #include "net/base/net_export.h" | 33 #include "net/base/net_export.h" |
| 31 | 34 |
| 32 #if defined(OS_POSIX) | 35 #if defined(OS_POSIX) |
| 33 typedef int SOCKET; | 36 typedef int SOCKET; |
| 34 #endif | 37 #endif |
| 35 | 38 |
| 36 namespace net { | 39 namespace net { |
| 37 | 40 |
| 38 // Implements a raw socket interface | 41 class NET_EXPORT DefaultListenSocket |
| 39 class NET_EXPORT TCPListenSocket : public ListenSocket, | 42 : public ListenSocket, |
| 40 #if defined(OS_WIN) | 43 #if defined(OS_WIN) |
| 41 public base::win::ObjectWatcher::Delegate { | 44 public base::win::ObjectWatcher::Delegate { |
| 42 #elif defined(OS_POSIX) | 45 #elif defined(OS_POSIX) |
| 43 public MessageLoopForIO::Watcher { | 46 public MessageLoopForIO::Watcher { |
| 44 #endif | 47 #endif |
| 45 public: | |
| 46 // Listen on port for the specified IP address. Use 127.0.0.1 to only | |
| 47 // accept local connections. | |
| 48 static TCPListenSocket* CreateAndListen(std::string ip, int port, | |
| 49 ListenSocketDelegate* del); | |
| 50 | |
| 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: | 48 protected: |
| 58 enum WaitState { | 49 enum WaitState { |
| 59 NOT_WAITING = 0, | 50 NOT_WAITING = 0, |
| 60 WAITING_ACCEPT = 1, | 51 WAITING_ACCEPT = 1, |
| 61 WAITING_READ = 2 | 52 WAITING_READ = 2 |
| 62 }; | 53 }; |
| 63 | 54 |
| 64 static const SOCKET kInvalidSocket; | 55 static const SOCKET kInvalidSocket; |
| 65 static const int kSocketError; | 56 static const int kSocketError; |
| 66 | 57 |
| 67 TCPListenSocket(SOCKET s, ListenSocketDelegate* del); | 58 DefaultListenSocket(SOCKET s, ListenSocketDelegate* del); |
| 68 virtual ~TCPListenSocket(); | 59 virtual ~DefaultListenSocket(); |
| 69 static SOCKET CreateAndBind(std::string ip, int port); | 60 |
| 70 // if valid, returned SOCKET is non-blocking | 61 static SOCKET AcceptInternal(SOCKET s); |
| 71 static SOCKET Accept(SOCKET s); | |
| 72 | 62 |
| 73 virtual void SendInternal(const char* bytes, int len) OVERRIDE; | 63 virtual void SendInternal(const char* bytes, int len) OVERRIDE; |
| 74 | 64 |
| 75 virtual void Listen(); | 65 virtual void Listen(); |
| 76 virtual void Accept(); | 66 virtual void Accept() = 0; |
| 77 virtual void Read(); | 67 virtual void Read(); |
| 78 virtual void Close(); | 68 virtual void Close(); |
| 79 virtual void CloseSocket(SOCKET s); | 69 virtual void CloseSocket(SOCKET s); |
| 80 | 70 |
| 81 // Pass any value in case of Windows, because in Windows | 71 // Pass any value in case of Windows, because in Windows |
| 82 // we are not using state. | 72 // we are not using state. |
| 83 void WatchSocket(WaitState state); | 73 void WatchSocket(WaitState state); |
| 84 void UnwatchSocket(); | 74 void UnwatchSocket(); |
| 85 | 75 |
| 86 #if defined(OS_WIN) | 76 #if defined(OS_WIN) |
| 87 // ObjectWatcher delegate | 77 // ObjectWatcher delegate |
| 88 virtual void OnObjectSignaled(HANDLE object); | 78 virtual void OnObjectSignaled(HANDLE object); |
| 89 base::win::ObjectWatcher watcher_; | 79 base::win::ObjectWatcher watcher_; |
| 90 HANDLE socket_event_; | 80 HANDLE socket_event_; |
| 91 #elif defined(OS_POSIX) | 81 #elif defined(OS_POSIX) |
| 92 // Called by MessagePumpLibevent when the socket is ready to do I/O | 82 // Called by MessagePumpLibevent when the socket is ready to do I/O |
| 93 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; | 83 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; |
| 94 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; | 84 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; |
| 95 WaitState wait_state_; | 85 WaitState wait_state_; |
| 96 // The socket's libevent wrapper | 86 // The socket's libevent wrapper |
| 97 MessageLoopForIO::FileDescriptorWatcher watcher_; | 87 MessageLoopForIO::FileDescriptorWatcher watcher_; |
| 98 #endif | 88 #endif |
| 99 | 89 |
| 100 SOCKET socket_; | 90 const SOCKET socket_; |
| 101 | 91 |
| 102 private: | 92 private: |
| 93 // NOTE: This is for unit test use only! |
| 94 // Pause/Resume calling Read(). Note that ResumeReads() will also call |
| 95 // Read() if there is anything to read. |
| 96 friend class TransportClientSocketTest; |
| 97 void PauseReads(); |
| 98 void ResumeReads(); |
| 99 |
| 103 bool reads_paused_; | 100 bool reads_paused_; |
| 104 bool has_pending_reads_; | 101 bool has_pending_reads_; |
| 105 | 102 |
| 106 DISALLOW_COPY_AND_ASSIGN(TCPListenSocket); | 103 DISALLOW_COPY_AND_ASSIGN(DefaultListenSocket); |
| 107 }; | 104 }; |
| 108 | 105 |
| 109 } // namespace net | 106 } // namespace net |
| 110 | 107 |
| 111 #endif // NET_BASE_TCP_LISTEN_SOCKET_H_ | 108 #endif // NET_BASE_DEFAULT_LISTEN_SOCKET_H_ |
| OLD | NEW |