/usr/include/net6/packet.hpp is in libnet6-1.3-dev 1:1.3.14-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | /* net6 - Library providing IPv4/IPv6 network access
* Copyright (C) 2005, 2006 Armin Burgmeier / 0x539 dev group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef _NET6_PACKET_HPP_
#define _NET6_PACKET_HPP_
#include <string>
#include <vector>
#include <sstream>
#include <stdexcept>
#include "serialise.hpp"
#include "queue.hpp"
namespace net6
{
/** Exception class that is thrown by packet::get_param if the
* requested parameter index is out of range.
*/
class bad_count: public std::runtime_error {
public:
bad_count():
std::runtime_error("Bad count") { }
};
/** Exception class that is thrown when a parameter has an illegal format for
* the requested type.
*/
class bad_format: public std::runtime_error {
public:
bad_format(const std::string& reason):
std::runtime_error(reason) { }
};
/** Exception that might be thrown by a packet reception handler if
* the packet has invalid parameters.
*/
class bad_value: public std::runtime_error {
public:
bad_value(const std::string& error_message):
std::runtime_error(error_message) { }
};
/** Packet parameter.
*/
class parameter
{
public:
/** Uses the given string as serialised parameter data.
*/
parameter(const std::string& value);
/** Serialises the object of <em>data_type</em> and serialises it
* through <em>ctx</em> as parameter value.
*/
template<typename data_type>
parameter(const data_type& type,
const serialise::context_base_to<data_type>& ctx =
serialise::hex_context_to<data_type>());
/** Returns the serialised data.
*/
const std::string& serialised() const;
/** Deserialises the parameter value with the given context.
*/
template<typename data_type>
data_type as(const serialise::context_base_from<data_type>& ctx =
serialise::hex_context_from<data_type>()) const;
protected:
serialise::data m_value;
};
template<typename data_type>
parameter::parameter(const data_type& type,
const serialise::context_base_to<data_type>& ctx):
m_value(type, ctx)
{
}
template<typename data_type>
data_type parameter::
as(const serialise::context_base_from<data_type>& ctx) const
{
try
{
// Deserialise value
return m_value.as<data_type>(ctx);
}
catch(serialise::conversion_error& e)
{
// Convert error to bad_format in net6 network layer
throw bad_format(e.what() );
}
}
/** High-level object that represents a packet that may be sent over the
* network. A packet exists of a command and a variable amount of parameters
* with variable type.
*/
class packet
{
public:
/** Thrown by packet::packet(connection::queue&) if there is no
* complete packet in the queue.
*/
class end_of_queue: public std::runtime_error
{
public:
end_of_queue():
std::runtime_error("No complete packet in queue") { }
};
/** Creates a new packet.
* @param command Command for the packet.
* @param size Number of parameters the packet will preallocate memory
* for.
*/
packet(const std::string& command,
unsigned int size = 0);
/** Reads a packet from a connection queue. end_of_queue is thrown if
* there is only a partial packet on the queue. This constructor is used
* by net6::connection to split incoming data into separate packets.
* You will most certainly not need it.
*/
packet(queue& queue);
/** Adds a new parameter to the packet.
*/
template<typename data_type>
void add_param(const data_type& value,
const serialise::context_base_to<data_type>& ctx =
serialise::hex_context_to<data_type>() );
/** Shortcut for add_param(T)
*/
template<typename data_type>
packet& operator<<(const data_type& value);
/** Returns the command of this packet.
*/
const std::string& get_command() const;
/** Returns the <em>index</em>d parameter of this packet.
*/
const parameter& get_param(unsigned int index) const;
/** Returns the amount of parameters of this packet
*/
unsigned int get_param_count() const;
/** Pushes this packet onto the given connection queue. This function
* is used by net6::connection to enqueue a packet for sending it to
* a remote host. You will most certainly not need it.
*/
void enqueue(queue& queue) const;
protected:
static std::string escape(const std::string& string);
static std::string unescape(const std::string& string);
std::string command;
std::vector<parameter> params;
};
template<typename data_type>
void packet::add_param(const data_type& value,
const serialise::context_base_to<data_type>& ctx)
{
params.push_back(parameter(value, ctx) );
}
template<typename data_type>
packet& packet::operator<<(const data_type& value)
{
// Add parameter with default context
add_param(value);
// Return self to allow chaining
return *this;
}
} // namespace net6
#endif // _NET6_PACKET_HPP_
|