Jump to content

create handle to a custom timer class?

- - - - -

  • Please log in to reply
4 replies to this topic

#1
alirezan

alirezan

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
Hi

I'm fairly new to many concepts in C/C++ and am just learning. I am trying to to create a timer class that can be used to check for timeouts on network operations. I was told I will need to assign a "handle" to each timer. I do know what handles are (i.e. handle to windows in visual C++) but I don't know in case of a custom timer how I should go about doing it. Can anyone help me understand this concept please?

Thanks

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
There is a lack of information on your end. If you are working with network sockets, I would recommend looking at the Winsock select function. select Function (Windows)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
alirezan

alirezan

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
Sorry!
I'm working with Serial communication not network protocols. I'm trying to write a simple timer code that creates a timer and returns a handle. The timer will be used to time the packet transmission and make sure we retransmit it the ACK doesn't arrive within certain period of time. The handle is used for operations such as checking, resetting, closing etc after it was created. A system can have multiple timers running at the same time. Each timer will have a unique handle.

Thanks

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1

class Timer {

    public:

        void start() {

        #ifdef _WIN32

            ::QueryPerformanceCounter(&m_start);

        #else

            timeval time;

            gettimeofday(&time, NULL);

            m_start = time.tv_usec;

        #endif

        }


        double elapsed() const {

        #ifdef _WIN32

            LARGE_INTEGER ticksPerSecond;

            LARGE_INTEGER end;

            ::QueryPerformanceFrequency(&ticksPerSecond);

            ::QueryPerformanceCounter(&end);

            return static_cast<double>(end.QuadPart - m_start.QuadPart) / ticksPerSecond.QuadPart;

        #else

            timeval time;

            gettimeofday(&time, NULL);

            return static_cast<double>(time.tv_usec - m_start);

        #endif

        }


        void restart() {

            Timer::start();

        }


        void sleep(int milisec) {

            if (milisec > 0)

            #ifdef _WIN32

                ::Sleep(milisec);

            #else

                usleep(milisec * 1000);

            #endif

        }


        #ifndef _WIN32

        void uSleep(int microsec) {

            if (microsec > 0)

                usleep(microsec);

        }

        #endif


    private:

    #ifdef _WIN32

        LARGE_INTEGER    m_start;

    #else

        double           m_start;

    #endif

};


A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
Microsecond timer for Windows

The above explains some timers in win32 and can help to understand what is being used in the code above.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users