Saving received data from CAsyncSocket to a queue
I'm using CASynchSocket in my application for networking using tcp/udp protocols.
I use a class namedMySocketwhich extends CAsynchSocket. It has a queue in which the received data on the OnReceive overried function is stored, this queue is accessed from outside MySocket to read/delete data.
MySocket has a function member IsDataAvailable which simply returns !queue.empty()
When running my application, I set a breakpoint in the OnReceive function, the data is received and stored in the queue, but if ask theMySocket class about the availability of the data, the answer is NO. I set a breakpoint in theIsDataAvailable(), thequeue is empty even though OnReceived function is called before theIsDataAvailable() memeber function:-o here is a snapshot of my code:
private:
deque <NetworkMsg> receivedData;
// Used to control access to receivedData structure.
CMutex queueProtector;
public:
ComSocket(){};
virtual ~ComSocket() {};
void Close() {CAsyncSocket::Close();};
BOOL IsOpen() const {return (m_hSocket != INVALID_SOCKET);};
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CUDPSocket)
public:
virtual void OnReceive(int nErrorCode);
//}}AFX_VIRTUAL
// Generated message map functions
//{{AFX_MSG(CUDPSocket)
// NOTE - the ClassWizard will add and remove member functions here.
virtual void OnSend(int nErrorCode);
virtual void OnConnect(int nErrorCode);
virtual void OnAccept(int nErrorCode);
//}}AFX_MSG
bool IsDataAvailable(void){
return !receivedData.empty()
};
NetworkMsg GetNextReceivedData(void);
};
Has any one of you already experienced this kind of problem?
And here is the OnReceive function
char buffer[1024]; // Increase size as needed
UINT Port;
int Size;
CAsyncSocket::OnReceive(nErrorCode);
if (nErrorCode) {
printf("\nOnReceive nErrorCode: %i", nErrorCode);
return;
}
Size = ReceiveFrom(buffer, sizeof buffer, IPAddress, Port);
if (!Size || Size == SOCKET_ERROR) {
int err=GetLastError();
printf("\nReceiveFrom error code: %u", GetLastError());
return;
}
NetworkMsg nMsg;
nMsg.data = new char[Size];
nMsg.dataSize = Size;
for(int i=0; i< Size; i++){
nMsg.data[i] = buffer[i];
}
int i = this ->m_hSocket;
CSingleLock lock( &queueProtector );
lock.Lock();
receivedData.push_back(nMsg);
lock.Unlock();

