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
33struct Peer {
34 std::string ip;
35 uint16_t port;
36
37 Peer() : port(0) {}
38 Peer(const std::string& ip, uint16_t port) : ip(ip), port(port) {}
39
40 bool operator==(const Peer& other) const {
41 return ip == other.ip && port == other.port;
42 }
43
44 bool operator!=(const Peer& other) const {
45 return !(*this == other);
46 }
47};
48
49// Socket Library Initialization
55
60
61// TCP Socket Functions
69socket_t create_tcp_client(const std::string& host, int port, int timeout_ms = 0);
70
78socket_t create_tcp_client_v4(const std::string& host, int port, int timeout_ms = 0);
79
87socket_t create_tcp_client_v6(const std::string& host, int port, int timeout_ms = 0);
88
96socket_t create_tcp_server(int port, int backlog = 5, const std::string& bind_address = "");
97
105socket_t create_tcp_server_v4(int port, int backlog = 5, const std::string& bind_address = "");
106
114socket_t create_tcp_server_v6(int port, int backlog = 5, const std::string& bind_address = "");
115
122
128std::string get_peer_address(socket_t socket);
129
136int send_tcp_data(socket_t socket, const std::vector<uint8_t>& data);
137
144std::vector<uint8_t> receive_tcp_data(socket_t socket, size_t buffer_size = 1024);
145
152int send_tcp_message_framed(socket_t socket, const std::vector<uint8_t>& message);
153
160std::vector<uint8_t> receive_exact_bytes(socket_t socket, size_t num_bytes);
161
167std::vector<uint8_t> receive_tcp_message_framed(socket_t socket);
168
169// Convenience functions for string compatibility
176int send_tcp_string(socket_t socket, const std::string& data);
177
184std::string receive_tcp_string(socket_t socket, size_t buffer_size = 1024);
185
192int send_tcp_string_framed(socket_t socket, const std::string& message);
193
200
201// UDP Socket Functions
208socket_t create_udp_socket(int port = 0, const std::string& bind_address = "");
209
216socket_t create_udp_socket_v4(int port = 0, const std::string& bind_address = "");
217
224socket_t create_udp_socket_v6(int port = 0, const std::string& bind_address = "");
225
233int send_udp_data(socket_t socket, const std::vector<uint8_t>& data, const Peer& peer);
234
243int send_udp_data_to(socket_t socket, const std::vector<uint8_t>& data, const std::string& hostname, int port);
244
252std::vector<uint8_t> receive_udp_data(socket_t socket, size_t buffer_size, Peer& sender_peer);
253
263std::vector<uint8_t> receive_udp_data_with_timeout(socket_t socket, size_t buffer_size, int timeout_ms,
264 std::string* sender_ip = nullptr, int* sender_port = nullptr);
265
266// Common Socket Functions
271void close_socket(socket_t socket, bool force = false);
272
279
286
293
300
309bool connect_with_timeout(socket_t socket, struct sockaddr* addr, socklen_t addr_len, int timeout_ms);
310
317
318} // namespace librats
int send_udp_data_to(socket_t socket, const std::vector< uint8_t > &data, const std::string &hostname, int port)
Send UDP data to a hostname and port directly.
int send_tcp_string_framed(socket_t socket, const std::string &message)
Send a framed string message through a TCP socket.
std::string receive_tcp_string_framed(socket_t socket)
Receive a complete framed string message from a TCP socket.
socket_t create_tcp_client_v6(const std::string &host, int port, int timeout_ms=0)
Create a TCP client socket and connect to a server using IPv6 only.
socket_t create_udp_socket(int port=0, const std::string &bind_address="")
Create a UDP socket with dual stack support (IPv6 with IPv4 support)
socket_t create_tcp_server_v4(int port, int backlog=5, const std::string &bind_address="")
Create a TCP server socket and bind to a port using IPv4 only.
int get_ephemeral_port(socket_t socket)
Get the ephemeral port that a socket is bound to.
socket_t create_udp_socket_v6(int port=0, const std::string &bind_address="")
Create a UDP socket with IPv6 support only.
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_message_framed(socket_t socket, const std::vector< uint8_t > &message)
Send a framed message with length prefix through a TCP socket.
int send_tcp_string(socket_t socket, const std::string &data)
Send string data through a TCP socket (converts to binary)
void close_socket(socket_t socket, bool force=false)
Close a socket.
bool is_tcp_socket(socket_t socket)
Check if a socket is a TCP 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)
socket_t create_tcp_server(int port, int backlog=5, const std::string &bind_address="")
Create a TCP server socket and bind to a port using dual stack (IPv6 with IPv4 support)
std::vector< uint8_t > receive_tcp_data(socket_t socket, size_t buffer_size=1024)
Receive data from a TCP socket.
std::vector< uint8_t > receive_tcp_message_framed(socket_t socket)
Receive a complete framed message 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.
socket_t create_tcp_server_v6(int port, int backlog=5, const std::string &bind_address="")
Create a TCP server socket and bind to a port using IPv6 only.
bool is_valid_socket(socket_t socket)
Check if a socket is valid.
bool set_socket_blocking(socket_t socket)
Set socket to blocking mode.
std::vector< uint8_t > receive_udp_data(socket_t socket, size_t buffer_size, Peer &sender_peer)
Receive UDP data from a peer.
socket_t accept_client(socket_t server_socket)
Accept a client connection on a server socket.
void cleanup_socket_library()
Cleanup the socket library.
std::vector< uint8_t > receive_exact_bytes(socket_t socket, size_t num_bytes)
Receive exact number of bytes from a TCP socket (blocking until complete)
int send_udp_data(socket_t socket, const std::vector< uint8_t > &data, const Peer &peer)
Send UDP data to a peer.
socket_t create_tcp_client_v4(const std::string &host, int port, int timeout_ms=0)
Create a TCP client socket and connect to a server using IPv4 only.
socket_t create_udp_socket_v4(int port=0, const std::string &bind_address="")
Create a UDP socket with IPv4 support only.
std::string get_peer_address(socket_t socket)
Get the peer address (IP:port) from a connected socket.
std::vector< uint8_t > receive_udp_data_with_timeout(socket_t socket, size_t buffer_size, int timeout_ms, std::string *sender_ip=nullptr, int *sender_port=nullptr)
Receive UDP data with timeout support.
std::string receive_tcp_string(socket_t socket, size_t buffer_size=1024)
Receive data from a TCP socket as string.
int socket_t
Definition socket.h:22
UDP peer information.
Definition socket.h:33
uint16_t port
Definition socket.h:35
Peer(const std::string &ip, uint16_t port)
Definition socket.h:38
bool operator!=(const Peer &other) const
Definition socket.h:44
std::string ip
Definition socket.h:34
bool operator==(const Peer &other) const
Definition socket.h:40