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
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
235 additions
and
2 deletions
+235
-2
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
+101
-2
No files found.
dlib/sockets/sockets_extensions.cpp
View file @
d955b492
...
@@ -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
(
...
...
dlib/sockets/sockets_extensions.h
View file @
d955b492
...
@@ -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
(
...
...
dlib/sockets/sockets_extensions_abstract.h
View file @
d955b492
...
@@ -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 befor
e
creating the connection or if timeout milliseconds elapses before th
e
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);
!*/
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
...
...
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