Click here to Skip to main content
16,013,338 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How to send huge data via sockets in continuous intervals Pin
Richard MacCutchan23-May-16 3:33
mveRichard MacCutchan23-May-16 3:33 
AnswerRe: How to send huge data via sockets in continuous intervals Pin
leon de boer22-May-16 22:16
leon de boer22-May-16 22:16 
GeneralRe: How to send huge data via sockets in continuous intervals Pin
manoharbalu23-May-16 3:07
manoharbalu23-May-16 3:07 
GeneralRe: How to send huge data via sockets in continuous intervals Pin
leon de boer23-May-16 5:41
leon de boer23-May-16 5:41 
GeneralRe: How to send huge data via sockets in continuous intervals Pin
manoharbalu24-May-16 1:57
manoharbalu24-May-16 1:57 
GeneralRe: How to send huge data via sockets in continuous intervals Pin
leon de boer24-May-16 4:01
leon de boer24-May-16 4:01 
GeneralRe: How to send huge data via sockets in continuous intervals Pin
manoharbalu24-May-16 20:44
manoharbalu24-May-16 20:44 
GeneralRe: How to send huge data via sockets in continuous intervals Pin
leon de boer25-May-16 22:08
leon de boer25-May-16 22:08 
Okay 1 polled 4 state parser code .. I commented it so you can follow what its doing. The parse buffer is 4K so you need to poll min rate of 20-30 times a second. You can increase or decrease the parse buffer size and change the poll rate requirements. Just remember your 90K data/ parse buffer size = min poll rate you need.

You can use a timer or hook into the MFC message system to fire of a poll request ... personally I would use a timer. If its just writing the data to a file or something you could just loop the poll call in a thread.

The data body is available in the dataReadyState and you haven't indicated what you do with it so its blank. The parser just cycles an maintains and cleans its own buffers.

enum ReadParseStatus {
	readSigState = 0,
	readDataSizeState,
	readDataBodyState,
	readDataAvailState
};

// This is some unique signature make it what you like
// It should be something unlikely to occur in the data itself
// Make sure the server sends an identical signature and the data size before each packet body
const char signature[] = "PACKETSIG%$#";

// Parse status always starts as read signature
enum ReadParseStatus parseState = readSigState;

// Parse buffer
char parseBuffer[4096];			// The parse buffer itself ... you may adjust this up or down depending on poll rate
int parsePos = 0;				// Position of parse buffer
long bodyDataSize = 0;			// Body data size for current packet
unsigned char* bodyData = NULL; // Body data storage
long bodyDataPos = 0;			// Body data position


// Parses socket receive data
BOOL Poll_Receive_Data (CAsyncSocket sock){

	// Read whatever data is available into the parse buffer up to parse buffer full
	// sock would be m_pSocket in your code
	int iResult = sock.Receive(&parseBuffer[parsePos], sizeof(parseBuffer) - parsePos);
	
	// If return is zero or less there is either no data of something is wrong
	// So return a false so caller knows and can do something
	if (iResult <= 0) return (FALSE);
	
	// Adjust the parse position so space in buffer left can be checked
	parsePos += iResult;

	switch (parseState){
		case readSigState: {	
				// Temp buffer .. its 32 bytes here which must be bigger than signature length
				char tempbuf[32];
				// Hold signature length we will use it a bit in this phase
				int siglen = strlen(signature);
				// Preset sigFound to false
				BOOL sigFound = FALSE;
				// Loop while signature not found and enough data to run signature test
				while ((sigFound == FALSE) && (parsePos > siglen)) {
					// Transfer signature length of data from parseBuffer
					memcpy(tempbuf, &parseBuffer[0], siglen);
					// make sure its asciiz
					tempbuf[siglen] = 0;
					// Look for exact signature match
					if (strcmp(signature, &tempbuf[0])){
						// You found the signature so dump it .. memory overlap needs memmove
						memmove(&parseBuffer[0], &parseBuffer[siglen], parsePos - siglen);
						// adjust parsePos to reflect we removed the signature and dumped it
						parsePos -= siglen;
                        // signature found obviously
                        sigFound = TRUE; 
						// parser moves to next state
						parseState = readDataSizeState;
					} else {
						// So dump first character in parse buffer ... memory overlap so needs memmove
						memmove(&parseBuffer[0], &parseBuffer[1], parsePos - 1);
						// decrement parsePos
						parsePos--;
						// We will loop around now with next data
					}
				}
			}
			break;
		case readDataSizeState: {
				// Temp buffer .. which is size of a long + a character #0
				char tempbuf[sizeof(long)+1];
				// Do we have enough data to recover our data size long
				if (parsePos > sizeof(long)) {
					// Transfer data size long from parseBuffer
					memcpy(tempbuf, &parseBuffer[0], sizeof(long));
					// Make asciiz
					tempbuf[sizeof(long)] = 0;
					// Recover our body data size for this packet
					bodyDataSize = atol(&tempbuf[0]);
					// If we have old body data free the memory because we are on next packet
					// This is a self cleanup to make sure we dont bleed memory
					if (bodyData) free(bodyData);
					// Ok allocate memory to hold the body data, now we know its size
					bodyData = (unsigned char*) malloc(bodyDataSize);
					// Zero the body data position
					bodyDataPos = 0;
					// Done with the body size so dump it from parser .. memory overlap so use memmove
					memmove(&parseBuffer[0], &parseBuffer[sizeof(long)], parsePos - sizeof(long));
					// adjust parsePos to reflect we removed the body size long
					parsePos -= sizeof(long);
					// Now move to read data body parse state
					parseState = readDataBodyState;
				}
			}
			break;
		case readDataBodyState: {
				// We have body data to transfer
				if (parsePos > 0) {
					// This is amount left we need from body data
					long amountLeft = bodyDataSize - bodyDataPos;
					// Calc transfer size, allowing for fact we may have too much
					int transferSize = parsePos;
					if (transferSize > amountLeft) transferSize = amountLeft;
					// Transfer the data from parse buffer
					memcpy(&bodyData[bodyDataPos], &parseBuffer[0], transferSize);
					// Adjust bodyDataPos by transferSize
					bodyDataPos += transferSize;
					// Remove the parse data we just transferred .. memory overlap so use memmove
					memmove(&parseBuffer[0], &parseBuffer[transferSize], parsePos - transferSize);
					// adjust parsePos to reflect we removed the data transferred
					parsePos -= transferSize;
				}
				// Check if we have transferred all the body data so bodyDataPos == bodyDataSize
				if (bodyDataPos == bodyDataSize) {
					// Now move to data available parse state ... your data is ready
					parseState = readDataAvailState;
				}
			}
			break;
		case readDataAvailState: {
			        // Your data is available in bodydata
					// Do what you want with it

			        //.... Use and abuse your bodyData 
					// You havent said what happens to data but its available HERE

					// Set the parser back to the search for signature state for nexy packet
					parseState = readSigState;
			}
			break;
	}

	return (TRUE);	// Parser still working normally return true
}

