diff --git a/Socket/CMakeLists.txt b/Socket/CMakeLists.txt index ff8b458..5b86927 100644 --- a/Socket/CMakeLists.txt +++ b/Socket/CMakeLists.txt @@ -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}) diff --git a/Socket/lfr_socket.cpp b/Socket/lfr_socket.cpp index 3a94278..971dfd2 100644 --- a/Socket/lfr_socket.cpp +++ b/Socket/lfr_socket.cpp @@ -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 lock(mutex); - std::cerr<<"socket exception:"< 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; } \ No newline at end of file diff --git a/Socket/lfr_socket.h b/Socket/lfr_socket.h index 43e7ac6..4229d03 100644 --- a/Socket/lfr_socket.h +++ b/Socket/lfr_socket.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include diff --git a/Socket/lfr_socket_main.cpp b/Socket/lfr_socket_main.cpp new file mode 100644 index 0000000..760e5ff --- /dev/null +++ b/Socket/lfr_socket_main.cpp @@ -0,0 +1,33 @@ +#include "lfr_socket.h" + +int main() +{ + std::mutex mutex; + LFR_Socket socket([&](std::exception const &ex) + { + std::unique_lock lock(mutex); + std::cerr<<"socket exception:"< 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; +} \ No newline at end of file