Alright, well, there's a lot of code.. but here's what I believe is important:
(part of AESocket class definition.. all member function definitions are here for the time being)
Code:
struct SockInfo
{
SockInfo() :desc(INVALID_SOCKET), notify(false, false), defEventHandler(NULL)
{
for(int i = 0; i < FD_MAX_EVENTS; ++i)
eventHandlers[i] = NULL;
}
SOCKET desc;
Event notify; //Event - simple wrapper. constructor: bool manualReset, bool initialState
//Callback routines, for each network event, and a user-specified 'default' handler
void (*eventHandlers[FD_MAX_EVENTS])(AESocket, int);
void (*defEventHandler)(AESocket, int, int);
};
RCPtr<SockInfo> si; //Generic reference-counted pointer class I developed earlier
void handleEvents()
{
if(si->notify.poll()) //Poll the event object associated with the socket
{
MB("handleEvents(): Event found to be set."); //MB is a macro for MessageBox()
WSANETWORKEVENTS n;
int res = WSAEnumNetworkEvents(si->desc, (HANDLE)(si->notify), &n);
if(res == SOCKET_ERROR)
{
MERR("handleEvents(): WSAEnumNetworkEvents error.");
return;
}
for(int i = 0; i < FD_MAX_EVENTS; ++i)
{
if(n.lNetworkEvents & (1 << i))
{
if(si->eventHandlers[i] != NULL)
si->eventHandlers[i](si, n.iErrorCode[i]);
else if(si->defEventHandler != NULL)
si->defEventHandler(si, (1 << i), n.iErrorCode[i]);
}
}
}
}
In main(), when accepting a new socket:
Code:
//v is a std::vector<AESocket>
v.push_back(s.accept(&clientCallback));
v.back().setEventHandler(FD_READ_BIT, &readCallback);
v.back().setEventHandler(FD_WRITE_BIT, &writeCallback);
readCallback() is defined as:
Code:
void readCallback(AESocket s, int err)
{
MB("Got data to read!");
}
And I have a polling loop, which I plan in the future to change:
Code:
for(;;)
{
MB("Listener");
s.handleEvents(); //s is the listening socket
MB("Client");
for(int i = 0; i < v.size(); ++i) //At this time, there should only ever be 1 element
v[i].handleEvents();
Sleep(1);
}
I apologize for all the gaps in the code, but there's a lot of code and it's all uncommented, and it's somewhat difficult to pick and choose what's relevant. I've attached the full source too if you're willing (or interested) to go through it, though I know it's a big favour to ask. I've tested *successfully* as far as listen() and accept() on the server socket, which seem to work as expected.
To reproduce the problem, compile and run the program, open telnet and connect to localhost, click through the messageboxes until it loops steadily between "client" and "listener", and then type something into telnet and begin clicking through the messageboxes again.
Thanks in advance!