sdlNetResolveHostname function

Pointer<SdlNetAddress> sdlNetResolveHostname(
  1. String? host
)

Resolve a human-readable hostname.

SDL_net doesn't operate on human-readable hostnames (like "www.libsdl.org") but on computer-readable addresses. This function converts from one to the other. This process is known as "resolving" an address.

You can also use this to turn IP address strings (like "159.203.69.7") into SDLNet_Address objects.

Note that resolving an address is an asynchronous operation, since the library will need to ask a server on the internet to get the information it needs, and this can take time (and possibly fail later). This function will not block. It either returns NULL (catastrophic failure) or an unresolved SDLNet_Address. Until the address resolves, it can't be used.

If you want to block until the resolution is finished, you can call SDLNet_WaitUntilResolved(). Otherwise, you can do a non-blocking check with SDLNet_GetAddressStatus().

When you are done with the returned SDLNet_Address, call SDLNet_UnrefAddress() to dispose of it. You need to do this even if resolution later fails asynchronously.

\param host The hostname to resolve. \returns A new SDLNet_Address on success, NULL on error; call SDL_GetError() for details.

\threadsafety It is safe to call this function from any thread.

\since This function is available since SDL_Net 3.0.0.

\sa SDLNet_WaitUntilResolved \sa SDLNet_GetAddressStatus \sa SDLNet_RefAddress \sa SDLNet_UnrefAddress

extern SDL_DECLSPEC SDLNet_Address * SDLCALL SDLNet_ResolveHostname(const char *host)

Implementation

Pointer<SdlNetAddress> sdlNetResolveHostname(String? host) {
  final sdlNetResolveHostnameLookupFunction = libSdl3Net.lookupFunction<
      Pointer<SdlNetAddress> Function(Pointer<Utf8> host),
      Pointer<SdlNetAddress> Function(
          Pointer<Utf8> host)>('SDLNet_ResolveHostname');
  final hostPointer = host != null ? host.toNativeUtf8() : nullptr;
  final result = sdlNetResolveHostnameLookupFunction(hostPointer);
  calloc.free(hostPointer);
  return result;
}