2021-09-28 15:01:01 +02:00
|
|
|
#include "TcpTmTcServer.h"
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
#include "TcpTmTcBridge.h"
|
|
|
|
#include "fsfw/FSFW.h"
|
2021-07-13 21:02:53 +02:00
|
|
|
#include "fsfw/container/SharedRingBuffer.h"
|
2022-02-02 10:29:30 +01:00
|
|
|
#include "fsfw/globalfunctions/arrayprinter.h"
|
2021-07-13 21:02:53 +02:00
|
|
|
#include "fsfw/ipc/MessageQueueSenderIF.h"
|
|
|
|
#include "fsfw/ipc/MutexGuard.h"
|
|
|
|
#include "fsfw/objectmanager/ObjectManager.h"
|
2022-02-02 10:29:30 +01:00
|
|
|
#include "fsfw/platform.h"
|
2021-07-13 21:02:53 +02:00
|
|
|
#include "fsfw/serviceinterface/ServiceInterface.h"
|
2022-02-02 10:29:30 +01:00
|
|
|
#include "fsfw/tasks/TaskFactory.h"
|
|
|
|
#include "fsfw/tmtcservices/SpacePacketParser.h"
|
2021-07-13 21:02:53 +02:00
|
|
|
#include "fsfw/tmtcservices/TmTcMessage.h"
|
2022-02-02 10:29:30 +01:00
|
|
|
#include "tcpipHelpers.h"
|
2021-03-11 14:47:47 +01:00
|
|
|
|
2021-05-12 16:47:53 +02:00
|
|
|
#ifdef PLATFORM_WIN
|
2021-03-11 14:47:47 +01:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
2021-05-12 16:47:53 +02:00
|
|
|
#elif defined(PLATFORM_UNIX)
|
2021-03-21 12:51:28 +01:00
|
|
|
#include <netdb.h>
|
2022-05-09 10:57:23 +02:00
|
|
|
|
|
|
|
#include <utility>
|
2021-03-21 00:30:33 +01:00
|
|
|
#endif
|
2021-05-05 12:59:42 +02:00
|
|
|
|
2021-08-09 18:12:25 +02:00
|
|
|
const std::string TcpTmTcServer::DEFAULT_SERVER_PORT = tcpip::DEFAULT_SERVER_PORT;
|
2021-03-11 14:47:47 +01:00
|
|
|
|
2021-05-05 12:59:42 +02:00
|
|
|
TcpTmTcServer::TcpTmTcServer(object_id_t objectId, object_id_t tmtcTcpBridge,
|
2022-02-02 10:29:30 +01:00
|
|
|
size_t receptionBufferSize, size_t ringBufferSize,
|
|
|
|
std::string customTcpServerPort, ReceptionModes receptionMode)
|
|
|
|
: SystemObject(objectId),
|
|
|
|
tmtcBridgeId(tmtcTcpBridge),
|
|
|
|
receptionMode(receptionMode),
|
2022-05-09 10:57:23 +02:00
|
|
|
tcpConfig(std::move(customTcpServerPort)),
|
2022-02-02 10:29:30 +01:00
|
|
|
receptionBuffer(receptionBufferSize),
|
|
|
|
ringBuffer(ringBufferSize, true) {}
|
2021-03-11 14:47:47 +01:00
|
|
|
|
2021-03-21 00:30:33 +01:00
|
|
|
ReturnValue_t TcpTmTcServer::initialize() {
|
2022-02-02 10:29:30 +01:00
|
|
|
using namespace tcpip;
|
2021-03-21 00:30:33 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
ReturnValue_t result = TcpIpBase::initialize();
|
2022-08-16 01:08:26 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-02-02 10:29:30 +01:00
|
|
|
return result;
|
|
|
|
}
|
2021-03-21 00:30:33 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
switch (receptionMode) {
|
|
|
|
case (ReceptionModes::SPACE_PACKETS): {
|
|
|
|
spacePacketParser = new SpacePacketParser(validPacketIds);
|
|
|
|
if (spacePacketParser == nullptr) {
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2021-09-28 17:26:02 +02:00
|
|
|
#if defined PLATFORM_UNIX
|
2022-02-02 10:29:30 +01:00
|
|
|
tcpConfig.tcpFlags |= MSG_DONTWAIT;
|
2021-09-28 17:26:02 +02:00
|
|
|
#endif
|
2021-09-28 15:01:01 +02:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
|
|
|
tcStore = ObjectManager::instance()->get<StorageManagerIF>(objects::TC_STORE);
|
|
|
|
if (tcStore == nullptr) {
|
2021-05-05 12:59:42 +02:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::error << "TcpTmTcServer::initialize: TC store uninitialized!" << std::endl;
|
2021-05-05 12:59:42 +02:00
|
|
|
#else
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::printError("TcpTmTcServer::initialize: TC store uninitialized!\n");
|
2021-05-05 12:59:42 +02:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
return ObjectManagerIF::CHILD_INIT_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmtcBridge = ObjectManager::instance()->get<TcpTmTcBridge>(tmtcBridgeId);
|
|
|
|
|
|
|
|
int retval = 0;
|
|
|
|
struct addrinfo* addrResult = nullptr;
|
|
|
|
struct addrinfo hints = {};
|
|
|
|
|
|
|
|
hints.ai_family = AF_INET;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
|
|
hints.ai_flags = AI_PASSIVE;
|
|
|
|
|
|
|
|
// Listen to all addresses (0.0.0.0) by using AI_PASSIVE in the hint flags
|
|
|
|
retval = getaddrinfo(nullptr, tcpConfig.tcpPort.c_str(), &hints, &addrResult);
|
|
|
|
if (retval != 0) {
|
|
|
|
handleError(Protocol::TCP, ErrorSources::GETADDRINFO_CALL);
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open TCP (stream) socket
|
|
|
|
listenerTcpSocket =
|
|
|
|
socket(addrResult->ai_family, addrResult->ai_socktype, addrResult->ai_protocol);
|
|
|
|
if (listenerTcpSocket == INVALID_SOCKET) {
|
2021-03-11 14:47:47 +01:00
|
|
|
freeaddrinfo(addrResult);
|
2022-02-02 10:29:30 +01:00
|
|
|
handleError(Protocol::TCP, ErrorSources::SOCKET_CALL);
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2021-03-11 14:47:47 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
// Bind to the address found by getaddrinfo
|
|
|
|
retval = bind(listenerTcpSocket, addrResult->ai_addr, static_cast<int>(addrResult->ai_addrlen));
|
|
|
|
if (retval == SOCKET_ERROR) {
|
|
|
|
freeaddrinfo(addrResult);
|
|
|
|
handleError(Protocol::TCP, ErrorSources::BIND_CALL);
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2021-03-11 14:47:47 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
freeaddrinfo(addrResult);
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::OK;
|
2021-03-11 14:47:47 +01:00
|
|
|
}
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
TcpTmTcServer::~TcpTmTcServer() { closeSocket(listenerTcpSocket); }
|
|
|
|
|
2022-05-09 10:57:23 +02:00
|
|
|
[[noreturn]] ReturnValue_t TcpTmTcServer::performOperation(uint8_t opCode) {
|
2022-02-02 10:29:30 +01:00
|
|
|
using namespace tcpip;
|
|
|
|
// If a connection is accepted, the corresponding socket will be assigned to the new socket
|
|
|
|
socket_t connSocket = 0;
|
2022-05-17 13:15:32 +02:00
|
|
|
sockaddr clientSockAddr = {};
|
|
|
|
socklen_t connectorSockAddrLen = 0;
|
2022-02-02 10:29:30 +01:00
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
// Listen for connection requests permanently for lifetime of program
|
|
|
|
while (true) {
|
|
|
|
retval = listen(listenerTcpSocket, tcpConfig.tcpBacklog);
|
|
|
|
if (retval == SOCKET_ERROR) {
|
|
|
|
handleError(Protocol::TCP, ErrorSources::LISTEN_CALL, 500);
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-11 14:47:47 +01:00
|
|
|
|
2022-05-17 13:15:32 +02:00
|
|
|
connSocket = accept(listenerTcpSocket, &clientSockAddr, &connectorSockAddrLen);
|
2021-03-11 14:47:47 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
if (connSocket == INVALID_SOCKET) {
|
|
|
|
handleError(Protocol::TCP, ErrorSources::ACCEPT_CALL, 500);
|
|
|
|
closeSocket(connSocket);
|
|
|
|
continue;
|
|
|
|
};
|
2021-03-11 14:47:47 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
handleServerOperation(connSocket);
|
2021-05-05 12:59:42 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
// Done, shut down connection and go back to listening for client requests
|
|
|
|
retval = shutdown(connSocket, SHUT_BOTH);
|
|
|
|
if (retval != 0) {
|
|
|
|
handleError(Protocol::TCP, ErrorSources::SHUTDOWN_CALL);
|
2021-05-05 12:59:42 +02:00
|
|
|
}
|
2022-05-17 13:15:32 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
closeSocket(connSocket);
|
|
|
|
connSocket = 0;
|
|
|
|
}
|
2021-05-05 12:59:42 +02:00
|
|
|
}
|
2021-03-11 14:47:47 +01:00
|
|
|
|
2021-05-05 12:59:42 +02:00
|
|
|
ReturnValue_t TcpTmTcServer::initializeAfterTaskCreation() {
|
2022-02-02 10:29:30 +01:00
|
|
|
if (tmtcBridge == nullptr) {
|
|
|
|
return ObjectManagerIF::CHILD_INIT_FAILED;
|
|
|
|
}
|
|
|
|
/* Initialize the destination after task creation. This ensures
|
|
|
|
that the destination has already been set in the TMTC bridge. */
|
|
|
|
targetTcDestination = tmtcBridge->getRequestQueue();
|
|
|
|
tcStore = tmtcBridge->tcStore;
|
|
|
|
tmStore = tmtcBridge->tmStore;
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::OK;
|
2021-05-05 12:59:42 +02:00
|
|
|
}
|
|
|
|
|
2021-09-28 15:01:01 +02:00
|
|
|
void TcpTmTcServer::handleServerOperation(socket_t& connSocket) {
|
2021-09-28 17:26:02 +02:00
|
|
|
#if defined PLATFORM_WIN
|
2022-02-02 10:29:30 +01:00
|
|
|
setSocketNonBlocking(connSocket);
|
2021-09-28 17:26:02 +02:00
|
|
|
#endif
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
while (true) {
|
2022-05-09 10:57:23 +02:00
|
|
|
ssize_t retval = recv(connSocket, reinterpret_cast<char*>(receptionBuffer.data()),
|
2022-08-31 22:47:58 +02:00
|
|
|
receptionBuffer.size(), tcpConfig.tcpFlags);
|
2022-02-02 10:29:30 +01:00
|
|
|
if (retval == 0) {
|
|
|
|
size_t availableReadData = ringBuffer.getAvailableReadData();
|
|
|
|
if (availableReadData > lastRingBufferSize) {
|
|
|
|
handleTcRingBufferData(availableReadData);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (retval > 0) {
|
|
|
|
// The ring buffer was configured for overwrite, so the returnvalue does not need to
|
|
|
|
// be checked for now
|
|
|
|
ringBuffer.writeData(receptionBuffer.data(), retval);
|
|
|
|
} else if (retval < 0) {
|
|
|
|
int errorValue = getLastSocketError();
|
2021-09-28 17:26:02 +02:00
|
|
|
#if defined PLATFORM_UNIX
|
2022-02-02 10:29:30 +01:00
|
|
|
int wouldBlockValue = EAGAIN;
|
2021-09-28 17:26:02 +02:00
|
|
|
#elif defined PLATFORM_WIN
|
2022-02-02 10:29:30 +01:00
|
|
|
int wouldBlockValue = WSAEWOULDBLOCK;
|
2021-09-28 17:26:02 +02:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
if (errorValue == wouldBlockValue) {
|
|
|
|
// No data available. Check whether any packets have been read, then send back
|
|
|
|
// telemetry if available
|
|
|
|
bool tcAvailable = false;
|
|
|
|
bool tmSent = false;
|
|
|
|
size_t availableReadData = ringBuffer.getAvailableReadData();
|
|
|
|
if (availableReadData > lastRingBufferSize) {
|
|
|
|
tcAvailable = true;
|
|
|
|
handleTcRingBufferData(availableReadData);
|
|
|
|
}
|
|
|
|
ReturnValue_t result = handleTmSending(connSocket, tmSent);
|
|
|
|
if (result == CONN_BROKEN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (not tcAvailable and not tmSent) {
|
|
|
|
TaskFactory::delayTask(tcpConfig.tcpLoopDelay);
|
2021-03-11 14:47:47 +01:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
} else {
|
|
|
|
tcpip::handleError(tcpip::Protocol::TCP, tcpip::ErrorSources::RECV_CALL, 300);
|
|
|
|
}
|
2021-09-28 15:01:01 +02:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2021-05-05 12:59:42 +02:00
|
|
|
}
|
2021-03-11 14:47:47 +01:00
|
|
|
|
2021-09-28 15:01:01 +02:00
|
|
|
ReturnValue_t TcpTmTcServer::handleTcReception(uint8_t* spacePacket, size_t packetSize) {
|
2022-02-02 10:29:30 +01:00
|
|
|
if (wiretappingEnabled) {
|
2021-09-29 11:45:20 +02:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::info << "Received TC:" << std::endl;
|
2021-09-29 11:45:20 +02:00
|
|
|
#else
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::printInfo("Received TC:\n");
|
2021-09-29 11:45:20 +02:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
arrayprinter::print(spacePacket, packetSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (spacePacket == nullptr or packetSize == 0) {
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
|
|
|
store_address_t storeId;
|
|
|
|
ReturnValue_t result = tcStore->addData(&storeId, spacePacket, packetSize);
|
2022-08-16 01:08:26 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2021-05-05 12:59:42 +02:00
|
|
|
#if FSFW_VERBOSE_LEVEL >= 1
|
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::warning << "TcpTmTcServer::handleServerOperation: Data storage with packet size"
|
|
|
|
<< packetSize << " failed" << std::endl;
|
2021-09-28 15:01:01 +02:00
|
|
|
#else
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::printWarning(
|
|
|
|
"TcpTmTcServer::handleServerOperation: Data storage with packet size %d "
|
|
|
|
"failed\n",
|
|
|
|
packetSize);
|
2021-05-05 12:59:42 +02:00
|
|
|
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
|
|
|
|
#endif /* FSFW_VERBOSE_LEVEL >= 1 */
|
2022-02-02 10:29:30 +01:00
|
|
|
return result;
|
|
|
|
}
|
2021-03-11 14:47:47 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
TmTcMessage message(storeId);
|
2021-03-11 14:47:47 +01:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
result = MessageQueueSenderIF::sendMessage(targetTcDestination, &message);
|
2022-08-16 01:08:26 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2021-05-05 12:59:42 +02:00
|
|
|
#if FSFW_VERBOSE_LEVEL >= 1
|
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::warning << "TcpTmTcServer::handleServerOperation: "
|
|
|
|
" Sending message to queue failed"
|
|
|
|
<< std::endl;
|
2021-09-28 15:01:01 +02:00
|
|
|
#else
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::printWarning(
|
|
|
|
"TcpTmTcServer::handleServerOperation: "
|
|
|
|
" Sending message to queue failed\n");
|
2021-05-05 12:59:42 +02:00
|
|
|
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
|
|
|
|
#endif /* FSFW_VERBOSE_LEVEL >= 1 */
|
2022-02-02 10:29:30 +01:00
|
|
|
tcStore->deleteData(storeId);
|
|
|
|
}
|
|
|
|
return result;
|
2021-03-11 14:47:47 +01:00
|
|
|
}
|
|
|
|
|
2022-05-09 10:57:23 +02:00
|
|
|
const std::string& TcpTmTcServer::getTcpPort() const { return tcpConfig.tcpPort; }
|
2021-03-11 14:47:47 +01:00
|
|
|
|
2022-05-09 10:57:23 +02:00
|
|
|
void TcpTmTcServer::setSpacePacketParsingOptions(std::vector<uint16_t> validPacketIds_) {
|
|
|
|
this->validPacketIds = std::move(validPacketIds_);
|
2021-09-28 15:01:01 +02:00
|
|
|
}
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
TcpTmTcServer::TcpConfig& TcpTmTcServer::getTcpConfigStruct() { return tcpConfig; }
|
2021-08-09 18:12:25 +02:00
|
|
|
|
2021-09-28 15:01:01 +02:00
|
|
|
ReturnValue_t TcpTmTcServer::handleTmSending(socket_t connSocket, bool& tmSent) {
|
2022-02-02 10:29:30 +01:00
|
|
|
// Access to the FIFO is mutex protected because it is filled by the bridge
|
2022-05-09 10:57:23 +02:00
|
|
|
MutexGuard mg(tmtcBridge->mutex, tmtcBridge->timeoutType, tmtcBridge->mutexTimeoutMs);
|
2022-02-02 10:29:30 +01:00
|
|
|
store_address_t storeId;
|
|
|
|
while ((not tmtcBridge->tmFifo->empty()) and
|
|
|
|
(tmtcBridge->packetSentCounter < tmtcBridge->sentPacketsPerCycle)) {
|
|
|
|
// Send can fail, so only peek from the FIFO
|
|
|
|
tmtcBridge->tmFifo->peek(&storeId);
|
|
|
|
|
|
|
|
// Using the store accessor will take care of deleting TM from the store automatically
|
|
|
|
ConstStorageAccessor storeAccessor(storeId);
|
|
|
|
ReturnValue_t result = tmStore->getData(storeId, storeAccessor);
|
2022-08-16 01:08:26 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-02-02 10:29:30 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (wiretappingEnabled) {
|
2021-09-29 11:45:20 +02:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::info << "Sending TM:" << std::endl;
|
2021-09-29 11:45:20 +02:00
|
|
|
#else
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::printInfo("Sending TM:\n");
|
2021-09-29 11:45:20 +02:00
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
arrayprinter::print(storeAccessor.data(), storeAccessor.size());
|
|
|
|
}
|
2022-05-09 10:57:23 +02:00
|
|
|
ssize_t retval = send(connSocket, reinterpret_cast<const char*>(storeAccessor.data()),
|
2022-05-13 13:21:01 +02:00
|
|
|
storeAccessor.size(), tcpConfig.tcpTmFlags);
|
2022-02-02 10:29:30 +01:00
|
|
|
if (retval == static_cast<int>(storeAccessor.size())) {
|
|
|
|
// Packet sent, clear FIFO entry
|
|
|
|
tmtcBridge->tmFifo->pop();
|
|
|
|
tmSent = true;
|
|
|
|
|
|
|
|
} else if (retval <= 0) {
|
|
|
|
// Assume that the client has closed the connection here for now
|
|
|
|
handleSocketError(storeAccessor);
|
|
|
|
return CONN_BROKEN;
|
2021-05-05 15:59:41 +02:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2022-08-16 01:08:26 +02:00
|
|
|
return returnvalue::OK;
|
2021-05-05 15:59:41 +02:00
|
|
|
}
|
2021-09-28 15:01:01 +02:00
|
|
|
|
|
|
|
ReturnValue_t TcpTmTcServer::handleTcRingBufferData(size_t availableReadData) {
|
2022-08-16 01:08:26 +02:00
|
|
|
ReturnValue_t status = returnvalue::OK;
|
|
|
|
ReturnValue_t result = returnvalue::OK;
|
2022-02-02 10:29:30 +01:00
|
|
|
size_t readAmount = availableReadData;
|
|
|
|
lastRingBufferSize = availableReadData;
|
|
|
|
if (readAmount >= ringBuffer.getMaxSize()) {
|
2021-09-28 15:01:01 +02:00
|
|
|
#if FSFW_VERBOSE_LEVEL >= 1
|
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
// Possible configuration error, too much data or/and data coming in too fast,
|
|
|
|
// requiring larger buffers
|
|
|
|
sif::warning << "TcpTmTcServer::handleServerOperation: Ring buffer reached "
|
|
|
|
<< "fill count" << std::endl;
|
2021-09-28 15:01:01 +02:00
|
|
|
#else
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::printWarning(
|
|
|
|
"TcpTmTcServer::handleServerOperation: Ring buffer reached "
|
|
|
|
"fill count");
|
2021-09-28 15:01:01 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
|
|
|
if (readAmount >= receptionBuffer.size()) {
|
2021-09-28 15:01:01 +02:00
|
|
|
#if FSFW_VERBOSE_LEVEL >= 1
|
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
// Possible configuration error, too much data or/and data coming in too fast,
|
|
|
|
// requiring larger buffers
|
|
|
|
sif::warning << "TcpTmTcServer::handleServerOperation: "
|
|
|
|
"Reception buffer too small "
|
|
|
|
<< std::endl;
|
2021-09-28 15:01:01 +02:00
|
|
|
#else
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::printWarning("TcpTmTcServer::handleServerOperation: Reception buffer too small\n");
|
2021-09-28 15:01:01 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
readAmount = receptionBuffer.size();
|
|
|
|
}
|
|
|
|
ringBuffer.readData(receptionBuffer.data(), readAmount, true);
|
|
|
|
const uint8_t* bufPtr = receptionBuffer.data();
|
2022-09-01 10:51:09 +02:00
|
|
|
SpacePacketParser::FoundPacketInfo info;
|
|
|
|
if (spacePacketParser == nullptr) {
|
|
|
|
return returnvalue::FAILED;
|
|
|
|
}
|
|
|
|
spacePacketParser->reset();
|
|
|
|
while (spacePacketParser->getAmountRead() < readAmount) {
|
|
|
|
result = spacePacketParser->parseSpacePackets(&bufPtr, readAmount, info);
|
2022-02-02 10:29:30 +01:00
|
|
|
switch (result) {
|
|
|
|
case (SpacePacketParser::NO_PACKET_FOUND):
|
|
|
|
case (SpacePacketParser::SPLIT_PACKET): {
|
|
|
|
break;
|
|
|
|
}
|
2022-08-16 01:08:26 +02:00
|
|
|
case (returnvalue::OK): {
|
2022-08-31 22:47:58 +02:00
|
|
|
result = handleTcReception(receptionBuffer.data() + info.startIdx, info.sizeFound);
|
2022-08-16 01:08:26 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-02-02 10:29:30 +01:00
|
|
|
status = result;
|
2021-09-28 15:01:01 +02:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2021-09-28 15:01:01 +02:00
|
|
|
}
|
2022-08-31 22:47:58 +02:00
|
|
|
ringBuffer.deleteData(info.sizeFound);
|
2022-02-02 10:29:30 +01:00
|
|
|
lastRingBufferSize = ringBuffer.getAvailableReadData();
|
|
|
|
}
|
|
|
|
return status;
|
2021-09-28 15:01:01 +02:00
|
|
|
}
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
void TcpTmTcServer::enableWiretapping(bool enable) { this->wiretappingEnabled = enable; }
|
2021-09-28 15:09:56 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
void TcpTmTcServer::handleSocketError(ConstStorageAccessor& accessor) {
|
|
|
|
// Don't delete data
|
|
|
|
accessor.release();
|
|
|
|
auto socketError = getLastSocketError();
|
|
|
|
switch (socketError) {
|
2021-09-28 15:01:01 +02:00
|
|
|
#if defined PLATFORM_WIN
|
2022-02-02 10:29:30 +01:00
|
|
|
case (WSAECONNRESET): {
|
|
|
|
// See https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-send
|
|
|
|
// Remote client might have shut down connection
|
|
|
|
return;
|
2021-09-28 15:01:01 +02:00
|
|
|
}
|
|
|
|
#else
|
2022-02-02 10:29:30 +01:00
|
|
|
case (EPIPE): {
|
|
|
|
// See https://man7.org/linux/man-pages/man2/send.2.html
|
|
|
|
// Remote client might have shut down connection
|
|
|
|
return;
|
2021-09-28 15:01:01 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default: {
|
2022-02-02 10:29:30 +01:00
|
|
|
tcpip::handleError(tcpip::Protocol::TCP, tcpip::ErrorSources::SEND_CALL);
|
2021-09-28 15:01:01 +02:00
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2021-09-28 15:01:01 +02:00
|
|
|
}
|
2021-09-28 17:26:02 +02:00
|
|
|
|
2021-09-28 17:38:35 +02:00
|
|
|
#if defined PLATFORM_WIN
|
2022-02-02 10:29:30 +01:00
|
|
|
void TcpTmTcServer::setSocketNonBlocking(socket_t& connSocket) {
|
|
|
|
u_long iMode = 1;
|
|
|
|
int iResult = ioctlsocket(connSocket, FIONBIO, &iMode);
|
|
|
|
if (iResult != NO_ERROR) {
|
2021-09-28 17:26:02 +02:00
|
|
|
#if FSFW_VERBOSE_LEVEL >= 1
|
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::warning << "TcpTmTcServer::handleServerOperation: Setting socket"
|
|
|
|
" non-blocking failed with error "
|
|
|
|
<< iResult;
|
2021-09-28 17:26:02 +02:00
|
|
|
#else
|
2022-02-02 10:29:30 +01:00
|
|
|
sif::printWarning(
|
|
|
|
"TcpTmTcServer::handleServerOperation: Setting socket"
|
|
|
|
" non-blocking failed with error %d\n",
|
|
|
|
iResult);
|
2021-09-28 17:26:02 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2021-09-28 17:26:02 +02:00
|
|
|
}
|
2021-09-28 17:38:35 +02:00
|
|
|
#endif
|