Do not assume that the orientation is PORTRAIT by default.
[chromium-blink-merge.git] / ppapi / c / ppb_tcp_socket.h
bloba6fe111f71ef8fe8b033dc4a61ab662a01464981
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.
4 */
6 /* From ppb_tcp_socket.idl modified Fri Sep 20 09:58:19 2013. */
8 #ifndef PPAPI_C_PPB_TCP_SOCKET_H_
9 #define PPAPI_C_PPB_TCP_SOCKET_H_
11 #include "ppapi/c/pp_bool.h"
12 #include "ppapi/c/pp_completion_callback.h"
13 #include "ppapi/c/pp_instance.h"
14 #include "ppapi/c/pp_macros.h"
15 #include "ppapi/c/pp_resource.h"
16 #include "ppapi/c/pp_stdint.h"
17 #include "ppapi/c/pp_var.h"
19 #define PPB_TCPSOCKET_INTERFACE_1_0 "PPB_TCPSocket;1.0"
20 #define PPB_TCPSOCKET_INTERFACE_1_1 "PPB_TCPSocket;1.1"
21 #define PPB_TCPSOCKET_INTERFACE PPB_TCPSOCKET_INTERFACE_1_1
23 /**
24 * @file
25 * This file defines the <code>PPB_TCPSocket</code> interface.
29 /**
30 * @addtogroup Enums
31 * @{
33 /**
34 * Option names used by <code>SetOption()</code>.
36 typedef enum {
37 /**
38 * Disables coalescing of small writes to make TCP segments, and instead
39 * delivers data immediately. Value's type is <code>PP_VARTYPE_BOOL</code>.
40 * This option can only be set after a successful <code>Connect()</code> call.
42 PP_TCPSOCKET_OPTION_NO_DELAY = 0,
43 /**
44 * Specifies the total per-socket buffer space reserved for sends. Value's
45 * type should be <code>PP_VARTYPE_INT32</code>.
46 * This option can only be set after a successful <code>Connect()</code> call.
48 * Note: This is only treated as a hint for the browser to set the buffer
49 * size. Even if <code>SetOption()</code> succeeds, the browser doesn't
50 * guarantee it will conform to the size.
52 PP_TCPSOCKET_OPTION_SEND_BUFFER_SIZE = 1,
53 /**
54 * Specifies the total per-socket buffer space reserved for receives. Value's
55 * type should be <code>PP_VARTYPE_INT32</code>.
56 * This option can only be set after a successful <code>Connect()</code> call.
58 * Note: This is only treated as a hint for the browser to set the buffer
59 * size. Even if <code>SetOption()</code> succeeds, the browser doesn't
60 * guarantee it will conform to the size.
62 PP_TCPSOCKET_OPTION_RECV_BUFFER_SIZE = 2
63 } PP_TCPSocket_Option;
64 PP_COMPILE_ASSERT_SIZE_IN_BYTES(PP_TCPSocket_Option, 4);
65 /**
66 * @}
69 /**
70 * @addtogroup Interfaces
71 * @{
73 /**
74 * The <code>PPB_TCPSocket</code> interface provides TCP socket operations.
76 * Permissions: Apps permission <code>socket</code> with subrule
77 * <code>tcp-connect</code> is required for <code>Connect()</code>; subrule
78 * <code>tcp-listen</code> is required for <code>Listen()</code>.
79 * For more details about network communication permissions, please see:
80 * http://developer.chrome.com/apps/app_network.html
82 struct PPB_TCPSocket_1_1 {
83 /**
84 * Creates a TCP socket resource.
86 * @param[in] instance A <code>PP_Instance</code> identifying one instance of
87 * a module.
89 * @return A <code>PP_Resource</code> corresponding to a TCP socket or 0
90 * on failure.
92 PP_Resource (*Create)(PP_Instance instance);
93 /**
94 * Determines if a given resource is a TCP socket.
96 * @param[in] resource A <code>PP_Resource</code> to check.
98 * @return <code>PP_TRUE</code> if the input is a
99 * <code>PPB_TCPSocket</code> resource; <code>PP_FALSE</code> otherwise.
101 PP_Bool (*IsTCPSocket)(PP_Resource resource);
103 * Binds the socket to the given address. The socket must not be bound.
105 * @param[in] tcp_socket A <code>PP_Resource</code> corresponding to a TCP
106 * socket.
107 * @param[in] addr A <code>PPB_NetAddress</code> resource.
108 * @param[in] callback A <code>PP_CompletionCallback</code> to be called upon
109 * completion.
111 * @return An int32_t containing an error code from <code>pp_errors.h</code>,
112 * including (but not limited to):
113 * - <code>PP_ERROR_ADDRESS_IN_USE</code>: the address is already in use.
114 * - <code>PP_ERROR_ADDRESS_INVALID</code>: the address is invalid.
116 int32_t (*Bind)(PP_Resource tcp_socket,
117 PP_Resource addr,
118 struct PP_CompletionCallback callback);
120 * Connects the socket to the given address. The socket must not be listening.
121 * Binding the socket beforehand is optional.
123 * @param[in] tcp_socket A <code>PP_Resource</code> corresponding to a TCP
124 * socket.
125 * @param[in] addr A <code>PPB_NetAddress</code> resource.
126 * @param[in] callback A <code>PP_CompletionCallback</code> to be called upon
127 * completion.
129 * @return An int32_t containing an error code from <code>pp_errors.h</code>,
130 * including (but not limited to):
131 * - <code>PP_ERROR_NOACCESS</code>: the caller doesn't have required
132 * permissions.
133 * - <code>PP_ERROR_ADDRESS_UNREACHABLE</code>: <code>addr</code> is
134 * unreachable.
135 * - <code>PP_ERROR_CONNECTION_REFUSED</code>: the connection attempt was
136 * refused.
137 * - <code>PP_ERROR_CONNECTION_FAILED</code>: the connection attempt failed.
138 * - <code>PP_ERROR_CONNECTION_TIMEDOUT</code>: the connection attempt timed
139 * out.
141 * Since version 1.1, if the socket is listening/connected or has a pending
142 * listen/connect request, <code>Connect()</code> will fail without starting a
143 * connection attempt; otherwise, any failure during the connection attempt
144 * will cause the socket to be closed.
146 int32_t (*Connect)(PP_Resource tcp_socket,
147 PP_Resource addr,
148 struct PP_CompletionCallback callback);
150 * Gets the local address of the socket, if it is bound.
152 * @param[in] tcp_socket A <code>PP_Resource</code> corresponding to a TCP
153 * socket.
155 * @return A <code>PPB_NetAddress</code> resource on success or 0 on failure.
157 PP_Resource (*GetLocalAddress)(PP_Resource tcp_socket);
159 * Gets the remote address of the socket, if it is connected.
161 * @param[in] tcp_socket A <code>PP_Resource</code> corresponding to a TCP
162 * socket.
164 * @return A <code>PPB_NetAddress</code> resource on success or 0 on failure.
166 PP_Resource (*GetRemoteAddress)(PP_Resource tcp_socket);
168 * Reads data from the socket. The socket must be connected. It may perform a
169 * partial read.
171 * @param[in] tcp_socket A <code>PP_Resource</code> corresponding to a TCP
172 * socket.
173 * @param[out] buffer The buffer to store the received data on success. It
174 * must be at least as large as <code>bytes_to_read</code>.
175 * @param[in] bytes_to_read The number of bytes to read.
176 * @param[in] callback A <code>PP_CompletionCallback</code> to be called upon
177 * completion.
179 * @return A non-negative number on success to indicate how many bytes have
180 * been read, 0 means that end-of-file was reached; otherwise, an error code
181 * from <code>pp_errors.h</code>.
183 int32_t (*Read)(PP_Resource tcp_socket,
184 char* buffer,
185 int32_t bytes_to_read,
186 struct PP_CompletionCallback callback);
188 * Writes data to the socket. The socket must be connected. It may perform a
189 * partial write.
191 * @param[in] tcp_socket A <code>PP_Resource</code> corresponding to a TCP
192 * socket.
193 * @param[in] buffer The buffer containing the data to write.
194 * @param[in] bytes_to_write The number of bytes to write.
195 * @param[in] callback A <code>PP_CompletionCallback</code> to be called upon
196 * completion.
198 * @return A non-negative number on success to indicate how many bytes have
199 * been written; otherwise, an error code from <code>pp_errors.h</code>.
201 int32_t (*Write)(PP_Resource tcp_socket,
202 const char* buffer,
203 int32_t bytes_to_write,
204 struct PP_CompletionCallback callback);
206 * Starts listening. The socket must be bound and not connected.
208 * @param[in] tcp_socket A <code>PP_Resource</code> corresponding to a TCP
209 * socket.
210 * @param[in] backlog A hint to determine the maximum length to which the
211 * queue of pending connections may grow.
212 * @param[in] callback A <code>PP_CompletionCallback</code> to be called upon
213 * completion.
215 * @return An int32_t containing an error code from <code>pp_errors.h</code>,
216 * including (but not limited to):
217 * - <code>PP_ERROR_NOACCESS</code>: the caller doesn't have required
218 * permissions.
219 * - <code>PP_ERROR_ADDRESS_IN_USE</code>: Another socket is already listening
220 * on the same port.
222 int32_t (*Listen)(PP_Resource tcp_socket,
223 int32_t backlog,
224 struct PP_CompletionCallback callback);
226 * Accepts a connection. The socket must be listening.
228 * @param[in] tcp_socket A <code>PP_Resource</code> corresponding to a TCP
229 * socket.
230 * @param[out] accepted_tcp_socket Stores the accepted TCP socket on success.
231 * @param[in] callback A <code>PP_CompletionCallback</code> to be called upon
232 * completion.
234 * @return An int32_t containing an error code from <code>pp_errors.h</code>,
235 * including (but not limited to):
236 * - <code>PP_ERROR_CONNECTION_ABORTED</code>: A connection has been aborted.
238 int32_t (*Accept)(PP_Resource tcp_socket,
239 PP_Resource* accepted_tcp_socket,
240 struct PP_CompletionCallback callback);
242 * Cancels all pending operations and closes the socket. Any pending callbacks
243 * will still run, reporting <code>PP_ERROR_ABORTED</code> if pending IO was
244 * interrupted. After a call to this method, no output buffer pointers passed
245 * into previous <code>Read()</code> or <code>Accept()</code> calls will be
246 * accessed. It is not valid to call <code>Connect()</code> or
247 * <code>Listen()</code> again.
249 * The socket is implicitly closed if it is destroyed, so you are not required
250 * to call this method.
252 * @param[in] tcp_socket A <code>PP_Resource</code> corresponding to a TCP
253 * socket.
255 void (*Close)(PP_Resource tcp_socket);
257 * Sets a socket option on the TCP socket.
258 * Please see the <code>PP_TCPSocket_Option</code> description for option
259 * names, value types and allowed values.
261 * @param[in] tcp_socket A <code>PP_Resource</code> corresponding to a TCP
262 * socket.
263 * @param[in] name The option to set.
264 * @param[in] value The option value to set.
265 * @param[in] callback A <code>PP_CompletionCallback</code> to be called upon
266 * completion.
268 * @return An int32_t containing an error code from <code>pp_errors.h</code>.
270 int32_t (*SetOption)(PP_Resource tcp_socket,
271 PP_TCPSocket_Option name,
272 struct PP_Var value,
273 struct PP_CompletionCallback callback);
276 typedef struct PPB_TCPSocket_1_1 PPB_TCPSocket;
278 struct PPB_TCPSocket_1_0 {
279 PP_Resource (*Create)(PP_Instance instance);
280 PP_Bool (*IsTCPSocket)(PP_Resource resource);
281 int32_t (*Connect)(PP_Resource tcp_socket,
282 PP_Resource addr,
283 struct PP_CompletionCallback callback);
284 PP_Resource (*GetLocalAddress)(PP_Resource tcp_socket);
285 PP_Resource (*GetRemoteAddress)(PP_Resource tcp_socket);
286 int32_t (*Read)(PP_Resource tcp_socket,
287 char* buffer,
288 int32_t bytes_to_read,
289 struct PP_CompletionCallback callback);
290 int32_t (*Write)(PP_Resource tcp_socket,
291 const char* buffer,
292 int32_t bytes_to_write,
293 struct PP_CompletionCallback callback);
294 void (*Close)(PP_Resource tcp_socket);
295 int32_t (*SetOption)(PP_Resource tcp_socket,
296 PP_TCPSocket_Option name,
297 struct PP_Var value,
298 struct PP_CompletionCallback callback);
301 * @}
304 #endif /* PPAPI_C_PPB_TCP_SOCKET_H_ */