Module tanya.network.socket

Low-level socket programming.

Current API supports only server-side TCP communication.

Here is an example of a cross-platform blocking server:

import std.stdio;
import tanya.memory;
import tanya.network;

void main()
{
    auto socket = defaultAllocator.make!StreamSocket(AddressFamily.inet);
    auto address = defaultAllocator.make!InternetAddress("127.0.0.1",
                                                         cast(ushort) 8192);

    socket.setOption(SocketOptionLevel.SOCKET, SocketOption.REUSEADDR, true);
    socket.blocking = true;
    socket.bind(address);
    socket.listen(5);

    auto client = socket.accept();
    client.send(cast(const(ubyte)[]) "Test\n");

    ubyte[100] buf;
    auto response = client.receive(buf[]);

    writeln(cast(const(char)[]) buf[0 .. response]);

    defaultAllocator.dispose(client);
    defaultAllocator.dispose(socket);
}

For an example of an asynchronous server refer to the documentation of the tanya.async.loop module.

Functions

NameDescription
wouldHaveBlocked() Checks if the last error is a serious error or just a special behaviour error of non-blocking sockets (for example an error returned because the socket would block or because the asynchronous operation was successfully started but not finished yet).

Interfaces

NameDescription
ConnectionOrientedSocket Interface with common fileds for stream and connected sockets.

Classes

NameDescription
Address Socket address representation.
ConnectedSocket Socket returned if a connection has been established.
Socket Class for creating a network communication endpoint using the Berkeley sockets interfaces of different types.
SocketException SocketException should be thrown only if one of the socket functions socketError and sets errno, because SocketException relies on the errno value.

Structs

NameDescription
Linger Socket option that specifies what should happen when the socket that promises reliable delivery still has untransmitted messages when it is closed.

Enums

NameDescription
AddressFamily AddressFamily specifies a communication domain; this selects the protocol family which will be used for communication.

Manifest constants

NameTypeDescription
socketError Value returned by socket operations on error.