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

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: Created 8 years, 8 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) 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.
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" 9 #include "build/build_config.h"
16 10
17 #if defined(OS_WIN)
18 #include <winsock2.h>
19 #endif
20 #include <string> 11 #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 12
27 #include "base/basictypes.h" 13 #include "base/basictypes.h"
28 #include "base/compiler_specific.h" 14 #include "base/compiler_specific.h"
29 #include "net/base/listen_socket.h" 15 #include "net/base/default_listen_socket.h"
30 #include "net/base/net_export.h" 16 #include "net/base/net_export.h"
31 17
32 #if defined(OS_POSIX)
33 typedef int SOCKET;
34 #endif
35
36 namespace net { 18 namespace net {
37 19
38 // Implements a raw socket interface 20 // Implements a TCP socket.
39 class NET_EXPORT TCPListenSocket : public ListenSocket, 21 class NET_EXPORT TCPListenSocket : public DefaultListenSocket {
40 #if defined(OS_WIN)
41 public base::win::ObjectWatcher::Delegate {
42 #elif defined(OS_POSIX)
43 public MessageLoopForIO::Watcher {
44 #endif
45 public: 22 public:
46 // Listen on port for the specified IP address. Use 127.0.0.1 to only 23 // Listen on port for the specified IP address. Use 127.0.0.1 to only
47 // accept local connections. 24 // accept local connections.
48 static TCPListenSocket* CreateAndListen(std::string ip, int port, 25 // Note that the deletion of the returned instance is under the
26 // responsibility of the caller.
27 static TCPListenSocket* CreateAndListen(const std::string& ip, int port,
49 ListenSocketDelegate* del); 28 ListenSocketDelegate* del);
50 29
51 // NOTE: This is for unit test use only!
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
57 protected: 30 protected:
58 enum WaitState {
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); 31 TCPListenSocket(SOCKET s, ListenSocketDelegate* del);
68 virtual ~TCPListenSocket(); 32 virtual ~TCPListenSocket();
69 static SOCKET CreateAndBind(std::string ip, int port);
70 // if valid, returned SOCKET is non-blocking
71 static SOCKET Accept(SOCKET s);
72 33
73 virtual void SendInternal(const char* bytes, int len) OVERRIDE; 34 static SOCKET CreateAndBind(const std::string& ip, int port);
74 35
75 virtual void Listen(); 36 virtual void Accept() OVERRIDE;
76 virtual void Accept();
77 virtual void Read();
78 virtual void Close();
79 virtual void CloseSocket(SOCKET s);
80
81 // Pass any value in case of Windows, because in Windows
82 // we are not using state.
83 void WatchSocket(WaitState state);
84 void UnwatchSocket();
85
86 #if defined(OS_WIN)
87 // ObjectWatcher delegate
88 virtual void OnObjectSignaled(HANDLE object);
89 base::win::ObjectWatcher watcher_;
90 HANDLE socket_event_;
91 #elif defined(OS_POSIX)
92 // Called by MessagePumpLibevent when the socket is ready to do I/O
93 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
94 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE;
95 WaitState wait_state_;
96 // The socket's libevent wrapper
97 MessageLoopForIO::FileDescriptorWatcher watcher_;
98 #endif
99
100 SOCKET socket_;
101 37
102 private: 38 private:
103 bool reads_paused_;
104 bool has_pending_reads_;
105
106 DISALLOW_COPY_AND_ASSIGN(TCPListenSocket); 39 DISALLOW_COPY_AND_ASSIGN(TCPListenSocket);
107 }; 40 };
108 41
109 } // namespace net 42 } // namespace net
110 43
111 #endif // NET_BASE_TCP_LISTEN_SOCKET_H_ 44 #endif // NET_BASE_TCP_LISTEN_SOCKET_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698