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
658c7ab1
Commit
658c7ab1
authored
Apr 12, 2018
by
Hubert Denkmair
Browse files
add WebsocketConnection class
parent
59a025b0
Changes
5
Hide whitespace changes
Inline
Side-by-side
relayserver/CMakeLists.txt
View file @
658c7ab1
...
...
@@ -16,6 +16,7 @@ add_executable(
RelayServer.h RelayServer.cpp
TcpProtocol.h TcpProtocol.cpp
SpatialMap.h MsgPackProtocol.h
WebsocketConnection.h WebsocketConnection.cpp
)
target_link_libraries
(
...
...
relayserver/RelayServer.cpp
View file @
658c7ab1
...
...
@@ -31,10 +31,15 @@ RelayServer::RelayServer()
}
);
msg
.
SetFrameCompleteCallback
(
[](
uint64_t
frame_id
)
_tcpProtocol
.
SetFrameCompleteCallback
(
[
this
](
uint64_t
frame_id
)
{
std
::
cout
<<
"frame "
<<
frame_id
<<
" complete."
<<
std
::
endl
;
for
(
auto
&
it
:
_connections
)
{
it
.
second
.
FrameComplete
(
frame_id
,
_tcpProtocol
);
}
}
);
...
...
@@ -73,7 +78,7 @@ int RelayServer::Run()
{
if
(
ev
.
data
.
fd
==
_clientSocket
)
{
return
msg
.
Read
(
_clientSocket
);
return
_tcpProtocol
.
Read
(
_clientSocket
);
}
else
{
...
...
@@ -112,7 +117,7 @@ bool RelayServer::OnConnectionEstablished(TcpSocket &socket)
);
con
->
start
();
_
w
ebsocketConnection
s
[
socket
.
GetFileDescriptor
()
]
=
con
;
_
connections
.
emplace
(
socket
.
GetFileDescriptor
(),
W
ebsocketConnection
{
socket
.
GetFileDescriptor
()
,
con
})
;
return
true
;
}
...
...
@@ -120,21 +125,21 @@ bool RelayServer::OnConnectionClosed(TcpSocket &socket)
{
std
::
cerr
<<
"connection to "
<<
socket
.
GetPeer
()
<<
" closed."
<<
std
::
endl
;
auto
it
=
_
websocketC
onnections
.
find
(
socket
.
GetFileDescriptor
());
if
(
it
==
_
websocketC
onnections
.
end
())
auto
it
=
_
c
onnections
.
find
(
socket
.
GetFileDescriptor
());
if
(
it
==
_
c
onnections
.
end
())
{
return
false
;
}
it
->
second
->
e
of
();
_
websocketC
onnections
.
erase
(
socket
.
GetFileDescriptor
());
it
->
second
.
E
of
();
_
c
onnections
.
erase
(
socket
.
GetFileDescriptor
());
return
true
;
}
bool
RelayServer
::
OnDataAvailable
(
TcpSocket
&
socket
)
{
auto
it
=
_
websocketC
onnections
.
find
(
socket
.
GetFileDescriptor
());
if
(
it
==
_
websocketC
onnections
.
end
())
auto
it
=
_
c
onnections
.
find
(
socket
.
GetFileDescriptor
());
if
(
it
==
_
c
onnections
.
end
())
{
return
false
;
}
...
...
@@ -143,7 +148,7 @@ bool RelayServer::OnDataAvailable(TcpSocket &socket)
ssize_t
count
=
socket
.
Read
(
data
,
sizeof
(
data
));
if
(
count
>
0
)
{
it
->
second
->
read_some
(
data
,
static_cast
<
size_t
>
(
count
));
it
->
second
.
DataReceived
(
data
,
static_cast
<
size_t
>
(
count
));
}
return
true
;
}
...
...
relayserver/RelayServer.h
View file @
658c7ab1
...
...
@@ -5,6 +5,7 @@
#include <websocketpp/config/core.hpp>
#include <websocketpp/server.hpp>
#include "TcpProtocol.h"
#include "WebsocketConnection.h"
class
RelayServer
{
...
...
@@ -15,12 +16,11 @@ class RelayServer
private:
int
_clientSocket
;
TcpServer
_tcpServer
;
TcpProtocol
msg
;
TcpProtocol
_tcpProtocol
;
typedef
websocketpp
::
server
<
websocketpp
::
config
::
core
>
WebsocketServer
;
WebsocketServer
_websocketServer
;
std
::
map
<
int
,
Websocket
Server
::
connection_ptr
>
_websocketC
onnections
;
std
::
map
<
int
,
Websocket
Connection
>
_c
onnections
;
bool
OnConnectionEstablished
(
TcpSocket
&
socket
);
bool
OnConnectionClosed
(
TcpSocket
&
socket
);
...
...
relayserver/WebsocketConnection.cpp
0 → 100644
View file @
658c7ab1
#include "WebsocketConnection.h"
WebsocketConnection
::
WebsocketConnection
(
int
socket
,
WebsocketServer
::
connection_ptr
websocket
)
:
_socket
(
socket
),
_websocket
(
websocket
)
{
}
void
WebsocketConnection
::
Eof
()
{
_websocket
->
eof
();
}
void
WebsocketConnection
::
DataReceived
(
const
char
*
data
,
size_t
count
)
{
_websocket
->
read_some
(
data
,
count
);
}
void
WebsocketConnection
::
FrameComplete
(
uint64_t
frame_id
,
const
TcpProtocol
&
proto
)
{
}
relayserver/WebsocketConnection.h
0 → 100644
View file @
658c7ab1
#pragma once
#include <websocketpp/config/core.hpp>
#include <websocketpp/server.hpp>
class
TcpProtocol
;
class
WebsocketConnection
{
public:
typedef
websocketpp
::
server
<
websocketpp
::
config
::
core
>
WebsocketServer
;
WebsocketConnection
(
int
socket
,
WebsocketServer
::
connection_ptr
websocket
);
void
Eof
();
void
DataReceived
(
const
char
*
data
,
size_t
count
);
void
FrameComplete
(
uint64_t
frame_id
,
const
TcpProtocol
&
proto
);
private:
int
_socket
;
WebsocketServer
::
connection_ptr
_websocket
;
bool
_firstFrameSent
=
false
;
};
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