From: Hector Santos on
Kursat wrote:

> "Hector Santos" <sant9442(a)nospam.gmail.com> wrote in message
> news:OuUqk3f5KHA.5476(a)TK2MSFTNGP06.phx.gbl...
>> Kursat,
>>
>> Probably a small snippet of code showing basically how you are doing this
>> because I can't duplicate it. However, my quick test has no association
>> with sockets, which to me, is pretty odd as to why and when to employ this
>> logic. I never can across a need to do something like this with
>> overlapping socket I/O. Are you trying to program some sort of socket I/O
>> timeout?
>>
>> I'm winging it but it sounds like you are "pulling the rug from within
>> threads feet" prematurely or out of sync and an invalid handle is created.
>> If all you are seeking here is to create an asynchronous socket I/O
>> timeout design, then why not simply use select().
>>
>> --
>> HLS
>
> Hi Hector,
>
> Yes, I use timer queue timers for socket IO timeout. I can not figure out
> why overlapped socket operations don't have any timeout mechanism by
> themselves but this is another story. I don't use select() because I am
> using IO completion ports.

What are the timeouts for? Outgoing connections?

Does your CreateTimerQueueTimer() fail always, the first time? Are
you recreating timers? At they periodic, one shots?

Anyway, I could not find of a situation where the function failed.

--
HLS
From: Kursat on

"Hector Santos" <sant9442(a)nospam.gmail.com> wrote in message
news:er2fRKk5KHA.1924(a)TK2MSFTNGP06.phx.gbl...
> Kursat wrote:
>
>> "Hector Santos" <sant9442(a)nospam.gmail.com> wrote in message
>> news:OuUqk3f5KHA.5476(a)TK2MSFTNGP06.phx.gbl...
>>> Kursat,
>>>
>>> Probably a small snippet of code showing basically how you are doing
>>> this because I can't duplicate it. However, my quick test has no
>>> association with sockets, which to me, is pretty odd as to why and when
>>> to employ this logic. I never can across a need to do something like
>>> this with overlapping socket I/O. Are you trying to program some sort
>>> of socket I/O timeout?
>>>
>>> I'm winging it but it sounds like you are "pulling the rug from within
>>> threads feet" prematurely or out of sync and an invalid handle is
>>> created. If all you are seeking here is to create an asynchronous socket
>>> I/O timeout design, then why not simply use select().
>>>
>>> --
>>> HLS
>>
>> Hi Hector,
>>
>> Yes, I use timer queue timers for socket IO timeout. I can not figure out
>> why overlapped socket operations don't have any timeout mechanism by
>> themselves but this is another story. I don't use select() because I am
>> using IO completion ports.
>
> What are the timeouts for? Outgoing connections?
>
> Does your CreateTimerQueueTimer() fail always, the first time? Are you
> recreating timers? At they periodic, one shots?
>
> Anyway, I could not find of a situation where the function failed.
>
> --
> HLS

The logic is very complex but, in essence;
I issue an overlapped WSARecv() for a socket. I send a command to a device
over the same socket and expect response in a certain time interval.
Because overlapped WSARecv() has no integrated timeout mechanism and it only
completes when some data is available for read, I should keep track of time
so that I can inform clients if the respond does not arrive in the desired
time period. So, I create a timer whenever I send a command. If response
comes in the desired time period then I delete the timer and send response
to registered clients. Otherwise the timer expires then I know that the
command has timed out and act accordingly. I developed a test application
for stress-testing this logic. The application simply sends commands as fast
as possible and generates a trace file about timeout and IO completion
behaviors. In that file I saw that CreateTimerQueueTimer () failed with
ERROR_INVALID_HANDLE once. I tried to reproduce the same situation but it
has not happened again. The logic seems working well now, but I suspect that
this subtle error points to a bug in my code so I want to know if there is a
certain situation in which CreateTimerQueueTimer() fails with
ERROR_INVALID_HANDLE so that I can review my logic against that situation. A
Microsoft guy who knows internals of timer queue API may explain the issue,
in the end it is nothing but a function and in its implementation there
should be something like this;

