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
ca602b9c
Commit
ca602b9c
authored
Apr 09, 2018
by
Hubert Denkmair
Browse files
basic msgpack decode
parent
55375cdf
Changes
4
Hide whitespace changes
Inline
Side-by-side
relayserver/RelayServer.cpp
View file @
ca602b9c
...
...
@@ -6,6 +6,7 @@
#include <arpa/inet.h>
#include <TcpServer/TcpSocket.h>
#include <TcpServer/EPoll.h>
#include <msgpack.hpp>
RelayServer
::
RelayServer
()
{
...
...
@@ -30,8 +31,8 @@ RelayServer::RelayServer()
}
);
_tcpProtocol
.
SetMessageReceivedCallback
(
[](
std
::
vector
<
uint8_t
>
data
)
msg
.
SetMessageReceivedCallback
(
[](
std
::
vector
<
char
>
data
)
{
std
::
cout
<<
"received "
<<
data
.
size
()
<<
" bytes."
<<
std
::
endl
;
}
...
...
@@ -72,7 +73,7 @@ int RelayServer::Run()
{
if
(
ev
.
data
.
fd
==
_clientSocket
)
{
return
_tcpProtocol
.
Read
(
_clientSocket
);
return
msg
.
Read
(
_clientSocket
);
}
else
{
...
...
relayserver/RelayServer.h
View file @
ca602b9c
...
...
@@ -15,7 +15,7 @@ class RelayServer
private:
int
_clientSocket
;
TcpServer
_tcpServer
;
TcpProtocol
_tcpProtocol
;
TcpProtocol
msg
;
typedef
websocketpp
::
server
<
websocketpp
::
config
::
core
>
WebsocketServer
;
...
...
relayserver/TcpProtocol.cpp
View file @
ca602b9c
#include "TcpProtocol.h"
#include <stdint.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <array>
#include <msgpack.hpp>
#include <iostream>
void
TcpProtocol
::
SetMessageReceivedCallback
(
TcpProtocol
::
MessageReceivedCallback
callback
)
{
...
...
@@ -11,24 +12,85 @@ void TcpProtocol::SetMessageReceivedCallback(TcpProtocol::MessageReceivedCallbac
bool
TcpProtocol
::
Read
(
int
socket
)
{
std
::
array
<
uint8_t
,
1024
>
readbuf
;
std
::
array
<
char
,
1024
>
readbuf
;
ssize_t
bytesRead
=
read
(
socket
,
readbuf
.
data
(),
readbuf
.
size
());
if
(
bytesRead
<=
0
)
{
return
false
;
}
_buf
.
insert
(
_buf
.
end
(),
&
readbuf
[
0
],
&
readbuf
[
static_cast
<
size_t
>
(
bytesRead
)]);
if
(
_awaitedSize
==
0
)
while
(
true
)
{
if
(
_buf
.
size
()
<
4
)
{
return
true
;
}
_awaitedSize
=
(
_buf
[
0
]
<<
24
|
_buf
[
1
]
<<
16
|
_buf
[
2
]
<<
8
|
_buf
[
3
]);
_buf
.
erase
(
_buf
.
begin
(),
_buf
.
begin
()
+
4
);
if
((
_awaitedSize
==
0
)
&&
(
_buf
.
size
()
>=
4
))
{
_awaitedSize
=
static_cast
<
uint8_t
>
(
_buf
[
0
])
<<
24
|
static_cast
<
uint8_t
>
(
_buf
[
1
])
<<
16
|
static_cast
<
uint8_t
>
(
_buf
[
2
])
<<
8
|
static_cast
<
uint8_t
>
(
_buf
[
3
])
<<
0
;
_buf
.
erase
(
_buf
.
begin
(),
_buf
.
begin
()
+
4
);
}
else
if
((
_awaitedSize
>
0
)
&&
(
_buf
.
size
()
>=
_awaitedSize
))
{
std
::
vector
<
char
>
v
(
_buf
.
begin
(),
_buf
.
begin
()
+
_awaitedSize
);
OnMessageReceived
(
v
);
_buf
.
erase
(
_buf
.
begin
(),
_buf
.
begin
()
+
_awaitedSize
);
_awaitedSize
=
0
;
}
else
{
return
true
;
}
}
}
void
TcpProtocol
::
OnMessageReceived
(
std
::
vector
<
char
>
&
data
)
{
_messageReceivedCallback
(
data
);
msgpack
::
unpacker
pac
;
msgpack
::
object_handle
obj
;
pac
.
reserve_buffer
(
data
.
size
());
memcpy
(
pac
.
buffer
(),
data
.
data
(),
data
.
size
());
pac
.
buffer_consumed
(
data
.
size
());
if
(
!
pac
.
next
(
obj
))
{
return
;
}
if
(
obj
.
get
().
type
!=
msgpack
::
type
::
ARRAY
)
{
return
;
}
auto
arr
=
obj
.
get
().
via
.
array
;
if
(
arr
.
size
<
2
)
{
return
;
}
uint64_t
version
=
arr
.
ptr
[
0
].
via
.
u64
;
uint64_t
message_type
=
arr
.
ptr
[
1
].
via
.
u64
;
if
(
_buf
.
size
()
>=
_awaitedSize
)
std
::
cerr
<<
"version: "
<<
version
<<
" message type: "
<<
message_type
<<
std
::
endl
;
switch
(
message_type
)
{
_messageReceivedCallback
(
std
::
vector
<
uint8_t
>
(
_buf
.
begin
(),
_buf
.
begin
()
+
_awaitedSize
));
_buf
.
erase
(
_buf
.
begin
(),
_buf
.
begin
()
+
_awaitedSize
);
_awaitedSize
=
0
;
case
0x00
:
if
(
arr
.
size
<
5
)
{
return
;
}
std
::
cerr
<<
"size_x "
<<
arr
.
ptr
[
2
].
via
.
f64
<<
" size_y "
<<
arr
.
ptr
[
3
].
via
.
f64
<<
" decrate "
<<
arr
.
ptr
[
4
].
via
.
f64
<<
std
::
endl
;
break
;
case
0x01
:
std
::
cerr
<<
"world update"
<<
std
::
endl
;
break
;
case
0x10
:
std
::
cerr
<<
"tick"
<<
std
::
endl
;
break
;
case
0x20
:
std
::
cerr
<<
"bot spawn"
<<
std
::
endl
;
break
;
case
0x21
:
std
::
cerr
<<
"bot kill"
<<
std
::
endl
;
break
;
case
0x22
:
std
::
cerr
<<
"bot move"
<<
std
::
endl
;
break
;
case
0x30
:
std
::
cerr
<<
"food spawn"
<<
std
::
endl
;
break
;
case
0x31
:
std
::
cerr
<<
"food consume"
<<
std
::
endl
;
break
;
case
0x32
:
std
::
cerr
<<
"food decay"
<<
std
::
endl
;
break
;
}
return
true
;
}
relayserver/TcpProtocol.h
View file @
ca602b9c
...
...
@@ -8,12 +8,14 @@
class
TcpProtocol
{
public:
typedef
std
::
function
<
void
(
std
::
vector
<
uint8_t
>
data
)
>
MessageReceivedCallback
;
typedef
std
::
function
<
void
(
std
::
vector
<
char
>&
)
>
MessageReceivedCallback
;
void
SetMessageReceivedCallback
(
MessageReceivedCallback
callback
);
bool
Read
(
int
socket
);
private:
size_t
_awaitedSize
=
0
;
std
::
deque
<
uint8_t
>
_buf
;
std
::
deque
<
char
>
_buf
;
MessageReceivedCallback
_messageReceivedCallback
;
void
OnMessageReceived
(
std
::
vector
<
char
>&
data
);
};
Write
Preview
Supports
Markdown
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