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 // Abstract socket server that handles IO asynchronously in the specified | 5 // Abstract socket server that handles IO asynchronously in the specified |
6 // MessageLoop. | 6 // MessageLoop. |
7 | 7 |
8 #ifndef NET_BASE_LISTEN_SOCKET_H_ | 8 #ifndef NET_BASE_LISTEN_SOCKET_H_ |
9 #define NET_BASE_LISTEN_SOCKET_H_ | 9 #define NET_BASE_LISTEN_SOCKET_H_ |
10 #pragma once | 10 #pragma once |
11 | 11 |
12 #include <string> | 12 #include <string> |
13 | 13 |
14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
17 #include "net/base/net_export.h" | 17 #include "net/base/net_export.h" |
18 | 18 |
19 namespace net { | 19 namespace net { |
20 | 20 |
21 // Defines a socket interface for a server. | 21 // Defines a socket interface for a server. |
22 class NET_EXPORT ListenSocket | 22 class NET_EXPORT ListenSocket |
23 : public base::RefCountedThreadSafe<ListenSocket> { | 23 : public base::RefCountedThreadSafe<ListenSocket> { |
24 public: | 24 public: |
25 // TODO(erikkay): this delegate should really be split into two parts | 25 // TODO(erikkay): this delegate should really be split into two parts |
26 // to split up the listener from the connected socket. Perhaps this class | 26 // to split up the listener from the connected socket. Perhaps this class |
27 // should be split up similarly. | 27 // should be split up similarly. |
28 class ListenSocketDelegate { | 28 class ListenSocketDelegate { |
29 public: | 29 public: |
30 virtual ~ListenSocketDelegate() {} | |
31 | |
32 // server is the original listening Socket, connection is the new | 30 // server is the original listening Socket, connection is the new |
33 // Socket that was created. Ownership of connection is transferred | 31 // Socket that was created. Ownership of connection is transferred |
34 // to the delegate with this call. | 32 // to the delegate with this call. |
35 virtual void DidAccept(ListenSocket *server, | 33 virtual void DidAccept(ListenSocket *server, |
36 ListenSocket *connection) = 0; | 34 ListenSocket *connection) = 0; |
37 virtual void DidRead(ListenSocket *connection, | 35 virtual void DidRead(ListenSocket *connection, |
38 const char* data, | 36 const char* data, |
39 int len) = 0; | 37 int len) = 0; |
40 virtual void DidClose(ListenSocket *sock) = 0; | 38 virtual void DidClose(ListenSocket *sock) = 0; |
| 39 |
| 40 protected: |
| 41 virtual ~ListenSocketDelegate() {} |
41 }; | 42 }; |
42 | 43 |
43 // Send data to the socket. | 44 // Send data to the socket. |
44 void Send(const char* bytes, int len, bool append_linefeed = false); | 45 void Send(const char* bytes, int len, bool append_linefeed = false); |
45 void Send(const std::string& str, bool append_linefeed = false); | 46 void Send(const std::string& str, bool append_linefeed = false); |
46 | 47 |
47 protected: | 48 protected: |
48 ListenSocket(ListenSocketDelegate* del); | 49 ListenSocket(ListenSocketDelegate* del); |
49 virtual ~ListenSocket(); | 50 virtual ~ListenSocket(); |
50 | 51 |
51 virtual void SendInternal(const char* bytes, int len) = 0; | 52 virtual void SendInternal(const char* bytes, int len) = 0; |
52 | 53 |
53 ListenSocketDelegate* const socket_delegate_; | 54 ListenSocketDelegate* const socket_delegate_; |
54 | 55 |
55 private: | 56 private: |
56 friend class base::RefCountedThreadSafe<ListenSocket>; | 57 friend class base::RefCountedThreadSafe<ListenSocket>; |
57 | 58 |
58 DISALLOW_COPY_AND_ASSIGN(ListenSocket); | 59 DISALLOW_COPY_AND_ASSIGN(ListenSocket); |
59 }; | 60 }; |
60 | 61 |
61 } // namespace net | 62 } // namespace net |
62 | 63 |
63 #endif // NET_BASE_LISTEN_SOCKET_H_ | 64 #endif // NET_BASE_LISTEN_SOCKET_H_ |
OLD | NEW |