www.pudn.com > crestroncontrol.rar > UProcessMessage.cpp, change:2005-11-22,size:5417b


//--------------------------------------------------------------------------- 
#pragma hdrstop 
 
#include "UProcessMessage.h" 
 
//--------------------------------------------------------------------------- 
TProcessMessage::TProcessMessage() 
{ 
    mpSocketBuffer  = new TSocketBuffer ; 
    mpSocketBuffer->Point = 0; 
    mpStructSize[0] = sizeof(TCCBaseMessage); 
    mpStructSize[1] = sizeof(TCCBaseMessage_512); 
} 
//--------------------------------------------------------------------------- 
 
TProcessMessage::~TProcessMessage() 
{ 
    delete mpSocketBuffer; 
} 
//--------------------------------------------------------------------------- 
void __fastcall TProcessMessage::ServerSocketMsgClientRead( 
    TObject *Sender, TCustomWinSocket *Socket) 
{ 
    int recCount; 
    int procReturn; 
    try 
    { 
        recCount = Socket->ReceiveLength(); 
        while(true) 
        { 
            if((mpSocketBuffer->Point+recCount)>CC_BUFFER_LENGTH) 
            { 
                Socket->ReceiveBuf(((char *)mpSocketBuffer->Data+mpSocketBuffer->Point), 
                    (CC_BUFFER_LENGTH-mpSocketBuffer->Point)); 
            } 
            else 
            { 
                Socket->ReceiveBuf(((char *)mpSocketBuffer->Data+mpSocketBuffer->Point),recCount); 
            } 
            mpSocketBuffer->Point += recCount; 
            procReturn = ProccessCCMessage(mpSocketBuffer); 
            if(procReturn < 0) 
            { 
                break; 
            } 
        } 
    } 
    catch(...) 
    { 
        OutputDebugString("ProcessMessage read error !"); 
    } 
} 
//--------------------------------------------------------------------------- 
int TProcessMessage::ProccessCCMessage(TSocketBuffer *socketBuffer) 
{ 
    static char buffer[CC_BUFFER_LENGTH]; 
    TCCBaseMessage      ccBaseMessage; 
    TCCBaseMessage_512  ccBaseMessage_512; 
    try 
    { 
        while(true) 
        { 
            memcpy(&ccBaseMessage, socketBuffer->Data, sizeof(TCCBaseMessage)); 
            if(ccBaseMessage.Length == mpStructSize[0]) 
            { 
                if((unsigned int)socketBuffer->Point >= mpStructSize[0]) 
                { 
                    memccpy(&ccBaseMessage, socketBuffer->Data, 0x00, mpStructSize[0]); 
                    socketBuffer->Point = socketBuffer->Point - mpStructSize[0]; 
                    memset(&buffer,0,CC_BUFFER_LENGTH); 
                    memcpy(&buffer, socketBuffer->Data + mpStructSize[0], socketBuffer->Point); 
                    memcpy(socketBuffer->Data, &buffer,socketBuffer->Point); 
 
                    proccessCCBaseMessage(&ccBaseMessage); 
                } 
                else 
                { 
                    socketBuffer->Point = 0; 
                    return -1; 
                } 
            } 
            else 
            { 
                if(ccBaseMessage.Length == mpStructSize[1]) 
                { 
                    if((unsigned int)socketBuffer->Point >= mpStructSize[1]) 
                    { 
                        memcpy(&ccBaseMessage_512, socketBuffer->Data, mpStructSize[1]); 
                        socketBuffer->Point = socketBuffer->Point - mpStructSize[1]; 
                        memcpy(&buffer, socketBuffer->Data+mpStructSize[1], socketBuffer->Point); 
                        memcpy(socketBuffer->Data, &buffer, socketBuffer->Point); 
 
                        proccessCCBaseMessage((TCCBaseMessage*)&ccBaseMessage_512); 
                    } 
                    else 
                    { 
                        socketBuffer->Point = 0; 
                        return -1; 
                    } 
                } 
                else 
                { 
                    memset(&buffer,0,CC_BUFFER_LENGTH); 
                    socketBuffer->Point = socketBuffer->Point -1; 
                    memcpy(&buffer, socketBuffer->Data + 1, socketBuffer->Point ); 
                    memcpy(socketBuffer->Data, &buffer,socketBuffer->Point ); 
                } 
            } 
            if(SeekNextMessage(socketBuffer)<0) 
            { 
                socketBuffer->Point = 0; 
                return -1; 
            } 
            if(socketBuffer->Point == 0) 
            { 
                return -1; 
            } 
        } 
    } 
    catch(...) 
    { 
        OutputDebugString("ProcessCCBaseMessage error!"); 
    } 
    return 0; 
} 
//--------------------------------------------------------------------------- 
 
int TProcessMessage::SeekNextMessage(TSocketBuffer *socketBuffer) 
{ 
    char buffer[CC_BUFFER_LENGTH]; 
    for(int i=0;i<socketBuffer->Point-4;i++) 
    { 
        if( ((unsigned char)socketBuffer->Data[i] == 0xFF)&& 
            ((unsigned char)socketBuffer->Data[i+1] == 0xFF)&& 
            ((unsigned char)socketBuffer->Data[i+2] == 0xFF)&& 
            ((unsigned char)socketBuffer->Data[i+3] == 0xFF) ) 
        { 
            if(i==0) 
            { 
                return 0; 
            } 
            else 
            { 
                memcpy(&buffer, socketBuffer->Data+i, (socketBuffer->Point-i)); 
                memcpy(socketBuffer->Data, &buffer, (socketBuffer->Point-i)); 
                socketBuffer->Point -= i; 
                return i; 
            } 
        } 
    } 
    return -1; 
} 
//--------------------------------------------------------------------------- 
 
#pragma package(smart_init)