Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
d955b492
Commit
d955b492
authored
Oct 21, 2012
by
Davis King
Browse files
Added the network_address object.
parent
3ebaff2e
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
242 additions
and
9 deletions
+242
-9
dlib/sockets/sockets_extensions.cpp
dlib/sockets/sockets_extensions.cpp
+85
-0
dlib/sockets/sockets_extensions.h
dlib/sockets/sockets_extensions.h
+49
-0
dlib/sockets/sockets_extensions_abstract.h
dlib/sockets/sockets_extensions_abstract.h
+108
-9
No files found.
dlib/sockets/sockets_extensions.cpp
View file @
d955b492
...
...
@@ -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
(
...
...
dlib/sockets/sockets_extensions.h
View file @
d955b492
...
...
@@ -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
(
...
...
dlib/sockets/sockets_extensions_abstract.h
View file @
d955b492
...
...
@@ -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
(
...
...
@@ -28,6 +110,14 @@ namespace dlib
- std::bad_alloc
!*/
connection
*
connect
(
const
network_address
&
addr
);
/*!
ensures
- returns connect(addr.host_address, addr_port);
!*/
// ----------------------------------------------------------------------------------------
connection
*
connect
(
...
...
@@ -43,11 +133,20 @@ namespace dlib
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.
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);
!*/
// ----------------------------------------------------------------------------------------
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment