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
797510c4
Commit
797510c4
authored
Apr 13, 2018
by
Hubert Denkmair
Browse files
cleanup
parent
927579d0
Changes
6
Hide whitespace changes
Inline
Side-by-side
relayserver/MsgPackProtocol.h
View file @
797510c4
...
...
@@ -52,32 +52,43 @@ namespace MsgPackProtocol
std
::
vector
<
uint32_t
>
color
;
};
struct
GameInfoMessage
struct
Message
{
virtual
~
Message
()
=
default
;
virtual
void
pack
(
msgpack
::
sbuffer
&
buf
)
const
=
0
;
};
struct
GameInfoMessage
:
public
Message
{
double
world_size_x
=
0
;
double
world_size_y
=
0
;
double
food_decay_per_frame
=
0
;
void
pack
(
msgpack
::
sbuffer
&
buf
)
const
override
{
msgpack
::
pack
(
buf
,
*
this
);
}
};
struct
PlayerInfoMessage
struct
PlayerInfoMessage
:
public
Message
{
guid_t
player_id
;
// id der von dieser Verbindung gesteuerten Schlange
void
pack
(
msgpack
::
sbuffer
&
buf
)
const
override
{
msgpack
::
pack
(
buf
,
*
this
);
}
};
struct
TickMessage
struct
TickMessage
:
public
Message
{
guid_t
frame_id
;
// frame counter since start of server
void
pack
(
msgpack
::
sbuffer
&
buf
)
const
override
{
msgpack
::
pack
(
buf
,
*
this
);
}
};
struct
WorldUpdateMessage
struct
WorldUpdateMessage
:
public
Message
{
std
::
vector
<
BotItem
>
bots
;
std
::
vector
<
FoodItem
>
food
;
void
pack
(
msgpack
::
sbuffer
&
buf
)
const
override
{
msgpack
::
pack
(
buf
,
*
this
);
}
};
struct
BotSpawnMessage
struct
BotSpawnMessage
:
public
Message
{
BotItem
bot
;
void
pack
(
msgpack
::
sbuffer
&
buf
)
const
override
{
msgpack
::
pack
(
buf
,
*
this
);
}
};
struct
BotMoveItem
...
...
@@ -88,20 +99,23 @@ namespace MsgPackProtocol
real_t
current_segment_radius
;
};
struct
BotMoveMessage
struct
BotMoveMessage
:
public
Message
{
std
::
vector
<
BotMoveItem
>
items
;
void
pack
(
msgpack
::
sbuffer
&
buf
)
const
override
{
msgpack
::
pack
(
buf
,
*
this
);
}
};
struct
BotKillMessage
struct
BotKillMessage
:
public
Message
{
guid_t
killer_id
;
guid_t
victim_id
;
// victim is deleted in this frame
void
pack
(
msgpack
::
sbuffer
&
buf
)
const
override
{
msgpack
::
pack
(
buf
,
*
this
);
}
};
struct
FoodSpawnMessage
struct
FoodSpawnMessage
:
public
Message
{
std
::
vector
<
FoodItem
>
new_food
;
void
pack
(
msgpack
::
sbuffer
&
buf
)
const
override
{
msgpack
::
pack
(
buf
,
*
this
);
}
};
struct
FoodConsumeItem
...
...
@@ -110,14 +124,16 @@ namespace MsgPackProtocol
guid_t
bot_id
;
// bot consuming the food
};
struct
FoodConsumeMessage
struct
FoodConsumeMessage
:
public
Message
{
std
::
vector
<
FoodConsumeItem
>
items
;
void
pack
(
msgpack
::
sbuffer
&
buf
)
const
override
{
msgpack
::
pack
(
buf
,
*
this
);
}
};
struct
FoodDecayMessage
struct
FoodDecayMessage
:
public
Message
{
std
::
vector
<
guid_t
>
food_ids
;
// food is deleted in this frame
void
pack
(
msgpack
::
sbuffer
&
buf
)
const
override
{
msgpack
::
pack
(
buf
,
*
this
);
}
};
}
...
...
relayserver/RelayServer.cpp
View file @
797510c4
...
...
@@ -32,7 +32,7 @@ int RelayServer::Run()
con
->
FrameComplete
(
frame_id
,
_tcpProtocol
);
}
);
std
::
cout
<<
"frame "
<<
frame_id
<<
" complete."
<<
std
::
endl
;
//
std::cout << "frame " << frame_id << " complete." << std::endl;
}
);
epoll
.
AddFileDescriptor
(
_clientSocket
,
EPOLLIN
|
EPOLLPRI
|
EPOLLERR
);
...
...
relayserver/TcpProtocol.cpp
View file @
797510c4
...
...
@@ -7,6 +7,8 @@
#include <iostream>
TcpProtocol
::
TcpProtocol
()
:
_food
(
_worldUpdate
.
food
)
,
_bots
(
_worldUpdate
.
bots
)
{
_buf
.
resize
(
BUFFER_SIZE
);
}
...
...
@@ -44,50 +46,6 @@ bool TcpProtocol::Read(int socket)
return
true
;
}
const
MsgPackProtocol
::
GameInfoMessage
&
TcpProtocol
::
GetGameInfo
()
const
{
return
_gameInfo
;
}
bool
TcpProtocol
::
GetWorldUpdate
(
msgpack
::
sbuffer
&
buf
)
const
{
MsgPackProtocol
::
WorldUpdateMessage
msg
;
msg
.
bots
=
_bots
;
msg
.
food
=
_foodVect
;
msgpack
::
pack
(
buf
,
msg
);
return
true
;
}
bool
TcpProtocol
::
GetFoodSpawnMessages
(
msgpack
::
sbuffer
&
buf
)
const
{
for
(
auto
&
msg
:
_foodSpawnMessages
)
{
msgpack
::
pack
(
buf
,
msg
);
// FIXME this will fail with multiple foodSpawnMessages, since only one message should be in one buffer
}
return
true
;
}
bool
TcpProtocol
::
GetFoodConsumeMessages
(
msgpack
::
sbuffer
&
buf
)
const
{
for
(
auto
&
msg
:
_foodConsumeMessages
)
{
msgpack
::
pack
(
buf
,
msg
);
// FIXME this will fail with multiple foodSpawnMessages, since only one message should be in one buffer
}
return
true
;
}
bool
TcpProtocol
::
GetFoodDecayMessages
(
msgpack
::
sbuffer
&
buf
)
const
{
for
(
auto
&
msg
:
_foodDecayMessages
)
{
msgpack
::
pack
(
buf
,
msg
);
// FIXME this will fail with multiple foodSpawnMessages, since only one message should be in one buffer
}
return
true
;
}
void
TcpProtocol
::
OnMessageReceived
(
const
char
*
data
,
size_t
count
)
{
msgpack
::
object_handle
obj
;
...
...
@@ -151,27 +109,25 @@ void TcpProtocol::OnGameInfoReceived(const MsgPackProtocol::GameInfoMessage& msg
void
TcpProtocol
::
OnWorldUpdateReceived
(
const
MsgPackProtocol
::
WorldUpdateMessage
&
msg
)
{
_bots
=
msg
.
bots
;
_food
Vect
=
msg
.
food
;
_food
=
msg
.
food
;
}
void
TcpProtocol
::
OnTickReceived
(
const
MsgPackProtocol
::
TickMessage
&
msg
)
{
_frameCompleteCallback
(
msg
.
frame_id
);
_foodSpawnMessages
.
clear
();
_foodConsumeMessages
.
clear
();
_foodDecayMessages
.
clear
();
_pendingMessages
.
clear
();
}
void
TcpProtocol
::
OnFoodSpawnReceived
(
const
MsgPackProtocol
::
FoodSpawnMessage
&
msg
)
{
_
foodSpawn
Messages
.
push_back
(
msg
);
_food
Vect
.
insert
(
_food
Vect
.
end
(),
msg
.
new_food
.
begin
(),
msg
.
new_food
.
end
());
_
pending
Messages
.
push_back
(
std
::
make_unique
<
MsgPackProtocol
::
FoodSpawnMessage
>
(
msg
)
);
_food
.
insert
(
_food
.
end
(),
msg
.
new_food
.
begin
(),
msg
.
new_food
.
end
());
}
void
TcpProtocol
::
OnFoodConsumedReceived
(
const
MsgPackProtocol
::
FoodConsumeMessage
&
msg
)
{
_
foodConsume
Messages
.
push_back
(
msg
);
_food
Vect
.
erase
(
std
::
remove_if
(
_food
Vect
.
begin
(),
_food
Vect
.
end
(),
[
&
msg
](
const
FoodItem
&
food
)
{
_
pending
Messages
.
push_back
(
std
::
make_unique
<
MsgPackProtocol
::
FoodConsumeMessage
>
(
msg
)
);
_food
.
erase
(
std
::
remove_if
(
_food
.
begin
(),
_food
.
end
(),
[
&
msg
](
const
FoodItem
&
food
)
{
for
(
auto
&
item
:
msg
.
items
)
{
if
(
food
.
guid
==
item
.
food_id
)
...
...
@@ -185,24 +141,27 @@ void TcpProtocol::OnFoodConsumedReceived(const MsgPackProtocol::FoodConsumeMessa
void
TcpProtocol
::
OnFoodDecayedReceived
(
const
MsgPackProtocol
::
FoodDecayMessage
&
msg
)
{
_
foodDecay
Messages
.
push_back
(
msg
);
_food
Vect
.
erase
(
std
::
remove_if
(
_food
Vect
.
begin
(),
_food
Vect
.
end
(),
[
&
msg
](
const
FoodItem
&
food
)
{
_
pending
Messages
.
push_back
(
std
::
make_unique
<
MsgPackProtocol
::
FoodDecayMessage
>
(
msg
)
);
_food
.
erase
(
std
::
remove_if
(
_food
.
begin
(),
_food
.
end
(),
[
&
msg
](
const
FoodItem
&
food
)
{
return
std
::
find
(
msg
.
food_ids
.
begin
(),
msg
.
food_ids
.
end
(),
food
.
guid
)
!=
msg
.
food_ids
.
end
();
}));
}
void
TcpProtocol
::
OnBotSpawnReceived
(
const
MsgPackProtocol
::
BotSpawnMessage
&
msg
)
{
_pendingMessages
.
push_back
(
std
::
make_unique
<
MsgPackProtocol
::
BotSpawnMessage
>
(
msg
));
_bots
.
push_back
(
msg
.
bot
);
}
void
TcpProtocol
::
OnBotKillReceived
(
const
MsgPackProtocol
::
BotKillMessage
&
msg
)
{
_pendingMessages
.
push_back
(
std
::
make_unique
<
MsgPackProtocol
::
BotKillMessage
>
(
msg
));
_bots
.
erase
(
std
::
remove_if
(
_bots
.
begin
(),
_bots
.
end
(),
[
msg
](
const
BotItem
&
bot
)
{
return
bot
.
guid
==
msg
.
victim_id
;
}));
}
void
TcpProtocol
::
OnBotMoveReceived
(
const
MsgPackProtocol
::
BotMoveMessage
&
msg
)
{
_pendingMessages
.
push_back
(
std
::
make_unique
<
MsgPackProtocol
::
BotMoveMessage
>
(
msg
));
for
(
auto
&
item
:
msg
.
items
)
{
auto
it
=
std
::
find_if
(
_bots
.
begin
(),
_bots
.
end
(),
[
item
](
const
BotItem
&
bot
)
{
return
bot
.
guid
==
item
.
bot_id
;
});
...
...
relayserver/TcpProtocol.h
View file @
797510c4
...
...
@@ -21,11 +21,10 @@ class TcpProtocol
void
SetFrameCompleteCallback
(
FrameCompleteCallback
callback
);
bool
Read
(
int
socket
);
const
MsgPackProtocol
::
GameInfoMessage
&
GetGameInfo
()
const
;
bool
GetWorldUpdate
(
msgpack
::
sbuffer
&
buf
)
const
;
bool
GetFoodSpawnMessages
(
msgpack
::
sbuffer
&
buf
)
const
;
bool
GetFoodConsumeMessages
(
msgpack
::
sbuffer
&
buf
)
const
;
bool
GetFoodDecayMessages
(
msgpack
::
sbuffer
&
buf
)
const
;
const
MsgPackProtocol
::
GameInfoMessage
&
GetGameInfo
()
const
{
return
_gameInfo
;
}
const
MsgPackProtocol
::
WorldUpdateMessage
&
GetWorldUpdate
()
const
{
return
_worldUpdate
;
}
const
std
::
vector
<
std
::
unique_ptr
<
MsgPackProtocol
::
Message
>>&
GetPendingMessages
()
const
{
return
_pendingMessages
;
}
private:
static
constexpr
const
size_t
SPATIAL_MAP_TILES_X
=
128
;
...
...
@@ -41,17 +40,16 @@ class TcpProtocol
size_t
_bufTail
=
0
;
FrameCompleteCallback
_frameCompleteCallback
;
std
::
vector
<
FoodItem
>
_foodVect
;
std
::
unique_ptr
<
FoodMap
>
_foodMap
;
MsgPackProtocol
::
GameInfoMessage
_gameInfo
;
MsgPackProtocol
::
WorldUpdateMessage
_worldUpdate
;
std
::
vector
<
FoodItem
>&
_food
;
std
::
vector
<
BotItem
>&
_bots
;
std
::
unique_ptr
<
FoodMap
>
_foodMap
;
std
::
vector
<
std
::
unique_ptr
<
MsgPackProtocol
::
Message
>>
_pendingMessages
;
std
::
vector
<
MsgPackProtocol
::
FoodSpawnMessage
>
_foodSpawnMessages
;
std
::
vector
<
MsgPackProtocol
::
FoodConsumeMessage
>
_foodConsumeMessages
;
std
::
vector
<
MsgPackProtocol
::
FoodDecayMessage
>
_foodDecayMessages
;
std
::
vector
<
BotItem
>
_bots
;
std
::
unique_ptr
<
SnakeSegmentMap
>
_segments
;
void
OnMessageReceived
(
const
char
*
data
,
size_t
count
);
void
OnGameInfoReceived
(
const
MsgPackProtocol
::
GameInfoMessage
&
msg
);
...
...
relayserver/WebsocketConnection.cpp
View file @
797510c4
...
...
@@ -9,28 +9,30 @@ WebsocketConnection::WebsocketConnection(uWS::WebSocket<uWS::SERVER> *websocket)
void
WebsocketConnection
::
FrameComplete
(
uint64_t
frame_id
,
const
TcpProtocol
&
proto
)
{
(
void
)
frame_id
;
if
(
!
_firstFrameSent
)
{
msgpack
::
sbuffer
buf
;
msgpack
::
pack
(
buf
,
proto
.
GetGameInfo
());
_websocket
->
send
(
buf
.
data
(),
buf
.
size
(),
uWS
::
OpCode
::
BINARY
);
buf
.
clear
();
proto
.
GetWorldUpdate
(
buf
);
_websocket
->
send
(
buf
.
data
(),
buf
.
size
(),
uWS
::
OpCode
::
BINARY
);
sendInitialData
(
proto
);
_firstFrameSent
=
true
;
}
else
msgpack
::
sbuffer
buf
;
for
(
auto
&
msg
:
proto
.
GetPendingMessages
())
{
msgpack
::
sbuffer
buf
;
proto
.
GetFoodSpawnMessages
(
buf
);
_websocket
->
send
(
buf
.
data
(),
buf
.
size
(),
uWS
::
OpCode
::
BINARY
);
buf
.
clear
();
proto
.
GetFoodConsumeMessages
(
buf
);
_websocket
->
send
(
buf
.
data
(),
buf
.
size
(),
uWS
::
OpCode
::
BINARY
);
buf
.
clear
();
proto
.
GetFoodDecayMessages
(
buf
);
msg
->
pack
(
buf
);
_websocket
->
send
(
buf
.
data
(),
buf
.
size
(),
uWS
::
OpCode
::
BINARY
);
}
}
void
WebsocketConnection
::
sendInitialData
(
const
TcpProtocol
&
proto
)
{
msgpack
::
sbuffer
buf
;
buf
.
clear
();
proto
.
GetGameInfo
().
pack
(
buf
);
_websocket
->
send
(
buf
.
data
(),
buf
.
size
(),
uWS
::
OpCode
::
BINARY
);
buf
.
clear
();
proto
.
GetWorldUpdate
().
pack
(
buf
);
_websocket
->
send
(
buf
.
data
(),
buf
.
size
(),
uWS
::
OpCode
::
BINARY
);
}
relayserver/WebsocketConnection.h
View file @
797510c4
...
...
@@ -14,4 +14,6 @@ class WebsocketConnection
uWS
::
WebSocket
<
uWS
::
SERVER
>
*
_websocket
;
bool
_firstFrameSent
=
false
;
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