Commit 658c7ab1 authored by Hubert Denkmair's avatar Hubert Denkmair

add WebsocketConnection class

parent 59a025b0
......@@ -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(
......
......@@ -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();
_websocketConnections[socket.GetFileDescriptor()] = con;
_connections.emplace(socket.GetFileDescriptor(), WebsocketConnection { 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 = _websocketConnections.find(socket.GetFileDescriptor());
if (it == _websocketConnections.end())
auto it = _connections.find(socket.GetFileDescriptor());
if (it == _connections.end())
{
return false;
}
it->second->eof();
_websocketConnections.erase(socket.GetFileDescriptor());
it->second.Eof();
_connections.erase(socket.GetFileDescriptor());
return true;
}
bool RelayServer::OnDataAvailable(TcpSocket &socket)
{
auto it = _websocketConnections.find(socket.GetFileDescriptor());
if (it == _websocketConnections.end())
auto it = _connections.find(socket.GetFileDescriptor());
if (it == _connections.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;
}
......
......@@ -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, WebsocketServer::connection_ptr> _websocketConnections;
std::map<int, WebsocketConnection> _connections;
bool OnConnectionEstablished(TcpSocket &socket);
bool OnConnectionClosed(TcpSocket &socket);
......
#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)
{
}
#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;
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment