added new tcp code

This commit is contained in:
Robin Müller 2021-09-27 15:35:09 +02:00
parent 98deac1ef1
commit df0adfb33c
No known key found for this signature in database
GPG Key ID: 11D4952C8CCEF814

View File

@ -5,6 +5,7 @@
#include "TcpTmTcBridge.h" #include "TcpTmTcBridge.h"
#include "tcpipHelpers.h" #include "tcpipHelpers.h"
#include "fsfw/tasks/TaskFactory.h"
#include "fsfw/container/SharedRingBuffer.h" #include "fsfw/container/SharedRingBuffer.h"
#include "fsfw/ipc/MessageQueueSenderIF.h" #include "fsfw/ipc/MessageQueueSenderIF.h"
#include "fsfw/ipc/MutexGuard.h" #include "fsfw/ipc/MutexGuard.h"
@ -19,6 +20,7 @@
#elif defined(PLATFORM_UNIX) #elif defined(PLATFORM_UNIX)
#include <netdb.h> #include <netdb.h>
#endif #endif
#include <chrono>
#ifndef FSFW_TCP_RECV_WIRETAPPING_ENABLED #ifndef FSFW_TCP_RECV_WIRETAPPING_ENABLED
#define FSFW_TCP_RECV_WIRETAPPING_ENABLED 0 #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, TcpTmTcServer::TcpTmTcServer(object_id_t objectId, object_id_t tmtcTcpBridge,
size_t receptionBufferSize, std::string customTcpServerPort): size_t receptionBufferSize, std::string customTcpServerPort):
SystemObject(objectId), tmtcBridgeId(tmtcTcpBridge), SystemObject(objectId), tmtcBridgeId(tmtcTcpBridge),
tcpPort(customTcpServerPort), receptionBuffer(receptionBufferSize) { tcpPort(customTcpServerPort), receptionBuffer(receptionBufferSize) {
if(tcpPort == "") { if(tcpPort == "") {
tcpPort = DEFAULT_SERVER_PORT; tcpPort = DEFAULT_SERVER_PORT;
} }
@ -148,24 +150,67 @@ ReturnValue_t TcpTmTcServer::initializeAfterTaskCreation() {
void TcpTmTcServer::handleServerOperation(socket_t connSocket) { void TcpTmTcServer::handleServerOperation(socket_t connSocket) {
int retval = 0; int retval = 0;
do { using namespace std::chrono_literals;
// Read all telecommands sent by the client
retval = recv(connSocket, // Receive until the peer shuts down the connection, use select to do this
reinterpret_cast<char*>(receptionBuffer.data()), fd_set rfds;
receptionBuffer.capacity(), fd_set efds;
tcpFlags);
if (retval > 0) { FD_ZERO(&rfds);
handleTcReception(retval); 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<char*>(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) { else if(retval > 0) {
// Client has finished sending telecommands, send telemetry now if(FD_ISSET(connSocket, &rfds)) {
handleTmSending(connSocket); // 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 { else {
// Should not happen // no data available
tcpip::handleError(tcpip::Protocol::TCP, tcpip::ErrorSources::RECV_CALL); TaskFactory::delayTask(500);
} }
} while(retval > 0); }
} }
ReturnValue_t TcpTmTcServer::handleTcReception(size_t bytesRecvd) { ReturnValue_t TcpTmTcServer::handleTcReception(size_t bytesRecvd) {