Commit d955b492 authored by Davis King's avatar Davis King

Added the network_address object.

parent 3ebaff2e
...@@ -12,10 +12,76 @@ ...@@ -12,10 +12,76 @@
#include "../algs.h" #include "../algs.h"
#include "../timeout.h" #include "../timeout.h"
#include "../misc_api.h" #include "../misc_api.h"
#include "../serialize.h"
namespace dlib 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 ( connection* connect (
...@@ -45,6 +111,15 @@ namespace dlib ...@@ -45,6 +111,15 @@ namespace dlib
return con; return con;
} }
// ----------------------------------------------------------------------------------------
connection* connect (
const network_address& addr
)
{
return connect(addr.host_address, addr.port);
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
namespace connect_timeout_helpers namespace connect_timeout_helpers
...@@ -167,6 +242,16 @@ namespace dlib ...@@ -167,6 +242,16 @@ namespace dlib
return data->con; return data->con;
} }
// ----------------------------------------------------------------------------------------
connection* connect (
const network_address& addr,
unsigned long timeout
)
{
return connect(addr.host_address, addr.port, timeout);
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
bool is_ip_address ( bool is_ip_address (
......
...@@ -7,10 +7,46 @@ ...@@ -7,10 +7,46 @@
#include "../sockets.h" #include "../sockets.h"
#include "sockets_extensions_abstract.h" #include "sockets_extensions_abstract.h"
#include "../smart_pointers.h" #include "../smart_pointers.h"
#include <iosfwd>
namespace dlib 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 ( connection* connect (
...@@ -18,6 +54,12 @@ namespace dlib ...@@ -18,6 +54,12 @@ namespace dlib
unsigned short port unsigned short port
); );
// ----------------------------------------------------------------------------------------
connection* connect (
const network_address& addr
);
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
connection* connect ( connection* connect (
...@@ -26,6 +68,13 @@ namespace dlib ...@@ -26,6 +68,13 @@ namespace dlib
unsigned long timeout unsigned long timeout
); );
// ----------------------------------------------------------------------------------------
connection* connect (
const network_address& addr,
unsigned long timeout
);
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
bool is_ip_address ( bool is_ip_address (
......
...@@ -11,6 +11,88 @@ ...@@ -11,6 +11,88 @@
namespace dlib 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 ( connection* connect (
...@@ -28,6 +110,14 @@ namespace dlib ...@@ -28,6 +110,14 @@ namespace dlib
- std::bad_alloc - std::bad_alloc
!*/ !*/
connection* connect (
const network_address& addr
);
/*!
ensures
- returns connect(addr.host_address, addr_port);
!*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
connection* connect ( connection* connect (
...@@ -43,11 +133,20 @@ namespace dlib ...@@ -43,11 +133,20 @@ namespace dlib
throws throws
- dlib::socket_error - dlib::socket_error
This exception is thrown if there is some problem that prevents us from This exception is thrown if there is some problem that prevents us from
creating the connection or if timeout milliseconds elapses before creating the connection or if timeout milliseconds elapses before the
the connect is successful. connect is successful.
- std::bad_alloc - std::bad_alloc
!*/ !*/
connection* connect (
const network_address& addr,
unsigned long timeout
);
/*!
ensures
- returns connect(addr.host_address, addr_port, timeout);
!*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment