Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Jochen Bauer
spn-relayserver
Commits
d232a1a0
Commit
d232a1a0
authored
May 07, 2018
by
Hubert Denkmair
Browse files
add basic http response for stats
parent
3ea8a1af
Changes
4
Hide whitespace changes
Inline
Side-by-side
relayserver/RelayServer.cpp
View file @
d232a1a0
#include "RelayServer.h"
#include <iostream>
#include <string>
#include <sstream>
#include <TcpServer/EPoll.h>
#include "JsonProtocol.h"
...
...
@@ -55,6 +56,18 @@ int RelayServer::Run()
}
}
);
_tcpProtocol
.
SetStatsReceivedCallback
([
this
](
const
MsgPackProtocol
::
BotStatsMessage
&
msg
)
{
/*std::string content = json(msg).dump();
std::stringstream s;
s << "HTTP/1.0 200 OK\r\n";
s << "Content-Length: " << content.size() << "\r\n";
s << "Content-Type: application/json\r\n\r\n";
s << content;
_statsHTTPResponse = s.str();*/
_statsHTTPResponse
=
json
(
msg
).
dump
();
});
epoll
.
AddFileDescriptor
(
_clientSocket
,
EPOLLIN
|
EPOLLPRI
|
EPOLLERR
);
h
.
onConnection
(
...
...
@@ -98,9 +111,14 @@ int RelayServer::Run()
}
});
std
::
string
response
=
"
Hello!
"
;
std
::
string
response
=
"
nope.
"
;
h
.
onHttpRequest
([
&
](
uWS
::
HttpResponse
*
res
,
uWS
::
HttpRequest
req
,
char
*
data
,
size_t
length
,
size_t
remainingBytes
)
{
if
((
req
.
getMethod
()
==
uWS
::
METHOD_GET
)
&&
(
req
.
getUrl
().
toString
()
==
"/stats"
))
{
res
->
end
(
_statsHTTPResponse
.
data
(),
_statsHTTPResponse
.
length
());
return
;
}
res
->
end
(
response
.
data
(),
response
.
length
());
});
...
...
relayserver/RelayServer.h
View file @
d232a1a0
...
...
@@ -12,6 +12,7 @@ class RelayServer
private:
int
_clientSocket
;
TcpProtocol
_tcpProtocol
;
std
::
string
_statsHTTPResponse
;
static
constexpr
const
char
*
ENV_GAMESERVER_HOST
=
"GAMESERVER_HOST"
;
static
constexpr
const
char
*
ENV_GAMESERVER_HOST_DEFAULT
=
"localhost"
;
...
...
relayserver/TcpProtocol.cpp
View file @
d232a1a0
...
...
@@ -16,6 +16,11 @@ void TcpProtocol::SetFrameCompleteCallback(TcpProtocol::FrameCompleteCallback ca
_frameCompleteCallback
=
callback
;
}
void
TcpProtocol
::
SetStatsReceivedCallback
(
StatsReceivedCallback
callback
)
{
_statsReceivedCallback
=
callback
;
}
bool
TcpProtocol
::
Read
(
int
socket
)
{
ssize_t
bytesRead
=
read
(
socket
,
&
_buf
[
_bufTail
],
_buf
.
size
()
-
_bufTail
);
...
...
@@ -179,7 +184,10 @@ void TcpProtocol::OnTickReceived(std::unique_ptr<MsgPackProtocol::TickMessage> m
{
auto
frame_id
=
msg
->
frame_id
;
_pendingMessages
.
push_back
(
std
::
move
(
msg
));
_frameCompleteCallback
(
frame_id
);
if
(
_frameCompleteCallback
!=
nullptr
)
{
_frameCompleteCallback
(
frame_id
);
}
_pendingMessages
.
clear
();
}
...
...
@@ -252,6 +260,10 @@ void TcpProtocol::OnBotLogReceived(std::unique_ptr<MsgPackProtocol::BotLogMessag
void
TcpProtocol
::
OnBotStatsReceived
(
std
::
unique_ptr
<
MsgPackProtocol
::
BotStatsMessage
>
msg
)
{
_botStats
=
*
msg
;
if
(
_statsReceivedCallback
!=
nullptr
)
{
_statsReceivedCallback
(
_botStats
);
}
_pendingMessages
.
push_back
(
std
::
move
(
msg
));
}
...
...
relayserver/TcpProtocol.h
View file @
d232a1a0
...
...
@@ -15,10 +15,12 @@ class TcpProtocol
{
public:
typedef
std
::
function
<
void
(
uint64_t
frame_id
)
>
FrameCompleteCallback
;
typedef
std
::
function
<
void
(
const
MsgPackProtocol
::
BotStatsMessage
&
msg
)
>
StatsReceivedCallback
;
static
constexpr
const
size_t
BUFFER_SIZE
=
1024
*
1024
;
TcpProtocol
();
void
SetFrameCompleteCallback
(
FrameCompleteCallback
callback
);
void
SetStatsReceivedCallback
(
StatsReceivedCallback
callback
);
bool
Read
(
int
socket
);
const
MsgPackProtocol
::
GameInfoMessage
&
GetGameInfo
()
const
{
return
_gameInfo
;
}
...
...
@@ -36,6 +38,7 @@ class TcpProtocol
size_t
_bufTail
=
0
;
FrameCompleteCallback
_frameCompleteCallback
;
StatsReceivedCallback
_statsReceivedCallback
;
MsgPackProtocol
::
GameInfoMessage
_gameInfo
;
MsgPackProtocol
::
BotStatsMessage
_botStats
;
std
::
map
<
guid_t
,
FoodItem
>
_foodMap
;
...
...
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