c - Handling asynchronous sockets in WinSock? -
i'm using message window , wsaasyncselect. how can keep track of multiple sockets (the clients) 1 message window?
windows supports several modes of socket operation, , need clear 1 using:
- blocking sockets. send , recv block.
- non-blocking sockets: send , recv return e_wouldblock, , select() used determine sockets ready
- asynchronous sockets: wsaasyncselect - sockets post event notifications hwnd.
- eventsockets: wsaeventselect - sockets signal events.
- overlapped sockets: wsasend , wsarecv used sockets passing in overlapped structures. overlapped sockets can combined iocompletionports , provide best scalability.
in terms of convenience, asynchronous sockets simple, , supported mfc casyncsocket class.
event sockets tricky use maximum number of objects passable waitformultipleobjects 32.
overlapped sockets, io completionports, scalable way handle sockets , allows windows based servers scale tens of thousands of sockets.
in experience, when using async sockets, following things come mind:
handling fd events via window messages can handle "lots" of sockets, performance begin suffer event handling done in 1 thread, serialized through message queue might busy handling ui events if used in single threaded gui app.
if hosting gui windows or timers on same thread lots of sockets: wm_timer , wm_paint messages low priority, , generated if message queue empty. busy sockets can cause gui painting, or settimer based timing fail.
creating dedicated worker thread handle sockets if hosting gui solves these problems. given worker thread have message loop, can use message queue inter-thread comms - post wm_app messages thread.
the easiest way map fd callbacks socket objects create array of socketobjects each hwnd receiving messages, , use wm_user+index message id each time call wasasyncselect. then, when receive messages in range wm_user wm_user+(array size) can extract corresponding state object. wm_user 0x400, , wm_app 0x8000, can index 31744 sockets per message window using method.
don't use static scope array. need associate array window might want create sockets on multiple threads. each thread need own message loop, , message window.
hwnd_message friend
Comments
Post a Comment