Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/examples/cpp11/porthopper/protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#include <array>
#include <cstring>
#include <iomanip>
#include <sstream>
#include <string>
#include <strstream>

// This request is sent by the client to the server over a TCP connection.
// The client uses it to perform three functions:
Expand Down Expand Up @@ -53,7 +53,7 @@ class control_request
// Get the old port. Returns 0 for start requests.
unsigned short old_port() const
{
std::istrstream is(data_, encoded_port_size);
std::istringstream is(std::string(data_, encoded_port_size));
unsigned short port = 0;
is >> std::setw(encoded_port_size) >> std::hex >> port;
return port;
Expand All @@ -62,7 +62,8 @@ class control_request
// Get the new port. Returns 0 for stop requests.
unsigned short new_port() const
{
std::istrstream is(data_ + encoded_port_size, encoded_port_size);
std::istringstream is(
std::string(data_ + encoded_port_size, encoded_port_size));
unsigned short port = 0;
is >> std::setw(encoded_port_size) >> std::hex >> port;
return port;
Expand All @@ -81,9 +82,11 @@ class control_request
control_request(unsigned short old_port_number,
unsigned short new_port_number)
{
std::ostrstream os(data_, control_request_size);
std::ostringstream os;
os << std::setw(encoded_port_size) << std::hex << old_port_number;
os << std::setw(encoded_port_size) << std::hex << new_port_number;
if (os && os.str().size() >= control_request_size)
os.str().copy(data_, control_request_size);
}

// The length in bytes of a control_request and its components.
Expand Down Expand Up @@ -112,16 +115,18 @@ class frame
// Construct a frame with specified frame number and payload.
frame(unsigned long frame_number, const std::string& payload_data)
{
std::ostrstream os(data_, frame_size);
std::ostringstream os;
os << std::setw(encoded_number_size) << std::hex << frame_number;
os << std::setw(payload_size)
<< std::setfill(' ') << payload_data.substr(0, payload_size);
if (os && os.str().size() >= frame_size)
os.str().copy(data_, frame_size);
}

// Get the frame number.
unsigned long number() const
{
std::istrstream is(data_, encoded_number_size);
std::istringstream is(std::string(data_, encoded_number_size));
unsigned long frame_number = 0;
is >> std::setw(encoded_number_size) >> std::hex >> frame_number;
return frame_number;
Expand Down