Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

timer

Status
Not open for further replies.

GhHss

Programmer
Sep 18, 2011
1
0
0
US
Hi,

I am working on a socket programming code and I need to modify part of it. In the code there is a line where a process receives a message :

receive(source, buf);

When a process receives a message, I don't want to deliver the message immediately, what I want is to deliver it at sometime later (with some delay).
do you know how I can do it? I tried the sleep() but it is not what I want, since when the process is asleep,it does not receive next messages until sleep time is elapsed. I think I should use timers, like start a timer when a message is received, and after the timer is expired I deliver the message, and I need to start a timer for each message. I don't know how to do this and which timers I should use. I f anybody can explain this to me, it will be a great help!

thanks a lot!
Ghazale
 
So how about a linked list of stored messages, with delivery times?
Code:
struct messagelist {
    struct messagelist *next;
    time_t deliveryTime;
    char *message;
};
So when you receive a message, you
- calculate the absolute delivery time (now + whatever delta is appropriate for this message)
- allocate space for the message, and copy it
- insert into the list in the correct time slot - the list is ordered by deliveryTime.

Each time you call select(), you specify a timeout from now until the head of the list.
- if you get a message, you add it to the list
- if select times out, you remove the head from the list and send the stored message on it's way.



--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top