continued tcp server
This commit is contained in:
parent
8be4f45969
commit
494dd0db32
@ -2,10 +2,32 @@
|
|||||||
#include "../../serviceinterface/ServiceInterface.h"
|
#include "../../serviceinterface/ServiceInterface.h"
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
|
|
||||||
TcWinTcpServer::TcWinTcpServer(object_id_t objectId, object_id_t tmtcUnixUdpBridge):
|
|
||||||
|
TcWinTcpServer::TcWinTcpServer(object_id_t objectId, object_id_t tmtcUnixUdpBridge,
|
||||||
|
uint16_t customTcpServerPort):
|
||||||
SystemObject(objectId) {
|
SystemObject(objectId) {
|
||||||
/* Open TCP socket */
|
/* Initiates Winsock DLL. */
|
||||||
|
WSAData wsaData;
|
||||||
|
WORD wVersionRequested = MAKEWORD(2, 2);
|
||||||
|
int err = WSAStartup(wVersionRequested, &wsaData);
|
||||||
|
if (err != 0) {
|
||||||
|
/* Tell the user that we could not find a usable */
|
||||||
|
/* Winsock DLL. */
|
||||||
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||||
|
sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge:"
|
||||||
|
"WSAStartup failed with error: " << err << std::endl;
|
||||||
|
#endif
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Open TCP (stream) socket */
|
||||||
serverTcpSocket = socket(AF_INET, SOCK_STREAM, 0);
|
serverTcpSocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
uint16_t tcpPort = customTcpServerPort;
|
||||||
|
|
||||||
|
if(customTcpServerPort == 0xffff) {
|
||||||
|
tcpPort = DEFAULT_TCP_SERVER_PORT;
|
||||||
|
}
|
||||||
|
|
||||||
if(serverTcpSocket == 0) {
|
if(serverTcpSocket == 0) {
|
||||||
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
||||||
sif::warning << "TcWinTcpServer::TcWinTcpServer: Socket creation failed!" << std::endl;
|
sif::warning << "TcWinTcpServer::TcWinTcpServer: Socket creation failed!" << std::endl;
|
||||||
@ -16,11 +38,52 @@ TcWinTcpServer::TcWinTcpServer(object_id_t objectId, object_id_t tmtcUnixUdpBrid
|
|||||||
int retval = setsockopt(serverTcpSocket, SOL_SOCKET, SO_REUSEADDR | SO_BROADCAST,
|
int retval = setsockopt(serverTcpSocket, SOL_SOCKET, SO_REUSEADDR | SO_BROADCAST,
|
||||||
reinterpret_cast<const char*>(&tcpSockOpt), sizeof(tcpSockOpt));
|
reinterpret_cast<const char*>(&tcpSockOpt), sizeof(tcpSockOpt));
|
||||||
if(retval != 0) {
|
if(retval != 0) {
|
||||||
|
sif::warning << "TcWinTcpServer::TcWinTcpServer: Setting socket options failed!" <<
|
||||||
|
std::endl;
|
||||||
|
handleError(ErrorSources::SETSOCKOPT_CALL);
|
||||||
}
|
}
|
||||||
|
tcpAddress.sin_family = AF_INET;
|
||||||
|
tcpAddress.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||||
|
tcpAddress.sin_port = htons(tcpPort);
|
||||||
|
|
||||||
|
retval = bind(serverTcpSocket, reinterpret_cast<const sockaddr*>(&tcpAddress),
|
||||||
|
tcpAddrLen);
|
||||||
|
if(retval != 0) {
|
||||||
|
sif::warning << "TcWinTcpServer::TcWinTcpServer: Binding socket failed!" <<
|
||||||
|
std::endl;
|
||||||
|
handleError(ErrorSources::BIND_CALL);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TcWinTcpServer::~TcWinTcpServer() {
|
TcWinTcpServer::~TcWinTcpServer() {
|
||||||
|
closesocket(serverTcpSocket);
|
||||||
|
WSACleanup();
|
||||||
|
}
|
||||||
|
|
||||||
|
ReturnValue_t TcWinTcpServer::initialize() {
|
||||||
|
return HasReturnvaluesIF::RETURN_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
ReturnValue_t TcWinTcpServer::performOperation(uint8_t opCode) {
|
||||||
|
/* If a connection is accepted, the corresponding scoket will be assigned to the new socket */
|
||||||
|
SOCKET connectorSocket;
|
||||||
|
sockaddr_in connectorSockAddr;
|
||||||
|
int connectorSockAddrLen = 0;
|
||||||
|
/* Listen for connection requests permanently for lifetime of program */
|
||||||
|
while(true) {
|
||||||
|
int retval = listen(serverTcpSocket, backlog);
|
||||||
|
if(retval != 0) {
|
||||||
|
handleError(ErrorSources::LISTEN_CALL);
|
||||||
|
}
|
||||||
|
|
||||||
|
connectorSocket = accept(serverTcpSocket, reinterpret_cast<sockaddr*>(&connectorSockAddr),
|
||||||
|
&connectorSockAddrLen);
|
||||||
|
|
||||||
|
if(connectorSocket) {};
|
||||||
|
|
||||||
|
}
|
||||||
|
return HasReturnvaluesIF::RETURN_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcWinTcpServer::handleError(ErrorSources errorSrc) {
|
void TcWinTcpServer::handleError(ErrorSources errorSrc) {
|
||||||
@ -33,6 +96,13 @@ void TcWinTcpServer::handleError(ErrorSources errorSrc) {
|
|||||||
else if(errorSrc == ErrorSources::SOCKET_CALL) {
|
else if(errorSrc == ErrorSources::SOCKET_CALL) {
|
||||||
errorSrcString = "socket call";
|
errorSrcString = "socket call";
|
||||||
}
|
}
|
||||||
|
else if(errorSrc == ErrorSources::LISTEN_CALL) {
|
||||||
|
errorSrcString = "listen call";
|
||||||
|
}
|
||||||
|
else if(errorSrc == ErrorSources::ACCEPT_CALL) {
|
||||||
|
errorSrcString = "accept call";
|
||||||
|
}
|
||||||
|
|
||||||
switch(errCode) {
|
switch(errCode) {
|
||||||
case(WSANOTINITIALISED): {
|
case(WSANOTINITIALISED): {
|
||||||
sif::warning << "TmTcWinUdpBridge::handleError: " << errorSrcString << " | "
|
sif::warning << "TmTcWinUdpBridge::handleError: " << errorSrcString << " | "
|
||||||
|
@ -14,19 +14,29 @@ public:
|
|||||||
static constexpr uint16_t DEFAULT_TCP_SERVER_PORT = 7301;
|
static constexpr uint16_t DEFAULT_TCP_SERVER_PORT = 7301;
|
||||||
static constexpr uint16_t DEFAULT_TCP_CLIENT_PORT = 7302;
|
static constexpr uint16_t DEFAULT_TCP_CLIENT_PORT = 7302;
|
||||||
|
|
||||||
TcWinTcpServer(object_id_t objectId, object_id_t tmtcUnixUdpBridge);
|
TcWinTcpServer(object_id_t objectId, object_id_t tmtcUnixUdpBridge,
|
||||||
|
uint16_t customTcpServerPort = 0xffff);
|
||||||
virtual~ TcWinTcpServer();
|
virtual~ TcWinTcpServer();
|
||||||
|
|
||||||
|
ReturnValue_t initialize() override;
|
||||||
|
ReturnValue_t performOperation(uint8_t opCode) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
SOCKET serverTcpSocket = 0;
|
SOCKET serverTcpSocket = 0;
|
||||||
|
struct sockaddr_in tcpAddress;
|
||||||
|
int tcpAddrLen = sizeof(tcpAddress);
|
||||||
|
int backlog = 3;
|
||||||
|
|
||||||
std::vector<uint8_t> receptionBuffer;
|
std::vector<uint8_t> receptionBuffer;
|
||||||
int tcpSockOpt = 0;
|
int tcpSockOpt = 0;
|
||||||
|
|
||||||
enum class ErrorSources {
|
enum class ErrorSources {
|
||||||
SOCKET_CALL,
|
SOCKET_CALL,
|
||||||
SETSOCKOPT_CALL
|
SETSOCKOPT_CALL,
|
||||||
|
BIND_CALL,
|
||||||
|
LISTEN_CALL,
|
||||||
|
ACCEPT_CALL
|
||||||
};
|
};
|
||||||
|
|
||||||
void handleError(ErrorSources errorSrc);
|
void handleError(ErrorSources errorSrc);
|
||||||
|
@ -14,7 +14,7 @@ TmTcWinUdpBridge::TmTcWinUdpBridge(object_id_t objectId,
|
|||||||
mutex = MutexFactory::instance()->createMutex();
|
mutex = MutexFactory::instance()->createMutex();
|
||||||
communicationLinkUp = false;
|
communicationLinkUp = false;
|
||||||
|
|
||||||
// Initiates Winsock DLL.
|
/* Initiates Winsock DLL. */
|
||||||
WSAData wsaData;
|
WSAData wsaData;
|
||||||
WORD wVersionRequested = MAKEWORD(2, 2);
|
WORD wVersionRequested = MAKEWORD(2, 2);
|
||||||
int err = WSAStartup(wVersionRequested, &wsaData);
|
int err = WSAStartup(wVersionRequested, &wsaData);
|
||||||
@ -87,6 +87,7 @@ TmTcWinUdpBridge::TmTcWinUdpBridge(object_id_t objectId,
|
|||||||
}
|
}
|
||||||
|
|
||||||
TmTcWinUdpBridge::~TmTcWinUdpBridge() {
|
TmTcWinUdpBridge::~TmTcWinUdpBridge() {
|
||||||
|
closesocket(serverSocket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user