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
c5e315c8
Commit
c5e315c8
authored
Apr 05, 2021
by
hyperxor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add ::shutdown call for client socket and more debug traces
parent
736f4ba8
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
45 additions
and
24 deletions
+45
-24
include/pistache/net.h
include/pistache/net.h
+4
-0
src/client/client.cc
src/client/client.cc
+4
-2
src/common/net.cc
src/common/net.cc
+6
-1
src/common/peer.cc
src/common/peer.cc
+2
-2
tests/http_server_test.cc
tests/http_server_test.cc
+29
-19
No files found.
include/pistache/net.h
View file @
c5e315c8
...
...
@@ -150,12 +150,16 @@ public:
Port
port
()
const
;
int
family
()
const
;
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
Address
&
address
);
private:
void
init
(
const
std
::
string
&
addr
);
IP
ip_
;
Port
port_
;
};
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
Address
&
address
);
namespace
helpers
{
inline
Address
httpAddr
(
const
StringView
&
view
)
{
auto
const
str
=
view
.
toString
();
...
...
src/client/client.cc
View file @
c5e315c8
...
...
@@ -501,6 +501,7 @@ bool Connection::isConnected() const {
void
Connection
::
close
()
{
connectionState_
.
store
(
NotConnected
);
::
shutdown
(
fd_
,
SHUT_RDWR
);
::
close
(
fd_
);
}
...
...
@@ -713,8 +714,9 @@ size_t ConnectionPool::availableConnections(const std::string &domain) const {
return
0
;
}
void
ConnectionPool
::
closeIdleConnections
(
const
std
::
string
&
domain
){
UNUSED
(
domain
)}
void
ConnectionPool
::
closeIdleConnections
(
const
std
::
string
&
domain
)
{
UNUSED
(
domain
)
}
void
ConnectionPool
::
shutdown
()
{
// close all connections
...
...
src/common/net.cc
View file @
c5e315c8
...
...
@@ -356,7 +356,7 @@ void Address::init(const std::string &addr) {
host
=
"127.0.0.1"
;
}
const
auto
&
addresses
=
HostToIPv4
(
host
,
portPart
);
const
auto
&
addresses
=
HostToIPv4
(
host
,
portPart
);
if
(
!
addresses
.
empty
())
{
ip_
=
GetIPv4
(
addresses
[
0
]);
}
else
{
...
...
@@ -367,6 +367,11 @@ void Address::init(const std::string &addr) {
}
}
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
Address
&
address
)
{
os
<<
address
.
host
()
<<
":"
<<
address
.
port
();
return
os
;
}
Error
::
Error
(
const
char
*
message
)
:
std
::
runtime_error
(
message
)
{}
Error
::
Error
(
std
::
string
message
)
:
std
::
runtime_error
(
std
::
move
(
message
))
{}
...
...
src/common/peer.cc
View file @
c5e315c8
...
...
@@ -98,8 +98,8 @@ Async::Promise<ssize_t> Peer::send(const RawBuffer &buffer, int flags) {
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
Peer
&
peer
)
{
const
auto
&
addr
=
peer
.
address
();
os
<<
"
("
<<
addr
.
host
()
<<
", "
<<
addr
.
port
()
<<
") ["
<<
peer
.
hostname
()
<<
"
]
"
;
os
<<
"
Peer with ID="
<<
peer
.
getID
()
<<
" (address="
<<
addr
<<
"
, hostname="
<<
peer
.
hostname
()
<<
", fd="
<<
peer
.
fd
()
<<
")
"
;
return
os
;
}
...
...
tests/http_server_test.cc
View file @
c5e315c8
...
...
@@ -13,14 +13,21 @@
#include <future>
#include <mutex>
#include <string>
#include <thread>
using
namespace
Pistache
;
#define THREAD_INFO \
"[" << std::hex << std::this_thread::get_id() << std::dec << "]"
#define SERVER_PREFIX "[server] " THREAD_INFO
#define CLIENT_PREFIX "[client] " THREAD_INFO
struct
HelloHandlerWithDelay
:
public
Http
::
Handler
{
HTTP_PROTOTYPE
(
HelloHandlerWithDelay
)
explicit
HelloHandlerWithDelay
(
int
delay
=
0
)
:
delay_
(
delay
)
{
std
::
cout
<<
"Init Hello handler with "
<<
delay_
<<
" seconds delay
\n
"
;
std
::
cout
<<
SERVER_PREFIX
<<
" Init Hello handler with "
<<
delay_
<<
" seconds delay
\n
"
;
}
void
onRequest
(
const
Http
::
Request
&
/*request*/
,
...
...
@@ -50,7 +57,7 @@ struct HandlerWithSlowPage : public Http::Handler {
}
writer
.
send
(
Http
::
Code
::
Ok
,
message
);
std
::
cout
<<
"[server]
Sent: "
<<
message
;
std
::
cout
<<
SERVER_PREFIX
<<
"
Sent: "
<<
message
;
}
int
delay_
;
...
...
@@ -66,8 +73,8 @@ struct FileHandler : public Http::Handler {
Http
::
serveFile
(
writer
,
fileName_
)
.
then
(
[
this
](
ssize_t
bytes
)
{
std
::
cout
<<
"Sent "
<<
bytes
<<
" bytes from "
<<
fileName_
<<
" file"
<<
std
::
endl
;
std
::
cout
<<
SERVER_PREFIX
<<
" Sent "
<<
bytes
<<
" bytes from "
<<
fileName_
<<
" file"
<<
std
::
endl
;
},
Async
::
IgnoreException
);
}
...
...
@@ -85,7 +92,7 @@ struct AddressEchoHandler : public Http::Handler {
Http
::
ResponseWriter
writer
)
override
{
std
::
string
requestAddress
=
request
.
address
().
host
();
writer
.
send
(
Http
::
Code
::
Ok
,
requestAddress
);
std
::
cout
<<
"[server]
Sent: "
<<
requestAddress
<<
std
::
endl
;
std
::
cout
<<
SERVER_PREFIX
<<
"
Sent: "
<<
requestAddress
<<
std
::
endl
;
}
};
...
...
@@ -103,14 +110,15 @@ int clientLogicFunc(int response_size, const std::string &server_page,
auto
response
=
rb
.
send
();
response
.
then
(
[
&
resolver_counter
](
Http
::
Response
resp
)
{
std
::
cout
<<
"Response code is "
<<
resp
.
code
()
<<
std
::
endl
;
std
::
cout
<<
CLIENT_PREFIX
<<
" Response code is "
<<
resp
.
code
()
<<
std
::
endl
;
if
(
resp
.
code
()
==
Http
::
Code
::
Ok
)
{
++
resolver_counter
;
}
},
[
&
reject_counter
](
std
::
exception_ptr
exc
)
{
PrintException
excPrinter
;
std
::
cout
<<
"
Reject with reason: "
;
std
::
cout
<<
CLIENT_PREFIX
<<
"
Reject with reason: "
;
excPrinter
(
exc
);
++
reject_counter
;
});
...
...
@@ -123,9 +131,10 @@ int clientLogicFunc(int response_size, const std::string &server_page,
client
.
shutdown
();
std
::
cout
<<
"
resolves: "
<<
resolver_counter
std
::
cout
<<
CLIENT_PREFIX
<<
"
resolves: "
<<
resolver_counter
<<
", rejects: "
<<
reject_counter
<<
", timeout: "
<<
timeout_seconds
<<
", wait: "
<<
wait_seconds
<<
", timeout: "
<<
timeout_seconds
<<
" seconds"
<<
", wait: "
<<
wait_seconds
<<
" seconds"
<<
"
\n
"
;
return
resolver_counter
;
...
...
@@ -333,7 +342,8 @@ TEST(http_server_test, server_request_copies_address) {
std
::
string
resultData
;
response
.
then
(
[
&
resultData
](
Http
::
Response
resp
)
{
std
::
cout
<<
"Response code is "
<<
resp
.
code
()
<<
std
::
endl
;
std
::
cout
<<
CLIENT_PREFIX
<<
" Response code is "
<<
resp
.
code
()
<<
std
::
endl
;
if
(
resp
.
code
()
==
Http
::
Code
::
Ok
)
{
resultData
=
resp
.
body
();
}
...
...
@@ -360,7 +370,7 @@ struct ResponseSizeHandler : public Http::Handler {
Http
::
ResponseWriter
writer
)
override
{
std
::
string
requestAddress
=
request
.
address
().
host
();
writer
.
send
(
Http
::
Code
::
Ok
,
requestAddress
);
std
::
cout
<<
"[server]
Sent: "
<<
requestAddress
<<
std
::
endl
;
std
::
cout
<<
SERVER_PREFIX
<<
"
Sent: "
<<
requestAddress
<<
std
::
endl
;
rsize_
=
writer
.
getResponseSize
();
rcode_
=
writer
.
getResponseCode
();
}
...
...
@@ -395,7 +405,8 @@ TEST(http_server_test, response_size_captured) {
std
::
string
resultData
;
response
.
then
(
[
&
resultData
](
Http
::
Response
resp
)
{
std
::
cout
<<
"Response code is "
<<
resp
.
code
()
<<
std
::
endl
;
std
::
cout
<<
CLIENT_PREFIX
<<
" Response code is "
<<
resp
.
code
()
<<
std
::
endl
;
if
(
resp
.
code
()
==
Http
::
Code
::
Ok
)
{
resultData
=
resp
.
body
();
}
...
...
@@ -412,7 +423,7 @@ TEST(http_server_test, response_size_captured) {
// Sanity check (stolen from AddressEchoHandler test).
ASSERT_EQ
(
"127.0.0.1"
,
resultData
);
std
::
cout
<<
"
Response size is "
<<
rsize
<<
"
\n
"
;
std
::
cout
<<
CLIENT_PREFIX
<<
"
Response size is "
<<
rsize
<<
"
\n
"
;
ASSERT_GT
(
rsize
,
1u
);
ASSERT_LT
(
rsize
,
300u
);
ASSERT_EQ
(
rcode
,
Http
::
Code
::
Ok
);
...
...
@@ -445,9 +456,7 @@ struct ClientCountingHandler : public Http::Handler {
HTTP_PROTOTYPE
(
ClientCountingHandler
)
explicit
ClientCountingHandler
(
std
::
shared_ptr
<
WaitHelper
>
waitHelper
)
:
waitHelper
(
waitHelper
)
{
std
::
cout
<<
"[server] Ininting..."
<<
std
::
endl
;
}
:
waitHelper
(
waitHelper
)
{}
void
onRequest
(
const
Http
::
Request
&
request
,
Http
::
ResponseWriter
writer
)
override
{
...
...
@@ -459,12 +468,13 @@ struct ClientCountingHandler : public Http::Handler {
}
std
::
string
requestAddress
=
request
.
address
().
host
();
writer
.
send
(
Http
::
Code
::
Ok
,
requestAddress
);
std
::
cout
<<
"[server] Sent: "
<<
requestAddress
<<
std
::
endl
;
std
::
cout
<<
SERVER_PREFIX
<<
" Sent: `"
<<
requestAddress
<<
"` data to "
<<
*
peer
<<
std
::
endl
;
}
void
onDisconnection
(
const
std
::
shared_ptr
<
Tcp
::
Peer
>
&
peer
)
override
{
std
::
cout
<<
"[server] Disconnect from peer ID "
<<
peer
->
getID
()
<<
" connecting from "
<<
peer
->
address
().
host
()
<<
std
::
endl
;
std
::
cout
<<
SERVER_PREFIX
<<
" Disconnect from peer "
<<
*
peer
<<
std
::
endl
;
activeConnections
.
erase
(
peer
->
getID
());
waitHelper
->
increment
();
}
...
...
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