5 // Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef PORTHOPPER_PROTOCOL_HPP
12 #define PORTHOPPER_PROTOCOL_HPP
14 #include <boost/array.hpp>
15 #include <boost/asio.hpp>
21 // This request is sent by the client to the server over a TCP connection.
22 // The client uses it to perform three functions:
23 // - To request that data start being sent to a given port.
24 // - To request that data is no longer sent to a given port.
25 // - To change the target port to another.
29 // Construct an empty request. Used when receiving.
34 // Create a request to start sending data to a given port.
35 static const control_request start(unsigned short port)
37 return control_request(0, port);
40 // Create a request to stop sending data to a given port.
41 static const control_request stop(unsigned short port)
43 return control_request(port, 0);
46 // Create a request to change the port that data is sent to.
47 static const control_request change(
48 unsigned short old_port, unsigned short new_port)
50 return control_request(old_port, new_port);
53 // Get the old port. Returns 0 for start requests.
54 unsigned short old_port() const
56 std::istrstream is(data_, encoded_port_size);
57 unsigned short port = 0;
58 is >> std::setw(encoded_port_size) >> std::hex >> port;
62 // Get the new port. Returns 0 for stop requests.
63 unsigned short new_port() const
65 std::istrstream is(data_ + encoded_port_size, encoded_port_size);
66 unsigned short port = 0;
67 is >> std::setw(encoded_port_size) >> std::hex >> port;
71 // Obtain buffers for reading from or writing to a socket.
72 boost::array<boost::asio::mutable_buffer, 1> to_buffers()
74 boost::array<boost::asio::mutable_buffer, 1> buffers
75 = { { boost::asio::buffer(data_) } };
80 // Construct with specified old and new ports.
81 control_request(unsigned short old_port_number,
82 unsigned short new_port_number)
84 std::ostrstream os(data_, control_request_size);
85 os << std::setw(encoded_port_size) << std::hex << old_port_number;
86 os << std::setw(encoded_port_size) << std::hex << new_port_number;
89 // The length in bytes of a control_request and its components.
92 encoded_port_size = 4, // 16-bit port in hex.
93 control_request_size = encoded_port_size * 2
96 // The encoded request data.
97 char data_[control_request_size];
100 // This frame is sent from the server to subscribed clients over UDP.
104 // The maximum allowable length of the payload.
105 enum { payload_size = 32 };
107 // Construct an empty frame. Used when receiving.
112 // Construct a frame with specified frame number and payload.
113 frame(unsigned long frame_number, const std::string& payload_data)
115 std::ostrstream os(data_, frame_size);
116 os << std::setw(encoded_number_size) << std::hex << frame_number;
117 os << std::setw(payload_size)
118 << std::setfill(' ') << payload_data.substr(0, payload_size);
121 // Get the frame number.
122 unsigned long number() const
124 std::istrstream is(data_, encoded_number_size);
125 unsigned long frame_number = 0;
126 is >> std::setw(encoded_number_size) >> std::hex >> frame_number;
130 // Get the payload data.
131 const std::string payload() const
133 return std::string(data_ + encoded_number_size, payload_size);
136 // Obtain buffers for reading from or writing to a socket.
137 boost::array<boost::asio::mutable_buffer, 1> to_buffers()
139 boost::array<boost::asio::mutable_buffer, 1> buffers
140 = { { boost::asio::buffer(data_) } };
145 // The length in bytes of a frame and its components.
148 encoded_number_size = 8, // Frame number in hex.
149 frame_size = encoded_number_size + payload_size
152 // The encoded frame data.
153 char data_[frame_size];
156 #endif // PORTHOPPER_PROTOCOL_HPP