Skip to content

Instantly share code, notes, and snippets.

@dnadlinger
Created September 12, 2011 17:20
Show Gist options
  • Select an option

  • Save dnadlinger/1211819 to your computer and use it in GitHub Desktop.

Select an option

Save dnadlinger/1211819 to your computer and use it in GitHub Desktop.
import std.datetime : AutoStart, Duration, StopWatch;
import std.socket;
import std.stdio;
void main() {
foreach (factory; [
cast(Socket delegate()){ return new TcpSocket(new InternetAddress("google.com", 80)); }, // Cast due to a DMD @@BUG@@.
{ auto pair = socketPair(); return pair[0]; } // Temporary due to a DMD @@BUG@@.
]) {
foreach (msecs; [1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000]) {
auto sock = factory();
sock.setOption(SocketOptionLevel.SOCKET,
SocketOption.RCVTIMEO, dur!"msecs"(msecs));
auto sw = StopWatch(AutoStart.yes);
ubyte[1] buf;
sock.receive(buf);
sw.stop();
Duration readBack = void;
sock.getOption(SocketOptionLevel.SOCKET, SocketOption.RCVTIMEO, readBack);
writefln("Set: %s ms (%s ms). Actual: %s ms.", msecs,
readBack.total!"msecs", sw.peek().msecs);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment