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 /// |
32 /// @param[in] instance The instance with which this resource will be | 35 /// @param[in] instance The instance with which this resource will be |
33 /// associated. | 36 /// associated. |
34 explicit TCPSocket(const InstanceHandle& instance); | 37 /// @param[in] faimly The address family type for the socket. |
| 38 TCPSocket(const InstanceHandle& instance, PP_NetAddress_Family family); |
35 | 39 |
36 /// A constructor used when you have received a <code>PP_Resource</code> as a | 40 /// A constructor used when you have received a <code>PP_Resource</code> as a |
37 /// return value that has had 1 ref added for you. | 41 /// return value that has had 1 ref added for you. |
38 /// | 42 /// |
39 /// @param[in] resource A <code>PPB_TCPSocket</code> resource. | 43 /// @param[in] resource A <code>PPB_TCPSocket</code> resource. |
40 TCPSocket(PassRef, PP_Resource resource); | 44 TCPSocket(PassRef, PP_Resource resource); |
41 | 45 |
42 /// The copy constructor for <code>TCPSocket</code>. | 46 /// The copy constructor for <code>TCPSocket</code>. |
43 /// | 47 /// |
44 /// @param[in] other A reference to another <code>TCPSocket</code>. | 48 /// @param[in] other A reference to another <code>TCPSocket</code>. |
45 TCPSocket(const TCPSocket& other); | 49 TCPSocket(const TCPSocket& other); |
46 | 50 |
47 /// The destructor. | 51 /// The destructor. |
48 virtual ~TCPSocket(); | 52 virtual ~TCPSocket(); |
49 | 53 |
50 /// The assignment operator for <code>TCPSocket</code>. | 54 /// The assignment operator for <code>TCPSocket</code>. |
51 /// | 55 /// |
52 /// @param[in] other A reference to another <code>TCPSocket</code>. | 56 /// @param[in] other A reference to another <code>TCPSocket</code>. |
53 /// | 57 /// |
54 /// @return A reference to this <code>TCPSocket</code> object. | 58 /// @return A reference to this <code>TCPSocket</code> object. |
55 TCPSocket& operator=(const TCPSocket& other); | 59 TCPSocket& operator=(const TCPSocket& other); |
56 | 60 |
57 /// Static function for determining whether the browser supports the | 61 /// Static function for determining whether the browser supports the |
58 /// <code>PPB_TCPSocket</code> interface. | 62 /// <code>PPB_TCPSocket</code> interface. |
59 /// | 63 /// |
60 /// @return true if the interface is available, false otherwise. | 64 /// @return true if the interface is available, false otherwise. |
61 static bool IsAvailable(); | 65 static bool IsAvailable(); |
62 | 66 |
63 /// Connects the socket to the given address. | 67 /// Binds the socket to the given address. The socket must not be bound. |
64 /// | 68 /// |
65 /// @param[in] addr A <code>NetAddress</code> object. | 69 /// @param[in] addr A <code>NetAddress</code> object. |
66 /// @param[in] callback A <code>CompletionCallback</code> to be called upon | 70 /// @param[in] callback A <code>CompletionCallback</code> to be called upon |
| 71 /// completion. |
| 72 /// |
| 73 /// @return An int32_t containing an error code from <code>pp_errors.h</code>, |
| 74 /// including (but not limited to): |
| 75 /// - <code>PP_ERROR_ADDRESS_IN_USE</code>: the address is already in use. |
| 76 /// - <code>PP_ERROR_ADDRESS_INVALID</code>: the address is invalid. |
| 77 int32_t Bind(const NetAddress& addr, const CompletionCallback& callback); |
| 78 |
| 79 /// Connects the socket to the given address. The socket must not be |
| 80 /// listening. |
| 81 /// |
| 82 /// @param[in] addr A <code>NetAddress</code> object. |
| 83 /// @param[in] callback A <code>CompletionCallback</code> to be called upon |
67 /// completion. | 84 /// completion. |
68 /// | 85 /// |
69 /// @return An int32_t containing an error code from <code>pp_errors.h</code>, | 86 /// @return An int32_t containing an error code from <code>pp_errors.h</code>, |
70 /// including (but not limited to): | 87 /// including (but not limited to): |
71 /// - <code>PP_ERROR_NOACCESS</code>: the caller doesn't have required | 88 /// - <code>PP_ERROR_NOACCESS</code>: the caller doesn't have required |
72 /// permissions. | 89 /// permissions. |
73 /// - <code>PP_ERROR_ADDRESS_UNREACHABLE</code>: <code>addr</code> is | 90 /// - <code>PP_ERROR_ADDRESS_UNREACHABLE</code>: <code>addr</code> is |
74 /// unreachable. | 91 /// unreachable. |
75 /// - <code>PP_ERROR_CONNECTION_REFUSED</code>: the connection attempt was | 92 /// - <code>PP_ERROR_CONNECTION_REFUSED</code>: the connection attempt was |
76 /// refused. | 93 /// refused. |
77 /// - <code>PP_ERROR_CONNECTION_FAILED</code>: the connection attempt failed. | 94 /// - <code>PP_ERROR_CONNECTION_FAILED</code>: the connection attempt failed. |
78 /// - <code>PP_ERROR_CONNECTION_TIMEDOUT</code>: the connection attempt timed | 95 /// - <code>PP_ERROR_CONNECTION_TIMEDOUT</code>: the connection attempt timed |
79 /// out. | 96 /// out. |
80 int32_t Connect(const NetAddress& addr, | 97 /// |
81 const CompletionCallback& callback); | 98 /// If the socket is listening/connected or has a pending listen/connect |
| 99 /// request, <code>Connect()</code> will fail without starting a connection |
| 100 /// attempt. Otherwise, any failure during the connection attempt will cause |
| 101 /// the socket to be closed. |
| 102 int32_t Connect(const NetAddress& addr, const CompletionCallback& callback); |
82 | 103 |
83 /// Gets the local address of the socket, if it is connected. | 104 /// Gets the local address of the socket, if it is bound. |
84 /// | 105 /// |
85 /// @return A <code>NetAddress</code> object. The object will be null | 106 /// @return A <code>NetAddress</code> object. The object will be null |
86 /// (i.e., is_null() returns true) on failure. | 107 /// (i.e., is_null() returns true) on failure. |
87 NetAddress GetLocalAddress() const; | 108 NetAddress GetLocalAddress() const; |
88 | 109 |
89 /// Gets the remote address of the socket, if it is connected. | 110 /// Gets the remote address of the socket, if it is connected. |
90 /// | 111 /// |
91 /// @return A <code>NetAddress</code> object. The object will be null | 112 /// @return A <code>NetAddress</code> object. The object will be null |
92 /// (i.e., is_null() returns true) on failure. | 113 /// (i.e., is_null() returns true) on failure. |
93 NetAddress GetRemoteAddress() const; | 114 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. | 149 /// @param[in] bytes_to_write The number of bytes to write. |
129 /// @param[in] callback A <code>CompletionCallback</code> to be called upon | 150 /// @param[in] callback A <code>CompletionCallback</code> to be called upon |
130 /// completion. | 151 /// completion. |
131 /// | 152 /// |
132 /// @return A non-negative number on success to indicate how many bytes have | 153 /// @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>. | 154 /// been written; otherwise, an error code from <code>pp_errors.h</code>. |
134 int32_t Write(const char* buffer, | 155 int32_t Write(const char* buffer, |
135 int32_t bytes_to_write, | 156 int32_t bytes_to_write, |
136 const CompletionCallback& callback); | 157 const CompletionCallback& callback); |
137 | 158 |
138 /// Cancels all pending reads and writes and disconnects the socket. Any | 159 /// Starts listening. The socket must be bound and not connected. |
139 /// pending callbacks will still run, reporting <code>PP_ERROR_ABORTED</code> | 160 /// |
140 /// if pending IO was interrupted. After a call to this method, no output | 161 /// @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 | 162 /// queue of pending connections may grow. |
142 /// accessed. It is not valid to call <code>Connect()</code> again. | 163 /// @param[in] callback A <code>CompletionCallback</code> to be called upon |
| 164 /// completion. |
| 165 /// |
| 166 /// @return An int32_t containing an error code from <code>pp_errors.h</code>, |
| 167 /// including (but not limited to): |
| 168 /// - <code>PP_ERROR_NOACCESS</code>: the caller doesn't have required |
| 169 /// permissions. |
| 170 /// - <code>PP_ERROR_ADDRESS_IN_USE</code>: Another socket is already |
| 171 /// listening on the same port. |
| 172 int32_t Listen(int32_t backlog, |
| 173 const CompletionCallback& callback); |
| 174 |
| 175 /// Accepts a connection. The socket must be listening. |
| 176 /// |
| 177 /// @param[in] callback A <code>CompletionCallbackWithOutput</code> to be |
| 178 /// called upon completion. |
| 179 /// |
| 180 /// @return An int32_t containing an error code from <code>pp_errors.h</code>, |
| 181 /// including (but not limited to): |
| 182 /// - <code>PP_ERROR_CONNECTION_ABORTED</code>: A connection has been aborted. |
| 183 int32_t Accept(const CompletionCallbackWithOutput<TCPSocket>& callback); |
| 184 |
| 185 /// Cancels all pending operations and closes the socket. Any pending |
| 186 /// callbacks will still run, reporting <code>PP_ERROR_ABORTED</code> if |
| 187 /// pending IO was interrupted. After a call to this method, no output buffer |
| 188 /// pointers passed into previous <code>Read()</code> or <code>Accept()</code> |
| 189 /// calls will be accessed. It is not valid to call <code>Connect()</code> or |
| 190 /// <code>Listen()</code> again. |
143 /// | 191 /// |
144 /// The socket is implicitly closed if it is destroyed, so you are not | 192 /// The socket is implicitly closed if it is destroyed, so you are not |
145 /// required to call this method. | 193 /// required to call this method. |
146 void Close(); | 194 void Close(); |
147 | 195 |
148 /// Sets a socket option on the TCP socket. | 196 /// Sets a socket option on the TCP socket. |
149 /// Please see the <code>PP_TCPSocket_Option</code> description for option | 197 /// Please see the <code>PP_TCPSocket_Option</code> description for option |
150 /// names, value types and allowed values. | 198 /// names, value types and allowed values. |
151 /// | 199 /// |
152 /// @param[in] name The option to set. | 200 /// @param[in] name The option to set. |
153 /// @param[in] value The option value to set. | 201 /// @param[in] value The option value to set. |
154 /// @param[in] callback A <code>CompletionCallback</code> to be called upon | 202 /// @param[in] callback A <code>CompletionCallback</code> to be called upon |
155 /// completion. | 203 /// completion. |
156 /// | 204 /// |
157 /// @return An int32_t containing an error code from <code>pp_errors.h</code>. | 205 /// @return An int32_t containing an error code from <code>pp_errors.h</code>. |
158 //// | |
159 int32_t SetOption(PP_TCPSocket_Option name, | 206 int32_t SetOption(PP_TCPSocket_Option name, |
160 const Var& value, | 207 const Var& value, |
161 const CompletionCallback& callback); | 208 const CompletionCallback& callback); |
162 }; | 209 }; |
163 | 210 |
164 } // namespace pp | 211 } // namespace pp |
165 | 212 |
166 #endif // PPAPI_CPP_TCP_SOCKET_H_ | 213 #endif // PPAPI_CPP_TCP_SOCKET_H_ |
OLD | NEW |