Search code examples
c++boost-asiostreambuf

Using streambuf when sending/getting struct


I'm working on boost::asio::streambuf and found out that I can send/get a struct using it, but when I send a struct I just can't get it as I have sent it. The documentation says that one should use commit() and consume(), but where should I use them here?

struct person
{
    int m_id;
    std::string m_message;
};

std::istream& operator>>(std::istream& in, struct person& p)
{
    return in >> p.m_id >> p.m_message;
}

std::ostream& operator<<(std::ostream& out, struct person& p)
{
    return out << p.m_id << " " << p.m_message;
}

int main()
{
    boost::asio::streambuf buf;
    std::ostream out(&buf);

    person p;

    p.m_id = 1;
    p.m_message = "Hello World!";

    out << p;

    std::istream in(&buf);

    person p1;

    in >> p1;

    cout << "ID: " << p1.m_id << endl;
    cout << "Message: " << p1.m_message << endl;

return 0;
}

The problem is with strings so when I type only "hello" (without world), it works fine, but if I add "world!" as shown above it just doesn't see the added "world!", why?


Solution

  • There are a number of issues.

    1. Firstly, make the arguments const& when possible:

      std::ostream &operator<<(std::ostream &out, person const &p) {
      
    2. Secondly, make sure the streams flush to the buffer. I think it's good practice to limit the lifetime of the ostream or istream instances

    3. Thirdly, choose a format that will be robust. Your sample already had bigger problems, when you had m_id = 1 and m_message = "123" (can you see it?).

      In text formats you need either fixed-length fields or a delimiting protocol. Let's fix it:

      std::ostream &operator<<(std::ostream &out, person const &p) {
          return out << p.m_id << ";" << p.m_message.length() << ";" << p.m_message;
      }
      

      Now when reading it back you will see how much more precise you need to be:

      std::istream &operator>>(std::istream &in, person &p) {
          char separator;
          size_t length;
      
          bool ok = in >> p.m_id 
                 && in >> separator && separator == ';' 
                 && in >> length
                 && in >> separator && separator == ';' 
                 ;
      
          if (ok) {
              p.m_message.resize(length);
              in.read(&p.m_message[0], length);
      
              p.m_message.resize(in.gcount());
          }
      
          // ensure the expected number of bytes were read
          ok = ok && (p.m_message.length() == length);
      
          if (!ok)
              in.setstate(std::ios::failbit);
      
          return in;
      }
      

      Yikes. Really? Yes really. At a minimum!

    4. Do error handling

    Full Demo

    Live On Coliru

    #include <boost/asio.hpp>
    #include <iostream>
    
    struct person {
        int m_id;
        std::string m_message;
    };
    
    std::ostream &operator<<(std::ostream &out, person const &p) {
        return out << p.m_id << ";" << p.m_message.length() << ";" << p.m_message;
    }
    
    std::istream &operator>>(std::istream &in, person &p) {
        char separator;
        size_t length;
    
        bool ok = in >> p.m_id 
               && in >> separator && separator == ';' 
               && in >> length
               && in >> separator && separator == ';' 
               ;
    
        if (ok) {
            p.m_message.resize(length);
            in.read(&p.m_message[0], length);
    
            p.m_message.resize(in.gcount());
        }
    
        // ensure the expected number of bytes were read
        ok = ok && (p.m_message.length() == length);
    
        if (!ok)
            in.setstate(std::ios::failbit);
    
        return in;
    }
    
    int main() {
        boost::asio::streambuf buf;
    
        std::ostream(&buf) << person{ 1, "Hello World!" }; 
    
        person received;
        if (std::istream(&buf) >> received) {
            std::cout << "ID:      " << received.m_id << std::endl;
            std::cout << "Message: " << received.m_message << std::endl;
        } else {
            std::cout << "Couldn't receive person\n";
        }
    }
    

    Prints

    ID:      1
    Message: Hello World!
    

    BONUS

    C++14 added std::quoted:

    #include <iomanip>
    std::ostream &operator<<(std::ostream &out, person const &p) { return out << p.m_id << std::quoted(p.m_message); }
    std::istream &operator>>(std::istream &in,  person       &p) { return in  >> p.m_id >> std::quoted(p.m_message); }
    

    Which, in this case, also does the job: Live On Coliru