UdpConnection

An asynchronous UDP stream. UDP does not have connections, so this class encapsulates a socket with a fixed destination (sendto) address, and optionally bound to a local address. Currently received packets' address is not exposed.

Constructors

this
this(Socket conn)
Undocumented in source.
this
this()

Default constructor

Members

Functions

bind
ushort bind(string host, ushort port)

Bind to a local address in order to receive packets sent there.

initialize
void initialize(AddressFamily family, SocketType type)

Initialize with the given AddressFamily, without binding to an address.

Inherited Members

From ConnectionlessSocketConnection

onWritable
void onWritable()

Called when a socket is writable.

doSend
sizediff_t doSend(const(void)[] buffer)
Undocumented in source. Be warned that the author may not have intended to support it.
doReceive
sizediff_t doReceive(void[] buffer)
Undocumented in source. Be warned that the author may not have intended to support it.
initialize
void initialize(AddressFamily family, SocketType type, ProtocolType protocol)

Initialize with the given AddressFamily, without binding to an address.

bind
ushort bind(AddressInfo addressInfo)

Bind to a local address in order to receive packets sent there.

remoteAddress
Address remoteAddress;

Where to send packets to.

Examples

t
{
	auto server = new UdpConnection();
	server.bind("127.0.0.1", 0);

	auto client = new UdpConnection();
	client.initialize(server.localAddress.addressFamily);

	string[] packets = ["", "Hello", "there"];
	client.remoteAddress = server.localAddress;
	client.send({
		DataVec data;
		foreach (packet; packets)
			data ~= Data(packet.asBytes);
		return data;
	}()[]);

	server.handleReadData = (Data data)
	{
		assert(data.unsafeContents == packets[0]);
		packets = packets[1..$];
		if (!packets.length)
		{
			server.close();
			client.close();
		}
	};
	socketManager.loop();
	assert(!packets.length

Meta