DEADSOFTWARE

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