Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(303)

Side by Side Diff: net/base/stream_listen_socket.h

Issue 10161005: Add DefaultListenSocket. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix build on Windows Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/base/listen_socket.cc ('k') | net/base/stream_listen_socket.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 // 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.
15
16 #ifndef NET_BASE_STREAM_LISTEN_SOCKET_H_
17 #define NET_BASE_STREAM_LISTEN_SOCKET_H_
18 #pragma once
19
20 #include <list>
21
22 #include "build/build_config.h"
23
24 #if defined(OS_WIN)
25 #include <winsock2.h>
26 #endif
27 #include <string>
28 #if defined(OS_WIN)
29 #include "base/win/object_watcher.h"
30 #elif defined(OS_POSIX)
31 #include "base/message_loop.h"
32 #endif
33
34 #include "base/basictypes.h"
35 #include "base/compiler_specific.h"
36 #include "base/memory/ref_counted.h"
37 #include "base/timer.h"
38 #include "net/base/backoff_entry.h"
39 #include "net/base/io_buffer.h"
40 #include "net/base/net_export.h"
41 #include "net/base/stream_listen_socket.h"
42
43 #if defined(OS_POSIX)
44 typedef int SOCKET;
45 #endif
46
47 namespace net {
48
49 class NET_EXPORT StreamListenSocket
50 : public base::RefCountedThreadSafe<StreamListenSocket>,
51 #if defined(OS_WIN)
52 public base::win::ObjectWatcher::Delegate {
53 #elif defined(OS_POSIX)
54 public MessageLoopForIO::Watcher {
55 #endif
56
57 public:
58 // TODO(erikkay): this delegate should really be split into two parts
59 // to split up the listener from the connected socket. Perhaps this class
60 // should be split up similarly.
61 class Delegate {
62 public:
63 virtual ~Delegate() {}
64
65 // |server| is the original listening Socket, connection is the new
66 // Socket that was created. Ownership of |connection| is transferred
67 // to the delegate with this call.
68 virtual void DidAccept(StreamListenSocket* server,
69 StreamListenSocket* connection) = 0;
70 virtual void DidRead(StreamListenSocket* connection,
71 const char* data,
72 int len) = 0;
73 virtual void DidClose(StreamListenSocket* sock) = 0;
74 };
75
76 // Send data to the socket.
77 void Send(const char* bytes, int len, bool append_linefeed = false);
78 void Send(const std::string& str, bool append_linefeed = false);
79
80 protected:
81 enum WaitState {
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 StreamListenSocket(SOCKET s, Delegate* del);
91 virtual ~StreamListenSocket();
92
93 SOCKET AcceptSocket();
94 virtual void Accept() = 0;
95
96 void Listen();
97 void Read();
98 void Close();
99 void CloseSocket(SOCKET s);
100
101 // Pass any value in case of Windows, because in Windows
102 // we are not using state.
103 void WatchSocket(WaitState state);
104 void UnwatchSocket();
105
106 Delegate* const socket_delegate_;
107
108 private:
109 friend class base::RefCountedThreadSafe<StreamListenSocket>;
110 friend class TransportClientSocketTest;
111
112 void SendData();
113 void SendInternal(const char* bytes, int len);
114
115 #if defined(OS_WIN)
116 // ObjectWatcher delegate.
117 virtual void OnObjectSignaled(HANDLE object);
118 base::win::ObjectWatcher watcher_;
119 HANDLE socket_event_;
120 #elif defined(OS_POSIX)
121 // Called by MessagePumpLibevent when the socket is ready to do I/O.
122 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
123 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE;
124 WaitState wait_state_;
125 // The socket's libevent wrapper.
126 MessageLoopForIO::FileDescriptorWatcher watcher_;
127 #endif
128
129 // NOTE: This is for unit test use only!
130 // Pause/Resume calling Read(). Note that ResumeReads() will also call
131 // Read() if there is anything to read.
132 void PauseReads();
133 void ResumeReads();
134
135 const SOCKET socket_;
136 bool reads_paused_;
137 bool has_pending_reads_;
138
139 std::list<scoped_refptr<DrainableIOBuffer> > send_buffers_;
140 int send_pending_size_;
141 bool send_error_;
142 net::BackoffEntry send_backoff_;
143
144 // Used to continue sending data asynchronously.
145 base::OneShotTimer<StreamListenSocket> send_timer_;
146
147 DISALLOW_COPY_AND_ASSIGN(StreamListenSocket);
148 };
149
150 } // namespace net
151
152 #endif // NET_BASE_STREAM_LISTEN_SOCKET_H_
OLDNEW
« no previous file with comments | « net/base/listen_socket.cc ('k') | net/base/stream_listen_socket.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698