From df0adfb33c376adb386a2b1df46ff79848a563e1 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Mon, 27 Sep 2021 15:35:09 +0200 Subject: [PATCH] added new tcp code --- src/fsfw/osal/common/TcpTmTcServer.cpp | 77 ++++++++++++++++++++------ 1 file changed, 61 insertions(+), 16 deletions(-) diff --git a/src/fsfw/osal/common/TcpTmTcServer.cpp b/src/fsfw/osal/common/TcpTmTcServer.cpp index c819f9e7e..06cace2dd 100644 --- a/src/fsfw/osal/common/TcpTmTcServer.cpp +++ b/src/fsfw/osal/common/TcpTmTcServer.cpp @@ -5,6 +5,7 @@ #include "TcpTmTcBridge.h" #include "tcpipHelpers.h" +#include "fsfw/tasks/TaskFactory.h" #include "fsfw/container/SharedRingBuffer.h" #include "fsfw/ipc/MessageQueueSenderIF.h" #include "fsfw/ipc/MutexGuard.h" @@ -19,6 +20,7 @@ #elif defined(PLATFORM_UNIX) #include #endif +#include #ifndef FSFW_TCP_RECV_WIRETAPPING_ENABLED #define FSFW_TCP_RECV_WIRETAPPING_ENABLED 0 @@ -28,8 +30,8 @@ const std::string TcpTmTcServer::DEFAULT_SERVER_PORT = tcpip::DEFAULT_SERVER_POR TcpTmTcServer::TcpTmTcServer(object_id_t objectId, object_id_t tmtcTcpBridge, size_t receptionBufferSize, std::string customTcpServerPort): - SystemObject(objectId), tmtcBridgeId(tmtcTcpBridge), - tcpPort(customTcpServerPort), receptionBuffer(receptionBufferSize) { + SystemObject(objectId), tmtcBridgeId(tmtcTcpBridge), + tcpPort(customTcpServerPort), receptionBuffer(receptionBufferSize) { if(tcpPort == "") { tcpPort = DEFAULT_SERVER_PORT; } @@ -148,24 +150,67 @@ ReturnValue_t TcpTmTcServer::initializeAfterTaskCreation() { void TcpTmTcServer::handleServerOperation(socket_t connSocket) { int retval = 0; - do { - // Read all telecommands sent by the client - retval = recv(connSocket, - reinterpret_cast(receptionBuffer.data()), - receptionBuffer.capacity(), - tcpFlags); - if (retval > 0) { - handleTcReception(retval); + using namespace std::chrono_literals; + + // Receive until the peer shuts down the connection, use select to do this + fd_set rfds; + fd_set efds; + + FD_ZERO(&rfds); + FD_SET(connSocket, &rfds); + + FD_ZERO(&efds); + FD_SET(connSocket, &efds); + + timeval tv; + tv.tv_sec = 1; + tv.tv_usec = 0; + + int nfds = connSocket + 1; + + // do { + // // Read all telecommands sent by the client + // retval = recv( + // connSocket, + // reinterpret_cast(receptionBuffer.data()), + // receptionBuffer.capacity(), + // tcpFlags + // ); + // if (retval > 0) { + // handleTcReception(retval); + // } + // else if(retval == 0) { + // // Client has finished sending telecommands, send telemetry now + // handleTmSending(connSocket); + // } + // else { + // // Should not happen + // tcpip::handleError(tcpip::Protocol::TCP, tcpip::ErrorSources::RECV_CALL); + // } + // } while(retval > 0); + while (true) { + uint32_t index = 0; + int retval = select(nfds, &rfds, nullptr, &efds, &tv); + if(retval < 0) { + // client might have shut down connection? } - else if(retval == 0) { - // Client has finished sending telecommands, send telemetry now - handleTmSending(connSocket); + else if(retval > 0) { + if(FD_ISSET(connSocket, &rfds)) { + // data available + //int result = receiveData(); + //if(result == 0) { + // break; + //} + } + if(FD_ISSET(connSocket, &efds)) { + //spdlog::error("{}: Exception detected on receive FD", tcpip::SERVER_PR); + } } else { - // Should not happen - tcpip::handleError(tcpip::Protocol::TCP, tcpip::ErrorSources::RECV_CALL); + // no data available + TaskFactory::delayTask(500); } - } while(retval > 0); + } } ReturnValue_t TcpTmTcServer::handleTcReception(size_t bytesRecvd) {