Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed May 15, 2024
1 parent 981a61e commit f7bfdb2
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 32 deletions.
2 changes: 1 addition & 1 deletion include/UDPSocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class UDPSocket{
public:
UDPSocket(int port);
explicit UDPSocket(int port);

bool sendTo(const std::string& address, uint16_t port, const std::string& data);

Expand Down
17 changes: 17 additions & 0 deletions include/WSASession.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

#ifndef SIMPLE_SOCKET_WSASESSION_HPP
#define SIMPLE_SOCKET_WSASESSION_HPP

#include <memory>

class WSASession {
public:
WSASession();
~WSASession();

private:
struct Impl;
std::unique_ptr<Impl> pimpl_;
};

#endif//SIMPLE_SOCKET_WSASESSION_HPP
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

add_library(simple_socket TCPSocket.cpp UDPSocket.cpp)
add_library(simple_socket TCPSocket.cpp UDPSocket.cpp WSASession.cpp)
target_compile_features(simple_socket PUBLIC "cxx_std_17")

if (WIN32)
Expand Down
19 changes: 9 additions & 10 deletions src/TCPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@
#include <sys/socket.h>
#include <unistd.h>
using SOCKET = int;
#define INVALID_SOCKET (SOCKET)(~0)
#define SOCKET_ERROR (-1)
#endif

#include <iostream>
#include <stdexcept>

struct TCPSocket::Impl {

Impl() {
Impl(): sockfd(socket(AF_INET, SOCK_STREAM, 0)) {
if (sockfd == INVALID_SOCKET) {
#ifdef _WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
throw std::runtime_error("Failed to initialize winsock");
}
throw std::system_error(WSAGetLastError(), std::system_category(), "Failed to create socket");
#else
throw std::system_error(errno, std::generic_category(), "Failed to create socket");
#endif
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
}
}

bool connect(const std::string& ip, int port) {
Expand Down Expand Up @@ -58,6 +60,7 @@ struct TCPSocket::Impl {
void listen(int backlog) {

if (::listen(sockfd, backlog) < 0) {

throw std::runtime_error("Listen failed");
}
}
Expand Down Expand Up @@ -163,10 +166,6 @@ struct TCPSocket::Impl {
~Impl() {

close();

#ifdef _WIN32
WSACleanup();
#endif
}

private:
Expand Down
16 changes: 1 addition & 15 deletions src/UDPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,8 @@

struct UDPSocket::Impl {

explicit Impl(int port) {
#ifdef _WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {

throw std::system_error(WSAGetLastError(), std::system_category(), "Failed to initialize winsock");
}
#endif
explicit Impl(int port): sockfd(socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) {

sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sockfd == INVALID_SOCKET) {
#ifdef _WIN32
throw std::system_error(WSAGetLastError(), std::system_category(), "Failed to create socket");
Expand All @@ -50,9 +42,6 @@ struct UDPSocket::Impl {
}
}

void bind(int port) {
}

bool sendTo(const std::string& address, uint16_t port, const std::string& data) {
sockaddr_in to{};
to.sin_family = AF_INET;
Expand Down Expand Up @@ -94,9 +83,6 @@ struct UDPSocket::Impl {
}
~Impl() {
close();
#ifdef _WIN32
WSACleanup();
#endif
}

private:
Expand Down
29 changes: 29 additions & 0 deletions src/WSASession.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

#include "WSASession.hpp"

#if WIN32
#include <WinSock2.h>
#include <system_error>
#endif

struct WSASession::Impl {
Impl() {
#if WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {

throw std::system_error(WSAGetLastError(), std::system_category(), "Failed to initialize winsock");
}
#endif
}

~Impl() {
#if WIN32
WSACleanup();
#endif
}
};

WSASession::WSASession() : pimpl_(std::make_unique<Impl>()) {}

WSASession::~WSASession() = default;
3 changes: 3 additions & 0 deletions tests/integration/run_tcp_client.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@

#include "TCPSocket.hpp"
#include "WSASession.hpp"

#include <iostream>
#include <vector>

int main() {

WSASession session;

TCPClient client;
if (client.connect("127.0.0.1", 8080)) {

Expand Down
7 changes: 4 additions & 3 deletions tests/integration/run_tcp_server.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "TCPSocket.hpp"
#include "WSASession.hpp"

#include <atomic>
#include <iostream>
Expand All @@ -17,9 +18,9 @@ void socketHandler(std::unique_ptr<Connection> conn) {

int main() {

TCPServer server;
server.bind(8080);
server.listen();
WSASession session;

TCPServer server(8080);

std::atomic_bool stop = false;
std::thread t([&] {
Expand Down
7 changes: 5 additions & 2 deletions tests/test_tcp.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include "TCPSocket.hpp"
#include "WSASession.hpp"

#include <thread>
#include <vector>
Expand Down Expand Up @@ -39,11 +40,12 @@ TEST_CASE("TCP read/write") {

int port = 8080;

WSASession session;

TCPServer server(port);
TCPClient client;

std::thread serverThread([&server] {

std::unique_ptr<Connection> conn;
REQUIRE_NOTHROW(conn = server.accept());
socketHandler(std::move(conn));
Expand Down Expand Up @@ -78,11 +80,12 @@ TEST_CASE("TCP readexact/write") {

int port = 8080;

WSASession session;

TCPServer server(port);
TCPClient client;

std::thread serverThread([&server] {

std::unique_ptr<Connection> conn;
REQUIRE_NOTHROW(conn = server.accept());
socketHandler(std::move(conn));
Expand Down
3 changes: 3 additions & 0 deletions tests/test_udp.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@

#include "UDPSocket.hpp"
#include "WSASession.hpp"

#include <catch2/catch_test_macros.hpp>

TEST_CASE("Test UDP") {

WSASession session;

std::string address{"127.0.0.1"};
int serverPort = 8070;
int clientPort = 8071;
Expand Down

0 comments on commit f7bfdb2

Please sign in to comment.