asio 0.3.7 Home | Reference | Tutorial | Examples | Design
Examples

http/server/win_main.cpp

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <string>
00003 #include <asio.hpp>
00004 #include <boost/bind.hpp>
00005 #include <boost/function.hpp>
00006 #include "server.hpp"
00007 
00008 #if defined(_WIN32)
00009 
00010 boost::function0<void> console_ctrl_function;
00011 
00012 BOOL WINAPI console_ctrl_handler(DWORD ctrl_type)
00013 {
00014   switch (ctrl_type)
00015   {
00016   case CTRL_C_EVENT:
00017   case CTRL_BREAK_EVENT:
00018   case CTRL_CLOSE_EVENT:
00019   case CTRL_SHUTDOWN_EVENT:
00020     console_ctrl_function();
00021     return TRUE;
00022   default:
00023     return FALSE;
00024   }
00025 }
00026 
00027 int main(int argc, char* argv[])
00028 {
00029   try
00030   {
00031     // Check command line arguments.
00032     if (argc != 4)
00033     {
00034       std::cerr << "Usage: http_server <address> <port> <doc_root>\n";
00035       std::cerr << "  For IPv4, try:\n";
00036       std::cerr << "    receiver 0.0.0.0 80 .\n";
00037       std::cerr << "  For IPv6, try:\n";
00038       std::cerr << "    receiver 0::0 80 .\n";
00039       return 1;
00040     }
00041 
00042     // Initialise server.
00043     http::server::server s(argv[1], argv[2], argv[3]);
00044 
00045     // Set console control handler to allow server to be stopped.
00046     console_ctrl_function = boost::bind(&http::server::server::stop, &s);
00047     SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
00048 
00049     // Run the server until stopped.
00050     s.run();
00051   }
00052   catch (asio::error& e)
00053   {
00054     std::cerr << "asio error: " << e << "\n";
00055   }
00056   catch (std::exception& e)
00057   {
00058     std::cerr << "exception: " << e.what() << "\n";
00059   }
00060 
00061   return 0;
00062 }
00063 
00064 #endif // defined(_WIN32)
asio 0.3.7 Home | Reference | Tutorial | Examples | Design