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

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

Issue 10161005: Add DefaultListenSocket. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address Matt's comments 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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.
6 // These objects are NOT thread safe. They use WSAEVENT handles to monitor
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
11 #ifndef NET_BASE_TCP_LISTEN_SOCKET_H_ 5 #ifndef NET_BASE_TCP_LISTEN_SOCKET_H_
12 #define NET_BASE_TCP_LISTEN_SOCKET_H_ 6 #define NET_BASE_TCP_LISTEN_SOCKET_H_
13 #pragma once 7 #pragma once
14 8
15 #include "build/build_config.h"
16
17 #if defined(OS_WIN)
18 #include <winsock2.h>
19 #endif
20 #include <string> 9 #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 10
27 #include "base/basictypes.h" 11 #include "base/basictypes.h"
28 #include "base/compiler_specific.h" 12 #include "base/compiler_specific.h"
29 #include "net/base/listen_socket.h" 13 #include "build/build_config.h"
30 #include "net/base/net_export.h" 14 #include "net/base/net_export.h"
31 15 #include "net/base/stream_listen_socket.h"
32 #if defined(OS_POSIX)
33 typedef int SOCKET;
34 #endif
35 16
36 namespace net { 17 namespace net {
37 18
38 // Implements a TCP socket interface. Note that this is ref counted. 19 // Implements a TCP socket. Note that this is ref counted.
39 class NET_EXPORT TCPListenSocket : public ListenSocket, 20 class NET_EXPORT TCPListenSocket : public StreamListenSocket {
40 #if defined(OS_WIN)
41 public base::win::ObjectWatcher::Delegate {
42 #elif defined(OS_POSIX)
43 public MessageLoopForIO::Watcher {
44 #endif
45 public: 21 public:
46 // Listen on port for the specified IP address. Use 127.0.0.1 to only 22 // Listen on port for the specified IP address. Use 127.0.0.1 to only
47 // accept local connections. 23 // accept local connections.
24 // Note that the deletion of the returned instance is under the
25 // responsibility of the caller.
mmenke 2012/04/30 19:11:44 nit: Think "The caller must take ownership of a p
Philippe 2012/05/03 14:27:52 Done.
48 static TCPListenSocket* CreateAndListen(const std::string& ip, int port, 26 static TCPListenSocket* CreateAndListen(const std::string& ip, int port,
49 ListenSocketDelegate* del); 27 StreamListenSocket::Delegate* del);
28 protected:
mmenke 2012/04/30 19:11:44 nit: Line break needed above this line.
Philippe 2012/05/03 14:27:52 Done.
29 TCPListenSocket(SOCKET s, StreamListenSocket::Delegate* del);
30 virtual ~TCPListenSocket();
50 31
51 // NOTE: This is for unit test use only! 32 static SOCKET CreateAndBind(const std::string& ip, int port);
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 33
57 protected: 34 // Implements StreamListenSocket::Accept.
58 enum WaitState { 35 virtual void Accept() OVERRIDE;
59 NOT_WAITING = 0,
60 WAITING_ACCEPT = 1,
61 WAITING_READ = 2
62 };
63
64 static const SOCKET kInvalidSocket;
65 static const int kSocketError;
66
67 TCPListenSocket(SOCKET s, ListenSocketDelegate* del);
68 virtual ~TCPListenSocket();
69 static SOCKET CreateAndBind(const std::string& ip, int port);
70 // if valid, returned SOCKET is non-blocking
71 static SOCKET Accept(SOCKET s);
72
73 // Implements ListenSocket::SendInternal.
74 virtual void SendInternal(const char* bytes, int len) OVERRIDE;
75
76 virtual void Listen();
77 virtual void Accept();
78 virtual void Read();
79 virtual void Close();
80 virtual void CloseSocket(SOCKET s);
81
82 // Pass any value in case of Windows, because in Windows
83 // we are not using state.
84 void WatchSocket(WaitState state);
85 void UnwatchSocket();
86
87 #if defined(OS_WIN)
88 // ObjectWatcher delegate
89 virtual void OnObjectSignaled(HANDLE object);
90 base::win::ObjectWatcher watcher_;
91 HANDLE socket_event_;
92 #elif defined(OS_POSIX)
93 // Called by MessagePumpLibevent when the socket is ready to do I/O
94 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
95 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE;
96 WaitState wait_state_;
97 // The socket's libevent wrapper
98 MessageLoopForIO::FileDescriptorWatcher watcher_;
99 #endif
100
101 SOCKET socket_;
102 36
103 private: 37 private:
104 bool reads_paused_;
105 bool has_pending_reads_;
106
107 DISALLOW_COPY_AND_ASSIGN(TCPListenSocket); 38 DISALLOW_COPY_AND_ASSIGN(TCPListenSocket);
108 }; 39 };
109 40
110 } // namespace net 41 } // namespace net
111 42
112 #endif // NET_BASE_TCP_LISTEN_SOCKET_H_ 43 #endif // NET_BASE_TCP_LISTEN_SOCKET_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698