Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • bias/connection-protocols/tcp-protocol
1 result
Show changes
Commits on Source (1)
...@@ -25,6 +25,7 @@ protected: ...@@ -25,6 +25,7 @@ protected:
int cli_sock; int cli_sock;
struct sockaddr_in servaddr, cliaddr; struct sockaddr_in servaddr, cliaddr;
bool checkConnFlag = true;
int m_connectToCli(std::string ip, int port); int m_connectToCli(std::string ip, int port);
void resetPacket(inaf::oasbo::PacketLib::BasePacket &pack, int bytes); void resetPacket(inaf::oasbo::PacketLib::BasePacket &pack, int bytes);
bool split_ip_port(const std::string &ip_port, std::string &ip, bool split_ip_port(const std::string &ip_port, std::string &ip,
......
...@@ -9,6 +9,10 @@ ...@@ -9,6 +9,10 @@
#include <ctime> #include <ctime>
#include <iomanip> #include <iomanip>
#include <fcntl.h>
#include <chrono>
#include <thread>
using namespace inaf::oasbo::Receivers; using namespace inaf::oasbo::Receivers;
...@@ -66,6 +70,7 @@ int TCPProtocol::connectToClient() { ...@@ -66,6 +70,7 @@ int TCPProtocol::connectToClient() {
} }
int TCPProtocol::closeConnectionToClient() { int TCPProtocol::closeConnectionToClient() {
this->checkConnFlag = false;
if (cli_sock != -1) { if (cli_sock != -1) {
::close(cli_sock); ::close(cli_sock);
cli_sock = -1; cli_sock = -1;
...@@ -86,7 +91,7 @@ int TCPProtocol::m_connectToCli(std::string ip, int port) { ...@@ -86,7 +91,7 @@ int TCPProtocol::m_connectToCli(std::string ip, int port) {
<< std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S") << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S")
<< "]\t[TCP Receiver]\t" << "TCP socket on " << host << "]\t[TCP Receiver]\t" << "TCP socket on " << host
<< " failed" << std::endl; << " failed" << std::endl;
exit(EXIT_FAILURE); return -1;
} }
servaddr.sin_family = AF_INET; servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = ::inet_addr(ip.c_str()); servaddr.sin_addr.s_addr = ::inet_addr(ip.c_str());
...@@ -99,13 +104,9 @@ int TCPProtocol::m_connectToCli(std::string ip, int port) { ...@@ -99,13 +104,9 @@ int TCPProtocol::m_connectToCli(std::string ip, int port) {
<< "]\t[TCP Receiver]\t" << "TCP CONNECTION on " << host << "]\t[TCP Receiver]\t" << "TCP CONNECTION on " << host
<< " Bind failed" << std::endl; << " Bind failed" << std::endl;
srv_sock = -1; srv_sock = -1;
exit(EXIT_FAILURE); return -1;
} }
} }
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
<< std::endl;
if (listen(srv_sock, 1) < 0) { if (listen(srv_sock, 1) < 0) {
time_t now = time(nullptr); time_t now = time(nullptr);
...@@ -115,6 +116,34 @@ int TCPProtocol::m_connectToCli(std::string ip, int port) { ...@@ -115,6 +116,34 @@ int TCPProtocol::m_connectToCli(std::string ip, int port) {
return -1; return -1;
} }
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
<< std::endl;
fd_set read_fds;
FD_ZERO(&read_fds);
FD_SET(srv_sock, &read_fds);
struct timeval timeout;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
int select_status;
while (this->checkConnFlag) {
select_status = select(srv_sock + 1, &read_fds, NULL, NULL, &timeout);
if (select_status > 0) {
break; // we have data, we can accept now
}
std::this_thread::sleep_for(std::chrono::seconds(1));
// otherwise (i.e. select_status==0) timeout, continue
}
if (!this->checkConnFlag) { // Interrupted
closeConnectionToClient();
return -1;
}
socklen_t clientAddressSize = sizeof(cliaddr); socklen_t clientAddressSize = sizeof(cliaddr);
if ((cli_sock = accept(srv_sock, (struct sockaddr*) &cliaddr, if ((cli_sock = accept(srv_sock, (struct sockaddr*) &cliaddr,
(socklen_t*) &clientAddressSize)) < 0) { (socklen_t*) &clientAddressSize)) < 0) {
......