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)
ushort bind(AddressInfo addressInfo)

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

doReceive
sizediff_t doReceive(void[] buffer)
Undocumented in source. Be warned that the author may not have intended to support it.
doSend
sizediff_t doSend(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.

initializeImpl
void initializeImpl(AddressFamily family, SocketType type, ProtocolType protocol)
Undocumented in source. Be warned that the author may not have intended to support it.
onWritable
void onWritable()

Called when a socket is writable.

Variables

remoteAddress
Address remoteAddress;

Where to send packets to.

Inherited Members

From Connection

state
ConnectionState state [@property getter]

Get connection state.

doSend
sizediff_t doSend(void[] buffer)
Undocumented in source.
doReceive
sizediff_t doReceive(void[] buffer)
Undocumented in source.
outQueue
Data[][MAX_PRIORITY + 1] outQueue;

The send buffers.

partiallySent
int partiallySent;

Whether the first item from this queue (if any) has been partially sent (and thus can't be canceled).

updateFlags
void updateFlags()
Undocumented in source. Be warned that the author may not have intended to support it.
onReadable
void onReadable()

Called when a socket is readable.

onError
void onError(string reason)

Called when an error occurs on the socket.

disconnect
void disconnect(string reason, DisconnectType type)

Close a connection. If there is queued data waiting to be sent, wait until it is sent before disconnecting. The disconnect handler will be called immediately, even when not all data has been flushed yet.

send
void send(Data[] data, int priority)
alias send = IConnection.send

Append data to the send buffer.

clearQueue
void clearQueue(int priority)
Undocumented in source. Be warned that the author may not have intended to support it.
writePending
bool writePending [@property getter]
Undocumented in source. Be warned that the author may not have intended to support it.
queuePresent
bool queuePresent(int priority)
Undocumented in source. Be warned that the author may not have intended to support it.
packetsQueued
size_t packetsQueued(int priority)
Undocumented in source. Be warned that the author may not have intended to support it.
bytesQueued
size_t bytesQueued(int priority)
Undocumented in source. Be warned that the author may not have intended to support it.
handleConnect
ConnectHandler handleConnect [@property setter]

Callback for when a connection has been established.

handleReadData
ReadDataHandler handleReadData [@property setter]

Callback for incoming data. Data will not be received unless this handler is set.

handleDisconnect
DisconnectHandler handleDisconnect [@property setter]

Callback for when a connection was closed.

handleBufferFlushed
BufferFlushedHandler handleBufferFlushed [@property setter]
Undocumented in source. Be warned that the author may not have intended to support it.

Examples

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

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

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

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

Meta