EasySockets v2.2.1
A simple, cross platform socket library for C++.
Loading...
Searching...
No Matches
WindowsSocket.hpp
1#pragma once
2
3#include <winsock2.h>
4#include <ws2tcpip.h>
5#include <EasySockets/Api/SocketTypes.hpp>
6#include <EasySockets/Api/Addresses.hpp>
7#include <EasySockets/Api/Documentation.hpp>
8
9namespace es
10{
12 ES_API_DOC(socket)
25 {
26 public:
27 ES_API_DOC(socket_default_constructor)
33
34 ES_API_DOC(socket_constructor)
40 WindowsSocket(IpVersion ip_version, Protocol protocol);
41
43
44 WindowsSocket(const WindowsSocket&) = delete;
45
47
48 WindowsSocket(WindowsSocket&& other) noexcept;
49
50 WindowsSocket& operator=(WindowsSocket&& other) noexcept;
51
52 ES_API_DOC(socket_bind_to)
59 void bind_to(const EndPoint& end_point);
60
61 ES_API_DOC(socket_connect_to)
66 void connect_to(const EndPoint& end_point);
67
68 ES_API_DOC(socket_listen_for_connections)
78 void listen_for_connections(int backlog);
79
80 ES_API_DOC(socket_accept_connection)
88
89 ES_API_DOC(socket_receive_data)
97 int64_t receive_data(char* buffer, int buffer_size);
98
99 ES_API_DOC(socket_receive_data_from)
109 int64_t receive_data_from(char* buffer, int buffer_size, EndPoint& sender_end_point);
110
111 ES_API_DOC(socket_send_data)
119 int64_t send_data(const char* buffer, int buffer_size);
120
121 ES_API_DOC(socket_send_data_to)
130 int64_t send_data_to(const char* buffer, int buffer_size, const EndPoint& end_point);
131
132 ES_API_DOC(socket_close)
139 void close();
140
141 ES_API_DOC(make_connected_tcp)
147 static WindowsSocket make_connected_tcp(const EndPoint& end_point);
148
149 ES_API_DOC(make_connected_udp)
155 static WindowsSocket make_connected_udp(const EndPoint& end_point);
156
157 ES_API_DOC(make_bound_tcp)
166
167 ES_API_DOC(make_bound_udp)
176
177 private:
178 // data that winsock needs for the socket
179 struct WinsockData
180 {
181 int af;
182 int type;
183 int protocol;
184 };
185
186 // must free with freeaddrinfo
187 static addrinfo* resolve_address(const WinsockData& winsock_data, const EndPoint& end_point, int flags);
188
189 SOCKET m_socket;
190 WinsockData m_winsock_data;
191 };
192}
static WindowsSocket make_bound_udp(Port port, IpVersion ip_version=IpVersion::DUAL_STACK)
Makes a UDP socket bound to the given port.
Definition WindowsSocket.cpp:317
int64_t receive_data(char *buffer, int buffer_size)
Waits to receive data from the connected end point.
Definition WindowsSocket.cpp:144
void close()
Closes the socket.
Definition WindowsSocket.cpp:217
static WindowsSocket make_connected_udp(const EndPoint &end_point)
Makes a UDP socket connected to the given endpoint.
Definition WindowsSocket.cpp:273
static WindowsSocket make_bound_tcp(Port port, IpVersion ip_version=IpVersion::DUAL_STACK)
Makes a TCP socket bound to the given port.
Definition WindowsSocket.cpp:303
WindowsSocket(const WindowsSocket &)=delete
WindowsSocket()
Default constructs a socket.
Definition WindowsSocket.cpp:10
int64_t receive_data_from(char *buffer, int buffer_size, EndPoint &sender_end_point)
Waits to receive data from any endpoint.
Definition WindowsSocket.cpp:154
static WindowsSocket make_connected_tcp(const EndPoint &end_point)
Makes a TCP socket connected to the given endpoint.
Definition WindowsSocket.cpp:243
void listen_for_connections(int backlog)
Makes the socket start listening for incoming connections.
Definition WindowsSocket.cpp:122
WindowsSocket & operator=(const WindowsSocket &)=delete
int64_t send_data(const char *buffer, int buffer_size)
Sends data to the connected end point.
Definition WindowsSocket.cpp:186
WindowsSocket accept_connection()
Accepts incoming connections.
Definition WindowsSocket.cpp:130
void connect_to(const EndPoint &end_point)
Connects a socket to the given end point.
Definition WindowsSocket.cpp:101
int64_t send_data_to(const char *buffer, int buffer_size, const EndPoint &end_point)
Sends data to the provided end point.
Definition WindowsSocket.cpp:196
void bind_to(const EndPoint &end_point)
Binds a socket to the given end point.
Definition WindowsSocket.cpp:80
Protocol
Layer 4/transport layer protocol.
Definition SocketTypes.hpp:26
IpVersion
Version of IP address.
Definition SocketTypes.hpp:11
uint16_t Port
16-bit port number.
Definition Addresses.hpp:20
@ DUAL_STACK
An IPv6 address which allows for conversions from IPv4.
Definition SocketTypes.hpp:17
Definition Addresses.hpp:7
Represents the address + port that identifies a connection. Initialized by {Address,...
Definition Addresses.hpp:29