コード例 #1
0
ファイル: utils.cpp プロジェクト: Dutch-Man/EventLoop
TimeVal TimeVal::operator-(const TimeVal& other) const {
  if (*this < other) return TimeVal(0, 0);
  uint32_t diff_sec = tv_.tv_usec;
  uint32_t diff_usec = tv_.tv_usec;
  if (diff_usec < other.USeconds()) {
    // 借位
    diff_sec -= 1;
    diff_usec += 1000000;
  }
  diff_sec -= other.Seconds();
  diff_usec -= other.USeconds();
  return TimeVal(diff_sec, diff_usec);
}
コード例 #2
0
ファイル: svr_proto.cpp プロジェクト: Zhanyin/taomee
/**
  * @brief connect to switch and send an init package of online information when online is launched
  */
void connect_to_switch()
{
	DEBUG_LOG("do  connect_to_switch");
	switch_fd = connect_to_service(config_get_strval("service_switch"), 0, 65535, 1);
	if (switch_fd != -1) {
		//连上时
 		is_add_timer_to_connect_switch=false;
	   	static uint32_t seqno = 0;
		sw_report_online_info_in sw_in;
	 	sw_in.domain_id= config_get_intval("domain", 0);
		sw_in.online_id= get_server_id();
		memcpy(sw_in.online_name,get_server_name(),sizeof(sw_in.online_name)); 
		memcpy(sw_in.online_ip,get_server_ip(),sizeof(sw_in.online_ip)); 
		sw_in.online_port=get_server_port();
		sw_in.seqno=seqno;
		g_sprite_map->get_userid_list(sw_in.userid_list);

	 	sw_in.user_num=sw_in.userid_list.size() ;
	
	    ++seqno;
	    send_msg_to_switch(NULL,sw_report_online_info_cmd,0,&sw_in );

		KDEBUG_LOG(0, "BEGIN CONNECTING TO CENTRAL ONLINE AND SEND INIT PKG");
	}else{//
		if(! is_add_timer_to_connect_switch){//还没有加入定时器
			DEBUG_LOG("ADD TIMER connect_switch ");
			g_timer_map->add_timer( TimeVal(3),n_sw_report_online_info,0 );
			is_add_timer_to_connect_switch=true;
		}
	}
}
コード例 #3
0
ファイル: DataTime.cpp プロジェクト: cckck1103/ProjectFrame
//-----------------------------------------------------------------------------
// 描述: 返回当前时间戳
//-----------------------------------------------------------------------------
Timestamp Timestamp::now()
{
	Timestamp result;

#ifdef _COMPILER_WIN
	FILETIME ft;
	::GetSystemTimeAsFileTime(&ft);

	ULARGE_INTEGER epoch; // UNIX epoch (1970-01-01 00:00:00) expressed in Windows NT FILETIME
	epoch.LowPart = 0xD53E8000;
	epoch.HighPart = 0x019DB1DE;

	ULARGE_INTEGER ts;
	ts.LowPart = ft.dwLowDateTime;
	ts.HighPart = ft.dwHighDateTime;
	ts.QuadPart -= epoch.QuadPart;
	result.value_ = ts.QuadPart / 10000;
#endif
#ifdef _COMPILER_LINUX
	struct timeval tv;
	gettimeofday(&tv, NULL);
	result.value_ = TimeVal(tv.tv_sec) * MILLISECS_PER_SECOND + tv.tv_usec;
#endif

	return result;
}
コード例 #4
0
ファイル: TimeVal.cpp プロジェクト: neilgroves/MlbDev
// ////////////////////////////////////////////////////////////////////////////
TimeVal TimeVal::GetDifference(const TimeVal &time_1, const TimeVal &time_2)
{
	double tmp_double = GetDifferenceTicksDouble(time_1, time_2);

	return(TimeVal(static_cast<long>(tmp_double / 1000000.0),
		static_cast<long>(fmod(tmp_double, 1000000.0))));
}
コード例 #5
0
ファイル: Legacy_Poco.cpp プロジェクト: noriter/nit
void PocoTimestamp::update()
{
#if defined(NIT_FAMILY_WIN32)

	FILETIME ft;
	GetSystemTimeAsFileTime(&ft);
	ULARGE_INTEGER epoch; // UNIX epoch (1970-01-01 00:00:00) expressed in Windows NT FILETIME
	epoch.LowPart  = 0xD53E8000;
	epoch.HighPart = 0x019DB1DE;

	ULARGE_INTEGER ts;
	ts.LowPart  = ft.dwLowDateTime;
	ts.HighPart = ft.dwHighDateTime;
	ts.QuadPart -= epoch.QuadPart;
	_ts = ts.QuadPart/10;

#else

	struct timeval tv;
	if (gettimeofday(&tv, NULL))
		NIT_THROW_FMT(EX_SYSTEM, "cannot get time of day");
	_ts = TimeVal(tv.tv_sec)*resolution() + tv.tv_usec;

#endif
}
コード例 #6
0
ファイル: Timeutil.cpp プロジェクト: blacklensama/TVPortable
    Timestamp::TimeVal Timestamp::CurrentTime() {
        TimeVal ts;
#if defined(UKN_OS_WINDOWS)
        
        FILETIME ft;
        GetSystemTimeAsFileTime(&ft);
        ULARGE_INTEGER epoch; // UNIX epoch (1970-01-01 00:00:00) expressed in Windows NT FILETIME
        epoch.LowPart  = 0xD53E8000;
        epoch.HighPart = 0x019DB1DE;
        
        ULARGE_INTEGER tts;
        tts.LowPart  = ft.dwLowDateTime;
        tts.HighPart = ft.dwHighDateTime;
        tts.QuadPart -= epoch.QuadPart;
        ts = tts.QuadPart/10;
        
#else
        
        struct timeval tv;
        if (gettimeofday(&tv, NULL))
            throw ("cannot get time of day");
        ts = TimeVal(tv.tv_sec)*Resolution() + tv.tv_usec;
        
#endif
        return ts;
    }
