|
|
@@ -21,7 +21,7 @@ LFR_Socket::LFR_Socket(ExceptionCallback cb): cb(cb) |
|
|
|
throw std::runtime_error("Failed binding a name to the socket"); |
|
|
|
} |
|
|
|
|
|
|
|
if (listen(server_fd, 3) != 0) |
|
|
|
if (listen(server_fd, 0) != 0) |
|
|
|
{ |
|
|
|
throw std::runtime_error("Failed listening for connections"); |
|
|
|
} |
|
|
@@ -30,7 +30,10 @@ LFR_Socket::LFR_Socket(ExceptionCallback cb): cb(cb) |
|
|
|
LFR_Socket::~LFR_Socket() |
|
|
|
{ |
|
|
|
endLoop(); |
|
|
|
thread->join(); |
|
|
|
if (thread) |
|
|
|
{ |
|
|
|
thread->join(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void LFR_Socket::removeListener(LFR_Socket::ListenerKey key) |
|
|
@@ -88,10 +91,18 @@ void LFR_Socket::createThread() |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
//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; |
|
|
|
int tmp_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen); |
|
|
|
close(tmp_socket); |
|
|
|
//std::cout << "second connection closed" << std::endl; |
|
|
|
} |
|
|
|
else if (pollfds[1].revents & POLLIN) |
|
|
|
{ |
|
|
|
bytes_received = recv(new_socket, buffer, sizeof(buffer), 0); |
|
|
@@ -99,6 +110,13 @@ void LFR_Socket::createThread() |
|
|
|
{ |
|
|
|
received = true; |
|
|
|
std::copy(std::begin(buffer), std::end(buffer), std::begin(telegram)); |
|
|
|
//std::cout << "message received" << std::endl; |
|
|
|
} |
|
|
|
else if(bytes_received == 0) |
|
|
|
{ |
|
|
|
connected = false; |
|
|
|
close(pollfds[1].fd); |
|
|
|
//std::cout << "connection closed by peer" << std::endl; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@@ -146,36 +164,4 @@ void LFR_Socket::startLoop() |
|
|
|
void LFR_Socket::endLoop() |
|
|
|
{ |
|
|
|
setStop(true); |
|
|
|
} |
|
|
|
|
|
|
|
int main() |
|
|
|
{ |
|
|
|
std::mutex mutex; |
|
|
|
LFR_Socket socket([&](std::exception const &ex) |
|
|
|
{ |
|
|
|
std::unique_lock<std::mutex> lock(mutex); |
|
|
|
std::cerr<<"socket exception:"<<ex.what()<<std::endl; |
|
|
|
return false; |
|
|
|
}); |
|
|
|
|
|
|
|
socket.addListener([&](LFR_Socket::LFR_Telegram telegram) |
|
|
|
{ |
|
|
|
std::unique_lock<std::mutex> lock(mutex); |
|
|
|
std::cout << telegram; |
|
|
|
}, &mutex); |
|
|
|
|
|
|
|
socket.startLoop(); |
|
|
|
|
|
|
|
//send(new_socket, "Hello from the server", sizeof("Hello from the server"), 0); |
|
|
|
char input; |
|
|
|
std::cout << "press q to quit" << std::endl; |
|
|
|
std::cin >> input; |
|
|
|
std::cout << "cinned" << std::endl; |
|
|
|
while (input != 'q') |
|
|
|
{ |
|
|
|
std::cin >> input; |
|
|
|
std::cout << "cinned" << std::endl; |
|
|
|
} |
|
|
|
std::cout << "im out" << std::endl; |
|
|
|
return 0; |
|
|
|
} |