This request is sent by the client to the server over a TCP connection. The client uses it to perform three functions: - To request that data start being sent to a given port. - To request that data is no longer sent to a given port. - To change the target port to another.
| 24 | // - To request that data is no longer sent to a given port. |
| 25 | // - To change the target port to another. |
| 26 | class control_request |
| 27 | { |
| 28 | public: |
| 29 | // Construct an empty request. Used when receiving. |
| 30 | control_request() |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | // Create a request to start sending data to a given port. |
| 35 | static const control_request start(unsigned short port) |
| 36 | { |
| 37 | return control_request(0, port); |
| 38 | } |
| 39 | |
| 40 | // Create a request to stop sending data to a given port. |
| 41 | static const control_request stop(unsigned short port) |
| 42 | { |
| 43 | return control_request(port, 0); |
| 44 | } |
| 45 | |
| 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) |
| 49 | { |
| 50 | return control_request(old_port, new_port); |
| 51 | } |
| 52 | |
| 53 | // Get the old port. Returns 0 for start requests. |
| 54 | unsigned short old_port() const |
| 55 | { |
| 56 | std::istrstream is(data_, encoded_port_size); |
| 57 | unsigned short port = 0; |
| 58 | is >> std::setw(encoded_port_size) >> std::hex >> port; |
| 59 | return port; |
| 60 | } |
| 61 | |
| 62 | // Get the new port. Returns 0 for stop requests. |
| 63 | unsigned short new_port() const |
| 64 | { |
| 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; |
| 68 | return port; |
| 69 | } |
| 70 | |
| 71 | // Obtain buffers for reading from or writing to a socket. |
| 72 | std::array<boost::asio::mutable_buffer, 1> to_buffers() |
| 73 | { |
| 74 | std::array<boost::asio::mutable_buffer, 1> buffers |
| 75 | = { { boost::asio::buffer(data_) } }; |
| 76 | return buffers; |
| 77 | } |
| 78 | |
| 79 | private: |
| 80 | // Construct with specified old and new ports. |
| 81 | control_request(unsigned short old_port_number, |
| 82 | unsigned short new_port_number) |
| 83 | { |