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
3 * found in the LICENSE file.
7 * This file defines the <code>PPB_TCPSocket</code> interface.
16 * Option names used by <code>SetOption()</code>.
19 enum PP_TCPSocket_Option
{
21 * Disables coalescing of small writes to make TCP segments, and instead
22 * delivers data immediately. Value's type is <code>PP_VARTYPE_BOOL</code>.
23 * This option can only be set after a successful <code>Connect()</code> call.
25 PP_TCPSOCKET_OPTION_NO_DELAY
= 0,
28 * Specifies the total per-socket buffer space reserved for sends. Value's
29 * type should be <code>PP_VARTYPE_INT32</code>.
30 * This option can only be set after a successful <code>Connect()</code> call.
32 * Note: This is only treated as a hint for the browser to set the buffer
33 * size. Even if <code>SetOption()</code> succeeds, the browser doesn't
34 * guarantee it will conform to the size.
36 PP_TCPSOCKET_OPTION_SEND_BUFFER_SIZE
= 1,
39 * Specifies the total per-socket buffer space reserved for receives. Value's
40 * type should be <code>PP_VARTYPE_INT32</code>.
41 * This option can only be set after a successful <code>Connect()</code> call.
43 * Note: This is only treated as a hint for the browser to set the buffer
44 * size. Even if <code>SetOption()</code> succeeds, the browser doesn't
45 * guarantee it will conform to the size.
47 PP_TCPSOCKET_OPTION_RECV_BUFFER_SIZE
= 2
51 * The <code>PPB_TCPSocket</code> interface provides TCP socket operations.
53 * Permissions: Apps permission <code>socket</code> with subrule
54 * <code>tcp-connect</code> is required for <code>Connect()</code>; subrule
55 * <code>tcp-listen</code> is required for <code>Listen()</code>.
56 * For more details about network communication permissions, please see:
57 * http://developer.chrome.com/apps/app_network.html
59 interface PPB_TCPSocket
{
61 * Creates a TCP socket resource.
63 * @param[in] instance A <code>PP_Instance</code> identifying one instance of
66 * @return A <code>PP_Resource</code> corresponding to a TCP socket or 0
69 PP_Resource Create
([in] PP_Instance instance
);
72 * Determines if a given resource is a TCP socket.
74 * @param[in] resource A <code>PP_Resource</code> to check.
76 * @return <code>PP_TRUE</code> if the input is a
77 * <code>PPB_TCPSocket</code> resource; <code>PP_FALSE</code> otherwise.
79 PP_Bool IsTCPSocket
([in] PP_Resource resource
);
82 * Binds the socket to the given address. The socket must not be bound.
84 * @param[in] tcp_socket A <code>PP_Resource</code> corresponding to a TCP
86 * @param[in] addr A <code>PPB_NetAddress</code> resource.
87 * @param[in] callback A <code>PP_CompletionCallback</code> to be called upon
90 * @return An int32_t containing an error code from <code>pp_errors.h</code>,
91 * including (but not limited to):
92 * - <code>PP_ERROR_ADDRESS_IN_USE</code>: the address is already in use.
93 * - <code>PP_ERROR_ADDRESS_INVALID</code>: the address is invalid.
96 int32_t Bind
([in] PP_Resource tcp_socket
,
97 [in] PP_Resource addr
,
98 [in] PP_CompletionCallback
callback);
101 * Connects the socket to the given address. The socket must not be listening.
102 * Binding the socket beforehand is optional.
104 * @param[in] tcp_socket A <code>PP_Resource</code> corresponding to a TCP
106 * @param[in] addr A <code>PPB_NetAddress</code> resource.
107 * @param[in] callback A <code>PP_CompletionCallback</code> to be called upon
110 * @return An int32_t containing an error code from <code>pp_errors.h</code>,
111 * including (but not limited to):
112 * - <code>PP_ERROR_NOACCESS</code>: the caller doesn't have required
114 * - <code>PP_ERROR_ADDRESS_UNREACHABLE</code>: <code>addr</code> is
116 * - <code>PP_ERROR_CONNECTION_REFUSED</code>: the connection attempt was
118 * - <code>PP_ERROR_CONNECTION_FAILED</code>: the connection attempt failed.
119 * - <code>PP_ERROR_CONNECTION_TIMEDOUT</code>: the connection attempt timed
122 * Since version 1.1, if the socket is listening/connected or has a pending
123 * listen/connect request, <code>Connect()</code> will fail without starting a
124 * connection attempt; otherwise, any failure during the connection attempt
125 * will cause the socket to be closed.
127 int32_t Connect
([in] PP_Resource tcp_socket
,
128 [in] PP_Resource addr
,
129 [in] PP_CompletionCallback
callback);
132 * Gets the local address of the socket, if it is bound.
134 * @param[in] tcp_socket A <code>PP_Resource</code> corresponding to a TCP
137 * @return A <code>PPB_NetAddress</code> resource on success or 0 on failure.
139 PP_Resource GetLocalAddress
([in] PP_Resource tcp_socket
);
142 * Gets the remote address of the socket, if it is connected.
144 * @param[in] tcp_socket A <code>PP_Resource</code> corresponding to a TCP
147 * @return A <code>PPB_NetAddress</code> resource on success or 0 on failure.
149 PP_Resource GetRemoteAddress
([in] PP_Resource tcp_socket
);
152 * Reads data from the socket. The socket must be connected. It may perform a
155 * @param[in] tcp_socket A <code>PP_Resource</code> corresponding to a TCP
157 * @param[out] buffer The buffer to store the received data on success. It
158 * must be at least as large as <code>bytes_to_read</code>.
159 * @param[in] bytes_to_read The number of bytes to read.
160 * @param[in] callback A <code>PP_CompletionCallback</code> to be called upon
163 * @return A non-negative number on success to indicate how many bytes have
164 * been read, 0 means that end-of-file was reached; otherwise, an error code
165 * from <code>pp_errors.h</code>.
167 int32_t Read
([in] PP_Resource tcp_socket
,
169 [in] int32_t bytes_to_read
,
170 [in] PP_CompletionCallback
callback);
173 * Writes data to the socket. The socket must be connected. It may perform a
176 * @param[in] tcp_socket A <code>PP_Resource</code> corresponding to a TCP
178 * @param[in] buffer The buffer containing the data to write.
179 * @param[in] bytes_to_write The number of bytes to write.
180 * @param[in] callback A <code>PP_CompletionCallback</code> to be called upon
183 * @return A non-negative number on success to indicate how many bytes have
184 * been written; otherwise, an error code from <code>pp_errors.h</code>.
186 int32_t Write
([in] PP_Resource tcp_socket
,
188 [in] int32_t bytes_to_write
,
189 [in] PP_CompletionCallback
callback);
191 * Starts listening. The socket must be bound and not connected.
193 * @param[in] tcp_socket A <code>PP_Resource</code> corresponding to a TCP
195 * @param[in] backlog A hint to determine the maximum length to which the
196 * queue of pending connections may grow.
197 * @param[in] callback A <code>PP_CompletionCallback</code> to be called upon
200 * @return An int32_t containing an error code from <code>pp_errors.h</code>,
201 * including (but not limited to):
202 * - <code>PP_ERROR_NOACCESS</code>: the caller doesn't have required
204 * - <code>PP_ERROR_ADDRESS_IN_USE</code>: Another socket is already listening
208 int32_t Listen
([in] PP_Resource tcp_socket
,
209 [in] int32_t backlog
,
210 [in] PP_CompletionCallback
callback);
213 * Accepts a connection. The socket must be listening.
215 * @param[in] tcp_socket A <code>PP_Resource</code> corresponding to a TCP
217 * @param[out] accepted_tcp_socket Stores the accepted TCP socket on success.
218 * @param[in] callback A <code>PP_CompletionCallback</code> to be called upon
221 * @return An int32_t containing an error code from <code>pp_errors.h</code>,
222 * including (but not limited to):
223 * - <code>PP_ERROR_CONNECTION_ABORTED</code>: A connection has been aborted.
226 int32_t Accept
([in] PP_Resource tcp_socket
,
227 [out] PP_Resource accepted_tcp_socket
,
228 [in] PP_CompletionCallback
callback);
231 * Cancels all pending operations and closes the socket. Any pending callbacks
232 * will still run, reporting <code>PP_ERROR_ABORTED</code> if pending IO was
233 * interrupted. After a call to this method, no output buffer pointers passed
234 * into previous <code>Read()</code> or <code>Accept()</code> calls will be
235 * accessed. It is not valid to call <code>Connect()</code> or
236 * <code>Listen()</code> again.
238 * The socket is implicitly closed if it is destroyed, so you are not required
239 * to call this method.
241 * @param[in] tcp_socket A <code>PP_Resource</code> corresponding to a TCP
244 void Close
([in] PP_Resource tcp_socket
);
247 * Sets a socket option on the TCP socket.
248 * Please see the <code>PP_TCPSocket_Option</code> description for option
249 * names, value types and allowed values.
251 * @param[in] tcp_socket A <code>PP_Resource</code> corresponding to a TCP
253 * @param[in] name The option to set.
254 * @param[in] value The option value to set.
255 * @param[in] callback A <code>PP_CompletionCallback</code> to be called upon
258 * @return An int32_t containing an error code from <code>pp_errors.h</code>.
260 int32_t SetOption
([in] PP_Resource tcp_socket
,
261 [in] PP_TCPSocket_Option name
,
263 [in] PP_CompletionCallback
callback);