0

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)
{

}
3
  • I dunno what you are trying to achieve, but cant you use sockets and byte buffers? Commented Nov 8, 2013 at 21:59
  • 1
    What you're describing is called "marshalling" arguments. All RPC libraries provide methods to do this automatically. Use one of them instead of reinventing the wheel. Commented Nov 8, 2013 at 22:10
  • Excellent info Barmar! I'll google around to find a good one, any suggestions? Commented Nov 8, 2013 at 22:12

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is very close to what I'm trying to accomplish! The RPC libraries that Barmar mentioned with automatic "Marshaling" is closer, though. Thanks for your input!
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.