mirror of https://github.com/davisking/dlib.git
Added the network_address object.
This commit is contained in:
parent
3ebaff2e73
commit
d955b4923e
|
@ -12,10 +12,76 @@
|
|||
#include "../algs.h"
|
||||
#include "../timeout.h"
|
||||
#include "../misc_api.h"
|
||||
#include "../serialize.h"
|
||||
|
||||
namespace dlib
|
||||
{
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
void serialize(
|
||||
const network_address& item,
|
||||
std::ostream& out
|
||||
)
|
||||
{
|
||||
serialize(item.host_address, out);
|
||||
serialize(item.port, out);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
void deserialize(
|
||||
network_address& item,
|
||||
std::istream& in
|
||||
)
|
||||
{
|
||||
deserialize(item.host_address, in);
|
||||
deserialize(item.port, in);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
std::ostream& operator<< (
|
||||
std::ostream& out,
|
||||
const network_address& item
|
||||
)
|
||||
{
|
||||
out << item.host_address << ":" << item.port;
|
||||
return out;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
std::istream& operator>> (
|
||||
std::istream& in,
|
||||
network_address& item
|
||||
)
|
||||
{
|
||||
std::string temp;
|
||||
in >> temp;
|
||||
|
||||
std::string::size_type pos = temp.find_last_of(":");
|
||||
if (pos == std::string::npos)
|
||||
{
|
||||
in.setstate(std::ios::badbit);
|
||||
return in;
|
||||
}
|
||||
|
||||
item.host_address = temp.substr(0, pos);
|
||||
try
|
||||
{
|
||||
item.port = sa = temp.substr(pos+1);
|
||||
} catch (std::exception& )
|
||||
{
|
||||
in.setstate(std::ios::badbit);
|
||||
return in;
|
||||
}
|
||||
|
||||
|
||||
return in;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
connection* connect (
|
||||
|
@ -45,6 +111,15 @@ namespace dlib
|
|||
return con;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
connection* connect (
|
||||
const network_address& addr
|
||||
)
|
||||
{
|
||||
return connect(addr.host_address, addr.port);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
namespace connect_timeout_helpers
|
||||
|
@ -167,6 +242,16 @@ namespace dlib
|
|||
return data->con;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
connection* connect (
|
||||
const network_address& addr,
|
||||
unsigned long timeout
|
||||
)
|
||||
{
|
||||
return connect(addr.host_address, addr.port, timeout);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
bool is_ip_address (
|
||||
|
|
|
@ -7,10 +7,46 @@
|
|||
#include "../sockets.h"
|
||||
#include "sockets_extensions_abstract.h"
|
||||
#include "../smart_pointers.h"
|
||||
#include <iosfwd>
|
||||
|
||||
namespace dlib
|
||||
{
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
struct network_address
|
||||
{
|
||||
network_address() : port(0){}
|
||||
|
||||
network_address(
|
||||
const std::string& host_address_,
|
||||
const unsigned short port_
|
||||
) : host_address(host_address_), port(port_) {}
|
||||
|
||||
std::string host_address;
|
||||
unsigned short port;
|
||||
};
|
||||
|
||||
void serialize(
|
||||
const network_address& item,
|
||||
std::ostream& out
|
||||
);
|
||||
|
||||
void deserialize(
|
||||
network_address& item,
|
||||
std::istream& in
|
||||
);
|
||||
|
||||
std::ostream& operator<< (
|
||||
std::ostream& out,
|
||||
const network_address& item
|
||||
);
|
||||
|
||||
std::istream& operator>> (
|
||||
std::istream& in,
|
||||
network_address& item
|
||||
);
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
connection* connect (
|
||||
|
@ -18,6 +54,12 @@ namespace dlib
|
|||
unsigned short port
|
||||
);
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
connection* connect (
|
||||
const network_address& addr
|
||||
);
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
connection* connect (
|
||||
|
@ -26,6 +68,13 @@ namespace dlib
|
|||
unsigned long timeout
|
||||
);
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
connection* connect (
|
||||
const network_address& addr,
|
||||
unsigned long timeout
|
||||
);
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
bool is_ip_address (
|
||||
|
|
|
@ -11,6 +11,88 @@
|
|||
namespace dlib
|
||||
{
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
struct network_address
|
||||
{
|
||||
/*!
|
||||
WHAT THIS OBJECT REPRESENTS
|
||||
This object is simply a container for two things:
|
||||
- A host machine address which is either an IP address or DNS name
|
||||
for a machine.
|
||||
- A port number.
|
||||
|
||||
Together, these things define a machine and port on that machine.
|
||||
!*/
|
||||
|
||||
network_address(
|
||||
);
|
||||
/*!
|
||||
ensures
|
||||
- host_address == ""
|
||||
- #port == 0
|
||||
!*/
|
||||
|
||||
network_address(
|
||||
const std::string& host_address_,
|
||||
const unsigned short port_
|
||||
);
|
||||
/*!
|
||||
ensures
|
||||
- #host_address == host_address_
|
||||
- #port == port_
|
||||
!*/
|
||||
|
||||
|
||||
std::string host_address;
|
||||
unsigned short port;
|
||||
};
|
||||
|
||||
void serialize(
|
||||
const network_address& item,
|
||||
std::ostream& out
|
||||
);
|
||||
/*!
|
||||
ensures
|
||||
- provides serialization support
|
||||
!*/
|
||||
|
||||
void deserialize(
|
||||
network_address& item,
|
||||
std::istream& in
|
||||
);
|
||||
/*!
|
||||
ensures
|
||||
- provides deserialization support
|
||||
!*/
|
||||
|
||||
std::ostream& operator<< (
|
||||
std::ostream& out,
|
||||
const network_address& item
|
||||
);
|
||||
/*!
|
||||
ensures
|
||||
- writes the given network_address to the output stream. The format is the
|
||||
host_address, then a colon, then the port number. So for example:
|
||||
cout << network_address("localhost", 80);
|
||||
would print:
|
||||
localhost:80
|
||||
- returns #out
|
||||
!*/
|
||||
|
||||
std::istream& operator>> (
|
||||
std::istream& in,
|
||||
network_address& item
|
||||
);
|
||||
/*!
|
||||
ensures
|
||||
- reads a network_address from the given input stream. The expected format is
|
||||
the same as the one used to print them by the above operator<<() routine.
|
||||
- returns #in
|
||||
- if (there is an error reading the network_address) then
|
||||
- #in.good() == false
|
||||
!*/
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
connection* connect (
|
||||
|
@ -23,11 +105,19 @@ namespace dlib
|
|||
given port
|
||||
throws
|
||||
- dlib::socket_error
|
||||
This exception is thrown if there is some problem that prevents us from
|
||||
creating the connection
|
||||
This exception is thrown if there is some problem that prevents us from
|
||||
creating the connection
|
||||
- std::bad_alloc
|
||||
!*/
|
||||
|
||||
connection* connect (
|
||||
const network_address& addr
|
||||
);
|
||||
/*!
|
||||
ensures
|
||||
- returns connect(addr.host_address, addr_port);
|
||||
!*/
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
connection* connect (
|
||||
|
@ -42,12 +132,21 @@ namespace dlib
|
|||
- blocks for at most timeout milliseconds
|
||||
throws
|
||||
- dlib::socket_error
|
||||
This exception is thrown if there is some problem that prevents us from
|
||||
creating the connection or if timeout milliseconds elapses before
|
||||
the connect is successful.
|
||||
This exception is thrown if there is some problem that prevents us from
|
||||
creating the connection or if timeout milliseconds elapses before the
|
||||
connect is successful.
|
||||
- std::bad_alloc
|
||||
!*/
|
||||
|
||||
connection* connect (
|
||||
const network_address& addr,
|
||||
unsigned long timeout
|
||||
);
|
||||
/*!
|
||||
ensures
|
||||
- returns connect(addr.host_address, addr_port, timeout);
|
||||
!*/
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
@ -83,8 +182,8 @@ namespace dlib
|
|||
has finished.
|
||||
throws
|
||||
- std::bad_alloc or dlib::thread_error
|
||||
If either of these exceptions are thrown con will still be closed via
|
||||
"delete con;"
|
||||
If either of these exceptions are thrown con will still be closed via
|
||||
"delete con;"
|
||||
!*/
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
@ -108,8 +207,8 @@ namespace dlib
|
|||
has finished.
|
||||
throws
|
||||
- std::bad_alloc or dlib::thread_error
|
||||
If either of these exceptions are thrown con will still be closed and
|
||||
deleted (i.e. #con.get() == 0).
|
||||
If either of these exceptions are thrown con will still be closed and
|
||||
deleted (i.e. #con.get() == 0).
|
||||
!*/
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
|
Loading…
Reference in New Issue