Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
pistache
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
pistache
Commits
385c78fb
Unverified
Commit
385c78fb
authored
Dec 25, 2018
by
Dennis Jenkins
Committed by
GitHub
Dec 25, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #385 from dennisjenkins75/ephemeral-ports
Ephemeral ports
parents
f8265da3
ce55436b
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
44 additions
and
1 deletion
+44
-1
include/pistache/endpoint.h
include/pistache/endpoint.h
+4
-0
include/pistache/listener.h
include/pistache/listener.h
+1
-0
src/server/listener.cc
src/server/listener.cc
+26
-1
tests/listener_test.cc
tests/listener_test.cc
+13
-0
No files found.
include/pistache/endpoint.h
View file @
385c78fb
...
...
@@ -53,6 +53,10 @@ public:
return
listener
.
isBound
();
}
Port
getPort
()
const
{
return
listener
.
getPort
();
}
Async
::
Promise
<
Tcp
::
Listener
::
Load
>
requestLoad
(
const
Tcp
::
Listener
::
Load
&
old
);
static
Options
options
();
...
...
include/pistache/listener.h
View file @
385c78fb
...
...
@@ -53,6 +53,7 @@ public:
void
bind
(
const
Address
&
address
);
bool
isBound
()
const
;
Port
getPort
()
const
;
void
run
();
void
runThreaded
();
...
...
src/server/listener.cc
View file @
385c78fb
...
...
@@ -184,7 +184,7 @@ Listener::bind(const Address& address) {
TRY
(
::
listen
(
fd
,
backlog_
));
break
;
}
// At this point, it is still possible that we couldn't bind any socket. If it is the case, the previous
// loop would have exited naturally and addr will be null.
if
(
addr
==
nullptr
)
{
...
...
@@ -207,6 +207,31 @@ Listener::isBound() const {
return
listen_fd
!=
-
1
;
}
// Return actual TCP port Listener is on, or 0 on error / no port.
// Notes:
// 1) Default constructor for 'Port()' sets value to 0.
// 2) Socket is created inside 'Listener::run()', which is called from
// 'Endpoint::serve()' and 'Endpoint::serveThreaded()'. So getting the
// port is only useful if you attempt to do so from a _different_ thread
// than the one running 'Listener::run()'. So for a traditional single-
// threaded program this method is of little value.
Port
Listener
::
getPort
()
const
{
if
(
listen_fd
==
-
1
)
{
return
Port
();
}
struct
sockaddr_in
sock_addr
=
{
0
};
socklen_t
addrlen
=
sizeof
(
sock_addr
);
auto
sock_addr_alias
=
reinterpret_cast
<
struct
sockaddr
*>
(
&
sock_addr
);
if
(
-
1
==
getsockname
(
listen_fd
,
sock_addr_alias
,
&
addrlen
))
{
return
Port
();
}
return
Port
(
ntohs
(
sock_addr
.
sin_port
));
}
void
Listener
::
run
()
{
reactor_
.
run
();
...
...
tests/listener_test.cc
View file @
385c78fb
...
...
@@ -179,3 +179,16 @@ TEST(listener_test, listener_bind_port_not_free_throw_runtime) {
FAIL
()
<<
"Expected std::runtime_error"
;
}
}
// Listener should be able to bind port 0 directly to get an ephemeral port.
TEST
(
listener_test
,
listener_bind_ephemeral_port
)
{
Pistache
::
Port
port
(
0
);
Pistache
::
Address
address
(
Pistache
::
Ipv4
::
any
(),
port
);
Pistache
::
Tcp
::
Listener
listener
;
listener
.
setHandler
(
Pistache
::
Http
::
make_handler
<
DummyHandler
>
());
listener
.
bind
(
address
);
Pistache
::
Port
bound_port
=
listener
.
getPort
();
ASSERT_TRUE
(
bound_port
>
(
uint16_t
)
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