Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
D
dlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
钟尚武
dlib
Commits
d955b492
Commit
d955b492
authored
Oct 21, 2012
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added the network_address object.
parent
3ebaff2e
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
242 additions
and
9 deletions
+242
-9
sockets_extensions.cpp
dlib/sockets/sockets_extensions.cpp
+85
-0
sockets_extensions.h
dlib/sockets/sockets_extensions.h
+49
-0
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
(
...
...
@@ -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 befor
e
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 th
e
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).
!*/
// ----------------------------------------------------------------------------------------
...
...
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