Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Stream-based listen socket implementation that handles reading and writing | |
| 6 // to the socket, but does not handle creating the socket nor connecting | |
| 7 // sockets, which are handled by subclasses on creation and in Accept, | |
| 8 // respectively. | |
| 9 | |
| 10 // Server that handles IO asynchronously in the specified MessageLoop. These | |
|
mmenke
2012/04/30 19:11:44
nit: "Server that" -> "This class" or "StreamList
Philippe
2012/05/03 14:27:52
Done.
| |
| 11 // objects are NOT thread safe. They use WSAEVENT handles to monitor activity | |
| 12 // in a given MessageLoop. This means that callbacks will happen in that loop's | |
| 13 // thread always and that all other methods (including constructors and | |
| 14 // destructors) should also be called from the same thread. | |
| 15 | |
| 16 #ifndef NET_BASE_STREAM_LISTEN_SOCKET_H_ | |
| 17 #define NET_BASE_STREAM_LISTEN_SOCKET_H_ | |
| 18 #pragma once | |
| 19 | |
| 20 #include "build/build_config.h" | |
| 21 | |
| 22 #if defined(OS_WIN) | |
| 23 #include <winsock2.h> | |
| 24 #endif | |
| 25 #include <string> | |
| 26 #if defined(OS_WIN) | |
| 27 #include "base/win/object_watcher.h" | |
| 28 #elif defined(OS_POSIX) | |
| 29 #include "base/message_loop.h" | |
| 30 #endif | |
| 31 | |
| 32 #include "base/basictypes.h" | |
| 33 #include "base/compiler_specific.h" | |
| 34 #include "base/memory/ref_counted.h" | |
| 35 #include "net/base/net_export.h" | |
| 36 | |
| 37 #if defined(OS_POSIX) | |
| 38 typedef int SOCKET; | |
| 39 #endif | |
| 40 | |
| 41 namespace net { | |
| 42 | |
| 43 class NET_EXPORT StreamListenSocket | |
| 44 : public base::RefCountedThreadSafe<StreamListenSocket>, | |
| 45 #if defined(OS_WIN) | |
| 46 public base::win::ObjectWatcher::Delegate { | |
| 47 #elif defined(OS_POSIX) | |
| 48 public MessageLoopForIO::Watcher { | |
| 49 #endif | |
| 50 | |
| 51 public: | |
| 52 // TODO(erikkay): this delegate should really be split into two parts | |
| 53 // to split up the listener from the connected socket. Perhaps this class | |
| 54 // should be split up similarly. | |
| 55 class Delegate { | |
| 56 public: | |
| 57 virtual ~Delegate() {} | |
| 58 | |
| 59 // |server| is the original listening Socket, connection is the new | |
| 60 // Socket that was created. Ownership of |connection| is transferred | |
| 61 // to the delegate with this call. | |
| 62 virtual void DidAccept(StreamListenSocket* server, | |
| 63 StreamListenSocket* connection) = 0; | |
| 64 virtual void DidRead(StreamListenSocket* connection, | |
| 65 const char* data, | |
| 66 int len) = 0; | |
| 67 virtual void DidClose(StreamListenSocket* sock) = 0; | |
| 68 }; | |
| 69 | |
| 70 // Send data to the socket. | |
| 71 void Send(const char* bytes, int len, bool append_linefeed = false); | |
| 72 void Send(const std::string& str, bool append_linefeed = false); | |
| 73 | |
| 74 protected: | |
| 75 enum WaitState { | |
| 76 NOT_WAITING = 0, | |
| 77 WAITING_ACCEPT = 1, | |
| 78 WAITING_READ = 2 | |
| 79 }; | |
| 80 | |
| 81 static const SOCKET kInvalidSocket; | |
| 82 static const int kSocketError; | |
| 83 | |
| 84 StreamListenSocket(SOCKET s, Delegate* del); | |
| 85 virtual ~StreamListenSocket(); | |
| 86 | |
| 87 SOCKET AcceptSocket(); | |
| 88 virtual void Accept() = 0; | |
| 89 | |
| 90 void Listen(); | |
| 91 void Read(); | |
| 92 void Close(); | |
| 93 void CloseSocket(SOCKET s); | |
| 94 | |
| 95 // Pass any value in case of Windows, because in Windows | |
| 96 // we are not using state. | |
| 97 void WatchSocket(WaitState state); | |
| 98 void UnwatchSocket(); | |
| 99 | |
| 100 #if defined(OS_WIN) | |
|
mmenke
2012/04/30 19:11:44
Looks like this whole block (WIN and POSIX) can be
Philippe
2012/05/03 14:27:52
Done.
| |
| 101 // ObjectWatcher delegate. | |
| 102 virtual void OnObjectSignaled(HANDLE object); | |
| 103 base::win::ObjectWatcher watcher_; | |
| 104 HANDLE socket_event_; | |
| 105 #elif defined(OS_POSIX) | |
| 106 // Called by MessagePumpLibevent when the socket is ready to do I/O. | |
| 107 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; | |
| 108 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; | |
| 109 WaitState wait_state_; | |
| 110 // The socket's libevent wrapper. | |
| 111 MessageLoopForIO::FileDescriptorWatcher watcher_; | |
| 112 #endif | |
| 113 | |
| 114 Delegate* const socket_delegate_; | |
| 115 | |
| 116 private: | |
| 117 friend class base::RefCountedThreadSafe<StreamListenSocket>; | |
| 118 | |
| 119 void SendInternal(const char* bytes, int len); | |
| 120 | |
| 121 // NOTE: This is for unit test use only! | |
| 122 // Pause/Resume calling Read(). Note that ResumeReads() will also call | |
| 123 // Read() if there is anything to read. | |
| 124 friend class TransportClientSocketTest; | |
|
mmenke
2012/04/30 19:11:44
This friend declaration should be above SendIntern
Philippe
2012/05/03 14:27:52
Done.
| |
| 125 | |
| 126 void PauseReads(); | |
| 127 void ResumeReads(); | |
| 128 | |
| 129 const SOCKET socket_; | |
| 130 bool reads_paused_; | |
| 131 bool has_pending_reads_; | |
| 132 | |
| 133 DISALLOW_COPY_AND_ASSIGN(StreamListenSocket); | |
| 134 }; | |
| 135 | |
| 136 } // namespace net | |
| 137 | |
| 138 #endif // NET_BASE_STREAM_LISTEN_SOCKET_H_ | |
| OLD | NEW |