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/udp-protocol
1 result
Show changes
Commits on Source (1)
...@@ -26,10 +26,7 @@ UDPProtocol::UDPProtocol(std::string ip, int port) : ...@@ -26,10 +26,7 @@ UDPProtocol::UDPProtocol(std::string ip, int port) :
srvaddr.sin_family = AF_INET; // IPv4 srvaddr.sin_family = AF_INET; // IPv4
srvaddr.sin_addr.s_addr = inet_addr(ip.c_str()); srvaddr.sin_addr.s_addr = inet_addr(ip.c_str());
srvaddr.sin_port = htons(this->port); srvaddr.sin_port = htons(this->port);
struct timeval timeout;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
setsockopt(srv_sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
} }
UDPProtocol::~UDPProtocol() { UDPProtocol::~UDPProtocol() {
...@@ -42,7 +39,8 @@ int UDPProtocol::receiveAtLeastHeaderSizeBytes(uint8_t *buff, int headerSize, ...@@ -42,7 +39,8 @@ int UDPProtocol::receiveAtLeastHeaderSizeBytes(uint8_t *buff, int headerSize,
socklen_t len = sizeof(cliaddr); socklen_t len = sizeof(cliaddr);
while (bytercv < headerSize) { while (bytercv < headerSize) {
int rcv = recvfrom(srv_sock, &buff[bytercv], packetSize + 1, // +1 to recognized if there are more bytes than expected int rcv = recvfrom(srv_sock, &buff[bytercv], packetSize + 1, // +1 to recognized if there are more bytes than expected
MSG_WAITFORONE, (struct sockaddr*) &cliaddr, &len); 0, (struct sockaddr*) &cliaddr, &len);
bytercv += rcv; bytercv += rcv;
if (rcv < 0) { if (rcv < 0) {
return -1; return -1;
...@@ -132,9 +130,15 @@ int UDPProtocol::connectToClient() { ...@@ -132,9 +130,15 @@ int UDPProtocol::connectToClient() {
// Set timeout to the socket // Set timeout to the socket
struct timeval tv; struct timeval tv;
tv.tv_sec = std::numeric_limits<time_t>::max(); tv.tv_sec = 1;
tv.tv_usec = 0; tv.tv_usec = 0;
setsockopt(srv_sock, SOL_SOCKET, SO_RCVTIMEO, (const char*) &tv, sizeof tv); if (setsockopt(srv_sock, SOL_SOCKET, SO_RCVTIMEO, (const char*) &tv,
sizeof tv) < 0) {
time_t now = time(nullptr);
std::cerr << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S")
<< "]\t[UDP Receiver]\t" << "Error setting timeout to socket" << std::endl;
return -1;
}
// Bind the socket with the server address // Bind the socket with the server address
if (bind(srv_sock, (const struct sockaddr*) &srvaddr, sizeof(srvaddr)) if (bind(srv_sock, (const struct sockaddr*) &srvaddr, sizeof(srvaddr))
......