/usr/include/dlib/bridge/bridge_abstract.h is in libdlib-dev 18.18-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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | // Copyright (C) 2011 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_BRIDGe_ABSTRACT_
#ifdef DLIB_BRIDGe_ABSTRACT_
#include <string>
#include "../pipe/pipe_kernel_abstract.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
struct connect_to_ip_and_port
{
connect_to_ip_and_port (
const std::string& ip,
unsigned short port
);
/*!
requires
- is_ip_address(ip) == true
- port != 0
ensures
- this object will represent a request to make a TCP connection
to the given IP address and port number.
!*/
};
connect_to_ip_and_port connect_to (
const network_address& addr
);
/*!
requires
- addr.port != 0
ensures
- converts the given network_address object into a connect_to_ip_and_port
object.
!*/
struct listen_on_port
{
listen_on_port(
unsigned short port
);
/*!
requires
- port != 0
ensures
- this object will represent a request to listen on the given
port number for incoming TCP connections.
!*/
};
template <
typename pipe_type
>
bridge_transmit_decoration<pipe_type> transmit (
pipe_type& p
);
/*!
requires
- pipe_type is some kind of dlib::pipe object
- the objects in the pipe must be serializable
ensures
- Adds a type decoration to the given pipe, marking it as a transmit pipe, and
then returns it.
!*/
template <
typename pipe_type
>
bridge_receive_decoration<pipe_type> receive (
pipe_type& p
);
/*!
requires
- pipe_type is some kind of dlib::pipe object
- the objects in the pipe must be serializable
ensures
- Adds a type decoration to the given pipe, marking it as a receive pipe, and
then returns it.
!*/
// ----------------------------------------------------------------------------------------
struct bridge_status
{
/*!
WHAT THIS OBJECT REPRESENTS
This simple struct represents the state of a bridge object. A
bridge is either connected or not. If it is connected then it
is connected to a foreign host with an IP address and port number
as indicated by this object.
!*/
bridge_status(
);
/*!
ensures
- #is_connected == false
- #foreign_port == 0
- #foreign_ip == ""
!*/
bool is_connected;
unsigned short foreign_port;
std::string foreign_ip;
};
// ----------------------------------------------------------------------------------------
class bridge : noncopyable
{
/*!
WHAT THIS OBJECT REPRESENTS
This object is a tool for bridging a dlib::pipe object between
two network connected applications.
Note also that this object contains a dlib::logger object
which will log various events taking place inside a bridge.
If you want to see these log messages then enable the logger
named "dlib.bridge".
BRIDGE PROTOCOL DETAILS
The bridge object creates a single TCP connection between
two applications. Whenever it sends an object from a pipe
over a TCP connection it sends a byte with the value 1 followed
immediately by the serialized copy of the object from the pipe.
The serialization is performed by calling the global serialize()
function.
Additionally, a bridge object will periodically send bytes with
a value of 0 to ensure the TCP connection remains alive. These
are just read and ignored.
!*/
public:
bridge (
);
/*!
ensures
- this object is properly initialized
- #get_bridge_status().is_connected == false
!*/
template <typename T, typename U, typename V>
bridge (
T network_parameters,
U pipe1,
V pipe2
);
/*!
requires
- T is of type connect_to_ip_and_port or listen_on_port
- U and V are of type bridge_transmit_decoration or bridge_receive_decoration,
however, U and V must be of different types (i.e. one is a receive type and
another a transmit type).
ensures
- this object is properly initialized
- performs: reconfigure(network_parameters, pipe1, pipe2)
(i.e. using this constructor is identical to using the default constructor
and then calling reconfigure())
!*/
template <typename T, typename U>
bridge (
T network_parameters,
U pipe
);
/*!
requires
- T is of type connect_to_ip_and_port or listen_on_port
- U is of type bridge_transmit_decoration or bridge_receive_decoration.
ensures
- this object is properly initialized
- performs: reconfigure(network_parameters, pipe)
(i.e. using this constructor is identical to using the default constructor
and then calling reconfigure())
!*/
~bridge (
);
/*!
ensures
- blocks until all resources associated with this object have been destroyed.
!*/
void clear (
);
/*!
ensures
- returns this object to its default constructed state. That is, it will
be inactive, neither maintaining a connection nor attempting to acquire one.
- Any active connections or listening sockets will be closed.
!*/
bridge_status get_bridge_status (
) const;
/*!
ensures
- returns the current status of this bridge object. In particular, returns
an object BS such that:
- BS.is_connected == true if and only if the bridge has an active TCP
connection to another computer.
- if (BS.is_connected) then
- BS.foreign_ip == the IP address of the remote host we are connected to.
- BS.foreign_port == the port number on the remote host we are connected to.
- else if (the bridge has previously been connected to a remote host but hasn't been
reconfigured or cleared since) then
- BS.foreign_ip == the IP address of the remote host we were connected to.
- BS.foreign_port == the port number on the remote host we were connected to.
- else
- BS.foreign_ip == ""
- BS.foreign_port == 0
!*/
template < typename T, typename R >
void reconfigure (
listen_on_port network_parameters,
bridge_transmit_decoration<T> transmit_pipe,
bridge_receive_decoration<R> receive_pipe
);
/*!
ensures
- This object will begin listening on the port specified by network_parameters
for incoming TCP connections. Any previous bridge state is cleared out.
- Onces a connection is established we will:
- Stop accepting new connections.
- Begin dequeuing objects from the transmit pipe and serializing them over
the TCP connection.
- Begin deserializing objects from the TCP connection and enqueueing them
onto the receive pipe.
- if (the current TCP connection is lost) then
- This object goes back to listening for a new connection.
- if (the receive pipe can contain bridge_status objects) then
- Whenever the bridge's status changes the updated bridge_status will be
enqueued onto the receive pipe unless the change was a TCP disconnect
resulting from a user calling reconfigure(), clear(), or destructing this
bridge. The status contents are defined by get_bridge_status().
throws
- socket_error
This exception is thrown if we are unable to open the listening socket.
!*/
template < typename T, typename R >
void reconfigure (
listen_on_port network_parameters,
bridge_receive_decoration<R> receive_pipe,
bridge_transmit_decoration<T> transmit_pipe
);
/*!
ensures
- performs reconfigure(network_parameters, transmit_pipe, receive_pipe)
!*/
template < typename T >
void reconfigure (
listen_on_port network_parameters,
bridge_transmit_decoration<T> transmit_pipe
);
/*!
ensures
- This function is identical to the above two reconfigure() functions
except that there is no receive pipe.
!*/
template < typename R >
void reconfigure (
listen_on_port network_parameters,
bridge_receive_decoration<R> receive_pipe
);
/*!
ensures
- This function is identical to the above three reconfigure() functions
except that there is no transmit pipe.
!*/
template <typename T, typename R>
void reconfigure (
connect_to_ip_and_port network_parameters,
bridge_transmit_decoration<T> transmit_pipe,
bridge_receive_decoration<R> receive_pipe
);
/*!
ensures
- This object will begin making TCP connection attempts to the IP address and port
specified by network_parameters. Any previous bridge state is cleared out.
- Onces a connection is established we will:
- Stop attempting new connections.
- Begin dequeuing objects from the transmit pipe and serializing them over
the TCP connection.
- Begin deserializing objects from the TCP connection and enqueueing them
onto the receive pipe.
- if (the current TCP connection is lost) then
- This object goes back to attempting to make a TCP connection with the
IP address and port specified by network_parameters.
- if (the receive pipe can contain bridge_status objects) then
- Whenever the bridge's status changes the updated bridge_status will be
enqueued onto the receive pipe unless the change was a TCP disconnect
resulting from a user calling reconfigure(), clear(), or destructing this
bridge. The status contents are defined by get_bridge_status().
!*/
template <typename T, typename R>
void reconfigure (
connect_to_ip_and_port network_parameters,
bridge_receive_decoration<R> receive_pipe,
bridge_transmit_decoration<T> transmit_pipe
);
/*!
ensures
- performs reconfigure(network_parameters, transmit_pipe, receive_pipe)
!*/
template <typename T>
void reconfigure (
connect_to_ip_and_port network_parameters,
bridge_transmit_decoration<T> transmit_pipe
);
/*!
ensures
- This function is identical to the above two reconfigure() functions
except that there is no receive pipe.
!*/
template <typename R>
void reconfigure (
connect_to_ip_and_port network_parameters,
bridge_receive_decoration<R> receive_pipe
);
/*!
ensures
- This function is identical to the above three reconfigure() functions
except that there is no transmit pipe.
!*/
};
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_BRIDGe_ABSTRACT_
|