if (something_goes_wrong)
{
SetLastError (ERROR_INVALID_HANDLE);
return FALSE;
}

I want to know what goes wrong.


From: m on
I am not sure what protocol you are attempting to implement, but from your
description I infer that it is UDP based and you intend to communicate with
a _single_ remote host. IO completion ports are designed and optimized to
be used by applications issuing many overlapped IO operations from a few
threads and IOOP timeouts are canonically incidental for this paradigm.
Consider that if the IOOPs are file IO, then timeout is irrelevant; and if
stream socket based, then a transport issue; and if datagram socket based,
conflict with the stateless completion processing model that underlies IOCP.
For cases where the protocol is very complex, a dedicated thread sync IO
model often works well, but if you opt for stateless IO, then you must
implement application protocol level timeouts using an out-of-band mechanism
(ie a timer) that will trigger a logical close.

There are many examples of IOCP based servers on the net.


"Kursat" <xx(a)yy.com> wrote in message
news:uVB#Rsk5KHA.980(a)TK2MSFTNGP04.phx.gbl...
>
> "Hector Santos" <sant9442(a)nospam.gmail.com> wrote in message
> news:er2fRKk5KHA.1924(a)TK2MSFTNGP06.phx.gbl...
>> Kursat wrote:
>>
>>> "Hector Santos" <sant9442(a)nospam.gmail.com> wrote in message
>>> news:OuUqk3f5KHA.5476(a)TK2MSFTNGP06.phx.gbl...
>>>> Kursat,
>>>>
>>>> Probably a small snippet of code showing basically how you are doing
>>>> this because I can't duplicate it. However, my quick test has no
>>>> association with sockets, which to me, is pretty odd as to why and when
>>>> to employ this logic. I never can across a need to do something like
>>>> this with overlapping socket I/O. Are you trying to program some sort
>>>> of socket I/O timeout?
>>>>
>>>> I'm winging it but it sounds like you are "pulling the rug from within
>>>> threads feet" prematurely or out of sync and an invalid handle is
>>>> created. If all you are seeking here is to create an asynchronous
>>>> socket I/O timeout design, then why not simply use select().
>>>>
>>>> --
>>>> HLS
>>>
>>> Hi Hector,
>>>
>>> Yes, I use timer queue timers for socket IO timeout. I can not figure
>>> out why overlapped socket operations don't have any timeout mechanism by
>>> themselves but this is another story. I don't use select() because I am
>>> using IO completion ports.
>>
>> What are the timeouts for? Outgoing connections?
>>
>> Does your CreateTimerQueueTimer() fail always, the first time? Are you
>> recreating timers? At they periodic, one shots?
>>
>> Anyway, I could not find of a situation where the function failed.
>>
>> --
>> HLS
>
> The logic is very complex but, in essence;
> I issue an overlapped WSARecv() for a socket. I send a command to a device
> over the same socket and expect response in a certain time interval.
> Because overlapped WSARecv() has no integrated timeout mechanism and it
> only completes when some data is available for read, I should keep track
> of time so that I can inform clients if the respond does not arrive in the
> desired time period. So, I create a timer whenever I send a command. If
> response comes in the desired time period then I delete the timer and send
> response to registered clients. Otherwise the timer expires then I know
> that the command has timed out and act accordingly. I developed a test
> application for stress-testing this logic. The application simply sends
> commands as fast as possible and generates a trace file about timeout and
> IO completion behaviors. In that file I saw that CreateTimerQueueTimer ()
> failed with ERROR_INVALID_HANDLE once. I tried to reproduce the same
> situation but it has not happened again. The logic seems working well now,
> but I suspect that this subtle error points to a bug in my code so I want
> to know if there is a certain situation in which CreateTimerQueueTimer()
> fails with ERROR_INVALID_HANDLE so that I can review my logic against that
> situation. A Microsoft guy who knows internals of timer queue API may
> explain the issue, in the end it is nothing but a function and in its
> implementation there should be something like this;
>
> if (something_goes_wrong)
> {
> SetLastError (ERROR_INVALID_HANDLE);
> return FALSE;
> }
>
> I want to know what goes wrong.
>
From: Hector Santos on
Kursat wrote:

> The logic is very complex but, in essence;
> I issue an overlapped WSARecv() for a socket. I send a command to a device
> over the same socket and expect response in a certain time interval.
> Because overlapped WSARecv() has no integrated timeout mechanism and it only
> completes when some data is available for read, I should keep track of time

> so that I can inform clients if the respond does not arrive in the
> desired time period.

If this is the sole reason for using the timer queue, then its not a
good one IMTO. If you are using overlapping I/O with WSARecv(), you
can couple this with

WSAWaitForMultipleEvents() (w/ non-infinite timeouts)
WSAGetOverlappedResult()

to introduce your own (efficient) polling timeout.

Roughly,

// Send Command

// Receive Response

HANDLE hOvrEvent = WSACreateEvent();
WSABUF buf[1] = {0};
buf[0].len = 4*1024;
buf[0].buf = new char[buf[i].len];

while (some loop)
{
DWORD dwRead = 0;
DWORD dwFlags = 0;
WSAOVERLAPPED ovr = {0};
ovr.hEvent = hOvrEvent;
if (WSARecv(hSocket,
buf,
MAX_WSABUF,
&dwRead,
&dwFlags,
&ovr,
NULL) == SOCKET_ERROR) {

if (WSAGetLastError() == WSA_IO_PENDING) {
WSAEVENT evts[2] = {0};
evts[0] = hOvrEvent;
evts[1] = hSomerGlobalTerminateEvent;

// 5 second idle TIMEOUT for receive

switch(WSAWaitForMultipleEvents(1,evts,5000,FALSE))
{
case WAIT_OBJECT_0:
DWORD iof = 0;
if (WSAGetOverlappedResult(hSocket,&ovr,
&dwRead,TRUE,&iof)) {
// process your data
} else {
// socket closed?
// break out of loop
}
break

case WAIT_OBJECT_0+1:
// global terminal event
// Break out of loop
break;

case WAIT_TIMEOUT:
// IDLE TIMEOUT
// Break out of loop
break;
}
} else {
// perhaps socket closed?
break;
}
}
}

The above works very efficiently. In general, for a receive, an idle
timeout is what you are looking for. No out of band timer required,
unless as M indicated, you want to use this in some fashion to
invalidate a handle to break out of the above. It might be perhaps a
global terminate event set by the main thread or for some reason
invalidate the socket.

Note: You can make the wait be 1 second perhaps, and have a
dwIdleCount++ in WAIT_TIMEOUT and when it reached X, you about the
reading. The dwIdleCount is reset to 0 with WAIT_OBJECT_0 event.

That allows you to make sensitive to some other monitoring in the
loop, like a ABORT button.

--
HLS
From: Kursat on
Hi m,

I use TCP not UDP and, in fact, timeout mechanism I mentioned is not related
to the transport layer. It is my command-response timeout. The application
communicates with some numbers of embedded devices over TCP. When I send a
command to a device, it must respond in, let's say, 2 seconds. For some
reasons, it may not be able to respond as fast as I expect. This is not
related to the physical or logical link's status. I should use a mechanism to
determine if a command has been responded in the desired time period. If so
then I process the received data, otherwise I suppose that the command has
timed out. I use IOCP because the server communicates with many devices and
performance and scalability are desired. I implemented same server with
serial communication and everything was easier because I can set timeouts for
serial communication and if no data comes in timeout period then a zero byte
ReadFile() completion occurs on IOCP thus I know there is no response. From
this explanation, is it appropriate using timer queue timers?

"m" wrote:

> I am not sure what protocol you are attempting to implement, but from your
> description I infer that it is UDP based and you intend to communicate with
> a _single_ remote host. IO completion ports are designed and optimized to
> be used by applications issuing many overlapped IO operations from a few
> threads and IOOP timeouts are canonically incidental for this paradigm.
> Consider that if the IOOPs are file IO, then timeout is irrelevant; and if
> stream socket based, then a transport issue; and if datagram socket based,
> conflict with the stateless completion processing model that underlies IOCP.
> For cases where the protocol is very complex, a dedicated thread sync IO
> model often works well, but if you opt for stateless IO, then you must
> implement application protocol level timeouts using an out-of-band mechanism
> (ie a timer) that will trigger a logical close.
>
> There are many examples of IOCP based servers on the net.
>
>
> "Kursat" <xx(a)yy.com> wrote in message
> news:uVB#Rsk5KHA.980(a)TK2MSFTNGP04.phx.gbl...
> >
> > "Hector Santos" <sant9442(a)nospam.gmail.com> wrote in message
> > news:er2fRKk5KHA.1924(a)TK2MSFTNGP06.phx.gbl...
> >> Kursat wrote:
> >>
> >>> "Hector Santos" <sant9442(a)nospam.gmail.com> wrote in message
> >>> news:OuUqk3f5KHA.5476(a)TK2MSFTNGP06.phx.gbl...
> >>>> Kursat,
> >>>>
> >>>> Probably a small snippet of code showing basically how you are doing
> >>>> this because I can't duplicate it. However, my quick test has no
> >>>> association with sockets, which to me, is pretty odd as to why and when
> >>>> to employ this logic. I never can across a need to do something like
> >>>> this with overlapping socket I/O. Are you trying to program some sort
> >>>> of socket I/O timeout?
> >>>>
> >>>> I'm winging it but it sounds like you are "pulling the rug from within
> >>>> threads feet" prematurely or out of sync and an invalid handle is
> >>>> created. If all you are seeking here is to create an asynchronous
> >>>> socket I/O timeout design, then why not simply use select().
> >>>>
> >>>> --
> >>>> HLS
> >>>
> >>> Hi Hector,
> >>>
> >>> Yes, I use timer queue timers for socket IO timeout. I can not figure
> >>> out why overlapped socket operations don't have any timeout mechanism by
> >>> themselves but this is another story. I don't use select() because I am
> >>> using IO completion ports.
> >>
> >> What are the timeouts for? Outgoing connections?
> >>
> >> Does your CreateTimerQueueTimer() fail always, the first time? Are you
> >> recreating timers? At they periodic, one shots?
> >>
> >> Anyway, I could not find of a situation where the function failed.
> >>
> >> --
> >> HLS
> >
> > The logic is very complex but, in essence;
> > I issue an overlapped WSARecv() for a socket. I send a command to a device
> > over the same socket and expect response in a certain time interval.
> > Because overlapped WSARecv() has no integrated timeout mechanism and it
> > only completes when some data is available for read, I should keep track
> > of time so that I can inform clients if the respond does not arrive in the
> > desired time period. So, I create a timer whenever I send a command. If
> > response comes in the desired time period then I delete the timer and send
> > response to registered clients. Otherwise the timer expires then I know
> > that the command has timed out and act accordingly. I developed a test
> > application for stress-testing this logic. The application simply sends
> > commands as fast as possible and generates a trace file about timeout and
> > IO completion behaviors. In that file I saw that CreateTimerQueueTimer ()
> > failed with ERROR_INVALID_HANDLE once. I tried to reproduce the same
> > situation but it has not happened again. The logic seems working well now,
> > but I suspect that this subtle error points to a bug in my code so I want
> > to know if there is a certain situation in which CreateTimerQueueTimer()
> > fails with ERROR_INVALID_HANDLE so that I can review my logic against that
> > situation. A Microsoft guy who knows internals of timer queue API may
> > explain the issue, in the end it is nothing but a function and in its
> > implementation there should be something like this;
> >
> > if (something_goes_wrong)
> > {
> > SetLastError (ERROR_INVALID_HANDLE);
> > return FALSE;
> > }
> >
> > I want to know what goes wrong.
> >
> .
>