コード例 #7
0
ファイル: utils.cpp プロジェクト: Dutch-Man/EventLoop
TimeVal TimeVal::operator+(const TimeVal& other) const {
  uint32_t diff_sec = tv_.tv_sec + other.Seconds();      // XXX: integer overflow???
  uint32_t diff_usec = tv_.tv_usec + other.USeconds();
  diff_sec += diff_usec / 1000000;
  diff_usec %= 1000000;
  //printf("diff_sec: %d, diff_usec: %d\n", diff_sec, diff_usec);
  return TimeVal(diff_sec, diff_usec);
}
コード例 #8
0
/** Get the current mono time. */
TimeVal TimeVal::mono()
{
    LARGE_INTEGER largeCounter;

    QueryPerformanceCounter( &largeCounter );
    uint64_t const frequency = TimeVal::getStaticFrequency();

    return TimeVal( (uint64_t) (( largeCounter.QuadPart * 1000000 ) / frequency ) );
}
コード例 #9
0
ファイル: Timestamp.cpp プロジェクト: jacklicn/macchina.io
void Timestamp::update()
{
#if defined(POCO_OS_FAMILY_WINDOWS)

	FILETIME ft;
#if defined(_WIN32_WCE) && defined(POCO_WINCE_TIMESTAMP_HACK)
	GetSystemTimeAsFileTimeWithMillisecondResolution(&ft);
#else
	GetSystemTimeAsFileTime(&ft);
#endif

	ULARGE_INTEGER epoch; // UNIX epoch (1970-01-01 00:00:00) expressed in Windows NT FILETIME
	epoch.LowPart  = 0xD53E8000;
	epoch.HighPart = 0x019DB1DE;

	ULARGE_INTEGER ts;
	ts.LowPart  = ft.dwLowDateTime;
	ts.HighPart = ft.dwHighDateTime;
	ts.QuadPart -= epoch.QuadPart;
	_ts = ts.QuadPart/10;

#elif defined(POCO_HAVE_CLOCK_GETTIME)

	struct timespec ts;
	if (clock_gettime(CLOCK_REALTIME, &ts))
		throw SystemException("cannot get time of day");
	_ts = TimeVal(ts.tv_sec)*resolution() + ts.tv_nsec/1000;

#else

	struct timeval tv;
	if (gettimeofday(&tv, NULL))
		throw SystemException("cannot get time of day");
	_ts = TimeVal(tv.tv_sec)*resolution() + tv.tv_usec;

#endif
}
コード例 #10
0
/** Get the current system time. */
TimeVal TimeVal::now()
{
    FILETIME ft;
    unsigned __int64 tmpres = 0;

    GetSystemTimeAsFileTime( &ft );

    tmpres |= ft.dwHighDateTime;
    tmpres <<= 32;
    tmpres |= ft.dwLowDateTime;

    /*converting file time to unix epoch*/
    tmpres /= 10;  /*convert into microseconds*/
    tmpres -= DELTA_EPOCH_IN_MICROSECS;

    return TimeVal( (uint64_t) (tmpres) );
}
コード例 #11
0
void Timestamp::update()
{
    FILETIME ft;

    //GetSystemTimeAsFileTimeWithMillisecondResolution(&ft);
    GetSystemTimeAsFileTime(&ft);

    ULARGE_INTEGER epoch; // UNIX epoch (1970-01-01 00:00:00) expressed in Windows NT FILETIME
    epoch.LowPart = 0xD53E8000;
    epoch.HighPart = 0x019DB1DE;

    ULARGE_INTEGER ts;
    ts.LowPart = ft.dwLowDateTime;
    ts.HighPart = ft.dwHighDateTime;
    ts.QuadPart -= epoch.QuadPart;
    _ts = ts.QuadPart / 10;

    struct timeval tv;
    if (gettimeofday(&tv, NULL))
        //	throw SystemException("cannot get time of day");
        _ts = TimeVal(tv.tv_sec)*resolution() + tv.tv_usec;
}
コード例 #12
0
ファイル: Timestamp.cpp プロジェクト: Web5design/firtex2
void Timestamp::update()
{
#if defined(FX_WINDOWS)

    FILETIME ft;
    GetSystemTimeAsFileTime(&ft);
    ULARGE_INTEGER epoch; // UNIX epoch (1970-01-01 00:00:00) expressed in Windows NT FILETIME
    epoch.LowPart  = 0xD53E8000;
    epoch.HighPart = 0x019DB1DE;

    ULARGE_INTEGER ts;
    ts.LowPart  = ft.dwLowDateTime;
    ts.HighPart = ft.dwHighDateTime;
    ts.QuadPart -= epoch.QuadPart;
    m_ts = ts.QuadPart/10;

#else

    struct timeval tv;
    if (gettimeofday(&tv, NULL))
        FIRTEX_THROW(RuntimeException, "cannot get time of day");
    m_ts = TimeVal(tv.tv_sec)*resolution() + tv.tv_usec;
#endif
}
コード例 #13
0
Timestamp Timestamp::fromEpochTime(std::time_t t)
{
    return Timestamp(TimeVal(t)*resolution());
}
コード例 #14
0
ファイル: TimeVal.cpp プロジェクト: neilgroves/MlbDev
//	////////////////////////////////////////////////////////////////////////////
TimeVal TimeVal::FromMicroseconds(unsigned long long usecs)
{
	return(TimeVal(static_cast<time_t>(usecs /
		static_cast<unsigned long long>(1000000)),
		static_cast<long>(usecs % static_cast<unsigned long long>(1000000))));
}
コード例 #15
0
ファイル: IPIDCollector.cpp プロジェクト: tartaruszen/treenet
void IPIDCollector::run()
{
    // Avoids probing null IPs (routing artifacts)
    if(this->target == InetAddress("0.0.0.0"))
        return;

    IPLookUpTable *table = env->getIPTable();
    IPTableEntry *targetEntry = table->lookUp(this->target); // Necessarily exists (see AliasHintCollector.cpp)
    unsigned short nbThreads = env->getNbIPIDs();
    unsigned short maxThreads = env->getMaxThreads(); // For range (see below)
    
    // Just in case (normally, should not occur, as stated above)
    if(targetEntry == NULL)
        return;
    
    /*
     * N.B.: since AliasHintCollector takes care of scheduling with respect to the maximum amount 
     * of threads allowed by the user, this class does neither check nbThreads nor performs a 
     * circular visit of the thread array.
     */
    
    // Initializes an array of threads
    Thread **th = new Thread*[nbThreads];
    for(unsigned short i = 0; i < nbThreads; i++)
        th[i] = NULL;

    // Starts spawning threads
    unsigned short range = (DirectProber::DEFAULT_UPPER_SRC_PORT_ICMP_ID - DirectProber::DEFAULT_LOWER_SRC_PORT_ICMP_ID) / maxThreads;
    for(unsigned long int i = 0; i < nbThreads; i++)
    {
        unsigned short trueOffset = this->offset + i;
        th[i] = new Thread(new IPIDUnit(this->env, 
                                        this->parent, 
                                        this->target, 
                                        DirectProber::DEFAULT_LOWER_SRC_PORT_ICMP_ID + (trueOffset * range), 
                                        DirectProber::DEFAULT_LOWER_SRC_PORT_ICMP_ID + (trueOffset * range) + range - 1, 
                                        DirectProber::DEFAULT_LOWER_DST_PORT_ICMP_SEQ, 
                                        DirectProber::DEFAULT_UPPER_DST_PORT_ICMP_SEQ));
        
        th[i]->start();
        
        // 0,01s of delay before next thread
        Thread::invokeSleep(TimeVal(0, 10000));
    }

    // Waiting for all threads to complete and getting token, IP-ID, time value tuples
    list<IPIDTuple*> tuples;
    for(unsigned short i = 0; i < nbThreads; i++)
    {
        if(th[i] != NULL)
        {
            th[i]->join();
            IPIDUnit *curUnit = (IPIDUnit*) th[i]->getRunnable();
            if(curUnit->hasExploitableResults())
            {
                tuples.push_back(curUnit->getTuple());
            }
            delete th[i];
            th[i] = NULL;
        }
    }
    
    // Sorts and stores collected data in targetEntry
    tuples.sort(IPIDTuple::compare);
    unsigned short index = 0;
    unsigned char inferredInitialTTL = 0;
    bool differentTTLs = false;
    while(tuples.size() > 0)
    {
        IPIDTuple *cur = tuples.front();
        tuples.pop_front();
        
        targetEntry->setProbeToken(index, cur->probeToken);
        targetEntry->setIPIdentifier(index, cur->IPID);
        if(cur->echo)
            targetEntry->setEcho(index);
        
        // Only if the same initial TTL was observed on every previous tuple
        if(!differentTTLs)
        {
            // Infers initial TTL value of the reply packet for the current tuple
            unsigned char initialTTL = 0;
            unsigned short replyTTLAsShort = (unsigned short) cur->replyTTL;
            if(replyTTLAsShort > 128)
                initialTTL = (unsigned char) 255;
            else if(replyTTLAsShort > 64)
                initialTTL = (unsigned char) 128;
            else if(replyTTLAsShort > 32)
                initialTTL = (unsigned char) 64;
            else
                initialTTL = (unsigned char) 32;
            
            // Checks if distinct initial TTLs are observed (it should always be the same)
            if(inferredInitialTTL == 0)
            {
                inferredInitialTTL = initialTTL;
            }
            else if(inferredInitialTTL != initialTTL)
            {
                differentTTLs = true;
            }
        }
        
        // Computes delay with next entry (no pop_front() this time)
        if(tuples.size() > 0)
        {
            IPIDTuple *next = tuples.front();
            
            unsigned long time1, time2;
            time1 = cur->timeValue.tv_sec * 1000000 + cur->timeValue.tv_usec;
            time2 = next->timeValue.tv_sec * 1000000 + next->timeValue.tv_usec;
                    
            unsigned long delay = time2 - time1;
            targetEntry->setDelay(index, delay);
        }
        
        // Frees memory used by current tuple and moves on
        delete cur;
        index++;
    }
    
    if(inferredInitialTTL > 0 && !differentTTLs)
        targetEntry->setEchoInitialTTL(inferredInitialTTL);
    
    delete[] th;
    
    // We're done with this IP.
}
コード例 #16
0
void AliasHintCollector::collect()
{
    IPLookUpTable *table = env->getIPTable();
    ostream *out = env->getOutputStream();
    
    /*
     * Sorts and removes duplicata (possible because ingress interface of neighborhood can be a 
     * contra-pivot). Also inserts the IPs that are missing from the IP table.
     */
    
    this->IPsToProbe.sort(InetAddress::smaller);
    InetAddress previous(0);
    for(list<InetAddress>::iterator i = this->IPsToProbe.begin(); i != this->IPsToProbe.end(); ++i)
    {
        InetAddress current = (*i);
        
        if(current == previous)
        {
            this->IPsToProbe.erase(i--);
        }
        else
        {
            if(table->lookUp(current) == NULL)
            {
                IPTableEntry *newEntry = table->create(current);
                newEntry->setTTL(this->currentTTL);
            }
        }
        
        previous = current;
    }
    
    // Amounts of threads and collected IP-IDs
    unsigned short maxThreads = env->getMaxThreads();
    unsigned short nbIPIDs = env->getNbIPIDs();
    unsigned long int nbIPs = (unsigned long int) this->IPsToProbe.size();
    if(nbIPs == 0)
    {
        return;
    }
    unsigned short nbThreads = 1;
    
    // Computes the amount of required threads (+1 is due to collector thread itself)
    unsigned short maxCollectors = maxThreads / (nbIPIDs + 1);
    if(nbIPs > (unsigned long int) maxCollectors)
        nbThreads = maxCollectors;
    else
        nbThreads = (unsigned short) nbIPs;
    
    // Initializes an array of threads
    Thread **th = new Thread*[nbThreads];
    for(unsigned short i = 0; i < nbThreads; i++)
        th[i] = NULL;

    // Does a copy of the IPs list for next hints
    list<InetAddress> backUp1(this->IPsToProbe);
    list<InetAddress> backUp2(this->IPsToProbe);
    list<InetAddress> backUp3(this->IPsToProbe);

    (*out) << "1. IP-ID collection... " << std::flush;

    // Starts scheduling for IP-ID retrieval
    unsigned short j = 0;
    for(unsigned long int i = 0; i < nbIPs; i++)
    {
        InetAddress IPToProbe(this->IPsToProbe.front());
        this->IPsToProbe.pop_front();
        
        if(th[j] != NULL)
        {
            th[j]->join();
            delete th[j];
            th[j] = NULL;
        }
        
        th[j] = new Thread(new IPIDCollector(env, this, IPToProbe, j * nbIPIDs));
        th[j]->start();
        
        j++;
        if(j == nbThreads)
            j = 0;
        
        // 0,01s of delay before next thread
        Thread::invokeSleep(TimeVal(0, 10000));
    }

    // Waiting for all remaining threads to complete
    for(unsigned short i = 0; i < nbThreads; i++)
    {
        if(th[i] != NULL)
        {
            th[i]->join();
            delete th[i];
            th[i] = NULL;
        }
    }
    
    delete[] th;
    
    (*out) << "done." << endl;
    
    // Re-sizes th[] for the other hints (because each time, there is a single thread per IP)
    if(nbIPs > (unsigned long int) maxThreads)
        nbThreads = maxThreads;
    else
        nbThreads = (unsigned short) nbIPs;
    
    th = new Thread*[nbThreads];
    for(unsigned short i = 0; i < nbThreads; i++)
        th[i] = NULL;
    
    // Now schedules threads to try the UDP/ICMP Port Unreachable approach
    (*out) << "2. Probing each IP with UDP (unreachable port)... " << std::flush;
    
    unsigned short range = (DirectProber::DEFAULT_UPPER_SRC_PORT_ICMP_ID - DirectProber::DEFAULT_LOWER_SRC_PORT_ICMP_ID) / maxThreads;
    j = 0;
    for(unsigned long int i = 0; i < nbIPs; i++)
    {
        InetAddress IPToProbe = backUp1.front();
        backUp1.pop_front();
        
        if(th[j] != NULL)
        {
            th[j]->join();
            delete th[j];
            th[j] = NULL;
        }
        
        th[j] = new Thread(new UDPUnreachablePortUnit(env, 
                                                      IPToProbe, 
                                                      DirectProber::DEFAULT_LOWER_SRC_PORT_ICMP_ID + (j * range), 
                                                      DirectProber::DEFAULT_LOWER_SRC_PORT_ICMP_ID + (j * range) + range - 1, 
                                                      DirectProber::DEFAULT_LOWER_DST_PORT_ICMP_SEQ, 
                                                      DirectProber::DEFAULT_UPPER_DST_PORT_ICMP_SEQ));
        th[j]->start();
        
        j++;
        if(j == maxThreads)
            j = 0;
        
        // 0,1s of delay before next thread (if same router, avoids to "bomb" it)
        Thread::invokeSleep(TimeVal(0, 100000));
    }
    
    // Waiting for all remaining threads to complete
    for(unsigned short i = 0; i < nbThreads; i++)
    {
        if(th[i] != NULL)
        {
            th[i]->join();
            delete th[i];
            th[i] = NULL;
        }
    }
    
    (*out) << "done." << endl;
    
    // Now schedules to check if each IP is compatible with prespecified timestamp option...
    (*out) << "3. Sending ICMP timestamp request to each IP... " << std::flush;
    
    j = 0;
    for(unsigned long int i = 0; i < nbIPs; i++)
    {
        InetAddress IPToProbe = backUp2.front();
        backUp2.pop_front();
        
        if(th[j] != NULL)
        {
            th[j]->join();
            delete th[j];
            th[j] = NULL;
        }
        
        th[j] = new Thread(new TimestampCheckUnit(env, 
                                                  IPToProbe, 
                                                  DirectProber::DEFAULT_LOWER_SRC_PORT_ICMP_ID + (j * range), 
                                                  DirectProber::DEFAULT_LOWER_SRC_PORT_ICMP_ID + (j * range) + range - 1, 
                                                  DirectProber::DEFAULT_LOWER_DST_PORT_ICMP_SEQ, 
                                                  DirectProber::DEFAULT_UPPER_DST_PORT_ICMP_SEQ));
        th[j]->start();
        
        j++;
        if(j == maxThreads)
            j = 0;
        
        // 0,1s of delay before next thread (if same router, avoids to "bomb" it)
        Thread::invokeSleep(TimeVal(0, 100000));
    }
    
    // Waiting for all remaining threads to complete
    for(unsigned short i = 0; i < nbThreads; i++)
    {
        if(th[i] != NULL)
        {
            th[i]->join();
            delete th[i];
            th[i] = NULL;
        }
    }
    
    (*out) << "done." << endl;
    
    // Now schedules to resolve host names of each IP by reverse DNS
    (*out) << "4. Reverse DNS... " << std::flush;
    
    j = 0;
    for(unsigned long int i = 0; i < nbIPs; i++)
    {
        InetAddress IPToProbe = backUp3.front();
        backUp3.pop_front();
        
        if(th[j] != NULL)
        {
            th[j]->join();
            delete th[j];
            th[j] = NULL;
        }
        
        th[j] = new Thread(new ReverseDNSUnit(env, IPToProbe));
        th[j]->start();
        
        j++;
        if(j == maxThreads)
            j = 0;
        
        // 0,01s of delay before next thread
        Thread::invokeSleep(TimeVal(0, 10000));
    }
    
    // Waiting for all remaining threads to complete
    for(unsigned short i = 0; i < nbThreads; i++)
    {
        if(th[i] != NULL)
        {
            th[i]->join();
            delete th[i];
            th[i] = NULL;
        }
    }
    
    delete[] th;
    
    (*out) << "done." << endl;
}