Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "netlib/netlib.h"
00012 #include "common/wave_ex.h"
00013 #include "datahash/datahash.h"
00014 #include "perf/perf.h"
00015
00016 #include <iostream>
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 int
00033 main
00034 (
00035 IN int argc,
00036 IN const char * argv[]
00037 )
00038 {
00039 ASSERT(2 == argc,
00040 "Usage: test_tcp_server <port>");
00041
00042 int port = atoi(argv[1]);
00043 ASSERT(port > 0, "Bad port: %d", port);
00044
00045 netlib::address_t address;
00046 address.setlocal(port);
00047 address.dump("Using local address");
00048
00049 netlib::conn_id_t conn_id = netlib::createTcpListener(address, 2);
00050 DPRINTF("Created listener socket with ID = 0x%lx", (long) conn_id);
00051
00052 try {
00053 perf::Timer timer("server-loop");
00054 while (1) {
00055 netlib::envelope_t envelope;
00056 smart_ptr<netlib::MessageBuffer> msgbuf;
00057 if (!netlib::getNextMessage(1000, envelope, msgbuf))
00058 continue;
00059
00060
00061 netlib::dumpMessage(std::cerr,
00062 "Received message from client",
00063 envelope, msgbuf);
00064
00065 char buffer[128];
00066 sprintf(buffer, "I received %ld bytes from you!",
00067 msgbuf->getBytes());
00068 int len = strlen(buffer);
00069 DPRINTF("Sending: %s", buffer);
00070
00071 smart_ptr<netlib::MessageBuffer> mb =
00072 netlib::MessageBuffer::create();
00073 ASSERT(mb, "failed to create message buffer?");
00074 mb->appendData(buffer, len);
00075 mb->close();
00076
00077 if (!netlib::enqueueMessage(envelope.fromConnId, mb)) {
00078 WAVE_EX(wex);
00079 wex << "Failed to enqueue message";
00080 }
00081 }
00082 } catch (std::exception& e) {
00083 DPRINTF("Exception! %s", e.what());
00084 } catch (...) {
00085 DPRINTF("Unknown exception!");
00086 }
00087
00088 netlib::closeConnection(conn_id);
00089
00090 std::string summary;
00091 perf::getTimingSummary(summary);
00092 DPRINTF("Timers:\n%s", summary.c_str());
00093
00094 netlib::dumpStats();
00095
00096 return 0;
00097 }
00098