Question

Consider the following situation: (Pseudo-ish code)

//All our luscious data
char theChar = 123;
int theInt = 4324;
char[] theCharArray = "sometext";

//Make an array to hold all of that data.
byte[] allTheVars = new *byte[sizeOfArray];

//Copy all vars into "allTheVars"
copyToEndOfArray(theChar, allTheVars);
copyToEndOfArray(theInt, allTheVars);
copyToEndOfArray(theCharArray, allTheVars);

So the idea is that you end up with a bunch of variables strung together into the same byte array. This array is then passed over the internet. Now say all these variables were sent over to call a remote function, like below.

//This is the function that will take in the data we sent over the network.
void remotelyCalledInternetFunction(char aChar, int anInt, char[] aCharArray)
{

}

Instead of manually splitting up each variable into it's specified type by means of tediously copying from the byte array, can you have the method "auto-split" the varaibles by doing something like this?

//Pass the byte array. The method knows what types it needs, maybe it will auto-split the data correctly?
remotelyCalledInternetFunction(allTheVars);

If not, is there anything similar I could do?


EDIT: Any way to do something like this?

remotelyCalledInternetFunction(allTheVars);
//Takes first 2 bytes of the array, the next 4 bytes, and the rest for the char[]?
void remotelyCalledInternetFunction(char aChar, int anInt, char[] aCharArray)
{

}
Was it helpful?

Solution 2

Ok, as Barmar said in the comments, what I'm trying to accomplish has already been done through RPC (Remote Procedure Calls) and marshaling. He recommended finding a good library instead of reinventing the wheel.

A library I found that seems pretty good: https://github.com/cinemast/libjson-rpc-cpp

(jozef also had a pretty good solution of using structs, thanks for that :D)

EDIT: I need a library for low-latency online game stuff, so I'll probably end up writing my own anyways.

OTHER TIPS

I would suggest to use a structure to store and transmit the data as below. That would take care of the splitting of the data at the receiving function automatically.

struct myStruct {
char theChar;
int theInt;
char[] theCharArray;
}

You can then use memcopy with arguments for this structure, refer -> Send struct over socket in C.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top