From c067a1dbe1aebce74b3bb36b9ccc3be8b09aa823 Mon Sep 17 00:00:00 2001 From: davidt0x Date: Thu, 21 Nov 2019 13:14:14 -0500 Subject: [PATCH] Removed linux and windows socket code. This is covered by zeromq now. I also took out the console output of "Writing to socket!". --- src/SocketRecorder.cpp | 3 +- src/SocketRecorder_linux.src | 112 -------------------------------- src/SocketRecorder_win.src | 120 ----------------------------------- 3 files changed, 1 insertion(+), 234 deletions(-) delete mode 100644 src/SocketRecorder_linux.src delete mode 100644 src/SocketRecorder_win.src diff --git a/src/SocketRecorder.cpp b/src/SocketRecorder.cpp index 391d237..523b52d 100644 --- a/src/SocketRecorder.cpp +++ b/src/SocketRecorder.cpp @@ -45,8 +45,7 @@ bool SocketRecorder::openRecord(std::string port) /// bool SocketRecorder::writeRecord(std::string s) { - std::cout << "Writing to socket!\n"; - if (_open) + if (_open) s_send(*publisher, s); return _open; diff --git a/src/SocketRecorder_linux.src b/src/SocketRecorder_linux.src deleted file mode 100644 index 26ef9ec..0000000 --- a/src/SocketRecorder_linux.src +++ /dev/null @@ -1,112 +0,0 @@ -/// FicTrac http://rjdmoore.net/fictrac/ -/// \file SocketRecorder_linux.cpp -/// \brief Linux implementation of socket recorder. -/// \author Richard Moore -/// \copyright CC BY-NC-SA 3.0 - -#include "SocketRecorder_linux.h" - -#include "Logger.h" - -#include -#include -#include -#include -#include -#include -#include - -#include // try, catch -#include // cout/cerr - -/// -/// -/// -SocketRecorder::SocketRecorder() - : _listenSocket(-1), _clientSocket(-1) -{ - _type = SOCK; -} - -/// -/// -/// -SocketRecorder::~SocketRecorder() -{ - closeRecord(); -} - -/// -/// -/// -bool SocketRecorder::openRecord(std::string port) -{ - struct sockaddr_in serv_addr, cli_addr; - - // Create socket - _listenSocket = socket(AF_INET, SOCK_STREAM, 0); - if (_listenSocket < 0) { - LOG_ERR("Error! Could not create valid socket on port %s.", port.c_str()); - return false; - } - - // Bind socket - bzero((char *) &serv_addr, sizeof(serv_addr)); - int portno = -1; - try { - portno = atoi(port.c_str()); - } - catch (...) { - LOG_ERR("Error! Invalid socket port number (%s).", port.c_str()); - return false; - } - serv_addr.sin_family = AF_INET; - serv_addr.sin_addr.s_addr = INADDR_ANY; - serv_addr.sin_port = htons(portno); - if (bind(_listenSocket, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { - LOG_ERR("Error! Failed to bind socket on port %s.", port.c_str()); - return false; - } - - // Listen on our socket - if (listen(_listenSocket,5) < 0) { - LOG_ERR("Error! Failed to listen to socket on port %s.", port.c_str()); - return false; - } - - // Wait for client connection... - socklen_t clilen = sizeof(cli_addr); - PRINT("\nWaiting for client connection to socket: %s ...\n", port.c_str()); - _clientSocket = accept(_listenSocket, (struct sockaddr *) &cli_addr, &clilen); // blocking - if (_clientSocket < 0) { - LOG_ERR("Error! Failed to accept socket connection on port %s.", port.c_str()); - return false; - } - - return (_open = true); -} - -/// -/// -/// -bool SocketRecorder::writeRecord(std::string s) -{ - if (_open) { - int n = write(_clientSocket,s.c_str(),s.size()); - if (n < 0) { - LOG_ERR("Error! Send failed."); - _open = false; // should this be a terminal error? - } - } - return _open; -} - -/// -/// -/// -void SocketRecorder::closeRecord() -{ - _open = false; - close(_clientSocket); - close(_listenSocket); -} diff --git a/src/SocketRecorder_win.src b/src/SocketRecorder_win.src deleted file mode 100644 index 243d1e9..0000000 --- a/src/SocketRecorder_win.src +++ /dev/null @@ -1,120 +0,0 @@ -/// FicTrac http://rjdmoore.net/fictrac/ -/// \file SocketRecorder_win.cpp -/// \brief Windows implementation of socket recorder. -/// \author Richard Moore -/// \copyright CC BY-NC-SA 3.0 - -#include "SocketRecorder_win.h" - -#include "Logger.h" - -#include -#include - -/// -/// -/// -SocketRecorder::SocketRecorder() - : _listenSocket(INVALID_SOCKET), _clientSocket(INVALID_SOCKET) -{ - _type = SOCK; -} - -/// -/// -/// -SocketRecorder::~SocketRecorder() -{ - closeRecord(); -} - -/// -/// -/// -bool SocketRecorder::openRecord(std::string port) -{ - // Initialize Winsock - int iResult = WSAStartup(MAKEWORD(2, 2), &_wsaData); - if (iResult != 0) { - LOG_ERR("Error! Failed to initialise WinSock library (err = %d)", iResult); - return false; - } - - struct addrinfo *result = nullptr, hints; - - ZeroMemory(&hints, sizeof(hints)); - hints.ai_family = AF_INET; - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = IPPROTO_TCP; - hints.ai_flags = AI_PASSIVE; - - // Resolve the local address and port to be used by the server - iResult = getaddrinfo(NULL, port.c_str(), &hints, &result); - if (iResult != 0) { - LOG_ERR("Error! Failed to resolve local address (err = %d).", iResult); - return false; - } - - // Create a SOCKET for the server to listen for client connections - _listenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); - if (_listenSocket == INVALID_SOCKET) { - //FIXME: include IP address in error msg. - LOG_ERR("Error! Could not create valid socket on port %s (err = %d).", port.c_str(), WSAGetLastError()); - freeaddrinfo(result); - return false; - } - - // Setup the TCP listening socket - iResult = bind(_listenSocket, result->ai_addr, (int)result->ai_addrlen); - if (iResult == SOCKET_ERROR) { - LOG_ERR("Error! Failed to bind socket on port %s (err = %d).", port.c_str(), WSAGetLastError()); - freeaddrinfo(result); - return false; - } - - // After successful bind(), we no longer need address info. - freeaddrinfo(result); - - // Listen on our socket. - if (listen(_listenSocket, SOMAXCONN) == SOCKET_ERROR) { - LOG_ERR("Error! Failed to listen to socket on port %s (err = %d).", port.c_str(), WSAGetLastError()); - return false; - } - - // Wait for client connection... - //FIXME: include IP:port info in this message - PRINT("\nWaiting for client connection to socket: %s ...\n", port.c_str()); - _clientSocket = accept(_listenSocket, NULL, NULL); // blocking - if (_clientSocket == INVALID_SOCKET) { - LOG_ERR("Error! Failed to accept socket connection (err = %d).", WSAGetLastError()); - return false; - } - - return (_open = true); -} - -/// -/// -/// -bool SocketRecorder::writeRecord(std::string s) -{ - if (_open) { - int iSendResult = send(_clientSocket, s.c_str(), s.size(), 0); - if (iSendResult == SOCKET_ERROR) { - LOG_ERR("Error! Send failed (err = %d).", WSAGetLastError()); - _open = false; // should this be a terminal error? - } - } - return _open; -} - -/// -/// -/// -void SocketRecorder::closeRecord() -{ - _open = false; - closesocket(_clientSocket); - closesocket(_listenSocket); - WSACleanup(); -}