Loading...
Searching...
No Matches
TcpSocket.hpp
Go to the documentation of this file.
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2026 Laurent Gomila (laurent@sfml-dev.org)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#pragma once
26
28// Headers
31
33
35#include <SFML/System/Time.hpp>
36
37#include <memory>
38#include <optional>
39#include <string>
40#include <string_view>
41#include <vector>
42
43#include <cstddef>
44#include <cstdint>
45
46
47namespace sf
48{
49class TcpListener;
50class IpAddress;
51class Packet;
52
58{
59public:
64 enum class TlsStatus
65 {
66 NotConnected,
67 HandshakeStarted,
68 HandshakeComplete,
69 Error
70 };
71
77
82 ~TcpSocket() override;
83
88 TcpSocket(const TcpSocket&) = delete;
89
94 TcpSocket& operator=(const TcpSocket&) = delete;
95
100 TcpSocket(TcpSocket&&) noexcept;
101
106 TcpSocket& operator=(TcpSocket&&) noexcept;
107
118 [[nodiscard]] unsigned short getLocalPort() const;
119
131 [[nodiscard]] std::optional<IpAddress> getRemoteAddress() const;
132
144 [[nodiscard]] unsigned short getRemotePort() const;
145
164 [[nodiscard]] Status connect(IpAddress remoteAddress, unsigned short remotePort, Time timeout = Time::Zero);
165
176
229 TlsStatus setupTlsClient(const sf::String& hostname, bool verifyPeer = true);
230
287 TlsStatus setupTlsClient(const sf::String& hostname, const char* certificateChainData);
288
342 TlsStatus setupTlsClient(const sf::String& hostname, const std::byte* certificateChainData, std::size_t certificateChainSize);
343
396 TlsStatus setupTlsClient(const sf::String& hostname, std::string_view certificateChainData);
397
445 TlsStatus setupTlsServer(const std::byte* certificateChainData,
446 std::size_t certificateChainSize,
447 const std::byte* privateKeyData,
448 std::size_t privateKeySize,
449 const std::byte* privateKeyPasswordData,
450 std::size_t privateKeyPasswordSize);
451
496 TlsStatus setupTlsServer(std::string_view certificateChainData,
497 std::string_view privateKeyData,
498 std::string_view privateKeyPasswordData = {});
499
508 [[nodiscard]] std::optional<std::string> getCurrentCiphersuiteName() const;
509
526 [[nodiscard]] Status send(const void* data, std::size_t size);
527
542 [[nodiscard]] Status send(const void* data, std::size_t size, std::size_t& sent);
543
560 [[nodiscard]] Status receive(void* data, std::size_t size, std::size_t& received);
561
578 [[nodiscard]] Status send(Packet& packet);
579
594 [[nodiscard]] Status receive(Packet& packet);
595
596private:
597 friend class TcpListener;
598
603 struct PendingPacket
604 {
605 std::uint32_t size{};
606 std::size_t sizeReceived{};
607 std::vector<std::byte> data;
608 };
609
611 // Member data
613 struct Impl;
614 std::unique_ptr<Impl> m_impl;
615 PendingPacket m_pendingPacket;
616 std::vector<std::byte> m_blockToSendBuffer;
617};
618
619} // namespace sf
620
621
#define SFML_NETWORK_API
Encapsulate an IPv4 network address.
Definition IpAddress.hpp:51
Utility class to build blocks of data to transfer over the network.
Definition Packet.hpp:49
Status
Status codes that may be returned by socket functions.
Definition Socket.hpp:50
Socket(const Socket &)=delete
Deleted copy constructor.
Utility string class that automatically handles conversions between types and encodings.
Definition String.hpp:91
Socket that listens to new TCP connections.
Status send(Packet &packet)
Send a formatted packet of data to the remote peer.
TlsStatus setupTlsClient(const sf::String &hostname, bool verifyPeer=true)
Set up transport layer security as a client.
friend class TcpListener
Status send(const void *data, std::size_t size, std::size_t &sent)
Send raw data to the remote peer.
std::optional< IpAddress > getRemoteAddress() const
Get the address of the connected peer.
Status connect(IpAddress remoteAddress, unsigned short remotePort, Time timeout=Time::Zero)
Connect the socket to a remote peer.
TcpSocket()
Default constructor.
TlsStatus setupTlsServer(const std::byte *certificateChainData, std::size_t certificateChainSize, const std::byte *privateKeyData, std::size_t privateKeySize, const std::byte *privateKeyPasswordData, std::size_t privateKeyPasswordSize)
Set up transport layer security as a server.
TcpSocket(const TcpSocket &)=delete
Deleted copy constructor.
TcpSocket & operator=(const TcpSocket &)=delete
Deleted copy assignment.
Status receive(void *data, std::size_t size, std::size_t &received)
Receive raw data from the remote peer.
std::optional< std::string > getCurrentCiphersuiteName() const
Get the name of the TLS ciphersuite currently in use.
unsigned short getRemotePort() const
Get the port of the connected peer to which the socket is connected.
unsigned short getLocalPort() const
Get the port to which the socket is bound locally.
TlsStatus
TLS status codes that may be returned by TLS setup.
Definition TcpSocket.hpp:65
Status receive(Packet &packet)
Receive a formatted packet of data from the remote peer.
void disconnect()
Disconnect the socket from its remote peer.
~TcpSocket() override
Destructor.
TcpSocket(TcpSocket &&) noexcept
Move constructor.
Status send(const void *data, std::size_t size)
Send raw data to the remote peer.
Represents a time value.
Definition Time.hpp:42