|
|
|
|
|
|
|
|
throw std::runtime_error("Failed binding a name to the socket"); |
|
|
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"); |
|
|
throw std::runtime_error("Failed listening for connections"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
LFR_Socket::~LFR_Socket() |
|
|
LFR_Socket::~LFR_Socket() |
|
|
{ |
|
|
{ |
|
|
endLoop(); |
|
|
endLoop(); |
|
|
thread->join(); |
|
|
|
|
|
|
|
|
if (thread) |
|
|
|
|
|
{ |
|
|
|
|
|
thread->join(); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void LFR_Socket::removeListener(LFR_Socket::ListenerKey key) |
|
|
void LFR_Socket::removeListener(LFR_Socket::ListenerKey key) |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
{ |
|
|
{ |
|
|
|
|
|
//std::cout << "accepted connection" << std::endl; |
|
|
pollfds[1].fd = new_socket; |
|
|
pollfds[1].fd = new_socket; |
|
|
connected = true; |
|
|
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) |
|
|
else if (pollfds[1].revents & POLLIN) |
|
|
{ |
|
|
{ |
|
|
bytes_received = recv(new_socket, buffer, sizeof(buffer), 0); |
|
|
bytes_received = recv(new_socket, buffer, sizeof(buffer), 0); |
|
|
|
|
|
|
|
|
{ |
|
|
{ |
|
|
received = true; |
|
|
received = true; |
|
|
std::copy(std::begin(buffer), std::end(buffer), std::begin(telegram)); |
|
|
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; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void LFR_Socket::endLoop() |
|
|
void LFR_Socket::endLoop() |
|
|
{ |
|
|
{ |
|
|
setStop(true); |
|
|
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; |
|
|
|
|
|
} |
|
|
} |