|
|
@@ -2,6 +2,7 @@ |
|
|
|
|
|
|
|
LFR_Socket::LFR_Socket(ExceptionCallback cb): cb(cb) |
|
|
|
{ |
|
|
|
bufferIterator = std::begin(buffer); |
|
|
|
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) |
|
|
|
{ |
|
|
|
throw std::runtime_error("Failed opening the socket"); |
|
|
@@ -66,7 +67,7 @@ void LFR_Socket::createThread() |
|
|
|
bool connected = false; |
|
|
|
while(true) |
|
|
|
{ |
|
|
|
LFR_Socket::LFR_Telegram telegram; |
|
|
|
LFR_Socket::LFR_Telegram telegram = {}; |
|
|
|
bool received = false; |
|
|
|
if(!stop && !listeners.empty()) |
|
|
|
{ |
|
|
@@ -91,32 +92,46 @@ void LFR_Socket::createThread() |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
//std::cout << "accepted connection" << std::endl; |
|
|
|
std::cout << "accepted connection" << std::endl; |
|
|
|
pollfds[1].fd = new_socket; |
|
|
|
connected = true; |
|
|
|
} |
|
|
|
} |
|
|
|
else if (connected && pollfds[0].revents & POLLIN) |
|
|
|
{ |
|
|
|
//std::cout << "second connection incoming" << std::endl; |
|
|
|
std::cout << "second connection incoming" << std::endl; |
|
|
|
int tmp_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen); |
|
|
|
close(tmp_socket); |
|
|
|
//std::cout << "second connection closed" << std::endl; |
|
|
|
std::cout << "second connection closed" << std::endl; |
|
|
|
} |
|
|
|
else if (pollfds[1].revents & POLLIN) |
|
|
|
{ |
|
|
|
bytes_received = recv(new_socket, buffer, sizeof(buffer), 0); |
|
|
|
if (bytes_received > 0) |
|
|
|
LFR_Telegram tmpBuffer = {0}; |
|
|
|
bytesReceived = recv(new_socket, tmpBuffer, sizeof(tmpBuffer), 0); |
|
|
|
if (bytesReceived > 0) |
|
|
|
{ |
|
|
|
received = true; |
|
|
|
std::copy(std::begin(buffer), std::end(buffer), std::begin(telegram)); |
|
|
|
//std::cout << "message received" << std::endl; |
|
|
|
if(bufferIterator - std::begin(buffer) + bytesReceived > 1024) |
|
|
|
{ |
|
|
|
//Too long for buffer. Clear it. |
|
|
|
std::fill(std::begin(buffer), std::end(buffer), 0); |
|
|
|
bufferIterator = std::begin(buffer); |
|
|
|
} |
|
|
|
bufferIterator = std::copy_if( |
|
|
|
std::begin(tmpBuffer), |
|
|
|
std::begin(tmpBuffer)+bytesReceived, |
|
|
|
bufferIterator, |
|
|
|
[](char c){ |
|
|
|
return(c != '\n' && c != '\r'); |
|
|
|
}); |
|
|
|
|
|
|
|
//std::cout << "bytes received: " << bytesReceived << std::endl; |
|
|
|
//received = true; |
|
|
|
} |
|
|
|
else if(bytes_received == 0) |
|
|
|
else if(bytesReceived == 0) |
|
|
|
{ |
|
|
|
connected = false; |
|
|
|
close(pollfds[1].fd); |
|
|
|
//std::cout << "connection closed by peer" << std::endl; |
|
|
|
std::cout << "connection closed by peer" << std::endl; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@@ -131,6 +146,32 @@ void LFR_Socket::createThread() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Search the buffer for the start of a telegram |
|
|
|
char *telegramStart = NULL; |
|
|
|
telegramStart = strstr(buffer, "aa"); |
|
|
|
if(telegramStart && telegramStart != std::begin(buffer)) { |
|
|
|
//Move the content of the buffer |
|
|
|
auto delta = bufferIterator - telegramStart; |
|
|
|
std::copy(telegramStart, std::end(buffer), std::begin(buffer)); |
|
|
|
bufferIterator = std::begin(buffer) + delta; |
|
|
|
} |
|
|
|
|
|
|
|
// Search the buffer for the end of a telegram |
|
|
|
char *telegramEnd = NULL; |
|
|
|
telegramEnd = strstr(buffer, "zz"); |
|
|
|
if(telegramEnd && telegramStart && telegramEnd-telegramStart>2) { |
|
|
|
std::copy(telegramStart+2, telegramEnd, telegram); |
|
|
|
received = true; |
|
|
|
std::copy(telegramEnd+2, std::end(buffer), std::begin(buffer)); |
|
|
|
bufferIterator = std::begin(buffer); |
|
|
|
} |
|
|
|
else if(telegramEnd && telegramStart && telegramEnd-telegramStart<=2) |
|
|
|
{ |
|
|
|
//Got an empty telegram |
|
|
|
std::copy(telegramEnd, std::end(buffer), std::begin(buffer)); |
|
|
|
bufferIterator = std::begin(buffer); |
|
|
|
} |
|
|
|
|
|
|
|
//Invoke the callback method (ListenerPair second -> ListenerCallback) |
|
|
|
if (received) |
|
|
|
{ |