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

add WebsocketConnection class

parent 59a025b0
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -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(
+16 −11
Original line number Diff line number Diff line
@@ -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;
}
+3 −3
Original line number Diff line number Diff line
@@ -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);
+21 −0
Original line number Diff line number Diff line
#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)
{

}
+23 −0
Original line number Diff line number Diff line
#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;

};