OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #ifndef PPAPI_CPP_TCP_SOCKET_H_ | 5 #ifndef PPAPI_CPP_TCP_SOCKET_H_ |
6 #define PPAPI_CPP_TCP_SOCKET_H_ | 6 #define PPAPI_CPP_TCP_SOCKET_H_ |
7 | 7 |
8 #include "ppapi/c/ppb_tcp_socket.h" | 8 #include "ppapi/c/ppb_tcp_socket.h" |
9 #include "ppapi/cpp/net_address.h" | 9 #include "ppapi/cpp/net_address.h" |
10 #include "ppapi/cpp/pass_ref.h" | 10 #include "ppapi/cpp/pass_ref.h" |
11 #include "ppapi/cpp/resource.h" | 11 #include "ppapi/cpp/resource.h" |
12 | 12 |
13 namespace pp { | 13 namespace pp { |
14 | 14 |
15 class CompletionCallback; | 15 class CompletionCallback; |
16 class InstanceHandle; | 16 class InstanceHandle; |
17 | 17 |
| 18 template <typename T> class CompletionCallbackWithOutput; |
| 19 |
18 /// The <code>TCPSocket</code> class provides TCP socket operations. | 20 /// The <code>TCPSocket</code> class provides TCP socket operations. |
19 /// | 21 /// |
20 /// Permissions: Apps permission <code>socket</code> with subrule | 22 /// Permissions: Apps permission <code>socket</code> with subrule |
21 /// <code>tcp-connect</code> is required for <code>Connect()</code>. | 23 /// <code>tcp-connect</code> is required for <code>Connect()</code>; subrule |
| 24 /// <code>tcp-listen</code> is required for <code>Listen()</code>. |
22 /// For more details about network communication permissions, please see: | 25 /// For more details about network communication permissions, please see: |
23 /// http://developer.chrome.com/apps/app_network.html | 26 /// http://developer.chrome.com/apps/app_network.html |
24 class TCPSocket : public Resource { | 27 class TCPSocket : public Resource { |
25 public: | 28 public: |
26 /// Default constructor for creating an is_null() <code>TCPSocket</code> | 29 /// Default constructor for creating an is_null() <code>TCPSocket</code> |
27 /// object. | 30 /// object. |
28 TCPSocket(); | 31 TCPSocket(); |
29 | 32 |
30 /// A constructor used to create a <code>TCPSocket</code> object. | 33 /// A constructor used to create a <code>TCPSocket</code> object. |
31 /// | 34 /// |
(...skipping 21 matching lines...) Expand all Loading... |
53 /// | 56 /// |
54 /// @return A reference to this <code>TCPSocket</code> object. | 57 /// @return A reference to this <code>TCPSocket</code> object. |
55 TCPSocket& operator=(const TCPSocket& other); | 58 TCPSocket& operator=(const TCPSocket& other); |
56 | 59 |
57 /// Static function for determining whether the browser supports the | 60 /// Static function for determining whether the browser supports the |
58 /// <code>PPB_TCPSocket</code> interface. | 61 /// <code>PPB_TCPSocket</code> interface. |
59 /// | 62 /// |
60 /// @return true if the interface is available, false otherwise. | 63 /// @return true if the interface is available, false otherwise. |
61 static bool IsAvailable(); | 64 static bool IsAvailable(); |
62 | 65 |
63 /// Connects the socket to the given address. | 66 /// Binds the socket to the given address. The socket must not be bound. |
64 /// | 67 /// |
65 /// @param[in] addr A <code>NetAddress</code> object. | 68 /// @param[in] addr A <code>NetAddress</code> object. |
66 /// @param[in] callback A <code>CompletionCallback</code> to be called upon | 69 /// @param[in] callback A <code>CompletionCallback</code> to be called upon |
| 70 /// completion. |
| 71 /// |
| 72 /// @return An int32_t containing an error code from <code>pp_errors.h</code>, |
| 73 /// including (but not limited to): |
| 74 /// - <code>PP_ERROR_ADDRESS_IN_USE</code>: the address is already in use. |
| 75 /// - <code>PP_ERROR_ADDRESS_INVALID</code>: the address is invalid. |
| 76 int32_t Bind(const NetAddress& addr, const CompletionCallback& callback); |
| 77 |
| 78 /// Connects the socket to the given address. The socket must not be |
| 79 /// listening. Binding the socket beforehand is optional. |
| 80 /// |
| 81 /// @param[in] addr A <code>NetAddress</code> object. |
| 82 /// @param[in] callback A <code>CompletionCallback</code> to be called upon |
67 /// completion. | 83 /// completion. |
68 /// | 84 /// |
69 /// @return An int32_t containing an error code from <code>pp_errors.h</code>, | 85 /// @return An int32_t containing an error code from <code>pp_errors.h</code>, |
70 /// including (but not limited to): | 86 /// including (but not limited to): |
71 /// - <code>PP_ERROR_NOACCESS</code>: the caller doesn't have required | 87 /// - <code>PP_ERROR_NOACCESS</code>: the caller doesn't have required |
72 /// permissions. | 88 /// permissions. |
73 /// - <code>PP_ERROR_ADDRESS_UNREACHABLE</code>: <code>addr</code> is | 89 /// - <code>PP_ERROR_ADDRESS_UNREACHABLE</code>: <code>addr</code> is |
74 /// unreachable. | 90 /// unreachable. |
75 /// - <code>PP_ERROR_CONNECTION_REFUSED</code>: the connection attempt was | 91 /// - <code>PP_ERROR_CONNECTION_REFUSED</code>: the connection attempt was |
76 /// refused. | 92 /// refused. |
77 /// - <code>PP_ERROR_CONNECTION_FAILED</code>: the connection attempt failed. | 93 /// - <code>PP_ERROR_CONNECTION_FAILED</code>: the connection attempt failed. |
78 /// - <code>PP_ERROR_CONNECTION_TIMEDOUT</code>: the connection attempt timed | 94 /// - <code>PP_ERROR_CONNECTION_TIMEDOUT</code>: the connection attempt timed |
79 /// out. | 95 /// out. |
80 int32_t Connect(const NetAddress& addr, | 96 /// |
81 const CompletionCallback& callback); | 97 /// If the socket is listening/connected or has a pending listen/connect |
| 98 /// request, <code>Connect()</code> will fail without starting a connection |
| 99 /// attempt. Otherwise, any failure during the connection attempt will cause |
| 100 /// the socket to be closed. |
| 101 int32_t Connect(const NetAddress& addr, const CompletionCallback& callback); |
82 | 102 |
83 /// Gets the local address of the socket, if it is connected. | 103 /// Gets the local address of the socket, if it is bound. |
84 /// | 104 /// |
85 /// @return A <code>NetAddress</code> object. The object will be null | 105 /// @return A <code>NetAddress</code> object. The object will be null |
86 /// (i.e., is_null() returns true) on failure. | 106 /// (i.e., is_null() returns true) on failure. |
87 NetAddress GetLocalAddress() const; | 107 NetAddress GetLocalAddress() const; |
88 | 108 |
89 /// Gets the remote address of the socket, if it is connected. | 109 /// Gets the remote address of the socket, if it is connected. |
90 /// | 110 /// |
91 /// @return A <code>NetAddress</code> object. The object will be null | 111 /// @return A <code>NetAddress</code> object. The object will be null |
92 /// (i.e., is_null() returns true) on failure. | 112 /// (i.e., is_null() returns true) on failure. |
93 NetAddress GetRemoteAddress() const; | 113 NetAddress GetRemoteAddress() const; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 /// @param[in] bytes_to_write The number of bytes to write. | 148 /// @param[in] bytes_to_write The number of bytes to write. |
129 /// @param[in] callback A <code>CompletionCallback</code> to be called upon | 149 /// @param[in] callback A <code>CompletionCallback</code> to be called upon |
130 /// completion. | 150 /// completion. |
131 /// | 151 /// |
132 /// @return A non-negative number on success to indicate how many bytes have | 152 /// @return A non-negative number on success to indicate how many bytes have |
133 /// been written; otherwise, an error code from <code>pp_errors.h</code>. | 153 /// been written; otherwise, an error code from <code>pp_errors.h</code>. |
134 int32_t Write(const char* buffer, | 154 int32_t Write(const char* buffer, |
135 int32_t bytes_to_write, | 155 int32_t bytes_to_write, |
136 const CompletionCallback& callback); | 156 const CompletionCallback& callback); |
137 | 157 |
138 /// Cancels all pending reads and writes and disconnects the socket. Any | 158 /// Starts listening. The socket must be bound and not connected. |
139 /// pending callbacks will still run, reporting <code>PP_ERROR_ABORTED</code> | 159 /// |
140 /// if pending IO was interrupted. After a call to this method, no output | 160 /// @param[in] backlog A hint to determine the maximum length to which the |
141 /// buffer pointers passed into previous <code>Read()</code> calls will be | 161 /// queue of pending connections may grow. |
142 /// accessed. It is not valid to call <code>Connect()</code> again. | 162 /// @param[in] callback A <code>CompletionCallback</code> to be called upon |
| 163 /// completion. |
| 164 /// |
| 165 /// @return An int32_t containing an error code from <code>pp_errors.h</code>, |
| 166 /// including (but not limited to): |
| 167 /// - <code>PP_ERROR_NOACCESS</code>: the caller doesn't have required |
| 168 /// permissions. |
| 169 /// - <code>PP_ERROR_ADDRESS_IN_USE</code>: Another socket is already |
| 170 /// listening on the same port. |
| 171 int32_t Listen(int32_t backlog, |
| 172 const CompletionCallback& callback); |
| 173 |
| 174 /// Accepts a connection. The socket must be listening. |
| 175 /// |
| 176 /// @param[in] callback A <code>CompletionCallbackWithOutput</code> to be |
| 177 /// called upon completion. |
| 178 /// |
| 179 /// @return An int32_t containing an error code from <code>pp_errors.h</code>, |
| 180 /// including (but not limited to): |
| 181 /// - <code>PP_ERROR_CONNECTION_ABORTED</code>: A connection has been aborted. |
| 182 int32_t Accept(const CompletionCallbackWithOutput<TCPSocket>& callback); |
| 183 |
| 184 /// Cancels all pending operations and closes the socket. Any pending |
| 185 /// callbacks will still run, reporting <code>PP_ERROR_ABORTED</code> if |
| 186 /// pending IO was interrupted. After a call to this method, no output buffer |
| 187 /// pointers passed into previous <code>Read()</code> or <code>Accept()</code> |
| 188 /// calls will be accessed. It is not valid to call <code>Connect()</code> or |
| 189 /// <code>Listen()</code> again. |
143 /// | 190 /// |
144 /// The socket is implicitly closed if it is destroyed, so you are not | 191 /// The socket is implicitly closed if it is destroyed, so you are not |
145 /// required to call this method. | 192 /// required to call this method. |
146 void Close(); | 193 void Close(); |
147 | 194 |
148 /// Sets a socket option on the TCP socket. | 195 /// Sets a socket option on the TCP socket. |
149 /// Please see the <code>PP_TCPSocket_Option</code> description for option | 196 /// Please see the <code>PP_TCPSocket_Option</code> description for option |
150 /// names, value types and allowed values. | 197 /// names, value types and allowed values. |
151 /// | 198 /// |
152 /// @param[in] name The option to set. | 199 /// @param[in] name The option to set. |
153 /// @param[in] value The option value to set. | 200 /// @param[in] value The option value to set. |
154 /// @param[in] callback A <code>CompletionCallback</code> to be called upon | 201 /// @param[in] callback A <code>CompletionCallback</code> to be called upon |
155 /// completion. | 202 /// completion. |
156 /// | 203 /// |
157 /// @return An int32_t containing an error code from <code>pp_errors.h</code>. | 204 /// @return An int32_t containing an error code from <code>pp_errors.h</code>. |
158 //// | |
159 int32_t SetOption(PP_TCPSocket_Option name, | 205 int32_t SetOption(PP_TCPSocket_Option name, |
160 const Var& value, | 206 const Var& value, |
161 const CompletionCallback& callback); | 207 const CompletionCallback& callback); |
162 }; | 208 }; |
163 | 209 |
164 } // namespace pp | 210 } // namespace pp |
165 | 211 |
166 #endif // PPAPI_CPP_TCP_SOCKET_H_ | 212 #endif // PPAPI_CPP_TCP_SOCKET_H_ |
OLD | NEW |