N
N
Nikita Savinykh2020-05-16 05:28:00
C++ / C#
Nikita Savinykh, 2020-05-16 05:28:00

What could be the problem when passing a container of "complex" objects over UDP?

There is, suppose, such a set of synthetic structures:

#include <cstdint>
#include <deque>
#include <algorithm>

struct Point;
struct Additional;

struct Track
{
    uint64_t id;
    std::deque<Point> points;

    void appendNewPoint(int x, int y, Additional additional);
};


struct Additional
{
    int dx;
    int dy;
};

struct Point
{
    Additional additional_data;
    int x;
    int y;
    uint64_t id;
    Track * parent;
    Point * next();
    Point * prev();
};

Point * Point::next()
{
    auto& container = parent->points;
    auto result = std::find_if(container.begin(), container.end() - 1, [this](Point& n) {
        return this == &n;
    });
    return (result == container.end() - 1 ) ? this : &(*(result + 1));
}

Point * Point::prev()
{
    auto& container = parent->points;
    auto result = std::find_if(container.rbegin(), container.rend() - 1, [this](Point& n) {
        return this == &n;
    });
    return (result == container.rend() - 1 ) ? this :  &(*(result + 1));
}

void Track::appendNewPoint(int x, int y, Additional additional)
{
    Point point = {additional, x, y, this->points.size(), this};
    this->points.push_back(point);
}


Let's say I have some container that stores data of the Track type: Question: How can I send this over UDP using unix sockets? As far as I understand, all this can be represented as a data set, imagine how to allocate half a megabyte for data, and send this whole thing to the server, and on the server to cast the received data back to or is it impossible to do this? If not, how can it be? PS: The struct methods are presented for informational purposes only to show that the structs I'm about to pass also have some methods whose mechanics are not much (but still) different from those shown here.
std::unordered_map<uint64_t, Track> Tracks;

void *std::unordered_map <uint64_t, Track>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2020-05-16
@Ukio_G

https://www.boost.org/doc/libs/1_72_0/libs/seriali...
in Russian -> https://github.com/Danchetto/Boost.Serialization/b...

J
jcmvbkbc, 2020-05-16
@jcmvbkbc

Question:
How can I transmit this over UDP using unix sockets?

Serialize into a contiguous memory area, make sure that it will fit into a UDP packet (and it has a length limit, a little less than 64Kb), transfer, receive, de-serialize into its structure.
After all, all this can be represented as a set of data, represented as void *, allocate half a megabyte for data, and send this whole thing to the server, and on the server to cast the received data back to std::unordered_map <uint64_t, Track>or is it impossible to do this?

You can't do that if you don't know for sure the internal structure of what you're sending. Because it's your std::unordered_map -- it's probably not one solid chunk of memory, and it contains pointers. And when you transfer it to another machine, the pointers will point to no one knows where.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question