00001
00002
00003
00004
00005
00006
00007
00008 #include "netCommon.h"
00009 #include "utils.h"
00010
00011 using namespace boost::posix_time;
00012 using boost::asio::ip::udp;
00013
00014 namespace FastEcslent{
00015
00016 NetCommon::NetCommon(){
00017 this->startTime = getCurrentTime();
00018 }
00019 std::pair<boost::asio::ip::udp::endpoint, std::string> NetCommon::getMyIP(){
00020 udp::endpoint me;
00021 std::string ipString;
00022 try {
00023 boost::asio::io_service netService;
00024 udp::resolver resolver(netService);
00025 udp::resolver::query query(udp::v4(), boost::asio::ip::host_name(), "");
00026 udp::resolver::iterator endpoints = resolver.resolve(query);
00027 std::cout << "Got endPoint:" << boost::asio::ip::host_name() << std::endl;
00028 udp::endpoint ep = *endpoints;
00029 std::cout << "Address: " << ep.address() << std::endl;
00030 std::cout << "IP Address:" << boost::asio::ip::address_v4().to_string() << std::endl;
00031
00032 if (ep.address().to_string().substr(0,3) != "127"){
00033 me = ep;
00034 ipString = boost::asio::ip::address_v4().to_string();
00035 }
00036 } catch (std::exception& e){
00037 std::cerr << "getMyIP could not deal with socket. Exception: " << e.what() << std::endl;
00038 }
00039 try {
00040 boost::asio::io_service netService;
00041 udp::resolver resolver(netService);
00042 udp::resolver::query query(udp::v4(), "google.com", "");
00043 udp::resolver::iterator endpoints = resolver.resolve(query);
00044 udp::endpoint ep = *endpoints;
00045 udp::socket socket(netService);
00046 socket.connect(ep);
00047 boost::asio::ip::address addr = socket.local_endpoint().address();
00048 std::cout << "My IP according to google is: " << addr.to_string() << std::endl;
00049 me = ep;
00050 ipString = addr.to_string();
00051 } catch (std::exception& e){
00052 std::cerr << "getMyIP could not deal with socket. Exception: " << e.what() << std::endl;
00053 }
00054 return std::pair<boost::asio::ip::udp::endpoint, std::string>(me, ipString);
00055 }
00056
00057 long NetCommon::getCurrentTimeLong(){
00058 return (getCurrentTime() - startTime).total_milliseconds();
00059 }
00060
00061 NetCommon::~NetCommon() {
00062 }
00063
00064 }
00065