Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
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 "../Foundation/StringViewData.h"
8#include "../Time/Time.h" // Milliseconds
9
10namespace SC
11{
12struct SocketNetworking;
13struct SocketClient;
14struct SocketServer;
15struct SocketDNS;
16} // namespace SC
17
20
26{
29 SocketServer(SocketDescriptor& socket) : socket(socket) {}
30
33 [[nodiscard]] Result close();
34
38 [[nodiscard]] Result bind(SocketIPAddress nativeAddress);
39
44 [[nodiscard]] Result listen(uint32_t numberOfWaitingConnections);
45
50 [[nodiscard]] Result accept(SocketFlags::AddressFamily addressFamily, SocketDescriptor& newClient);
51
52 private:
53 SocketDescriptor& socket;
54};
55
67{
70 SocketClient(const SocketDescriptor& socket) : socket(socket) {}
71
77 [[nodiscard]] Result connect(StringViewData address, uint16_t port);
78
82 [[nodiscard]] Result connect(SocketIPAddress ipAddress);
83
87 [[nodiscard]] Result write(Span<const char> data);
88
93 [[nodiscard]] Result read(Span<char> data, Span<char>& readData);
94
100 [[nodiscard]] Result readWithTimeout(Span<char> data, Span<char>& readData, Time::Milliseconds timeout);
101
102 private:
103 const SocketDescriptor& socket;
104};
105
111{
119 [[nodiscard]] static Result resolveDNS(StringViewData host, Span<char>& ipAddress);
120};
121
124{
127 [[nodiscard]] static Result initNetworking();
128
131 [[nodiscard]] static Result shutdownNetworking();
132
135 [[nodiscard]] static bool isNetworkingInited();
136
137 private:
138 struct Internal;
139};
140
unsigned short uint16_t
Platform independent (2) bytes unsigned int.
Definition PrimitiveTypes.h:37
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition PrimitiveTypes.h:38
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:67
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:70
Result connect(StringViewData address, uint16_t port)
Connect to a given address and port combination.
Result connect(SocketIPAddress ipAddress)
Connect to a given address and port combination.
Result write(Span< const char > data)
Writes bytes to this socket.
Synchronous DNS Resolution.
Definition Socket.h:111
static Result resolveDNS(StringViewData host, Span< char > &ipAddress)
Resolve an host string to an ip address (blocking until DNS response arrives)
Low-level OS socket handle.
Definition SocketDescriptor.h:178
AddressFamily
Sets the address family of an IP Address (IPv4 or IPV6)
Definition SocketDescriptor.h:74
Native representation of an IP Address.
Definition SocketDescriptor.h:119
Networking globals initialization (Winsock2 WSAStartup)
Definition Socket.h:124
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:26
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:29
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:29
An read-only view over a string (to avoid including Strings library)
Definition StringViewData.h:31
Type-safe wrapper of uint64 used to represent milliseconds.
Definition Time.h:44