Main method in separate file; Better cmake; Bugfix for calling destructor; Handle closing connections, reconnecting and refusing multiple connections
This commit is contained in:
parent
0218391ea0
commit
3149466a37
@ -8,9 +8,13 @@ set(THREADS_PREFER_PTHREAD_FLAG ON)
|
||||
|
||||
find_package(Threads REQUIRED)
|
||||
|
||||
add_executable(lfr_socket lfr_socket.cpp)
|
||||
add_executable(lfr_socket lfr_socket.cpp lfr_socket_main.cpp)
|
||||
add_library(lfr_socket_lib lfr_socket.cpp)
|
||||
|
||||
target_link_libraries( lfr_socket Threads::Threads)
|
||||
target_link_libraries( lfr_socket_lib Threads::Threads)
|
||||
target_include_directories(lfr_socket PUBLIC .)
|
||||
target_include_directories(lfr_socket_lib PUBLIC .)
|
||||
|
||||
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
|
||||
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
|
||||
|
@ -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,8 +30,11 @@ LFR_Socket::LFR_Socket(ExceptionCallback cb): cb(cb)
|
||||
LFR_Socket::~LFR_Socket()
|
||||
{
|
||||
endLoop();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -147,35 +165,3 @@ 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;
|
||||
}
|
@ -8,6 +8,7 @@
|
||||
#include <thread>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
#include <poll.h>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
|
33
Socket/lfr_socket_main.cpp
Normal file
33
Socket/lfr_socket_main.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include "lfr_socket.h"
|
||||
|
||||
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 << "fress 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;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user