Data shipment through socket (package) with++



  • It's necessary to send a type of data:float/int/char through the juice, how to arrange a package on the sender's side to send it all in one package, and to unpack it on the host side.

    Did you find a scream for the game as well as packing?



  • A primitive method for simple cases - describe the data structure:

    struct Data {
        int a;
        float b;
        char c[32];
    } data;
    

    Put it in the juket on the front side.send(socket, &data, sizeof(data), 0) and at the host to read the same structurerecv(socket, &data, sizeof(data), 0)) Very important. To ensure that the statues on both sides (transfer and receiver) are identical in memory (single sizes, the same order of the baytes in the system, the same levelling of the structure fields, the same representation of the chips with a floating point). Otherwise, it's not what they sent. In practice, if the host side is also written in another language, we will get an extra smell and a spark to make mistakes.


    The next option is to manually fill the data buffer:

    int foo = 42;
    long bar = 0;
    std::string str;
    str.append((char*)&foo, sizeof(int));
    str.append((char*)&bar, sizeof(long));
    

    There is no longer a problem with the levelling of the structure fields as in the first option, since the data we're clicking on ourselves, without intervals. But the rest of the problems are still with us (there's still the order of the bikes, the size of the types, the presentation of the floating point chips should be identical on the transmitter and receiver).


    Hand, side-by-side flow.

    uint32_t foo = 42;
    std::vector<uint8_t> buffer;
    buffer.push_back(static_cast<uint8_t>(foo >> 0));
    buffer.push_back(static_cast<uint8_t>(foo >> 8));
    buffer.push_back(static_cast<uint8_t>(foo >> 16));
    buffer.push_back(static_cast<uint8_t>(foo >> 24));
    

    We're just taking every piece of data here and hand-delivering to the weekend flow, independently of the system. It's gonna have to be handwritten. The most universal method is that all aspects of the generated flow are controlled by themselves. For convenience, a classifier/decider for the required types (including user) may be added.


    Over time (and may be immediately) the difficulties associated with changing the data transferred are added (e.g. additional data were needed, or some old ones have become inaccurate). Especially if the receiver has to accept data in the old and new format. I'll have to add some version identifiers. Cases where optical data (which may not be available) or dynamic data (masses) should be further processed.


    In order not to deal with all these tasks on their own, it is possible to take a ready solution, for example. https://github.com/google/protobuf from google. Supports different languages, has a version system, supports integrated data. Or a slightly more simple solution (but faster) from google, too. https://google.github.io/flatbuffers/ ♪


    If the volume of information transmitted is not critical, it may be convenient to generate data as json (e.g. by https://github.com/nlohmann/json ) If on the host of JavaScript, he'll be very grateful to you. Also, as a programmer from a typical language, I recommend the use of http://json-schema.org to test json.

    As an alternative json can be taken http://msgpack.org/index.html which is "like json," but more compact. If you need more compact, you can squeeze the zlib line.


    All options should also take into account what to transfer indicators It's pointless, because on the host side they'll indicate where they don't know. Also understand the subtleness of network data transmission. For example, data sent to UDP may not reach the recipient, data sent to TCP may be fragmented or glued with accessories, etc. You might want to think about ready web libraries, for example. https://github.com/OculusVR/RakNet which includes almost everything to build a multiplier game.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2