SFML 3.1.0
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
30#include <SFML/Network/Export.hpp>
31
32#include <SFML/Network/Socket.hpp>
33
34#include <SFML/System/String.hpp>
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
47 namespace sf
48{
49 class TcpListener;
50 class IpAddress;
51 class Packet;
52
57 class SFML_NETWORK_API TcpSocket : public Socket
58{
59 public:
64 enum class TlsStatus
65 {
66 NotConnected,
67 HandshakeStarted,
68 HandshakeComplete,
69 Error
70 };
71
76 TcpSocket();
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
175 void disconnect();
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
596 private:
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
Export.hpp
SFML_NETWORK_API
#define SFML_NETWORK_API
Definition Network/Export.hpp:42
Socket.hpp
String.hpp
Time.hpp
sf::IpAddress
Encapsulate an IPv4 network address.
Definition IpAddress.hpp:51
sf::Packet
Utility class to build blocks of data to transfer over the network.
Definition Packet.hpp:49
sf::Socket::Status
Status
Status codes that may be returned by socket functions.
Definition Socket.hpp:50
sf::Socket::Socket
Socket(const Socket &)=delete
Deleted copy constructor.
sf::String
Utility string class that automatically handles conversions between types and encodings.
Definition String.hpp:91
sf::TcpListener
Socket that listens to new TCP connections.
Definition TcpListener.hpp:45
sf::TcpSocket::send
Status send(Packet &packet)
Send a formatted packet of data to the remote peer.
sf::TcpSocket::setupTlsClient
TlsStatus setupTlsClient(const sf::String &hostname, bool verifyPeer=true)
Set up transport layer security as a client.
sf::TcpSocket::TcpListener
friend class TcpListener
Definition TcpSocket.hpp:597
sf::TcpSocket::send
Status send(const void *data, std::size_t size, std::size_t &sent)
Send raw data to the remote peer.
sf::TcpSocket::getRemoteAddress
std::optional< IpAddress > getRemoteAddress() const
Get the address of the connected peer.
sf::TcpSocket::connect
Status connect(IpAddress remoteAddress, unsigned short remotePort, Time timeout=Time::Zero)
Connect the socket to a remote peer.
sf::TcpSocket::TcpSocket
TcpSocket()
Default constructor.
sf::TcpSocket::setupTlsServer
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.
sf::TcpSocket::TcpSocket
TcpSocket(const TcpSocket &)=delete
Deleted copy constructor.
sf::TcpSocket::operator=
TcpSocket & operator=(const TcpSocket &)=delete
Deleted copy assignment.
sf::TcpSocket::receive
Status receive(void *data, std::size_t size, std::size_t &received)
Receive raw data from the remote peer.
sf::TcpSocket::getCurrentCiphersuiteName
std::optional< std::string > getCurrentCiphersuiteName() const
Get the name of the TLS ciphersuite currently in use.
sf::TcpSocket::getRemotePort
unsigned short getRemotePort() const
Get the port of the connected peer to which the socket is connected.
sf::TcpSocket::getLocalPort
unsigned short getLocalPort() const
Get the port to which the socket is bound locally.
sf::TcpSocket::TlsStatus
TlsStatus
TLS status codes that may be returned by TLS setup.
Definition TcpSocket.hpp:65
sf::TcpSocket::receive
Status receive(Packet &packet)
Receive a formatted packet of data from the remote peer.
sf::TcpSocket::disconnect
void disconnect()
Disconnect the socket from its remote peer.
sf::TcpSocket::~TcpSocket
~TcpSocket() override
Destructor.
sf::TcpSocket::TcpSocket
TcpSocket(TcpSocket &&) noexcept
Move constructor.
sf::TcpSocket::send
Status send(const void *data, std::size_t size)
Send raw data to the remote peer.
sf::Time
Represents a time value.
Definition Time.hpp:42
sf
Definition AudioResource.hpp:36