ProcessLink.cpp
// Continually reads a mesage in 1024 byte chunks from OctocadCs, and adds it to the message queue
//  when the message has finished being read.
//  WARNING: Messages are limited to the size of an unsigned Int64. I'm fairly certain other things will break before I hit that limit.
//  WARNING2: This only works if messages don't get squished together. That *should* not occur with the server write structure.
Continually reads a mesage in 1024 byte chunks from OctocadCs, and adds it to the message queue
when the message has finished being read.
WARNING: Messages are limited to the size of an unsigned Int64. I'm fairly certain other things will break before I hit that limit.
WARNING2: This only works if messages don't get squished together. That *should* not occur with the server write structure.
Continually reads a mesage in 1024 byte chunks from OctocadCs and adds it to the message queue when the message has finished being read . WARNING : Messages are limited to the size of an unsigned Int64 . I ' m fairly certain other things will break before I hit that limit . WARNING2 : This only works if messages don ' t get squished together . That * should * not occur with the server write structure .
void ProcessLink::ReadFromCsServer()
{
    const int BUFFER_LENGTH = 2048;
    char data[BUFFER_LENGTH];
    DWORD numRead = 1;
    unsigned long long remainingMessageLength = 0;
    bool receivingMessage = false;
    while (numRead > 0)
    {
        ReadFile(csOutputHandle, data, BUFFER_LENGTH, &numRead, NULL);
        if (numRead > 0)
        {
            if (receivingMessage)
            {
                unsigned long long offset = newMessage.messageLength - remainingMessageLength;
                std::copy(&data[0], &data[0] + numRead, &newMessage.messageData[offset]);
                if (numRead > remainingMessageLength)
                {
                    std::cout << "ERROR: Squishing detected! Rewrite the message receiving backend!" << std::endl;
                    remainingMessageLength = 0;
                }
                else
                {
                    remainingMessageLength -= numRead;
                }
            }
            else
            {
                if (numRead < sizeof(unsigned long long))
                {
                    std::cout << "Read in insufficient data for the start of a message!" << std::endl;
                    return;
                }
                newMessage.messageType = data[0];
                std::copy(&data[1], &data[1] + sizeof(unsigned long long), reinterpret_cast<char*>(&remainingMessageLength));
                newMessage.messageLength = remainingMessageLength;
                if (remainingMessageLength != 0)
                {
                    newMessage.messageData = new char[(std::size_t)remainingMessageLength]; 
                    std::copy(&data[9], &data[9] + numRead - 9, &newMessage.messageData[0]);
                    remainingMessageLength = newMessage.messageLength + 9 - numRead;
                }
                else
                {
                    newMessage.messageData = nullptr;
                }
                receivingMessage = true;
            }
            if (receivingMessage && remainingMessageLength == 0)
            {
                stackModificationMutex.lock();
                receivedMessages.push(newMessage);
                stackModificationMutex.unlock();
                receivingMessage = false;
            }
        }
    }
}
void ProcessLink :: ReadFromCsServer ( ) { const int BUFFER_LENGTH = 2048 ; char data [ BUFFER_LENGTH ] ; DWORD numRead = 1 ; unsigned long long remainingMessageLength = 0 ; bool receivingMessage = false ; while ( numRead > 0 ) { ReadFile ( csOutputHandle , data , BUFFER_LENGTH , & numRead , NULL ) ; if ( numRead > 0 ) { if ( receivingMessage ) { unsigned long long offset = newMessage . messageLength - remainingMessageLength ; std :: copy ( & data [ 0 ] , & data [ 0 ] + numRead , & newMessage . messageData [ offset ] ) ; if ( numRead > remainingMessageLength ) { std :: cout << " " << std :: endl ; remainingMessageLength = 0 ; } else { remainingMessageLength -= numRead ; } } else { if ( numRead < sizeof ( unsigned long long ) ) { std :: cout << " " << std :: endl ; return ; } newMessage . messageType = data [ 0 ] ; std :: copy ( & data [ 1 ] , & data [ 1 ] + sizeof ( unsigned long long ) , reinterpret_cast < char * > ( & remainingMessageLength ) ) ; newMessage . messageLength = remainingMessageLength ; if ( remainingMessageLength != 0 ) { newMessage . messageData = new char [ ( std :: size_t ) remainingMessageLength ] ; std :: copy ( & data [ 9 ] , & data [ 9 ] + numRead - 9 , & newMessage . messageData [ 0 ] ) ; remainingMessageLength = newMessage . messageLength + 9 - numRead ; } else { newMessage . messageData = nullptr ; } receivingMessage = true ; } if ( receivingMessage && remainingMessageLength == 0 ) { stackModificationMutex . lock ( ) ; receivedMessages . push ( newMessage ) ; stackModificationMutex . unlock ( ) ; receivingMessage = false ; } } } }
Continually reads a mesage in 1024 byte chunks from OctocadCs, and adds it to the message queue
when the message has finished being read.
Continually reads a mesage in 1024 byte chunks from OctocadCs and adds it to the message queue when the message has finished being read .
// Add this onto the amount we have currently read for this message. // Extract the message length and type from the stream // Create and copy in the rest of the message that was transmitted // Avoid auto-conversion to an int. // Finished receiving all the bytes we want.