Click here to Skip to main content
15,902,189 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Win32 Menu bitmaps Pin
Jonathan Davies18-Aug-13 5:05
Jonathan Davies18-Aug-13 5:05 
GeneralRe: Win32 Menu bitmaps Pin
pasztorpisti18-Aug-13 5:27
pasztorpisti18-Aug-13 5:27 
GeneralRe: Win32 Menu bitmaps Pin
Jonathan Davies18-Aug-13 5:40
Jonathan Davies18-Aug-13 5:40 
GeneralRe: Win32 Menu bitmaps Pin
pasztorpisti18-Aug-13 5:43
pasztorpisti18-Aug-13 5:43 
QuestionUDP sockets - not getting FD_READ after some time of activity on a socket Pin
csrss17-Aug-13 6:28
csrss17-Aug-13 6:28 
SuggestionRe: UDP sockets - not getting FD_READ after some time of activity on a socket Pin
pasztorpisti17-Aug-13 7:01
pasztorpisti17-Aug-13 7:01 
GeneralRe: UDP sockets - not getting FD_READ after some time of activity on a socket Pin
csrss17-Aug-13 7:45
csrss17-Aug-13 7:45 
GeneralRe: UDP sockets - not getting FD_READ after some time of activity on a socket Pin
pasztorpisti17-Aug-13 8:16
pasztorpisti17-Aug-13 8:16 
The main loop of your network thread that handles the send and receive should be relatively small and should be kept together in a small place separated from the other parts of the code. If this critical piece of code is not coupled well separated from your higher level logic, then you should refactor it. This piece of code should handle just raw outgoing and incoming packets on the network thread and all the logic should be implemented separately on top of this layer. A low-level data recv/send loop implementation should be around 100-200 lines of code and all it knows is sending bytes/packets from your send queue, receiving them into a recv queue (and notifying your high level handlers) and exiting when your application exits.
C++
if( socket_event_read * pReadEvent = dynamic_cast<socket_event_read *>(pSocketEvent) )
{
    WSANETWORKEVENTS nt = { 0x00 };
    if( SOCKET_ERROR != ::WSAEnumNetworkEvents(pReadEvent->get_socket(), pSocketEvent, &nt) )
    {
        if( nt.lNetworkEvents & FD_READ && nt.iErrorCode[FD_READ_BIT] == false )
        {
            ::WSARecvFrom(...);
        }
    }
}

For example here your application logic and the a part of your raw network data receiver logic is already mixed that is bad and dynamic_cast is always the indicator of some hacks and bad OO design that needs refactoring. We don't even know when does that dynamic_cast result in a non-null pointer... It seems that you have a big bunch of refactorable code that you will be fixing till the end of your life without a rewrite (and it is worth rewriting stuff... Smile | :) )

BTW, there is another thing that came to my mind regarding WSAWaitForMultipleEvents: FD_READ is level triggered but FD_WRITE is edge triggered and many guys get confused of this. This means that waiting for an FD_READ is safe even if you wait for it twice without reading anything from the socket. The event isn't cleared until data is available for read in the recv buffer of the socket. But FD_WRITE is edge triggered: If you receive an FD_WRITE event then you must assume that FD_WRITE is set until your write fails with EWOULDBLOCK. Receiving an FD_WRITE event clears the FD_WRITE event immediately and it will be set again only when the send buffer of the socket transitions from full to non-full next time. If you try to wait for an FD_WRITE without writing all the available buffer space by receiving EWOULDBLOCK then you will wait for FD_WRITE forever! Depending on how you handle WSAEventSelect and WSAWaitForMultipleEvents your logic can get stuck in many different ways if you don't handle this well.

EDIT: Besides the wakeup event there should be a lock/critical_section as well and these should be used together well along with the shared data (like cancel flag). My loop often got stuck in initial/new network code by screwing up the usage of these or the communication between threads somehow.

modified 17-Aug-13 14:27pm.

GeneralRe: UDP sockets - not getting FD_READ after some time of activity on a socket Pin
csrss17-Aug-13 8:48
csrss17-Aug-13 8:48 
GeneralRe: UDP sockets - not getting FD_READ after some time of activity on a socket Pin
pasztorpisti17-Aug-13 8:53
pasztorpisti17-Aug-13 8:53 
QuestionDirectX hierarchy - who is on first? Pin
Vaclav_17-Aug-13 3:07
Vaclav_17-Aug-13 3:07 
AnswerRe: DirectX hierarchy - who is on first? Pin
Richard MacCutchan17-Aug-13 5:45
mveRichard MacCutchan17-Aug-13 5:45 
AnswerRe: DirectX hierarchy - who is on first? SOLVED Pin
Vaclav_17-Aug-13 18:12
Vaclav_17-Aug-13 18:12 
Questionerror -1.#IND00 in vc++2010 Pin
yahya7716-Aug-13 5:40
yahya7716-Aug-13 5:40 
AnswerRe: error -1.#IND00 in vc++2010 Pin
Chris Losinger16-Aug-13 5:55
professionalChris Losinger16-Aug-13 5:55 
AnswerRe: error -1.#IND00 in vc++2010 Pin
Thong LeTrung16-Aug-13 6:22
Thong LeTrung16-Aug-13 6:22 
AnswerRe: error -1.#IND00 in vc++2010 Pin
Thong LeTrung16-Aug-13 6:40
Thong LeTrung16-Aug-13 6:40 
GeneralRe: error -1.#IND00 in vc++2010 Pin
yahya7716-Aug-13 7:07
yahya7716-Aug-13 7:07 
AnswerRe: error -1.#IND00 in vc++2010 Pin
Thong LeTrung16-Aug-13 7:04
Thong LeTrung16-Aug-13 7:04 
GeneralRe: error -1.#IND00 in vc++2010 Pin
yahya7716-Aug-13 7:17
yahya7716-Aug-13 7:17 
QuestionHow To Save Data for Microsoft Word 2007 or 2010 on Windows OS Pin
Thong LeTrung15-Aug-13 23:07
Thong LeTrung15-Aug-13 23:07 
AnswerRe: How To Save Data for Microsoft Word 2007 or 2010 on Windows OS Pin
Erudite_Eric15-Aug-13 23:13
Erudite_Eric15-Aug-13 23:13 
GeneralRe: How To Save Data for Microsoft Word 2007 or 2010 on Windows OS Pin
Thong LeTrung15-Aug-13 23:44
Thong LeTrung15-Aug-13 23:44 
GeneralRe: How To Save Data for Microsoft Word 2007 or 2010 on Windows OS Pin
Erudite_Eric16-Aug-13 1:25
Erudite_Eric16-Aug-13 1:25 
GeneralRe: How To Save Data for Microsoft Word 2007 or 2010 on Windows OS Pin
Thong LeTrung16-Aug-13 19:03
Thong LeTrung16-Aug-13 19:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.