| 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" | |
| 16 | |
| 17 #if defined(OS_WIN) | |
| 18 #include <winsock2.h> | |
| 19 #endif | |
| 20 #include <string> | 12 #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 | 13 |
| 27 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 28 #include "base/compiler_specific.h" | |
| 29 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "build/build_config.h" |
| 30 #include "net/base/net_export.h" | 17 #include "net/base/net_export.h" |
| 31 | 18 |
| 32 #if defined(OS_POSIX) | |
| 33 typedef int SOCKET; | |
| 34 #endif | |
| 35 | |
| 36 namespace net { | 19 namespace net { |
| 37 | 20 |
| 38 // Implements a raw socket interface | 21 // Defines a socket interface for a server. |
| 39 class NET_EXPORT ListenSocket : public base::RefCountedThreadSafe<ListenSocket>, | 22 class NET_EXPORT ListenSocket |
| 40 #if defined(OS_WIN) | 23 : public base::RefCountedThreadSafe<ListenSocket> { |
| 41 public base::win::ObjectWatcher::Delegate { | |
| 42 #elif defined(OS_POSIX) | |
| 43 public MessageLoopForIO::Watcher { | |
| 44 #endif | |
| 45 public: | 24 public: |
| 46 // TODO(erikkay): this delegate should really be split into two parts | 25 // TODO(erikkay): this delegate should really be split into two parts |
| 47 // to split up the listener from the connected socket. Perhaps this class | 26 // to split up the listener from the connected socket. Perhaps this class |
| 48 // should be split up similarly. | 27 // should be split up similarly. |
| 49 class ListenSocketDelegate { | 28 class ListenSocketDelegate { |
| 50 public: | 29 public: |
| 51 virtual ~ListenSocketDelegate() {} | 30 virtual ~ListenSocketDelegate() {} |
| 52 | 31 |
| 53 // server is the original listening Socket, connection is the new | 32 // server is the original listening Socket, connection is the new |
| 54 // Socket that was created. Ownership of connection is transferred | 33 // Socket that was created. Ownership of connection is transferred |
| 55 // to the delegate with this call. | 34 // to the delegate with this call. |
| 56 virtual void DidAccept(ListenSocket *server, ListenSocket *connection) = 0; | 35 virtual void DidAccept(ListenSocket *server, |
| 36 ListenSocket *connection) = 0; |
| 57 virtual void DidRead(ListenSocket *connection, | 37 virtual void DidRead(ListenSocket *connection, |
| 58 const char* data, | 38 const char* data, |
| 59 int len) = 0; | 39 int len) = 0; |
| 60 virtual void DidClose(ListenSocket *sock) = 0; | 40 virtual void DidClose(ListenSocket *sock) = 0; |
| 61 }; | 41 }; |
| 62 | 42 |
| 63 // Listen on port for the specified IP address. Use 127.0.0.1 to only | |
| 64 // accept local connections. | |
| 65 static ListenSocket* Listen(std::string ip, int port, | |
| 66 ListenSocketDelegate* del); | |
| 67 | |
| 68 // Send data to the socket. | 43 // Send data to the socket. |
| 69 void Send(const char* bytes, int len, bool append_linefeed = false); | 44 void Send(const char* bytes, int len, bool append_linefeed = false); |
| 70 void Send(const std::string& str, bool append_linefeed = false); | 45 void Send(const std::string& str, bool append_linefeed = false); |
| 71 | 46 |
| 72 // NOTE: This is for unit test use only! | 47 protected: |
| 73 // Pause/Resume calling Read(). Note that ResumeReads() will also call | 48 ListenSocket(ListenSocketDelegate* del); |
| 74 // Read() if there is anything to read. | 49 virtual ~ListenSocket(); |
| 75 void PauseReads(); | |
| 76 void ResumeReads(); | |
| 77 | 50 |
| 78 protected: | 51 virtual void SendInternal(const char* bytes, int len) = 0; |
| 79 friend class base::RefCountedThreadSafe<ListenSocket>; | |
| 80 | 52 |
| 81 enum WaitState { | 53 ListenSocketDelegate* const socket_delegate_; |
| 82 NOT_WAITING = 0, | |
| 83 WAITING_ACCEPT = 1, | |
| 84 WAITING_READ = 2 | |
| 85 }; | |
| 86 | |
| 87 static const SOCKET kInvalidSocket; | |
| 88 static const int kSocketError; | |
| 89 | |
| 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_; | |
| 125 | 54 |
| 126 private: | 55 private: |
| 127 bool reads_paused_; | 56 friend class base::RefCountedThreadSafe<ListenSocket>; |
| 128 bool has_pending_reads_; | |
| 129 | 57 |
| 130 DISALLOW_COPY_AND_ASSIGN(ListenSocket); | 58 DISALLOW_COPY_AND_ASSIGN(ListenSocket); |
| 131 }; | 59 }; |
| 132 | 60 |
| 133 } // namespace net | 61 } // namespace net |
| 134 | 62 |
| 135 #endif // NET_BASE_LISTEN_SOCKET_H_ | 63 #endif // NET_BASE_LISTEN_SOCKET_H_ |
| OLD | NEW |