Re: CSocket, best way to wait for a reply?



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
>
>


.



Relevant Pages

  • [NFSD] kconfig: Select things at the closest tristate instead of bool
    ... In general when we have a bool sitting under a tristate it is ... If you want your Linux box to act as an NFS *server*, ... bool "Provide server support for the NFSv3 ACL protocol extension" ... Implement the NFSv3 ACL protocol extension for manipulating POSIX ...
    (Linux-Kernel)
  • [NFSD]: Select things at the closest tristate instead of bool
    ... In general when we have a bool sitting under a tristate it is ... If you want your Linux box to act as an NFS *server*, ... bool "Provide server support for the NFSv3 ACL protocol extension" ... Implement the NFSv3 ACL protocol extension for manipulating POSIX ...
    (Linux-Kernel)
  • LSASS errors
    ... Last night a server had unusual errors with winlogon and ... What would you do if you found this on your server? ... Microsoft.SharePoint.WebPartPages.ImageWebPart o, bool isNullable, bool ... Microsoft.SharePoint.WebPartPages.WebPart o, bool isNullable, bool needType) ...
    (microsoft.public.windows.server.security)
  • FTP API
    ... BOOL CFtp::FTP_OpenSession(LPCSTR server, LPCSTR user, LPCSTR pass) ...
    (microsoft.public.vc.language)