Newer
Older
#include <unistd.h>
#include <netdb.h>
#include <regex>
using namespace inaf::oasbo::ConnectionProtocols;
memset(&cliaddr, 0, sizeof(cliaddr));
memset(&servaddr, 0, sizeof(servaddr));
TCPProtocol::~TCPProtocol() {
closeConnectionToClient();
int TCPProtocol::receiveFromClient(PacketLib::BasePacket &pack) {
uint8_t *buff = new uint8_t[pack.getPacketStructureByteSize()];
ssize_t rec = ::recv(this->cli_sock, &buff[0], headerSize, MSG_WAITALL);
pack.copyToBinaryPointer(buff, headerSize);
int to_be_rcv = pack.getPayloadSize() + pack.getTailSize();
rec += ::recv(this->cli_sock, &buff[headerSize], to_be_rcv, MSG_WAITALL);
return -1;
}
pack.copyToBinaryPointer(&buff[headerSize],
pack.getPayloadSize() + pack.getTailSize(), headerSize);
return m_connectToCli(ip, port);
}
int TCPProtocol::closeConnectionToClient() {
if (cli_sock != -1) {
::close(cli_sock);
cli_sock = -1;
}
return 1;
}
bool TCPProtocol::isConnectedToClient() const {
}
int TCPProtocol::m_connectToCli(std::string ip, int port) {
if ((srv_sock = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
time_t now = time(nullptr);
std::cerr << "["
<< std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S")
<< "]\t[TCP Receiver]\t" << "TCP socket on " << host
<< " failed" << std::endl;
exit(EXIT_FAILURE);
}
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = ::inet_addr(ip.c_str());
servaddr.sin_port = htons(port);
if (bind(srv_sock, (struct sockaddr*) &servaddr, sizeof(servaddr))
< 0) {
time_t now = time(nullptr);
std::cerr << "["
<< std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S")
<< "]\t[TCP Receiver]\t" << "TCP CONNECTION on " << host
<< " Bind failed" << std::endl;
time_t now = time(nullptr);
std::cout << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S")
<< "]\t[TCP Receiver]\t" << "Waiting for connection on " << host
time_t now = time(nullptr);
std::cerr << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S")
<< "]\t[TCP Receiver]\t" << "Listen error" << std::endl;
socklen_t clientAddressSize = sizeof(cliaddr);
if ((cli_sock = accept(srv_sock, (struct sockaddr*) &cliaddr,
(socklen_t*) &clientAddressSize)) < 0) {
time_t now = time(nullptr);
std::cerr << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S")
<< "]\t[TCP Receiver]\t" << "accept error" << std::endl;
now = time(nullptr);
std::cout << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S")
<< "]\t[TCP Receiver]\t" << "Connected with "
<< inet_ntoa(cliaddr.sin_addr) << std::endl;
void TCPProtocol::resetPacket(PacketLib::BasePacket &pack, int bytes) {
uint8_t *buff = new uint8_t[bytes];
std::memset(buff, 0, bytes);
int toBeReset = std::min(
static_cast<int>(pack.getPacketStructureByteSize()), bytes);
std::string TCPProtocol::getHost() {
return ip + ":" + std::to_string(port);
void TCPProtocol::setHost(std::string host) {
std::string ip = { };
time_t now = time(nullptr);
std::cerr << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S") <<"]\t[TCP Receiver]\t" << "invalid IP address and port format: "
// Check if the input string matches the expected format
bool ok = true;
if (!std::regex_match(ip, pattern)) {
time_t now = time(nullptr);
std::cerr << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S") <<"]\t[TCP Receiver]\t"<< "invalid IP address: " << ip
void TCPProtocol::setPort(int port) {
if (port > 1023 && port < 65535)
time_t now = time(nullptr);
std::cerr << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S") <<"]\t[TCP Receiver]\t" << "invalid port: " << port
}
bool TCPProtocol::split_ip_port(const std::string &ip_port,
// Regex pattern to match IP address and port in the format "xxx.xxx.xxx.xxx:xxxx"
// Check if the input string matches the expected format
if (!std::regex_match(ip_port, pattern)) {
return false;
}
// Split the input string into IP address and port
int colon_pos = ip_port.find(":");
ip_address = ip_port.substr(0, colon_pos);