Rename InputLatency::ScrollUpdate to Latency::ScrollUpdate
[chromium-blink-merge.git] / net / socket / stream_listen_socket.h
blobc83657bccbc2277d2adfb4340da7714b07cde199
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.
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.
10 // StreamListenSocket handles IO asynchronously in the specified MessageLoop.
11 // This class is NOT thread safe. It uses 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 constructor and
14 // destructor) should also be called from the same thread.
16 #ifndef NET_SOCKET_STREAM_LISTEN_SOCKET_H_
17 #define NET_SOCKET_STREAM_LISTEN_SOCKET_H_
19 #include "build/build_config.h"
21 #if defined(OS_WIN)
22 #include <winsock2.h>
23 #endif
24 #include <string>
25 #if defined(OS_WIN)
26 #include "base/win/object_watcher.h"
27 #elif defined(OS_POSIX)
28 #include "base/message_loop/message_loop.h"
29 #endif
31 #include "base/basictypes.h"
32 #include "base/compiler_specific.h"
33 #include "base/memory/scoped_ptr.h"
34 #include "net/base/net_export.h"
35 #include "net/socket/socket_descriptor.h"
37 namespace net {
39 class IPEndPoint;
41 class NET_EXPORT StreamListenSocket
43 #if defined(OS_WIN)
44 public base::win::ObjectWatcher::Delegate {
45 #elif defined(OS_POSIX)
46 public base::MessageLoopForIO::Watcher {
47 #endif
49 public:
50 ~StreamListenSocket() override;
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 // |server| is the original listening Socket, connection is the new
58 // Socket that was created.
59 virtual void DidAccept(StreamListenSocket* server,
60 scoped_ptr<StreamListenSocket> connection) = 0;
61 virtual void DidRead(StreamListenSocket* connection,
62 const char* data,
63 int len) = 0;
64 virtual void DidClose(StreamListenSocket* sock) = 0;
66 protected:
67 virtual ~Delegate() {}
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);
74 // Copies the local address to |address|. Returns a network error code.
75 // This method is virtual to support unit testing.
76 virtual int GetLocalAddress(IPEndPoint* address);
77 // Copies the peer address to |address|. Returns a network error code.
78 // This method is virtual to support unit testing.
79 virtual int GetPeerAddress(IPEndPoint* address);
81 static const int kSocketError;
83 protected:
84 enum WaitState {
85 NOT_WAITING = 0,
86 WAITING_ACCEPT = 1,
87 WAITING_READ = 2
90 StreamListenSocket(SocketDescriptor s, Delegate* del);
92 SocketDescriptor AcceptSocket();
93 virtual void Accept() = 0;
95 void Listen();
96 void Read();
97 void Close();
98 void CloseSocket();
100 // Pass any value in case of Windows, because in Windows
101 // we are not using state.
102 void WatchSocket(WaitState state);
103 void UnwatchSocket();
105 Delegate* const socket_delegate_;
107 private:
108 friend class TransportClientSocketTest;
110 void SendInternal(const char* bytes, int len);
112 #if defined(OS_WIN)
113 // ObjectWatcher delegate.
114 void OnObjectSignaled(HANDLE object) override;
115 base::win::ObjectWatcher watcher_;
116 HANDLE socket_event_;
117 #elif defined(OS_POSIX)
118 // Called by MessagePumpLibevent when the socket is ready to do I/O.
119 void OnFileCanReadWithoutBlocking(int fd) override;
120 void OnFileCanWriteWithoutBlocking(int fd) override;
121 WaitState wait_state_;
122 // The socket's libevent wrapper.
123 base::MessageLoopForIO::FileDescriptorWatcher watcher_;
124 #endif
126 // NOTE: This is for unit test use only!
127 // Pause/Resume calling Read(). Note that ResumeReads() will also call
128 // Read() if there is anything to read.
129 void PauseReads();
130 void ResumeReads();
132 const SocketDescriptor socket_;
133 bool reads_paused_;
134 bool has_pending_reads_;
136 DISALLOW_COPY_AND_ASSIGN(StreamListenSocket);
139 // Abstract factory that must be subclassed for each subclass of
140 // StreamListenSocket.
141 class NET_EXPORT StreamListenSocketFactory {
142 public:
143 virtual ~StreamListenSocketFactory() {}
145 // Returns a new instance of StreamListenSocket or NULL if an error occurred.
146 virtual scoped_ptr<StreamListenSocket> CreateAndListen(
147 StreamListenSocket::Delegate* delegate) const = 0;
150 } // namespace net
152 #endif // NET_SOCKET_STREAM_LISTEN_SOCKET_H_