From a15cf65c44d5c224169c32ef5495b68c758134b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Sun, 18 May 2014 16:08:14 +0200 Subject: Imported Upstream version 3.3.0.2 --- libcult/examples/os/net/ipv4/datagram/server.cxx | 123 +++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 libcult/examples/os/net/ipv4/datagram/server.cxx (limited to 'libcult/examples/os/net/ipv4/datagram/server.cxx') diff --git a/libcult/examples/os/net/ipv4/datagram/server.cxx b/libcult/examples/os/net/ipv4/datagram/server.cxx new file mode 100644 index 0000000..e788056 --- /dev/null +++ b/libcult/examples/os/net/ipv4/datagram/server.cxx @@ -0,0 +1,123 @@ +// file : examples/os/net/ipv4/datagram/server.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2010 Boris Kolpackov +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include +#include + +#include +#include + +#include +#include // memcmp + +#include "protocol.hxx" + +using std::cerr; +using std::endl; + +using namespace Cult; +using namespace OS::Net::IPv4; + +typedef Containers::Vector StatusList; + +Int +main () +{ + try + { + Address addr (INADDR_ANY, 10000); + + DatagramSocket socket (addr); + + Message expected_msg; + expected_msg.sn = 0; + + for (UnsignedShort i = 0; i < payload_size; ++i) + { + expected_msg.payload[i] = i; + } + + StatusList received (message_count, 0); + StatusList damaged (message_count, 0); + StatusList duplicate (message_count, 0); + + Message msg; + + while (true) + { + socket.recv (&msg, sizeof (msg)); + + if (received[msg.sn]) + { + duplicate[msg.sn] = true; + } + else + { + received[msg.sn] = true; + + if (std::memcmp (expected_msg.payload, msg.payload, payload_size) != 0) + { + damaged[msg.sn] = true; + } + } + + if (msg.sn + 1 == message_count) break; + } + + UnsignedLong lost_count (0), damaged_count (0), duplicate_count (0); + + for (StatusList::Iterator i (received.begin ()), end (received.end ()); + i != end; + ++i) + if (!*i) ++lost_count; + + for (StatusList::Iterator i (damaged.begin ()), end (damaged.end ()); + i != end; + ++i) + if (*i) ++damaged_count; + + for (StatusList::Iterator i (duplicate.begin ()), end (duplicate.end ()); + i != end; + ++i) + if (*i) ++duplicate_count; + + cerr << "lost : " << lost_count << endl + << "damaged : " << damaged_count << endl + << "duplicate : " << duplicate_count << endl << endl; + + if (lost_count != 0) + { + cerr << "lost message dump:" << endl; + + UnsignedLong total = 0; + + for (StatusList::Iterator + begin (received.begin ()), i (begin), end (received.end ()); + i != end;) + { + if (!*i) + { + UnsignedLong count = 1; + + for (StatusList::Iterator j = i + 1; j < end && !*j; j++, count++) ; + + cerr << '\t' << i - begin << " : " << count << endl; + + i += count; + total += count; + } + else + ++i; + } + + if (total != lost_count) + cerr << "trouble" << endl; + } + } + catch (OS::Exception const& e) + { + cerr << "errno: " << e.code () << endl; + } +} -- cgit v1.2.3