Commit 55375cdf authored by Hubert Denkmair's avatar Hubert Denkmair
Browse files

receive tcp frames

parent 29b47103
...@@ -14,6 +14,7 @@ add_executable( ...@@ -14,6 +14,7 @@ add_executable(
${PROJECT_NAME} ${PROJECT_NAME}
main.cpp main.cpp
RelayServer.h RelayServer.cpp RelayServer.h RelayServer.cpp
TcpProtocol.h TcpProtocol.cpp
) )
target_link_libraries( target_link_libraries(
......
#include "RelayServer.h" #include "RelayServer.h"
#include <iostream> #include <iostream>
#include <unistd.h> #include <unistd.h>
#include <TcpServer/TcpSocket.h>
#include <TcpServer/EPoll.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <TcpServer/TcpSocket.h>
#include <TcpServer/EPoll.h>
RelayServer::RelayServer() RelayServer::RelayServer()
{ {
...@@ -30,6 +30,13 @@ RelayServer::RelayServer() ...@@ -30,6 +30,13 @@ RelayServer::RelayServer()
} }
); );
_tcpProtocol.SetMessageReceivedCallback(
[](std::vector<uint8_t> data)
{
std::cout << "received " << data.size() << " bytes." << std::endl;
}
);
_websocketServer.clear_access_channels(websocketpp::log::alevel::all); _websocketServer.clear_access_channels(websocketpp::log::alevel::all);
_websocketServer.set_access_channels(websocketpp::log::alevel::connect); _websocketServer.set_access_channels(websocketpp::log::alevel::connect);
_websocketServer.set_access_channels(websocketpp::log::alevel::disconnect); _websocketServer.set_access_channels(websocketpp::log::alevel::disconnect);
...@@ -65,7 +72,7 @@ int RelayServer::Run() ...@@ -65,7 +72,7 @@ int RelayServer::Run()
{ {
if (ev.data.fd == _clientSocket) if (ev.data.fd == _clientSocket)
{ {
OnServerDataReceived(ev); return _tcpProtocol.Read(_clientSocket);
} }
else else
{ {
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include <TcpServer/TcpServer.h> #include <TcpServer/TcpServer.h>
#include <websocketpp/config/core.hpp> #include <websocketpp/config/core.hpp>
#include <websocketpp/server.hpp> #include <websocketpp/server.hpp>
#include "TcpProtocol.h"
class RelayServer class RelayServer
{ {
...@@ -14,6 +15,8 @@ class RelayServer ...@@ -14,6 +15,8 @@ class RelayServer
private: private:
int _clientSocket; int _clientSocket;
TcpServer _tcpServer; TcpServer _tcpServer;
TcpProtocol _tcpProtocol;
typedef websocketpp::server<websocketpp::config::core> WebsocketServer; typedef websocketpp::server<websocketpp::config::core> WebsocketServer;
WebsocketServer _websocketServer; WebsocketServer _websocketServer;
......
#include "TcpProtocol.h"
#include <stdint.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <array>
void TcpProtocol::SetMessageReceivedCallback(TcpProtocol::MessageReceivedCallback callback)
{
_messageReceivedCallback = callback;
}
bool TcpProtocol::Read(int socket)
{
std::array<uint8_t, 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)
{
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 (_buf.size() >= _awaitedSize)
{
_messageReceivedCallback(std::vector<uint8_t>(_buf.begin(), _buf.begin()+_awaitedSize));
_buf.erase(_buf.begin(), _buf.begin()+_awaitedSize);
_awaitedSize = 0;
}
return true;
}
#pragma once
#include <stddef.h>
#include <stdint.h>
#include <functional>
#include <deque>
#include <vector>
class TcpProtocol
{
public:
typedef std::function<void(std::vector<uint8_t> data)> MessageReceivedCallback;
void SetMessageReceivedCallback(MessageReceivedCallback callback);
bool Read(int socket);
private:
size_t _awaitedSize = 0;
std::deque<uint8_t> _buf;
MessageReceivedCallback _messageReceivedCallback;
};
Supports Markdown
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