-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
250 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
#ifndef SIMPLE_SOCKET_UDPSOCKET_HPP | ||
#define SIMPLE_SOCKET_UDPSOCKET_HPP | ||
|
||
|
||
#include <memory> | ||
#include <string> | ||
#include <vector> | ||
|
||
class UDPSocket{ | ||
public: | ||
UDPSocket(int port); | ||
|
||
bool sendTo(const std::string& address, uint16_t port, const std::string& data); | ||
|
||
int recvFrom(const std::string& address, uint16_t port, std::vector<unsigned char>& buffer); | ||
|
||
void close(); | ||
|
||
~UDPSocket(); | ||
|
||
private: | ||
struct Impl; | ||
std::unique_ptr<Impl> pimpl_; | ||
}; | ||
|
||
#endif//SIMPLE_SOCKET_UDPSOCKET_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
|
||
#include "UDPSocket.hpp" | ||
|
||
#ifdef _WIN32 | ||
#include <WS2tcpip.h> | ||
#include <winsock2.h> | ||
#else | ||
#include <arpa/inet.h> | ||
#include <sys/socket.h> | ||
#include <unistd.h> | ||
#define SOCKET int | ||
#define INVALID_SOCKET (SOCKET)(~0) | ||
#define SOCKET_ERROR (-1) | ||
#endif | ||
|
||
#include <stdexcept> | ||
#include <system_error> | ||
|
||
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 | ||
|
||
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"); | ||
#else | ||
throw std::system_error(errno, std::generic_category(), "Failed to create socket"); | ||
#endif | ||
} | ||
|
||
sockaddr_in local{}; | ||
local.sin_family = AF_INET; | ||
local.sin_addr.s_addr = INADDR_ANY; | ||
local.sin_port = htons(port); | ||
|
||
if (::bind(sockfd, (sockaddr*) &local, sizeof(local)) == SOCKET_ERROR) { | ||
#if WIN32 | ||
throw std::system_error(WSAGetLastError(), std::system_category(), "Bind failed"); | ||
#else | ||
throw std::system_error(errno, std::generic_category(), "Bind failed"); | ||
#endif | ||
} | ||
} | ||
|
||
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; | ||
to.sin_port = htons(port); | ||
if (!inet_pton(AF_INET, address.c_str(), &to.sin_addr)) { | ||
return false; | ||
} | ||
|
||
return sendto(sockfd, data.c_str(), data.size(), 0, (sockaddr*) &to, sizeof(to)) != SOCKET_ERROR; | ||
} | ||
|
||
int recvFrom(const std::string& address, uint16_t port, std::vector<unsigned char>& buffer) { | ||
|
||
sockaddr_in from{}; | ||
from.sin_family = AF_INET; | ||
from.sin_port = htons(port); | ||
if (!inet_pton(AF_INET, address.c_str(), &from.sin_addr)) { | ||
return false; | ||
} | ||
socklen_t fromLength = sizeof(from); | ||
|
||
int receive = recvfrom(sockfd, reinterpret_cast<char*>(buffer.data()), buffer.size(), 0, (sockaddr*) &from, &fromLength); | ||
if (receive == SOCKET_ERROR) { | ||
return -1; | ||
} | ||
|
||
return receive; | ||
} | ||
|
||
void close() { | ||
if (!closed) { | ||
closed = true; | ||
#ifdef _WIN32 | ||
closesocket(sockfd); | ||
#else | ||
::close(sockfd); | ||
#endif | ||
} | ||
} | ||
~Impl() { | ||
close(); | ||
#ifdef _WIN32 | ||
WSACleanup(); | ||
#endif | ||
} | ||
|
||
private: | ||
bool closed{false}; | ||
SOCKET sockfd; | ||
}; | ||
|
||
|
||
UDPSocket::UDPSocket(int port) | ||
: pimpl_(std::make_unique<Impl>(port)) {} | ||
|
||
bool UDPSocket::sendTo(const std::string& address, uint16_t port, const std::string& data) { | ||
|
||
return pimpl_->sendTo(address, port, data); | ||
} | ||
|
||
int UDPSocket::recvFrom(const std::string& address, uint16_t port, std::vector<unsigned char>& buffer) { | ||
|
||
return pimpl_->recvFrom(address, port, buffer); | ||
} | ||
|
||
void UDPSocket::close() { | ||
|
||
pimpl_->close(); | ||
} | ||
|
||
UDPSocket::~UDPSocket() = default; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
|
||
add_executable(test_socketlib test_socketlib.cpp) | ||
target_link_libraries(test_socketlib PRIVATE simple_socket Catch2::Catch2WithMain) | ||
add_executable(test_tcp test_tcp.cpp) | ||
target_link_libraries(test_tcp PRIVATE simple_socket Catch2::Catch2WithMain) | ||
|
||
|
||
add_executable(test_udp test_udp.cpp) | ||
target_link_libraries(test_udp PRIVATE simple_socket Catch2::Catch2WithMain) | ||
|
||
add_subdirectory(integration) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
|
||
add_executable(run_server run_server.cpp) | ||
target_link_libraries(run_server PRIVATE simple_socket) | ||
add_executable(run_tcp_server run_tcp_server.cpp) | ||
target_link_libraries(run_tcp_server PRIVATE simple_socket) | ||
|
||
add_executable(run_client run_client.cpp) | ||
target_link_libraries(run_client PRIVATE simple_socket) | ||
add_executable(run_tcp_client run_tcp_client.cpp) | ||
target_link_libraries(run_tcp_client PRIVATE simple_socket) | ||
|
||
if (UNIX) | ||
target_link_libraries(run_server PRIVATE pthread) | ||
target_link_libraries(run_client PRIVATE pthread) | ||
target_link_libraries(run_tcp_server PRIVATE pthread) | ||
target_link_libraries(run_tcp_client PRIVATE pthread) | ||
endif () |
Oops, something went wrong.