1 [CCode (cprefix="SDLNet_", cheader_filename="SDL_net.h")]
3 [CCode (cname="SDLNet_Linked_Version")]
4 public static SDL.Version linked();
7 * Initialize the network API
8 * SDL must be initialized before calls to functions in this library,
9 * because this library uses utility functions from the SDL library.
11 [CCode (cname="SDLNet_Init")]
12 public static int init();
14 /** Cleanup the network API */
15 [CCode (cname="SDLNet_Quit")]
16 public static void quit();
18 /** Write a 16bit value to a network packet buffer */
19 [CCode (cname="SDLNet_Write16")]
20 public static void write16(uint16 value, void *area);
22 /** Write a 32bit value to a network packet buffer */
23 [CCode (cname="SDLNet_Write32")]
24 public static void write32(uint value, void *area);
26 /** Read a 16bit value from a network packet buffer */
27 [CCode (cname="SDLNet_Read16")]
28 public static uint16 read16(void *area);
30 /** Read a 32bit value from a network packet buffer */
31 [CCode (cname="SDLNet_Read32")]
32 public static uint read32(void *area);
37 [CCode (cname="IPaddress", free_function="g_free", has_type_id=false)]
38 public struct IPAddress {
42 [CCode (cname="INADDR_ANY")]
43 public const uint ANY;
45 [CCode (cname="INADDR_NONE")]
46 public const uint NONE;
49 * Resolve a host name and port to an IP address in network form.
50 * @return If the function succeeds, it will return 0.
51 * If the host couldn't be resolved, the host portion of the returned
52 * address will be INADDR_NONE, and the function will return -1.
53 * If 'host' is NULL, the resolved host will be set to INADDR_ANY.
55 [CCode (cname="SDLNet_ResolveHost")]
56 public static int from_host(out IPAddress address, string? host, uint16 port);
59 * Resolve an ip address to a host name in canonical form.
60 * Note that this function is not thread-safe.
61 * @return If the ip couldn't be resolved, this function returns null,
62 * otherwise a pointer to a static buffer containing the hostname
65 [CCode (cname="SDLNet_ResolveIP")]
66 public unowned string? lookup();
69 [CCode (cname="UDPpacket", free_function="SDLNet_FreePacket")]
71 public class UDPPacket {
77 public IPAddress address;
79 /** Allocate a single UDP packet 'size' bytes long. */
80 [CCode (cname="SDLNet_AllocPacket")]
81 public UDPPacket(int size);
83 /** Resize a single UDP packet 'newsize' bytes long. */
84 [CCode (cname="SDLNet_ResizePacket")]
85 public int resize(int newsize);
88 [CCode (cname="void")]
94 [CCode (cname="struct _TCPsocket", free_function="SDLNet_TCP_Close")]
96 public class TCPSocket: Socket {
98 * Open a TCP network socket
99 * If ip.host is INADDR_NONE or INADDR_ANY, this creates a local server
100 * socket on the given port, otherwise a TCP connection to the remote
101 * host and port is attempted.
102 * @param ip The address passed in should already be
103 * swapped to network byte order (addresses returned from
104 * SDLNet_ResolveHost() are already in the correct form).
105 * @return The newly created socket is returned, or null if there was an error.
107 [CCode (cname="SDLNet_TCP_Open")]
108 public TCPSocket(IPAddress ip);
111 * Accept an incoming connection on the given server socket.
112 * @return The newly created socket is returned, or null if there was an error.
114 [CCode (cname="SDLNet_TCP_Accept")]
115 public TCPSocket? accept();
118 * Get the IP address of the remote system associated with the socket.
119 * @return If the socket is a server socket, this function returns null.
121 [CCode (cname="SDLNet_TCP_GetPeerAddress")]
122 public IPAddress? get_address();
125 * Send data over the non-server socket 'sock'
126 * @param data The data to send
127 * @return This function returns the actual amount of data sent. If the return value
128 * is less than the amount of data sent, then either the remote connection was
129 * closed, or an unknown socket error occurred.
131 [CCode (cname="SDLNet_TCP_Send")]
132 public int send(uchar[] data);
135 * Receive up to (the length of data) bytes of data over the non-server socket 'sock',
136 * and store them in the buffer pointed to by 'data'.
137 * @param data The buffer to store received data
138 * @return This function returns the actual amount of data received. If the return
139 * value is less than or equal to zero, then either the remote connection was
140 * closed, or an unknown socket error occurred.
142 [CCode (cname="SDLNet_TCP_Recv")]
143 public int receive(uchar[] data);
146 [CCode (cname="struct _UDPsocket", free_function="SDLNet_UDP_Close")]
148 public class UDPSocket: Socket {
150 * Open a UDP network socket
151 * @param port If 'port' is non-zero, the UDP socket is bound to a local port.
152 * The 'port' should be given in native byte order, but is used
153 * internally in network (big endian) byte order, in addresses, etc.
154 * This allows other systems to send to this socket via a known port.
156 [CCode (cname="SDLNet_UDP_Open")]
157 public UDPSocket(uint16 port);
160 * Bind the address 'address' to the requested channel on the UDP socket.
161 * @param channel If the channel is -1, then the first unbound channel that has not yet
162 * been bound to the maximum number of addresses will be bound with
163 * the given address as it's primary address.
164 * If the channel is already bound, this new address will be added to the
165 * list of valid source addresses for packets arriving on the channel.
166 * If the channel is not already bound, then the address becomes the primary
167 * address, to which all outbound packets on the channel are sent.
168 * @param address If the channel is -1, then the first unbound channel that has not yet
169 * been bound to the maximum number of addresses will be bound with
170 * the given address as it's primary address.
171 * If the channel is already bound, this new address will be added to the
172 * list of valid source addresses for packets arriving on the channel.
173 * If the channel is not already bound, then the address becomes the primary
174 * address, to which all outbound packets on the channel are sent.
175 * @return This function returns the channel which was bound, or -1 on error.
177 [CCode (cname="SDLNet_UDP_Bind")]
178 public int bind(int channel, IPAddress address);
180 /** Unbind all addresses from the given channel */
181 [CCode (cname="SDLNet_UDP_Unbind")]
182 public void unbind(int channel);
185 * Get the primary IP address of the remote system associated with the
186 * socket and channel.
187 * @return If the channel is -1, then the primary IP port
188 * of the UDP socket is returned -- this is only meaningful for sockets
189 * opened with a specific port.
190 * If the channel is not bound and not -1, this function returns null
192 [CCode (cname="SDLNet_UDP_GetPeerAddress")]
193 public IPAddress? get_address(int channel);
196 * Send a single packet to the specified channel.
198 * The maximum size of the packet is limited by the MTU (Maximum Transfer Unit)
199 * of the transport medium. It can be as low as 250 bytes for some PPP links,
200 * and as high as 1500 bytes for ethernet.
201 * @param channel If the channel specified in the packet is -1, the packet will be sent to
202 * the address in the 'src' member of the packet.
203 * @param packet The packet will be updated with the status of the packet after it has
205 * @return This function returns 1 if the packet was sent, or 0 on error.
207 [CCode (cname="SDLNet_UDP_Send")]
208 public int send(int channel, UDPPacket packet);
211 * Receive a single packet from the UDP socket.
212 * @param packet The returned packet contains the source address and the channel it arrived
213 * on. If it did not arrive on a bound channel, the the channel will be set
215 * The channels are checked in highest to lowest order, so if an address is
216 * bound to multiple channels, the highest channel with the source address
217 * bound will be returned.
218 * @return This function returns the number of packets read from the network, or -1
219 * on error. This function does not block, so can return 0 packets pending.
221 [CCode (cname="SDLNet_UDP_Recv")]
222 public int receive(UDPPacket packet);
225 * Send a vector of packets to the the channels specified within the packet.
226 * If the channel specified in the packet is -1, the packet will be sent to
227 * the address in the 'src' member of the packet.
228 * Each packet will be updated with the status of the packet after it has
229 * been sent, -1 if the packet send failed.
230 * @param packets The packets to send
231 * @return This function returns the number of packets sent.
233 [CCode (cname="SDLNet_UDP_SendV")]
234 public int send_many(UDPPacket[] packets);
237 * Receive a vector of pending packets from the UDP socket.
238 * @param packets The returned packets contain the source address and the channel they arrived
239 * on. If they did not arrive on a bound channel, the the channel will be set
241 * The channels are checked in highest to lowest order, so if an address is
242 * bound to multiple channels, the highest channel with the source address
243 * bound will be returned.
244 * @return This function returns the number of packets read from the network, or -1
245 * on error. This function does not block, so can return 0 packets pending.
247 [CCode (cname="SDLNet_UDP_RecvV")]
248 public int receive_many([CCode (array_length = false)] UDPPacket[] packets);
251 [CCode (cname="struct _SDLNet_SocketSet", free_function="SDLNet_FreeSocketSet")]
253 public class SocketSet {
255 * Allocate a socket set
256 * @param maxsockets This creates a socket set for up to 'maxsockets' sockets
258 [CCode (cname="SDLNet_AllocSocketSet")]
259 public SocketSet(int maxsockets);
261 /** Add a socket to a set of sockets to be checked for available data */
262 [CCode (cname="SDLNet_AddSocket")]
263 public int add(Socket socket);
265 /** Remove a socket from a set of sockets to be checked for available data */
266 [CCode (cname="SDLNet_DelSocket")]
267 public int remove(Socket socket);
270 * This function checks to see if data is available for reading on the
271 * given set of sockets.
272 * @param timeout If 'timeout' is 0, it performs a quick poll,
273 * otherwise the function returns when either data is available for
274 * reading, or the timeout in milliseconds has elapsed, which ever occurs
276 * @return This function returns the number of sockets ready for reading,
277 * or -1 if there was an error with the select() system call.
279 [CCode (cname="SDLNet_CheckSockets")]
280 public int has_data(uint timeout);