OLD | NEW |
---|---|
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. | 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 | 6 // These objects are NOT thread safe. They use WSAEVENT handles to monitor |
7 // activity in a given MessageLoop. This means that callbacks will | 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 | 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. | 9 // constructors and destructors) should also be called from the same thread. |
10 | 10 |
11 #ifndef NET_BASE_LISTEN_SOCKET_H_ | 11 #ifndef NET_BASE_LISTEN_SOCKET_H_ |
12 #define NET_BASE_LISTEN_SOCKET_H_ | 12 #define NET_BASE_LISTEN_SOCKET_H_ |
13 #pragma once | 13 #pragma once |
14 | 14 |
15 #include <netinet/in.h> | |
16 | |
15 #include "build/build_config.h" | 17 #include "build/build_config.h" |
16 | 18 |
17 #if defined(OS_WIN) | 19 #if defined(OS_WIN) |
18 #include <winsock2.h> | 20 #include <winsock2.h> |
19 #endif | 21 #endif |
20 #include <string> | 22 #include <string> |
21 #if defined(OS_WIN) | 23 #if defined(OS_WIN) |
22 #include "base/win/object_watcher.h" | 24 #include "base/win/object_watcher.h" |
23 #elif defined(OS_POSIX) | 25 #elif defined(OS_POSIX) |
24 #include "base/message_loop.h" | 26 #include "base/message_loop.h" |
(...skipping 24 matching lines...) Expand all Loading... | |
49 virtual ~ListenSocketDelegate() {} | 51 virtual ~ListenSocketDelegate() {} |
50 | 52 |
51 // server is the original listening Socket, connection is the new | 53 // server is the original listening Socket, connection is the new |
52 // Socket that was created. Ownership of connection is transferred | 54 // Socket that was created. Ownership of connection is transferred |
53 // to the delegate with this call. | 55 // to the delegate with this call. |
54 virtual void DidAccept(ListenSocket *server, ListenSocket *connection) = 0; | 56 virtual void DidAccept(ListenSocket *server, ListenSocket *connection) = 0; |
55 virtual void DidRead(ListenSocket *connection, | 57 virtual void DidRead(ListenSocket *connection, |
56 const char* data, | 58 const char* data, |
57 int len) = 0; | 59 int len) = 0; |
58 virtual void DidClose(ListenSocket *sock) = 0; | 60 virtual void DidClose(ListenSocket *sock) = 0; |
61 virtual int protocol() { return IPPROTO_TCP; } | |
Mike Belshe
2011/04/06 18:32:53
nit: const function
| |
59 }; | 62 }; |
60 | 63 |
61 // Listen on port for the specified IP address. Use 127.0.0.1 to only | 64 // Listen on port for the specified IP address. Use 127.0.0.1 to only |
62 // accept local connections. | 65 // accept local connections. |
63 static ListenSocket* Listen(std::string ip, int port, | 66 static ListenSocket* Listen(std::string ip, int port, |
64 ListenSocketDelegate* del); | 67 ListenSocketDelegate* del); |
65 | 68 |
66 // Send data to the socket. | 69 // Send data to the socket. |
67 void Send(const char* bytes, int len, bool append_linefeed = false); | 70 void Send(const char* bytes, int len, bool append_linefeed = false); |
68 void Send(const std::string& str, bool append_linefeed = false); | 71 void Send(const std::string& str, bool append_linefeed = false); |
(...skipping 12 matching lines...) Expand all Loading... | |
81 WAITING_ACCEPT = 1, | 84 WAITING_ACCEPT = 1, |
82 WAITING_READ = 3, | 85 WAITING_READ = 3, |
83 WAITING_CLOSE = 4 | 86 WAITING_CLOSE = 4 |
84 }; | 87 }; |
85 | 88 |
86 static const SOCKET kInvalidSocket; | 89 static const SOCKET kInvalidSocket; |
87 static const int kSocketError; | 90 static const int kSocketError; |
88 | 91 |
89 ListenSocket(SOCKET s, ListenSocketDelegate* del); | 92 ListenSocket(SOCKET s, ListenSocketDelegate* del); |
90 virtual ~ListenSocket(); | 93 virtual ~ListenSocket(); |
91 static SOCKET Listen(std::string ip, int port); | 94 static SOCKET Listen(std::string ip, int port, int protocol); |
92 // if valid, returned SOCKET is non-blocking | 95 // if valid, returned SOCKET is non-blocking |
93 static SOCKET Accept(SOCKET s); | 96 static SOCKET Accept(SOCKET s); |
94 | 97 |
95 virtual void SendInternal(const char* bytes, int len); | 98 virtual void SendInternal(const char* bytes, int len); |
96 | 99 |
97 virtual void Listen(); | 100 virtual void Listen(); |
98 virtual void Accept(); | 101 virtual void Accept(); |
99 virtual void Read(); | 102 virtual void Read(); |
100 virtual void Close(); | 103 virtual void Close(); |
101 virtual void CloseSocket(SOCKET s); | 104 virtual void CloseSocket(SOCKET s); |
(...skipping 21 matching lines...) Expand all Loading... | |
123 ListenSocketDelegate *socket_delegate_; | 126 ListenSocketDelegate *socket_delegate_; |
124 | 127 |
125 private: | 128 private: |
126 bool reads_paused_; | 129 bool reads_paused_; |
127 bool has_pending_reads_; | 130 bool has_pending_reads_; |
128 | 131 |
129 DISALLOW_COPY_AND_ASSIGN(ListenSocket); | 132 DISALLOW_COPY_AND_ASSIGN(ListenSocket); |
130 }; | 133 }; |
131 | 134 |
132 #endif // NET_BASE_LISTEN_SOCKET_H_ | 135 #endif // NET_BASE_LISTEN_SOCKET_H_ |
OLD | NEW |