Commit 51a79c5e authored by Hubert Denkmair's avatar Hubert Denkmair

add webbsocketpp skeleton

parent a36af0e4
CMakeLists.txt.user
cmake_minimum_required (VERSION 3.2) cmake_minimum_required (VERSION 3.2)
project (RelayServer VERSION 0.1 LANGUAGES CXX)
add_subdirectory(lib/TcpServer/TcpServer)
set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "-Wall -pedantic") set(CMAKE_CXX_FLAGS "-Wall -pedantic")
add_subdirectory(lib/TcpServer/TcpServer)
find_package(Eigen3 REQUIRED) add_subdirectory(relayserver)
include_directories(${EIGEN3_INCLUDE_DIR})
include_directories(
${CMAKE_PROJECT_NAME}
lib/msgpack-c/include/
lib/TcpServer/TcpServer/include/
)
add_executable(
${CMAKE_PROJECT_NAME}
relayserver/main.cpp
)
target_link_libraries(
${CMAKE_PROJECT_NAME}
tcpserver
)
set(PROJECT_NAME RelayServer)
project (${PROJECT_NAME} VERSION 0.1 LANGUAGES CXX)
find_package(Eigen3 REQUIRED)
include_directories(${EIGEN3_INCLUDE_DIR})
include_directories(
${PROJECT_NAME}
lib/msgpack-c/include/
lib/TcpServer/TcpServer/include/
)
add_executable(
${PROJECT_NAME}
main.cpp
RelayServer.h RelayServer.cpp
)
target_link_libraries(
${PROJECT_NAME}
tcpserver
)
#include "RelayServer.h"
#include <iostream>
RelayServer::RelayServer()
{
_tcpServer.AddConnectionEstablishedListener(
[this](TcpSocket& socket)
{
return OnConnectionEstablished(socket);
}
);
_tcpServer.AddConnectionClosedListener(
[this](TcpSocket& socket)
{
return OnConnectionClosed(socket);
}
);
_tcpServer.AddDataAvailableListener(
[this](TcpSocket& socket)
{
return OnDataAvailable(socket);
}
);
_websocketServer.clear_access_channels(websocketpp::log::alevel::all);
_websocketServer.set_access_channels(websocketpp::log::alevel::connect);
_websocketServer.set_access_channels(websocketpp::log::alevel::disconnect);
_websocketServer.set_access_channels(websocketpp::log::alevel::app);
// websocketServer.set_message_handler()...
}
int RelayServer::Run()
{
if(!_tcpServer.Listen(9009))
{
return -1;
}
while(true)
{
_tcpServer.Poll(1000);
}
}
bool RelayServer::OnConnectionEstablished(TcpSocket &socket)
{
std::cerr << "connection established to " << socket.GetPeer() << std::endl;
auto con = _websocketServer.get_connection();
con->set_write_handler(
[&socket](websocketpp::connection_hdl, char const* data, size_t size)
{
std::cerr << "reply" << std::endl;
if (socket.Write(data, size, false) != static_cast<ssize_t>(size))
{
return websocketpp::transport::iostream::error::make_error_code(
websocketpp::transport::iostream::error::general
);
}
return websocketpp::lib::error_code();
}
);
con->set_shutdown_handler(
[&socket](websocketpp::connection_hdl)
{
socket.Close();
return websocketpp::lib::error_code();
}
);
con->start();
_websocketConnections[socket.GetFileDescriptor()] = con;
return true;
}
bool RelayServer::OnConnectionClosed(TcpSocket &socket)
{
std::cerr << "connection to " << socket.GetPeer() << " closed." << std::endl;
auto it = _websocketConnections.find(socket.GetFileDescriptor());
if (it == _websocketConnections.end())
{
return false;
}
it->second->eof();
_websocketConnections.erase(socket.GetFileDescriptor());
return true;
}
bool RelayServer::OnDataAvailable(TcpSocket &socket)
{
auto it = _websocketConnections.find(socket.GetFileDescriptor());
if (it == _websocketConnections.end())
{
return false;
}
char data[1024];
ssize_t count = socket.Read(data, sizeof(data));
if (count > 0)
{
it->second->read_some(data, static_cast<size_t>(count));
}
return true;
}
#pragma once
#include <map>
#include <TcpServer/TcpServer.h>
#include <websocketpp/config/core.hpp>
#include <websocketpp/server.hpp>
class RelayServer
{
public:
RelayServer();
int Run();
private:
TcpServer _tcpServer;
typedef websocketpp::server<websocketpp::config::core> WebsocketServer;
WebsocketServer _websocketServer;
std::map<int, WebsocketServer::connection_ptr> _websocketConnections;
bool OnConnectionEstablished(TcpSocket &socket);
bool OnConnectionClosed(TcpSocket &socket);
bool OnDataAvailable(TcpSocket &socket);
};
#include "RelayServer.h"
static RelayServer server;
int main()
{
return server.Run();
}
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