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
318a2880
Commit
318a2880
authored
Apr 29, 2018
by
Hubert Denkmair
Browse files
start implement viewer keys and logging
parent
f0672c5b
Changes
7
Hide whitespace changes
Inline
Side-by-side
relayserver/MsgPackProtocol.cpp
View file @
318a2880
...
...
@@ -22,6 +22,9 @@ void MsgPackProtocol::pack(msgpack::sbuffer &buf, const Message &msg)
case
MESSAGE_TYPE_BOT_MOVE
:
msgpack
::
pack
(
buf
,
*
static_cast
<
const
BotMoveMessage
*>
(
&
msg
));
break
;
case
MESSAGE_TYPE_BOT_LOG
:
msgpack
::
pack
(
buf
,
*
static_cast
<
const
BotLogMessage
*>
(
&
msg
));
break
;
case
MESSAGE_TYPE_FOOD_SPAWN
:
msgpack
::
pack
(
buf
,
*
static_cast
<
const
FoodSpawnMessage
*>
(
&
msg
));
break
;
...
...
relayserver/MsgPackProtocol.h
View file @
318a2880
...
...
@@ -16,6 +16,7 @@ namespace MsgPackProtocol
MESSAGE_TYPE_BOT_SPAWN
=
0x20
,
MESSAGE_TYPE_BOT_KILL
=
0x21
,
MESSAGE_TYPE_BOT_MOVE
=
0x22
,
MESSAGE_TYPE_BOT_LOG
=
0x23
,
MESSAGE_TYPE_FOOD_SPAWN
=
0x30
,
MESSAGE_TYPE_FOOD_CONSUME
=
0x31
,
...
...
@@ -52,6 +53,12 @@ namespace MsgPackProtocol
int
database_id
;
};
struct
BotLogItem
{
uint64_t
viewer_key
;
std
::
string
message
;
};
struct
Message
{
MessageType
messageType
;
...
...
@@ -113,6 +120,12 @@ namespace MsgPackProtocol
BotKillMessage
()
:
Message
(
MESSAGE_TYPE_BOT_KILL
)
{}
};
struct
BotLogMessage
:
public
Message
{
std
::
vector
<
BotLogItem
>
items
;
BotLogMessage
()
:
Message
(
MESSAGE_TYPE_BOT_LOG
)
{}
};
struct
FoodSpawnMessage
:
public
Message
{
std
::
vector
<
FoodItem
>
new_food
;
...
...
@@ -340,6 +353,52 @@ namespace msgpack {
}
};
template
<
>
struct
pack
<
MsgPackProtocol
::
BotLogMessage
>
{
template
<
typename
Stream
>
msgpack
::
packer
<
Stream
>&
operator
()(
msgpack
::
packer
<
Stream
>&
o
,
MsgPackProtocol
::
BotLogMessage
const
&
v
)
const
{
o
.
pack_array
(
3
);
o
.
pack
(
MsgPackProtocol
::
PROTOCOL_VERSION
);
o
.
pack
(
static_cast
<
int
>
(
MsgPackProtocol
::
MESSAGE_TYPE_BOT_LOG
));
o
.
pack
(
v
.
items
);
return
o
;
}
};
template
<
>
struct
convert
<
MsgPackProtocol
::
BotLogMessage
>
{
msgpack
::
object
const
&
operator
()(
msgpack
::
object
const
&
o
,
MsgPackProtocol
::
BotLogMessage
&
v
)
const
{
if
(
o
.
type
!=
msgpack
::
type
::
ARRAY
)
throw
msgpack
::
type_error
();
if
(
o
.
via
.
array
.
size
!=
3
)
throw
msgpack
::
type_error
();
o
.
via
.
array
.
ptr
[
2
]
>>
v
.
items
;
return
o
;
}
};
template
<
>
struct
pack
<
MsgPackProtocol
::
BotLogItem
>
{
template
<
typename
Stream
>
msgpack
::
packer
<
Stream
>&
operator
()(
msgpack
::
packer
<
Stream
>&
o
,
MsgPackProtocol
::
BotLogItem
const
&
v
)
const
{
o
.
pack_array
(
2
);
o
.
pack
(
v
.
viewer_key
);
o
.
pack
(
v
.
message
);
return
o
;
}
};
template
<
>
struct
convert
<
MsgPackProtocol
::
BotLogItem
>
{
msgpack
::
object
const
&
operator
()(
msgpack
::
object
const
&
o
,
MsgPackProtocol
::
BotLogItem
&
v
)
const
{
if
(
o
.
type
!=
msgpack
::
type
::
ARRAY
)
throw
msgpack
::
type_error
();
if
(
o
.
via
.
array
.
size
!=
2
)
throw
msgpack
::
type_error
();
o
.
via
.
array
.
ptr
[
0
]
>>
v
.
viewer_key
;
o
.
via
.
array
.
ptr
[
1
]
>>
v
.
message
;
return
o
;
}
};
template
<
>
struct
pack
<
MsgPackProtocol
::
FoodSpawnMessage
>
{
template
<
typename
Stream
>
msgpack
::
packer
<
Stream
>&
operator
()(
msgpack
::
packer
<
Stream
>&
o
,
MsgPackProtocol
::
FoodSpawnMessage
const
&
v
)
const
...
...
relayserver/RelayServer.cpp
View file @
318a2880
...
...
@@ -27,13 +27,27 @@ int RelayServer::Run()
_tcpProtocol
.
SetFrameCompleteCallback
(
[
this
,
&
h
](
uint64_t
frame_id
)
{
auto
&
logMessages
=
_tcpProtocol
.
GetPendingLogItems
();
h
.
getDefaultGroup
<
uWS
::
SERVER
>
().
forEach
(
[
this
,
frame_id
](
uWS
::
WebSocket
<
uWS
::
SERVER
>*
sock
)
[
this
,
frame_id
,
&
logMessages
](
uWS
::
WebSocket
<
uWS
::
SERVER
>*
sock
)
{
auto
con
=
static_cast
<
WebsocketConnection
*>
(
sock
->
getUserData
());
con
->
FrameComplete
(
frame_id
,
_tcpProtocol
);
auto
key
=
con
->
getViewerKey
();
//if (key!=0) TODO implement
{
for
(
auto
&
item
:
logMessages
)
{
//if (item->viewer_key == key) // TODO implement
{
con
->
LogMessage
(
frame_id
,
item
.
message
);
}
}
}
}
);
_tcpProtocol
.
ClearLogItems
();
//std::cout << "frame " << frame_id << " complete." << std::endl;
}
);
...
...
relayserver/TcpProtocol.cpp
View file @
318a2880
...
...
@@ -89,6 +89,14 @@ void TcpProtocol::OnMessageReceived(const char* data, size_t count)
break
;
}
case
MsgPackProtocol
::
MESSAGE_TYPE_BOT_LOG
:
{
auto
msg
=
std
::
make_unique
<
MsgPackProtocol
::
BotLogMessage
>
();
obj
.
get
().
convert
(
*
msg
);
OnBotLogReceived
(
std
::
move
(
msg
));
break
;
}
case
MsgPackProtocol
::
MESSAGE_TYPE_FOOD_SPAWN
:
OnFoodSpawnReceived
(
obj
.
get
().
as
<
MsgPackProtocol
::
FoodSpawnMessage
>
());
break
;
...
...
@@ -180,3 +188,11 @@ void TcpProtocol::OnBotMoveReceived(std::unique_ptr<MsgPackProtocol::BotMoveMess
_pendingMessages
.
push_back
(
std
::
move
(
msg
));
}
void
TcpProtocol
::
OnBotLogReceived
(
std
::
unique_ptr
<
MsgPackProtocol
::
BotLogMessage
>
msg
)
{
for
(
auto
&
item
:
msg
->
items
)
{
_pendingLogItems
.
push_back
(
item
);
}
}
relayserver/TcpProtocol.h
View file @
318a2880
...
...
@@ -25,6 +25,8 @@ class TcpProtocol
const
MsgPackProtocol
::
WorldUpdateMessage
&
GetWorldUpdate
()
const
{
return
_worldUpdate
;
}
const
std
::
vector
<
std
::
unique_ptr
<
MsgPackProtocol
::
Message
>>&
GetPendingMessages
()
const
{
return
_pendingMessages
;
}
const
std
::
vector
<
MsgPackProtocol
::
BotLogItem
>&
GetPendingLogItems
()
const
{
return
_pendingLogItems
;
}
void
ClearLogItems
()
{
_pendingLogItems
.
clear
();
}
private:
static
constexpr
const
size_t
SPATIAL_MAP_TILES_X
=
128
;
...
...
@@ -45,9 +47,7 @@ class TcpProtocol
std
::
vector
<
FoodItem
>&
_food
;
std
::
vector
<
BotItem
>&
_bots
;
std
::
vector
<
std
::
unique_ptr
<
MsgPackProtocol
::
Message
>>
_pendingMessages
;
//std::unique_ptr<FoodMap> _foodMap;
//std::unique_ptr<SnakeSegmentMap> _segments;
std
::
vector
<
MsgPackProtocol
::
BotLogItem
>
_pendingLogItems
;
void
OnMessageReceived
(
const
char
*
data
,
size_t
count
);
...
...
@@ -62,4 +62,5 @@ class TcpProtocol
void
OnBotSpawnReceived
(
const
MsgPackProtocol
::
BotSpawnMessage
&
msg
);
void
OnBotKillReceived
(
const
MsgPackProtocol
::
BotKillMessage
&
msg
);
void
OnBotMoveReceived
(
std
::
unique_ptr
<
MsgPackProtocol
::
BotMoveMessage
>
msg
);
void
OnBotLogReceived
(
std
::
unique_ptr
<
MsgPackProtocol
::
BotLogMessage
>
msg
);
};
relayserver/WebsocketConnection.cpp
View file @
318a2880
...
...
@@ -24,6 +24,12 @@ void WebsocketConnection::FrameComplete(uint64_t frame_id, const TcpProtocol &pr
}
}
void
WebsocketConnection
::
LogMessage
(
uint64_t
frame_id
,
const
std
::
string
&
message
)
{
json
j
=
{
{
"t"
,
"Log"
},
{
"frame"
,
frame_id
},
{
"msg"
,
message
}
};
sendString
(
j
.
dump
());
}
void
WebsocketConnection
::
sendInitialData
(
const
TcpProtocol
&
proto
)
{
sendString
(
json
(
proto
.
GetGameInfo
()).
dump
());
...
...
relayserver/WebsocketConnection.h
View file @
318a2880
...
...
@@ -9,11 +9,14 @@ class WebsocketConnection
public:
WebsocketConnection
(
uWS
::
WebSocket
<
uWS
::
SERVER
>
*
websocket
);
void
FrameComplete
(
uint64_t
frame_id
,
const
TcpProtocol
&
proto
);
void
LogMessage
(
uint64_t
frame_id
,
const
std
::
string
&
message
);
void
sendString
(
std
::
string
data
);
uint64_t
getViewerKey
()
{
return
_viewerKey
;
}
private:
uWS
::
WebSocket
<
uWS
::
SERVER
>
*
_websocket
;
bool
_firstFrameSent
=
false
;
uint64_t
_viewerKey
=
0
;
void
sendInitialData
(
const
TcpProtocol
&
proto
);
...
...
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