Sending a StringStream of Binary Data from Cereal with ENet

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
kklouzal
Posts: 343
Joined: Sun Mar 28, 2010 8:14 pm
Location: USA - Arizona

Sending a StringStream of Binary Data from Cereal with ENet

Post by kklouzal »

I have been working on wrapping ENet into a set of easy to use functions for a few weeks now and seem to have a bit of an issue.

I have a std::stringstream and am attempting to send the contents to a remote machine using ENet then reconstruct the std::stringstream on the remote machine.

The reason I need to use a std::stringstream is due to the fact that I'm serializing my data with the Cereal Serialization Library which requires a stream.

It has been identified that I need to be using std::istringstream and std::ostringstream. Previously I was only using std::stringstream which was causing an exception.

However now an exception is being thrown within Cereal at portable_binary.hpp line 156:

Code: Select all

    throw Exception("Failed to read " + std::to_string(size) + " bytes from input stream! Read " + std::to_string(readSize));
Here's what I'm doing:

Code: Select all

    void Send(ENetHost* Host)
    {
        std::ostringstream SData;
        {
            cereal::PortableBinaryOutputArchive Archive(SData);
            Archive(PacketData);
        }
 
        std::string Out = SData.str();
 
        ENetPacket* Packet = enet_packet_create(Out.c_str(), Out.size(), ENET_PACKET_FLAG_RELIABLE);
        enet_host_broadcast(Host, 0, Packet);
    }
A Cereal Portable Binary Data Archive is constructed to hold a single vector.
The std::ostringstream is sent off to the host using ENet.

This part seems to work okay, I can print the information out before and after and it appears to be the same, albeit some weird symbols, but they print out the same on both ends.

Now a std::istringstream is created on the host with the data we received.

Code: Select all

    NetPacket(enet_uint8 const* Data)
    {
        std::istringstream SData(reinterpret_cast<char const*>(Data));
        {
            cereal::PortableBinaryInputArchive Archive(SData);
            Archive(PacketData);
        }
    }
At this point I receive the exception at line:

Code: Select all

    Archive(PacketData)
I have a feeling the data is being changed somehow when it's sent through ENet and/or I'm not pulling the data out of the std::ostringstream correctly and/or not putting the data back into the std::istringstream correctly.

Thank you very much for your time I greatly appreciate it.
Dream Big Or Go Home.
Help Me Help You.
kklouzal
Posts: 343
Joined: Sun Mar 28, 2010 8:14 pm
Location: USA - Arizona

Re: Sending a StringStream of Binary Data from Cereal with E

Post by kklouzal »

The original exception was being thrown because I was using std::stringstream when I needed to use std::istringstrem and std::ostringstream appropriately, now the OP has been updated to reflect the current state of the issue.

Anyone who knows ENet or Cereal and can help here would be really appreciated, thank you.
Dream Big Or Go Home.
Help Me Help You.
Post Reply