OLD | NEW |
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 // Stream-based listen socket implementation that handles reading and writing | 5 // Stream-based listen socket implementation that handles reading and writing |
6 // to the socket, but does not handle creating the socket nor connecting | 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, | 7 // sockets, which are handled by subclasses on creation and in Accept, |
8 // respectively. | 8 // respectively. |
9 | 9 |
10 // StreamListenSocket handles IO asynchronously in the specified MessageLoop. | 10 // StreamListenSocket handles IO asynchronously in the specified MessageLoop. |
(...skipping 12 matching lines...) Expand all Loading... |
23 #endif | 23 #endif |
24 #include <string> | 24 #include <string> |
25 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
26 #include "base/win/object_watcher.h" | 26 #include "base/win/object_watcher.h" |
27 #elif defined(OS_POSIX) | 27 #elif defined(OS_POSIX) |
28 #include "base/message_loop/message_loop.h" | 28 #include "base/message_loop/message_loop.h" |
29 #endif | 29 #endif |
30 | 30 |
31 #include "base/basictypes.h" | 31 #include "base/basictypes.h" |
32 #include "base/compiler_specific.h" | 32 #include "base/compiler_specific.h" |
| 33 #include "base/memory/scoped_ptr.h" |
33 #include "net/base/net_export.h" | 34 #include "net/base/net_export.h" |
34 #include "net/socket/socket_descriptor.h" | 35 #include "net/socket/socket_descriptor.h" |
35 | 36 |
36 namespace net { | 37 namespace net { |
37 | 38 |
38 class IPEndPoint; | 39 class IPEndPoint; |
39 | 40 |
40 class NET_EXPORT StreamListenSocket | 41 class NET_EXPORT StreamListenSocket |
41 : public base::RefCountedThreadSafe<StreamListenSocket>, | 42 : |
42 #if defined(OS_WIN) | 43 #if defined(OS_WIN) |
43 public base::win::ObjectWatcher::Delegate { | 44 public base::win::ObjectWatcher::Delegate { |
44 #elif defined(OS_POSIX) | 45 #elif defined(OS_POSIX) |
45 public base::MessageLoopForIO::Watcher { | 46 public base::MessageLoopForIO::Watcher { |
46 #endif | 47 #endif |
47 | 48 |
48 public: | 49 public: |
| 50 virtual ~StreamListenSocket(); |
| 51 |
49 // TODO(erikkay): this delegate should really be split into two parts | 52 // TODO(erikkay): this delegate should really be split into two parts |
50 // to split up the listener from the connected socket. Perhaps this class | 53 // to split up the listener from the connected socket. Perhaps this class |
51 // should be split up similarly. | 54 // should be split up similarly. |
52 class Delegate { | 55 class Delegate { |
53 public: | 56 public: |
54 // |server| is the original listening Socket, connection is the new | 57 // |server| is the original listening Socket, connection is the new |
55 // Socket that was created. Ownership of |connection| is transferred | 58 // Socket that was created. |
56 // to the delegate with this call. | |
57 virtual void DidAccept(StreamListenSocket* server, | 59 virtual void DidAccept(StreamListenSocket* server, |
58 StreamListenSocket* connection) = 0; | 60 scoped_ptr<StreamListenSocket> connection) = 0; |
59 virtual void DidRead(StreamListenSocket* connection, | 61 virtual void DidRead(StreamListenSocket* connection, |
60 const char* data, | 62 const char* data, |
61 int len) = 0; | 63 int len) = 0; |
62 virtual void DidClose(StreamListenSocket* sock) = 0; | 64 virtual void DidClose(StreamListenSocket* sock) = 0; |
63 | 65 |
64 protected: | 66 protected: |
65 virtual ~Delegate() {} | 67 virtual ~Delegate() {} |
66 }; | 68 }; |
67 | 69 |
68 // Send data to the socket. | 70 // Send data to the socket. |
69 void Send(const char* bytes, int len, bool append_linefeed = false); | 71 void Send(const char* bytes, int len, bool append_linefeed = false); |
70 void Send(const std::string& str, bool append_linefeed = false); | 72 void Send(const std::string& str, bool append_linefeed = false); |
71 | 73 |
72 // Copies the local address to |address|. Returns a network error code. | 74 // Copies the local address to |address|. Returns a network error code. |
73 int GetLocalAddress(IPEndPoint* address); | 75 int GetLocalAddress(IPEndPoint* address); |
74 | 76 |
75 static const int kSocketError; | 77 static const int kSocketError; |
76 | 78 |
77 protected: | 79 protected: |
78 enum WaitState { | 80 enum WaitState { |
79 NOT_WAITING = 0, | 81 NOT_WAITING = 0, |
80 WAITING_ACCEPT = 1, | 82 WAITING_ACCEPT = 1, |
81 WAITING_READ = 2 | 83 WAITING_READ = 2 |
82 }; | 84 }; |
83 | 85 |
84 StreamListenSocket(SocketDescriptor s, Delegate* del); | 86 StreamListenSocket(SocketDescriptor s, Delegate* del); |
85 virtual ~StreamListenSocket(); | |
86 | 87 |
87 SocketDescriptor AcceptSocket(); | 88 SocketDescriptor AcceptSocket(); |
88 virtual void Accept() = 0; | 89 virtual void Accept() = 0; |
89 | 90 |
90 void Listen(); | 91 void Listen(); |
91 void Read(); | 92 void Read(); |
92 void Close(); | 93 void Close(); |
93 void CloseSocket(SocketDescriptor s); | 94 void CloseSocket(SocketDescriptor s); |
94 | 95 |
95 // Pass any value in case of Windows, because in Windows | 96 // Pass any value in case of Windows, because in Windows |
96 // we are not using state. | 97 // we are not using state. |
97 void WatchSocket(WaitState state); | 98 void WatchSocket(WaitState state); |
98 void UnwatchSocket(); | 99 void UnwatchSocket(); |
99 | 100 |
100 Delegate* const socket_delegate_; | 101 Delegate* const socket_delegate_; |
101 | 102 |
102 private: | 103 private: |
103 friend class base::RefCountedThreadSafe<StreamListenSocket>; | |
104 friend class TransportClientSocketTest; | 104 friend class TransportClientSocketTest; |
105 | 105 |
106 void SendInternal(const char* bytes, int len); | 106 void SendInternal(const char* bytes, int len); |
107 | 107 |
108 #if defined(OS_WIN) | 108 #if defined(OS_WIN) |
109 // ObjectWatcher delegate. | 109 // ObjectWatcher delegate. |
110 virtual void OnObjectSignaled(HANDLE object); | 110 virtual void OnObjectSignaled(HANDLE object); |
111 base::win::ObjectWatcher watcher_; | 111 base::win::ObjectWatcher watcher_; |
112 HANDLE socket_event_; | 112 HANDLE socket_event_; |
113 #elif defined(OS_POSIX) | 113 #elif defined(OS_POSIX) |
(...skipping 18 matching lines...) Expand all Loading... |
132 DISALLOW_COPY_AND_ASSIGN(StreamListenSocket); | 132 DISALLOW_COPY_AND_ASSIGN(StreamListenSocket); |
133 }; | 133 }; |
134 | 134 |
135 // Abstract factory that must be subclassed for each subclass of | 135 // Abstract factory that must be subclassed for each subclass of |
136 // StreamListenSocket. | 136 // StreamListenSocket. |
137 class NET_EXPORT StreamListenSocketFactory { | 137 class NET_EXPORT StreamListenSocketFactory { |
138 public: | 138 public: |
139 virtual ~StreamListenSocketFactory() {} | 139 virtual ~StreamListenSocketFactory() {} |
140 | 140 |
141 // Returns a new instance of StreamListenSocket or NULL if an error occurred. | 141 // Returns a new instance of StreamListenSocket or NULL if an error occurred. |
142 virtual scoped_refptr<StreamListenSocket> CreateAndListen( | 142 virtual scoped_ptr<StreamListenSocket> CreateAndListen( |
143 StreamListenSocket::Delegate* delegate) const = 0; | 143 StreamListenSocket::Delegate* delegate) const = 0; |
144 }; | 144 }; |
145 | 145 |
146 } // namespace net | 146 } // namespace net |
147 | 147 |
148 #endif // NET_SOCKET_STREAM_LISTEN_SOCKET_H_ | 148 #endif // NET_SOCKET_STREAM_LISTEN_SOCKET_H_ |
OLD | NEW |