Back to Site
Loading...
Searching...
No Matches
socket.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <functional>
5#include <vector>
6#include <cstdint>
7
8#ifdef _WIN32
9 #include <winsock2.h>
10 #include <ws2tcpip.h>
11 #ifdef _MSC_VER
12 #pragma comment(lib, "ws2_32.lib")
13 #endif
14 typedef SOCKET socket_t;
15 #define INVALID_SOCKET_VALUE INVALID_SOCKET
16 #define SOCKET_ERROR_VALUE SOCKET_ERROR
17#else
18 #include <sys/socket.h>
19 #include <arpa/inet.h>
20 #include <netinet/in.h>
21 #include <unistd.h>
22 typedef int socket_t;
23 #define INVALID_SOCKET_VALUE -1
24 #define SOCKET_ERROR_VALUE -1
25 #define closesocket close
26#endif
27
28namespace librats {
29
33enum class AddressFamily {
34 IPv4, // IPv4 only
35 IPv6, // IPv6 only
36 DualStack // IPv6 socket with IPv4 support (default)
37};
38
42struct Peer {
43 std::string ip;
44 uint16_t port;
45
46 Peer() : port(0) {}
47 Peer(const std::string& ip, uint16_t port) : ip(ip), port(port) {}
48
49 bool operator==(const Peer& other) const {
50 return ip == other.ip && port == other.port;
51 }
52
53 bool operator!=(const Peer& other) const {
54 return !(*this == other);
55 }
56};
57
58// Socket Library Initialization
64
69
70// TCP Socket Functions
78socket_t create_tcp_client(const std::string& host, int port, int timeout_ms = 0);
79
88socket_t create_tcp_server(int port, int backlog = 5, const std::string& bind_address = "",
90
97
103std::string get_peer_address(socket_t socket);
104
111int send_tcp_data(socket_t socket, const std::vector<uint8_t>& data);
112
119std::vector<uint8_t> receive_tcp_data(socket_t socket, size_t buffer_size = 1024);
120
127int send_tcp_message(socket_t socket, const std::vector<uint8_t>& message);
128
134std::vector<uint8_t> receive_tcp_message(socket_t socket);
135
142int send_tcp_string(socket_t socket, const std::string& data);
143
144// UDP Socket Functions
152socket_t create_udp_socket(int port = 0, const std::string& bind_address = "",
154
164int send_udp_data(socket_t socket, const std::vector<uint8_t>& data, const std::string& host, int port,
166
175std::vector<uint8_t> receive_udp_data(socket_t socket, size_t buffer_size, Peer& sender_peer,
176 int timeout_ms = -1);
177
178// Common Socket Functions
184void close_socket(socket_t socket, bool force = false);
185
192
199
206
215bool connect_with_timeout(socket_t socket, struct sockaddr* addr, socklen_t addr_len, int timeout_ms);
216
223
224} // namespace librats
int get_bound_port(socket_t socket)
Get the port that a socket is bound to.
bool connect_with_timeout(socket_t socket, struct sockaddr *addr, socklen_t addr_len, int timeout_ms)
Connect to a socket address with timeout.
bool set_socket_nonblocking(socket_t socket)
Set socket to non-blocking mode.
int send_tcp_string(socket_t socket, const std::string &data)
Send string data through a TCP socket (converts to binary)
std::vector< uint8_t > receive_udp_data(socket_t socket, size_t buffer_size, Peer &sender_peer, int timeout_ms=-1)
Receive UDP data with optional timeout.
void close_socket(socket_t socket, bool force=false)
Close a socket.
socket_t create_tcp_client(const std::string &host, int port, int timeout_ms=0)
Create a TCP client socket and connect to a server using dual stack (IPv6 with IPv4 fallback)
std::vector< uint8_t > receive_tcp_data(socket_t socket, size_t buffer_size=1024)
Receive data from a TCP socket.
int send_tcp_data(socket_t socket, const std::vector< uint8_t > &data)
Send data through a TCP socket.
bool init_socket_library()
Initialize the socket library.
bool is_valid_socket(socket_t socket)
Check if a socket is valid.
std::vector< uint8_t > receive_tcp_message(socket_t socket)
Receive a complete length-prefixed framed message from a TCP socket.
socket_t create_udp_socket(int port=0, const std::string &bind_address="", AddressFamily af=AddressFamily::DualStack)
Create a UDP socket and bind to a port.
socket_t create_tcp_server(int port, int backlog=5, const std::string &bind_address="", AddressFamily af=AddressFamily::DualStack)
Create a TCP server socket and bind to a port.
bool set_socket_blocking(socket_t socket)
Set socket to blocking mode.
int send_udp_data(socket_t socket, const std::vector< uint8_t > &data, const std::string &host, int port, AddressFamily af=AddressFamily::DualStack)
Send UDP data to a destination host and port.
socket_t accept_client(socket_t server_socket)
Accept a client connection on a server socket.
void cleanup_socket_library()
Cleanup the socket library.
int send_tcp_message(socket_t socket, const std::vector< uint8_t > &message)
Send a length-prefixed framed message through a TCP socket.
AddressFamily
Address family for socket creation.
Definition socket.h:33
std::string get_peer_address(socket_t socket)
Get the peer address (IP:port) from a connected socket.
int socket_t
Definition socket.h:22
UDP peer information.
Definition socket.h:42
uint16_t port
Definition socket.h:44
Peer(const std::string &ip, uint16_t port)
Definition socket.h:47
bool operator!=(const Peer &other) const
Definition socket.h:53
std::string ip
Definition socket.h:43
bool operator==(const Peer &other) const
Definition socket.h:49