Re: CSocket, best way to wait for a reply?
- From: "AliR" <AliR@xxxxxxxxxxxxxxxx>
- Date: Fri, 08 Apr 2005 16:08:33 GMT
You are on the right track for what you want to do.
With one exception. You are implying that you are waiting for a receive of
some sort in your function.
What you should actually do is wait for a status change or something that
signals the function that the reply has been received. You can either use a
message pump type function, and check for the status from time to time,
until a specified timeout, or you can use events and WaitForSingleObject.
So what you end up with is a socket that tells the owner class that there is
some data, the owner class then interperts the data and sets a status or
fires an event.
If code is easier to read there is some suedo code
void CMySocket::OnReceive()
{
CAsyncSocket::Receive(....);
ProcessMessage(Buffer,Length);
}
//pares the buffer and the get the message out, I would create a structure
or a class for this
//class CMessage
//{
// long m_Command;
// char *m_Buffer;
// long m_Length;
//};
void CMySocket::ProcessMessage(char *Buffer,long Length)
{
ASSERT(Length > 4);
CMessage Msg;
memcopy(&Msg.m_Command,Buffer,4);
memcopy(Msg.m_Buffer,Buffer,Length-4);
if (Msg.m_Command == the reply you are looking for)
{
Set a Status that the other function is checking or fire off and
event
}
}
BOOL CMySocket::SendAndWaitForReply()
{
SendMessage(...);
//or you can use the message loop wait function in my original post,
easier but uglier ;)
if (WaitForSingleObject(...) == WAIT_TIMEOUT)
{
return FALSE;
}
return TRUE;
}
This way you are more event driven and you aren't actually doing reads
outside of OnRecieve,
AliR.
"Simon" <spambucket@xxxxxxxxxxxx> wrote in message
news:uBTLIEEPFHA.3336@xxxxxxxxxxxxxxxxxxxxxxx
>
> > I answered that earlier: If you let the MFC message pump operate
> > normally then your OnReceive override will be called when a reply comes
in
> > from the server. It is a message handler: If you get a mouse click a
> > message comes in; If you get data from the server OnReceive is called.
> >
>
> Yes, I understood that, but what I was talking about is a reply from
within
> a function
> Something like
>
> BOOL IsTodatMonday()
> {
> pSock->Send( &askdate, sizeof(askdate) );
> // wait for a reply from the server
> BOOL bWhatEverTheServerSaid = FALSE;
> GetServerReply( &bWhatEverTheServerSaid ); // <-- it will wait for
> onreceive(...)
>
> return ( bWhatEverTheServerSaid );
> }
>
> This is not possible to do without blocking the function itself.
> Either that or I misunderstood something.
>
> How would you do it?
>
> Simon
>
>
.
- Follow-Ups:
- Re: CSocket, best way to wait for a reply?
- From: Michael K. O'Neill
- Re: CSocket, best way to wait for a reply?
- References:
- CSocket, best way to wait for a reply?
- From: simon
- Re: CSocket, best way to wait for a reply?
- From: Joseph M . Newcomer
- Re: CSocket, best way to wait for a reply?
- From: simon
- Re: CSocket, best way to wait for a reply?
- From: Scott McPhillips [MVP]
- Re: CSocket, best way to wait for a reply?
- From: simon
- Re: CSocket, best way to wait for a reply?
- From: Scott McPhillips [MVP]
- Re: CSocket, best way to wait for a reply?
- From: Simon
- CSocket, best way to wait for a reply?
- Prev by Date: Re: why microsoft choose mfc rather than wtl?
- Next by Date: Re: visual studio c++ .net 2005 and mfc?
- Previous by thread: Re: CSocket, best way to wait for a reply?
- Next by thread: Re: CSocket, best way to wait for a reply?
- Index(es):
Relevant Pages
|