In vino veritas


modified 26-May-16 5:41am.

SuggestionRe: How to send huge data via sockets in continuous intervals Pin
David Crow23-May-16 3:41
David Crow23-May-16 3:41 
AnswerRe: How to send huge data via sockets in continuous intervals Pin
Joe Woodbury25-May-16 6:42
professionalJoe Woodbury25-May-16 6:42 
QuestionInput multiple data types from a file into a linked list Pin
Farhan_Karim20-May-16 17:51
Farhan_Karim20-May-16 17:51 
AnswerRe: Input multiple data types from a file into a linked list Pin
Richard MacCutchan20-May-16 21:54
mveRichard MacCutchan20-May-16 21:54 
GeneralRe: Input multiple data types from a file into a linked list Pin
Farhan_Karim20-May-16 22:08
Farhan_Karim20-May-16 22:08 
GeneralRe: Input multiple data types from a file into a linked list Pin
Richard MacCutchan20-May-16 22:30
mveRichard MacCutchan20-May-16 22:30 
GeneralRe: Input multiple data types from a file into a linked list Pin
leon de boer22-May-16 4:00
leon de boer22-May-16 4:00 
QuestionRe: Input multiple data types from a file into a linked list Pin
David Crow21-May-16 14:45
David Crow21-May-16 14:45 
QuestionIs it good practice for a beginner C++ coder to use autocomplete? Pin
Member KL20-May-16 10:16
Member KL20-May-16 10:16 
SuggestionRe: Is it good practice for a beginner C++ coder to use autocomplete? Pin
David Crow20-May-16 10:26
David Crow20-May-16 10:26 
GeneralRe: Is it good practice for a beginner C++ coder to use autocomplete? Pin
Albert Holguin20-May-16 13:10
professionalAlbert Holguin20-May-16 13:10 
GeneralRe: Is it good practice for a beginner C++ coder to use autocomplete? Pin
Richard MacCutchan20-May-16 21:40
mveRichard MacCutchan20-May-16 21:40 
GeneralRe: Is it good practice for a beginner C++ coder to use autocomplete? Pin
Albert Holguin22-May-16 8:40
professionalAlbert Holguin22-May-16 8:40 
AnswerRe: Is it good practice for a beginner C++ coder to use autocomplete? Pin
Albert Holguin20-May-16 13:13
professionalAlbert Holguin20-May-16 13:13 
AnswerRe: Is it good practice for a beginner C++ coder to use autocomplete? Pin
Patrice T20-May-16 19:24
mvePatrice T20-May-16 19:24 
GeneralRe: Is it good practice for a beginner C++ coder to use autocomplete? Pin
Albert Holguin22-May-16 8:43
professionalAlbert Holguin22-May-16 8:43 
GeneralRe: Is it good practice for a beginner C++ coder to use autocomplete? Pin
Patrice T22-May-16 8:50
mvePatrice T22-May-16 8:50 

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.