Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

DatagramConduit.read behaves unexpectedly

Moderators: kris

Posted: 06/12/09 20:57:24

If I pass a buffer to DatagramConduit?.read(void[]) that's too small to hold the entire contents, it always returns Eof. Isn't it supposed to just fill the given buffer, and return the number of bytes put into the buffer?

Author Message

Posted: 06/13/09 19:22:08

Aye, it is

Posted: 06/13/09 20:20:07 -- Modified: 06/13/09 20:34:13 by
torhu -- Modified 4 Times

I tried it again. The buffer does contain data, it's just that read() returns Eof, so it looks like you got nothing.

Here's the actual code I use, in an early stage of development:

///
private class Rcon
{
	///
	this(in char[] address, int port, in char[] password)
	{
		conn_ = new DatagramConduit;
		conn_.connect(new InternetAddress(address, port));
		password_ = password;
	}


	/// Run a command on the server, return the output.
	char[] command(in char[] cmd)
	{
		const size_t growBy = 300;
		char[] buf = new char[growBy];
		size_t total = 0;

		conn_.write("\xff\xff\xff\xff" ~ "rcon \"" ~ password_ ~ "\" " ~ cmd);

		while (true) {
			size_t received = conn_.read(buf[total..$]);
			if (received == IConduit.Eof)
				break;
			total += received;
			if (total < buf.length)
				break;
			buf.length = buf.length + growBy;
		}

		if (total < "\xff\xff\xff\xffprint\n".length) {
			return null;
		}
		else {
			buf = buf[4..total-4];  // first four bytes are just 0xFF
			assert(buf[0..6] == "print\n");
			return buf[6..$];
		}
	}

	private {
		char[] password_;
		DatagramConduit conn_;
	}

}

Posted: 06/13/09 20:41:45

ok, thanks torhu

Posted: 07/18/09 02:27:28

Turns out I don't know what I'm doing. read() doesn't work that way for UPD packages, the buffer has to be big enough, or the message is just truncated. Not sure know if returning Eof is correct or not, though.