DEADSOFTWARE

5f1309a85b597aaafb30513f00f2f9659646ad3e
[d2df-sdl.git] / src / lib / sdl2 / SDL2_net.pas
3 {*
4 SDL_net: An example cross-platform network library for use with SDL
5 Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
6 Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
8 This software is provided 'as-is', without any express or implied
9 warranty. In no event will the authors be held liable for any damages
10 arising from the use of this software.
12 Permission is granted to anyone to use this software for any purpose,
13 including commercial applications, and to alter it and redistribute it
14 freely, subject to the following restrictions:
16 1. The origin of this software must not be misrepresented; you must not
17 claim that you wrote the original software. If you use this software
18 in a product, an acknowledgment in the product documentation would be
19 appreciated but is not required.
20 2. Altered source versions must be plainly marked as such, and must not be
21 misrepresented as being the original software.
22 3. This notice may not be removed or altered from any source distribution.
23 *}
24 unit SDL2_net;
26 {$INCLUDE jedi.inc}
28 interface
30 uses SDL2;
32 const
33 {$IFDEF WINDOWS}
34 SDLNet_LibName = 'SDL2_net.dll';
35 {$ENDIF}
37 {$IFDEF UNIX}
38 {$IFDEF DARWIN}
39 SDLNet_LibName = 'libSDL2_net.dylib';
40 {$ELSE}
41 {$IFDEF FPC}
42 SDLNet_LibName = 'libSDL2_net.so';
43 {$ELSE}
44 SDLNet_LibName = 'libSDL2_net-2.0.so.0';
45 {$ENDIF}
46 {$ENDIF}
47 {$ENDIF}
49 {$IFDEF MACOS}
50 SDLNet_LibName = 'SDL2_net';
51 {$IFDEF FPC}
52 {$linklib libSDL2_net}
53 {$ENDIF}
54 {$ENDIF}
57 type
58 TSDLNet_Version = TSDL_Version;
60 const
61 {* Printable format: "%d.%d.%d", MAJOR, MINOR, PATCHLEVEL *}
62 SDL_NET_MAJOR_VERSION = 2;
63 SDL_NET_MINOR_VERSION = 0;
64 SDL_NET_PATCHLEVEL = 0;
66 {* This macro can be used to fill a version structure with the compile-time
67 * version of the SDL_net library.
68 *}
69 procedure SDL_NET_VERSION(Out X: TSDL_Version);
71 {* This function gets the version of the dynamically linked SDL_net library.
72 it should NOT be used to fill a version structure, instead you should
73 use the SDL_NET_VERSION() macro.
74 *}
75 procedure SDLNet_Linked_Version() cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_Linked_Version' {$ENDIF} {$ENDIF};
77 {* Initialize/Cleanup the network API
78 SDL must be initialized before calls to functions in this library,
79 because this library uses utility functions from the SDL library.
80 *}
81 function SDLNet_Init(): Integer cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_Init' {$ENDIF} {$ENDIF};
82 procedure SDLNet_Quit() cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_Quit' {$ENDIF} {$ENDIF};
84 type
85 {***********************************************************************}
86 {* IPv4 hostname resolution API *}
87 {***********************************************************************}
88 TIPaddress = record
89 host: UInt32; {* 32-bit IPv4 host address *}
90 port: UInt16; {* 16-bit protocol port *}
91 end;
92 PIPaddress = ^TIPaddress;
94 {* Resolve a host name and port to an IP address in network form.
95 If the function succeeds, it will return 0.
96 If the host couldn't be resolved, the host portion of the returned
97 address will be INADDR_NONE, and the function will return -1.
98 If 'host' is NULL, the resolved host will be set to INADDR_ANY.
99 *}
100 const
101 INADDR_ANY = $00000000;
102 INADDR_NONE = $FFFFFFFF;
103 INADDR_LOOPBACK = $7f000001;
104 INADDR_BROADCAST = $FFFFFFFF;
106 function SDLNet_ResolveHost(address: PIPaddress; const host: PAnsiChar; port: UInt16): Integer cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_ResolveHost' {$ENDIF} {$ENDIF};
108 {* Resolve an ip address to a host name in canonical form.
109 If the ip couldn't be resolved, this function returns NULL,
110 otherwise a pointer to a static buffer containing the hostname
111 is returned. Note that this function is not thread-safe.
112 *}
113 function SDLNet_ResolveIP(const ip: PIPaddress): PAnsiChar cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_ResolveIP' {$ENDIF} {$ENDIF};
115 {* Get the addresses of network interfaces on this system.
116 This returns the number of addresses saved in 'addresses'
117 *}
118 function SDLNet_GetLocalAddresses(addresses: PIPaddress; maxcount: Integer): Integer cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_GetLocalAddresses' {$ENDIF} {$ENDIF};
120 {***********************************************************************}
121 {* TCP network API *}
122 {***********************************************************************}
123 type
124 _TCPSocket = record
125 end;
126 TTCPSocket = ^_TCPSocket;
128 {* Open a TCP network socket
129 If ip.host is INADDR_NONE or INADDR_ANY, this creates a local server
130 socket on the given port, otherwise a TCP connection to the remote
131 host and port is attempted. The address passed in should already be
132 swapped to network byte order (addresses returned from
133 SDLNet_ResolveHost() are already in the correct form).
134 The newly created socket is returned, or NULL if there was an error.
135 *}
136 function SDLNet_TCP_Open(ip: PIPaddress): TTCPSocket cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_TCP_Open' {$ENDIF} {$ENDIF};
138 {* Accept an incoming connection on the given server socket.
139 The newly created socket is returned, or NULL if there was an error.
140 *}
141 function SDLNet_TCP_Accept(server: TTCPSocket): TTCPSocket cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_TCP_Accept' {$ENDIF} {$ENDIF};
143 {* Get the IP address of the remote system associated with the socket.
144 If the socket is a server socket, this function returns NULL.
145 *}
146 function SDLNet_TCP_GetPeerAddress(sock: TTCPSocket): PIPaddress cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_TCP_GetPeerAddress' {$ENDIF} {$ENDIF};
148 {* Send 'len' bytes of 'data' over the non-server socket 'sock'
149 This function returns the actual amount of data sent. If the return value
150 is less than the amount of data sent, then either the remote connection was
151 closed, or an unknown socket error occurred.
152 *}
153 function SDLNet_TCP_Send(sock: TTCPSocket; const data: Pointer; len: Integer): Integer cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_TCP_Send' {$ENDIF} {$ENDIF};
155 {* Receive up to 'maxlen' bytes of data over the non-server socket 'sock',
156 and store them in the buffer pointed to by 'data'.
157 This function returns the actual amount of data received. If the return
158 value is less than or equal to zero, then either the remote connection was
159 closed, or an unknown socket error occurred.
160 *}
161 function SDLNet_TCP_Recv(sock: TTCPSocket; data: Pointer; maxlen: Integer): Integer cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_TCP_Recv' {$ENDIF} {$ENDIF};
163 {* Close a TCP network socket *}
164 procedure SDLNet_TCP_Close(sock: TTCPSocket) cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_TCP_Close' {$ENDIF} {$ENDIF};
166 {***********************************************************************}
167 {* UDP network API *}
168 {***********************************************************************}
170 const
171 {* The maximum channels on a a UDP socket *}
172 SDLNET_MAX_UDPCHANNELS = 32;
173 {* The maximum addresses bound to a single UDP socket channel *}
174 SDLNET_MAX_UDPADDRESSES = 4;
176 type
177 TUDPSocket = record
178 end;
179 PUDPSocket = ^TUDPSocket;
181 TUDPPacket = record
182 channel: Integer; {* The src/dst channel of the packet *}
183 data: PUInt8; {* The packet data *}
184 len: Integer; {* The length of the packet data *}
185 maxlen: Integer; {* The size of the data buffer *}
186 status: Integer; {* packet status after sending *}
187 address: TIPaddress; {* The source/dest address of an incoming/outgoing packet *}
188 end;
189 PUDPPacket = ^TUDPPacket;
190 PPUDPPacket = ^PUDPPacket;
192 {* Allocate/resize/free a single UDP packet 'size' bytes long.
193 The new packet is returned, or NULL if the function ran out of memory.
194 *}
195 function SDLNet_AllocPacket(size: Integer): PUDPPacket cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_AllocPacket' {$ENDIF} {$ENDIF};
196 function SDLNet_ResizePacket(packet: PUDPPacket; newsize: Integer): Integer cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_ResizePacket' {$ENDIF} {$ENDIF};
197 procedure SDLNet_FreePacket(packet: PUDPPacket) cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_FreePacket' {$ENDIF} {$ENDIF};
199 {* Allocate/Free a UDP packet vector (array of packets) of 'howmany' packets,
200 each 'size' bytes long.
201 A pointer to the first packet in the array is returned, or NULL if the
202 function ran out of memory.
203 *}
204 function SDLNet_AllocPacketV(howmany: Integer; size: Integer): PPUDPPacket cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_AllocPacketV' {$ENDIF} {$ENDIF};
205 procedure SDLNet_FreePacketV(packetV: PPUDPPacket) cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_FreePacketV' {$ENDIF} {$ENDIF};
207 {* Open a UDP network socket
208 If 'port' is non-zero, the UDP socket is bound to a local port.
209 The 'port' should be given in native byte order, but is used
210 internally in network (big endian) byte order, in addresses, etc.
211 This allows other systems to send to this socket via a known port.
212 *}
213 function SDLNet_UDP_Open(port: UInt16): TUDPSocket cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_UDP_Open' {$ENDIF} {$ENDIF};
215 {* Set the percentage of simulated packet loss for packets sent on the socket. *}
216 procedure SDLNet_UDP_SetPacketLoss(sock: TUDPSocket; percent: Integer) cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_UDP_SetPacketLoss' {$ENDIF} {$ENDIF};
218 {* Bind the address 'address' to the requested channel on the UDP socket.
219 If the channel is -1, then the first unbound channel that has not yet
220 been bound to the maximum number of addresses will be bound with
221 the given address as it's primary address.
222 If the channel is already bound, this new address will be added to the
223 list of valid source addresses for packets arriving on the channel.
224 If the channel is not already bound, then the address becomes the primary
225 address, to which all outbound packets on the channel are sent.
226 This function returns the channel which was bound, or -1 on error.
227 *}
228 function SDLNet_UDP_Bind(sock: TUDPSocket; channel: Integer; const address: PIPaddress): Integer cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_UDP_Bind' {$ENDIF} {$ENDIF};
230 {* Unbind all addresses from the given channel *}
231 procedure SDLNet_UDP_Unbind(sock: TUDPSocket; channel: Integer) cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_UDP_Unbind' {$ENDIF} {$ENDIF};
233 {* Get the primary IP address of the remote system associated with the
234 socket and channel. If the channel is -1, then the primary IP port
235 of the UDP socket is returned -- this is only meaningful for sockets
236 opened with a specific port.
237 If the channel is not bound and not -1, this function returns NULL.
238 *}
239 function SDLNet_UDP_GetPeerAddress(sock: TUDPSocket; channel: Integer): PIPaddress cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_UDP_GetPeerAddress' {$ENDIF} {$ENDIF};
241 {* Send a vector of packets to the the channels specified within the packet.
242 If the channel specified in the packet is -1, the packet will be sent to
243 the address in the 'src' member of the packet.
244 Each packet will be updated with the status of the packet after it has
245 been sent, -1 if the packet send failed.
246 This function returns the number of packets sent.
247 *}
248 function SDLNet_UDP_SendV(sock: TUDPSocket; packets: PPUDPPacket; npackets: Integer): Integer cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_UDP_SendV' {$ENDIF} {$ENDIF};
250 {* Send a single packet to the specified channel.
251 If the channel specified in the packet is -1, the packet will be sent to
252 the address in the 'src' member of the packet.
253 The packet will be updated with the status of the packet after it has
254 been sent.
255 This function returns 1 if the packet was sent, or 0 on error.
257 NOTE:
258 The maximum size of the packet is limited by the MTU (Maximum Transfer Unit)
259 of the transport medium. It can be as low as 250 bytes for some PPP links,
260 and as high as 1500 bytes for ethernet.
261 *}
262 function SDLNet_UDP_Send(sock: TUDPSocket; channel: Integer; packet: PUDPPacket): Integer cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_UDP_Send' {$ENDIF} {$ENDIF};
264 {* Receive a vector of pending packets from the UDP socket.
265 The returned packets contain the source address and the channel they arrived
266 on. If they did not arrive on a bound channel, the the channel will be set
267 to -1.
268 The channels are checked in highest to lowest order, so if an address is
269 bound to multiple channels, the highest channel with the source address
270 bound will be returned.
271 This function returns the number of packets read from the network, or -1
272 on error. This function does not block, so can return 0 packets pending.
273 *}
274 function SDLNet_UDP_RecvV(sock: TUDPSocket; packets: PPUDPPacket): Integer cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_UDP_RecvV' {$ENDIF} {$ENDIF};
276 {* Receive a single packet from the UDP socket.
277 The returned packet contains the source address and the channel it arrived
278 on. If it did not arrive on a bound channel, the the channel will be set
279 to -1.
280 The channels are checked in highest to lowest order, so if an address is
281 bound to multiple channels, the highest channel with the source address
282 bound will be returned.
283 This function returns the number of packets read from the network, or -1
284 on error. This function does not block, so can return 0 packets pending.
285 *}
286 function SDLNet_UDP_Recv(sock: TUDPSocket; packet: PUDPPacket): Integer cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_UDP_Recv' {$ENDIF} {$ENDIF};
288 {* Close a UDP network socket *}
289 procedure SDLNet_UDP_Close(sock: TUDPSocket) cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_UDP_Close' {$ENDIF} {$ENDIF};
291 {***********************************************************************}
292 {* Hooks for checking sockets for available data *}
293 {***********************************************************************}
295 type
296 TSDLNet_SocketSet = record
297 end;
298 PSDLNet_SocketSet = ^TSDLNet_SocketSet;
300 {* Any network socket can be safely cast to this socket type *}
301 TSDLNet_GenericSocket = record
302 ready: Integer;
303 end;
304 PSDLNet_GenericSocket = ^TSDLNet_GenericSocket;
306 {* Allocate a socket set for use with SDLNet_CheckSockets()
307 This returns a socket set for up to 'maxsockets' sockets, or NULL if
308 the function ran out of memory.
309 *}
310 function SDLNet_AllocSocketSet(maxsockets: Integer): TSDLNet_GenericSocket cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_AllocSocketSet' {$ENDIF} {$ENDIF};
312 {* Add a socket to a set of sockets to be checked for available data *}
313 function SDLNet_AddSocket(set_: TSDLNet_SocketSet; sock: TSDLNet_GenericSocket): Integer cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_AddSocket' {$ENDIF} {$ENDIF};
314 //function SDLNet_TCP_AddSocket(set_: TSDLNet_SocketSet; sock: TTCPSocket): Integer; inline;
315 //function SDLNet_UDP_AddSocket(set_: TSDLNet_SocketSet; sock: TUDPSocket): Integer; inline;
317 {* Remove a socket from a set of sockets to be checked for available data *}
318 function SDLNet_DelSocket(set_: TSDLNet_SocketSet; sock: TSDLNet_GenericSocket): Integer cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_DelSocket' {$ENDIF} {$ENDIF};
319 //function SDLNet_TCP_DelSocket(set_: TSDLNet_SocketSet; sock: TTCPSocket): Integer; inline;
320 //function SDLNet_UDP_DelSocket(set_: TSDLNet_SocketSet; sock: TUDPSocket): Integer; inline;
322 {* This function checks to see if data is available for reading on the
323 given set of sockets. If 'timeout' is 0, it performs a quick poll,
324 otherwise the function returns when either data is available for
325 reading, or the timeout in milliseconds has elapsed, which ever occurs
326 first. This function returns the number of sockets ready for reading,
327 or -1 if there was an error with the select() system call.
328 *}
329 function SDLNet_CheckSockets(set_: TSDLNet_SocketSet; timeout: UInt32): Integer cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_CheckSockets' {$ENDIF} {$ENDIF};
331 {* After calling SDLNet_CheckSockets(), you can use this function on a
332 socket that was in the socket set, to find out if data is available
333 for reading.
334 *}
335 function SDLNet_SocketReady(sock: TSDLNet_GenericSocket): Integer; {$IFNDEF DELPHI} inline; {$ELSE} {$IFDEF DELPHI10UP} inline; {$ENDIF} {$ENDIF};
337 {* Free a set of sockets allocated by SDL_NetAllocSocketSet() *}
338 procedure SDLNet_FreeSocketSet(set_: TSDLNet_SocketSet) cdecl; external SDLNet_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDLNet_FreeSocketSet' {$ENDIF} {$ENDIF};
340 {***********************************************************************}
341 {* Error reporting functions *}
342 {***********************************************************************}
344 procedure SDLNet_SetError(const fmt: PAnsiChar); cdecl;
345 function SDLNet_GetError(): PAnsiChar; cdecl;
347 {***********************************************************************}
348 {* Inline functions to read/write network data *}
349 {***********************************************************************}
351 {* Write a 16/32-bit value to network packet buffer *}
353 //procedure SDLNet_Write16(value: UInt16; areap: Pointer); inline;
354 //procedure SDLNet_Write32(value: UInt32; areap: Pointer); inline;
356 {* Read a 16/32-bit value from network packet buffer *}
357 //function SDLNet_Read16(const areap: Pointer): UInt16; inline;
358 //function SDLNet_Read32(const areap: Pointer): UInt32; inline;
360 implementation
362 procedure SDL_NET_VERSION(Out X: TSDL_Version);
363 begin
364 X.major := SDL_NET_MAJOR_VERSION;
365 X.minor := SDL_NET_MINOR_VERSION;
366 X.patch := SDL_NET_PATCHLEVEL;
367 end;
369 (*
370 function SDLNet_TCP_AddSocket(set_: TSDLNet_SocketSet; sock: TTCPSocket): Integer;
371 begin
372 Result := SDLNet_AddSocket(set_, TSDLNet_GenericSocket(sock));
373 end;
375 function SDLNet_UDP_AddSocket(set_: TSDLNet_SocketSet; sock: TUDPSocket): Integer;
376 begin
377 Result := SDLNet_AddSocket(set_, TSDLNet_GenericSocket(sock));
378 end;
380 function SDLNet_TCP_DelSocket(set_: TSDLNet_SocketSet; sock: TTCPSocket): Integer;
381 begin
382 Result := SDLNet_DelSocket(set_, TSDLNet_GenericSocket(sock));
383 end;
385 function SDLNet_UDP_DelSocket(set_: TSDLNet_SocketSet; sock: TUDPSocket): Integer;
386 begin
387 Result := SDLNet_DelSocket(set_, TSDLNet_GenericSocket(sock));
388 end;
389 *)
391 function SDLNet_SocketReady(sock: TSDLNet_GenericSocket): Integer;
392 begin
393 Result := sock.ready;
394 end;
396 procedure SDLNet_SetError(const fmt: PAnsiChar); cdecl;
397 begin
398 SDL_SetError(fmt);
399 end;
401 function SDLNet_GetError(): PAnsiChar; cdecl;
402 begin
403 Result := SDL_GetError();
404 end;
406 (*
407 procedure SDLNet_Write16(value: UInt16; areap: Pointer);
408 begin
409 PUInt16(areap) := SDL_SwapBE16(value);
410 end;
412 procedure SDLNet_Write32(value: UInt32; areap: Pointer);
413 begin
414 PUInt32(areap) := SDL_SwapBE32(value);
415 end;
417 {* Read a 16/32-bit value from network packet buffer *}
418 function SDLNet_Read16(const areap: Pointer): UInt16;
419 begin
420 Result := SDL_SwapBE16(PUInt16(areap));
421 end;
423 function SDLNet_Read32(const areap: Pointer): UInt32;
424 begin
425 Result := SDL_SwapBE32(PUInt32(areap));
426 end;
427 *)
428 end.