Sane C++ Libraries
C++ Platform Abstraction Libraries
Socket.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "SocketDescriptor.h"
5
6#include "../Foundation/Span.h"
7#include "../Time/Time.h" // Milliseconds
8
9namespace SC
10{
11struct SocketNetworking;
12struct SocketClient;
13struct SocketServer;
14struct SocketDNS;
15} // namespace SC
16
19
25{
28 SocketServer(SocketDescriptor& socket) : socket(socket) {}
29
32 [[nodiscard]] Result close();
33
37 [[nodiscard]] Result bind(SocketIPAddress nativeAddress);
38
43 [[nodiscard]] Result listen(uint32_t numberOfWaitingConnections);
44
49 [[nodiscard]] Result accept(SocketFlags::AddressFamily addressFamily, SocketDescriptor& newClient);
50
51 private:
52 SocketDescriptor& socket;
53};
54
66{
69 SocketClient(const SocketDescriptor& socket) : socket(socket) {}
70
76 [[nodiscard]] Result connect(SpanStringView address, uint16_t port);
77
81 [[nodiscard]] Result connect(SocketIPAddress ipAddress);
82
86 [[nodiscard]] Result write(Span<const char> data);
87
92 [[nodiscard]] Result read(Span<char> data, Span<char>& readData);
93
99 [[nodiscard]] Result readWithTimeout(Span<char> data, Span<char>& readData, Time::Milliseconds timeout);
100
101 private:
102 const SocketDescriptor& socket;
103};
104
110{
118 [[nodiscard]] static Result resolveDNS(SpanStringView host, SpanString& ipAddress);
119};
120
123{
126 [[nodiscard]] static Result initNetworking();
127
130 [[nodiscard]] static Result shutdownNetworking();
131
134 [[nodiscard]] static bool isNetworkingInited();
135
136 private:
137 struct Internal;
138};
139
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition: PrimitiveTypes.h:38
unsigned short uint16_t
Platform independent (2) bytes unsigned int.
Definition: PrimitiveTypes.h:37
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition: Result.h:12
Use a SocketDescriptor as a client (example a TCP or UDP socket client).
Definition: Socket.h:66
Result read(Span< char > data, Span< char > &readData)
Read bytes from this socket blocking until they're actually received.
Result readWithTimeout(Span< char > data, Span< char > &readData, Time::Milliseconds timeout)
Read bytes from this socket blocking until they're actually received or timeout occurs.
SocketClient(const SocketDescriptor &socket)
Constructs this SocketClient from a SocketDescriptor (already created with SocketDescriptor::create)
Definition: Socket.h:69
Result connect(SocketIPAddress ipAddress)
Connect to a given address and port combination.
Result write(Span< const char > data)
Writes bytes to this socket.
Result connect(SpanStringView address, uint16_t port)
Connect to a given address and port combination.
Synchronous DNS Resolution.
Definition: Socket.h:110
static Result resolveDNS(SpanStringView host, SpanString &ipAddress)
Resolve an host string to an ip address (blocking until DNS response arrives)
Low-level OS socket handle.
Definition: SocketDescriptor.h:147
AddressFamily
Sets the address family of an IP Address (IPv4 or IPV6)
Definition: SocketDescriptor.h:73
Native representation of an IP Address.
Definition: SocketDescriptor.h:110
Networking globals initialization (Winsock2 WSAStartup)
Definition: Socket.h:123
static Result shutdownNetworking()
Shutdowns Winsock2 on Windows (WSAStartup)
static bool isNetworkingInited()
Check if initNetworking has been previously called.
static Result initNetworking()
Initializes Winsock2 on Windows (WSAStartup)
Use a SocketDescriptor as a Server (example TCP or UDP Socket Server).
Definition: Socket.h:25
Result listen(uint32_t numberOfWaitingConnections)
Start listening for incoming connections at a specific address / port combination (after bind)
SocketServer(SocketDescriptor &socket)
Build a SocketServer from a SocketDescriptor (already created with SocketDescriptor::create)
Definition: Socket.h:28
Result close()
Calls SocketDescriptor::close.
Result bind(SocketIPAddress nativeAddress)
Binds this socket to a given address / port combination.
Result accept(SocketFlags::AddressFamily addressFamily, SocketDescriptor &newClient)
Accepts a new client, blocking while waiting for it.
View over a contiguous sequence of items (pointer + size in elements).
Definition: Span.h:24
An writable view over an ASCII string (to avoid including Strings library)
Definition: Span.h:272
An read-only view over an ASCII string (to avoid including Strings library)
Definition: Span.h:249
Type-safe wrapper of uint64 used to represent milliseconds.
Definition: Time.h:29