VirtualBox

source: vbox/trunk/include/iprt/tcp.h@ 31061

Last change on this file since 31061 was 30468, checked in by vboxsync, 14 years ago

IPRT:

  • Corrected RTSGBUF member names.
  • Corrected RTSgBufInit parameter names.
  • Don't use AssertReturnVoid (and variations) in RTSgBuf* as there the result is that we use uninitialized structures. Better to crash and burn in IPRT then.
  • Added RTSocketSgWriteL/LV and RTTcpSgWriteL/LV for lazy guys like me.
  • RTSocketSgWrite doesn't need a do {} while (0).
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.4 KB
Line 
1/** @file
2 * IPRT - TCP/IP.
3 */
4
5/*
6 * Copyright (C) 2006-2010 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___iprt_tcp_h
27#define ___iprt_tcp_h
28
29#include <iprt/cdefs.h>
30#include <iprt/types.h>
31#include <iprt/thread.h>
32#include <iprt/net.h>
33#include <iprt/sg.h>
34
35#ifdef IN_RING0
36# error "There are no RTFile APIs available Ring-0 Host Context!"
37#endif
38
39
40RT_C_DECLS_BEGIN
41
42/** @defgroup grp_rt_tcp RTTcp - TCP/IP
43 * @ingroup grp_rt
44 * @{
45 */
46
47
48/**
49 * Serve a TCP Server connection.
50 *
51 * @returns iprt status code.
52 * @returns VERR_TCP_SERVER_STOP to terminate the server loop forcing
53 * the RTTcpCreateServer() call to return.
54 * @param Sock The socket which the client is connected to.
55 * The call will close this socket.
56 * @param pvUser User argument.
57 */
58typedef DECLCALLBACK(int) FNRTTCPSERVE(RTSOCKET Sock, void *pvUser);
59/** Pointer to a RTTCPSERVE(). */
60typedef FNRTTCPSERVE *PFNRTTCPSERVE;
61
62/** Pointer to a RTTCPSERVER handle. */
63typedef struct RTTCPSERVER *PRTTCPSERVER;
64/** Pointer to a RTTCPSERVER handle. */
65typedef PRTTCPSERVER *PPRTTCPSERVER;
66
67/**
68 * Create single connection at a time TCP Server in a separate thread.
69 *
70 * The thread will loop accepting connections and call pfnServe for
71 * each of the incoming connections in turn. The pfnServe function can
72 * return VERR_TCP_SERVER_STOP too terminate this loop. RTTcpServerDestroy()
73 * should be used to terminate the server.
74 *
75 * @returns iprt status code.
76 * @param pszAddress The address for creating a listening socket.
77 * If NULL or empty string the server is bound to all interfaces.
78 * @param uPort The port for creating a listening socket.
79 * @param enmType The thread type.
80 * @param pszThrdName The name of the worker thread.
81 * @param pfnServe The function which will serve a new client connection.
82 * @param pvUser User argument passed to pfnServe.
83 * @param ppServer Where to store the serverhandle.
84 */
85RTR3DECL(int) RTTcpServerCreate(const char *pszAddress, unsigned uPort, RTTHREADTYPE enmType, const char *pszThrdName,
86 PFNRTTCPSERVE pfnServe, void *pvUser, PPRTTCPSERVER ppServer);
87
88/**
89 * Create single connection at a time TCP Server.
90 * The caller must call RTTcpServerListen() to actually start the server.
91 *
92 * @returns iprt status code.
93 * @param pszAddress The address for creating a listening socket.
94 * If NULL the server is bound to all interfaces.
95 * @param uPort The port for creating a listening socket.
96 * @param ppServer Where to store the serverhandle.
97 */
98RTR3DECL(int) RTTcpServerCreateEx(const char *pszAddress, uint32_t uPort, PPRTTCPSERVER ppServer);
99
100/**
101 * Closes down and frees a TCP Server.
102 * This will also terminate any open connections to the server.
103 *
104 * @returns iprt status code.
105 * @param pServer Handle to the server.
106 */
107RTR3DECL(int) RTTcpServerDestroy(PRTTCPSERVER pServer);
108
109/**
110 * Listen for incoming connections.
111 *
112 * The function will loop accepting connections and call pfnServe for
113 * each of the incoming connections in turn. The pfnServe function can
114 * return VERR_TCP_SERVER_STOP too terminate this loop. A stopped server
115 * can only be destroyed.
116 *
117 * @returns iprt status code.
118 * @param pServer The server handle as returned from RTTcpServerCreateEx().
119 * @param pfnServe The function which will serve a new client connection.
120 * @param pvUser User argument passed to pfnServe.
121 */
122RTR3DECL(int) RTTcpServerListen(PRTTCPSERVER pServer, PFNRTTCPSERVE pfnServe, void *pvUser);
123
124/**
125 * Listen and accept one incomming connection.
126 *
127 * This is an alternative to RTTcpServerListen for the use the callbacks are not
128 * possible.
129 *
130 * @returns IPRT status code.
131 * @retval VERR_TCP_SERVER_SHUTDOWN if shut down by RTTcpServerShutdown.
132 * @retval VERR_INTERRUPTED if the listening was interrupted.
133 *
134 * @param pServer The server handle as returned from RTTcpServerCreateEx().
135 * @param phClientSocket Where to return the socket handle to the client
136 * connection (on success only). This must be closed
137 * by calling RTTcpServerDisconnectClient2().
138 */
139RTR3DECL(int) RTTcpServerListen2(PRTTCPSERVER pServer, PRTSOCKET phClientSocket);
140
141/**
142 * Terminate the open connection to the server.
143 *
144 * @returns iprt status code.
145 * @param pServer Handle to the server.
146 */
147RTR3DECL(int) RTTcpServerDisconnectClient(PRTTCPSERVER pServer);
148
149/**
150 * Terminates an open client connect when using RTTcpListen2
151 *
152 * @returns IPRT status code.
153 * @param hClientSocket The client socket handle. This will be invalid upon
154 * return, whether successful or not. NIL is quietly
155 * ignored (VINF_SUCCESS).
156 */
157RTR3DECL(int) RTTcpServerDisconnectClient2(RTSOCKET hClientSocket);
158
159/**
160 * Shuts down the server, leaving client connections open.
161 *
162 * @returns IPRT status code.
163 * @param pServer Handle to the server.
164 */
165RTR3DECL(int) RTTcpServerShutdown(PRTTCPSERVER pServer);
166
167/**
168 * Connect (as a client) to a TCP Server.
169 *
170 * @returns iprt status code.
171 * @param pszAddress The address to connect to.
172 * @param uPort The port to connect to.
173 * @param pSock Where to store the handle to the established connection.
174 */
175RTR3DECL(int) RTTcpClientConnect(const char *pszAddress, uint32_t uPort, PRTSOCKET pSock);
176
177/**
178 * Close a socket returned by RTTcpClientConnect().
179 *
180 * @returns iprt status code.
181 * @param Sock Socket descriptor.
182 */
183RTR3DECL(int) RTTcpClientClose(RTSOCKET Sock);
184
185/**
186 * Receive data from a socket.
187 *
188 * @returns iprt status code.
189 * @param Sock Socket descriptor.
190 * @param pvBuffer Where to put the data we read.
191 * @param cbBuffer Read buffer size.
192 * @param pcbRead Number of bytes read.
193 * If NULL the entire buffer will be filled upon successful return.
194 * If not NULL a partial read can be done successfully.
195 */
196RTR3DECL(int) RTTcpRead(RTSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
197
198/**
199 * Send data to a socket.
200 *
201 * @returns iprt status code.
202 * @retval VERR_INTERRUPTED if interrupted before anything was written.
203 *
204 * @param Sock Socket descriptor.
205 * @param pvBuffer Buffer to write data to socket.
206 * @param cbBuffer How much to write.
207 */
208RTR3DECL(int) RTTcpWrite(RTSOCKET Sock, const void *pvBuffer, size_t cbBuffer);
209
210/**
211 * Flush socket write buffers.
212 *
213 * @returns iprt status code.
214 * @param Sock Socket descriptor.
215 */
216RTR3DECL(int) RTTcpFlush(RTSOCKET Sock);
217
218/**
219 * Enables or disables delaying sends to coalesce packets.
220 *
221 * The TCP/IP stack usually uses the Nagle algorithm (RFC 896) to implement the
222 * coalescing.
223 *
224 * @returns iprt status code.
225 * @param Sock Socket descriptor.
226 * @param fEnable When set to true enables coalescing.
227 */
228RTR3DECL(int) RTTcpSetSendCoalescing(RTSOCKET Sock, bool fEnable);
229
230/**
231 * Socket I/O multiplexing.
232 * Checks if the socket is ready for reading.
233 *
234 * @returns iprt status code.
235 * @param Sock Socket descriptor.
236 * @param cMillies Number of milliseconds to wait for the socket.
237 * Use RT_INDEFINITE_WAIT to wait for ever.
238 */
239RTR3DECL(int) RTTcpSelectOne(RTSOCKET Sock, RTMSINTERVAL cMillies);
240
241
242#if 0 /* skipping these for now - RTTcpServer* handles this. */
243/**
244 * Listen for connection on a socket.
245 *
246 * @returns iprt status code.
247 * @param Sock Socket descriptor.
248 * @param cBackLog The maximum length the queue of pending connections
249 * may grow to.
250 */
251RTR3DECL(int) RTTcpListen(RTSOCKET Sock, int cBackLog);
252
253/**
254 * Accept a connection on a socket.
255 *
256 * @returns iprt status code.
257 * @param Sock Socket descriptor.
258 * @param uPort The port for accepting connection.
259 * @param pSockAccepted Where to store the handle to the accepted connection.
260 */
261RTR3DECL(int) RTTcpAccept(RTSOCKET Sock, unsigned uPort, PRTSOCKET pSockAccepted);
262
263#endif
264
265/**
266 * Gets the address of the local side.
267 *
268 * @returns IPRT status code.
269 * @param Sock Socket descriptor.
270 * @param pAddr Where to store the local address on success.
271 */
272RTR3DECL(int) RTTcpGetLocalAddress(RTSOCKET Sock, PRTNETADDR pAddr);
273
274/**
275 * Gets the address of the other party.
276 *
277 * @returns IPRT status code.
278 * @param Sock Socket descriptor.
279 * @param pAddr Where to store the peer address on success.
280 */
281RTR3DECL(int) RTTcpGetPeerAddress(RTSOCKET Sock, PRTNETADDR pAddr);
282
283/**
284 * Send data from a scatter/gather buffer to a socket.
285 *
286 * @returns iprt status code.
287 * @retval VERR_INTERRUPTED if interrupted before anything was written.
288 *
289 * @param Sock Socket descriptor.
290 * @param pSgBuf Scatter/gather buffer to write data to socket.
291 */
292RTR3DECL(int) RTTcpSgWrite(RTSOCKET Sock, PCRTSGBUF pSgBuf);
293
294
295/**
296 * Send data from multiple buffers to a socket.
297 *
298 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
299 * for lazy coders. The "L" in the function name is short for "list" just like
300 * in the execl libc API.
301 *
302 * @returns IPRT status code.
303 * @retval VERR_INTERRUPTED if interrupted before anything was written.
304 *
305 * @param hSocket The socket handle.
306 * @param cSegs The number of data segments in the following
307 * ellipsis.
308 * @param ... Pairs of buffer pointers (void const *) and buffer
309 * sizes (size_t). Make 101% sure the pointer is
310 * really size_t.
311 */
312RTR3DECL(int) RTTcpSgWriteL(RTSOCKET hSocket, size_t cSegs, ...);
313
314/**
315 * Send data from multiple buffers to a socket.
316 *
317 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
318 * for lazy coders. The "L" in the function name is short for "list" just like
319 * in the execl libc API.
320 *
321 * @returns IPRT status code.
322 * @retval VERR_INTERRUPTED if interrupted before anything was written.
323 *
324 * @param hSocket The socket handle.
325 * @param cSegs The number of data segments in the following
326 * argument list.
327 * @param va Pairs of buffer pointers (void const *) and buffer
328 * sizes (size_t). Make 101% sure the pointer is
329 * really size_t.
330 */
331RTR3DECL(int) RTTcpSgWriteLV(RTSOCKET hSocket, size_t cSegs, va_list va);
332
333/** @} */
334RT_C_DECLS_END
335
336#endif
337
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette