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