Browse Source

Main method in separate file; Better cmake; Bugfix for calling destructor; Handle closing connections, reconnecting and refusing multiple connections

master
Tim Zeuner 1 year ago
parent
commit
3149466a37
4 changed files with 59 additions and 35 deletions
  1. 5
    1
      Socket/CMakeLists.txt
  2. 20
    34
      Socket/lfr_socket.cpp
  3. 1
    0
      Socket/lfr_socket.h
  4. 33
    0
      Socket/lfr_socket_main.cpp

+ 5
- 1
Socket/CMakeLists.txt View File



find_package(Threads REQUIRED) 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 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_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})

+ 20
- 34
Socket/lfr_socket.cpp View File

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;
} }

+ 1
- 0
Socket/lfr_socket.h View File

#include <thread> #include <thread>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <unistd.h>
#include <poll.h> #include <poll.h>
#include <string> #include <string>
#include <algorithm> #include <algorithm>

+ 33
- 0
Socket/lfr_socket_main.cpp View File

#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…
Cancel
Save