jsocketpp 1.0
A cross-platform C++20 socket library.
Loading...
Searching...
No Matches
MulticastSocket.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include "common.hpp"
12#include "DatagramSocket.hpp"
13
14#include <cstddef>
15#include <optional>
16#include <string>
17#include <string_view>
18
19namespace jsocketpp
20{
21
75{
76 public:
170 MulticastSocket(Port localPort, std::string_view localAddress,
171 std::optional<std::size_t> recvBufferSize = std::nullopt,
172 std::optional<std::size_t> sendBufferSize = std::nullopt,
173 std::optional<std::size_t> internalBufferSize = std::nullopt, bool reuseAddress = true,
174 int soRecvTimeoutMillis = -1, int soSendTimeoutMillis = -1, bool nonBlocking = false,
175 bool dualStack = true, bool autoBind = true);
176
252 void joinGroup(const std::string& groupAddr, const std::string& iface);
253
337 void leaveGroup(const std::string& groupAddr, const std::string& iface = "");
338
413 void setMulticastInterface(const std::string& iface);
414
478 [[nodiscard]] std::string getMulticastInterface() const { return _currentInterface; }
479
549 void setTimeToLive(int ttl);
550
597 int getTimeToLive() const { return _ttl; }
598
663 void setLoopbackMode(bool enable);
664
713 [[nodiscard]] bool getLoopbackMode() const { return _loopbackEnabled; }
714
719 std::string getCurrentGroup() const;
720
721 protected:
787 static in_addr resolveIPv4(std::string_view host);
788
849 static in6_addr resolveIPv6(std::string_view host);
850
924 static unsigned int toIfIndexFromString(const std::string& iface);
925
926 private:
927 std::string _currentGroup{};
928 std::string _currentInterface{};
929
941 int _ttl = 1;
942
952 bool _loopbackEnabled = true;
953};
954
955} // namespace jsocketpp
UDP datagram socket abstraction for jsocketpp.
std::string getCurrentGroup() const
Returns the last multicast group joined (for reference/debug).
Definition MulticastSocket.cpp:89
std::string _currentInterface
Interface used for multicast.
Definition MulticastSocket.hpp:928
std::string _currentGroup
Last joined multicast group address.
Definition MulticastSocket.hpp:927
static unsigned int toIfIndexFromString(const std::string &iface)
Convert a human-friendly interface identifier to an IPv6 interface index.
Definition MulticastSocket.cpp:152
int _ttl
Default TTL (Time To Live) value for multicast packets.
Definition MulticastSocket.hpp:941
bool _loopbackEnabled
Controls whether multicast packets loop back to the sending host.
Definition MulticastSocket.hpp:952
Common platform and utility includes for jsocketpp.
std::uint16_t Port
Type alias representing a TCP or UDP port number (1–65535).
Definition common.hpp:392
bool getLoopbackMode() const
Get the current multicast loopback mode cached on this socket.
Definition MulticastSocket.hpp:713
int getTimeToLive() const
Get the current default multicast TTL / hop limit cached on this socket.
Definition MulticastSocket.hpp:597
static in6_addr resolveIPv6(std::string_view host)
Resolve a host string to an IPv6 address (in6_addr).
Definition MulticastSocket.cpp:124
DatagramSocket(Port localPort=0, std::string_view localAddress="", std::optional< std::size_t > recvBufferSize=std::nullopt, std::optional< std::size_t > sendBufferSize=std::nullopt, std::optional< std::size_t > internalBufferSize=std::nullopt, bool reuseAddress=true, int soRecvTimeoutMillis=-1, int soSendTimeoutMillis=-1, bool nonBlocking=false, bool dualStack=true, bool autoBind=true, bool autoConnect=false, std::string_view remoteAddress="", Port remotePort=0, int connectTimeoutMillis=-1)
Creates a UDP socket, optionally binds to a local address, and optionally connects to a remote peer.
Definition DatagramSocket.cpp:12
static in_addr resolveIPv4(std::string_view host)
Resolve a host string to an IPv4 address (in_addr, network byte order).
Definition MulticastSocket.cpp:94
void setTimeToLive(int ttl)
Set the time-to-live (TTL) / hop limit for outgoing multicast packets.
Definition MulticastSocket.cpp:69
void joinGroup(const std::string &groupAddr, const std::string &iface)
Join a multicast group on an optional interface (string-friendly).
Definition MulticastSocket.cpp:173
MulticastSocket(Port localPort, std::string_view localAddress, std::optional< std::size_t > recvBufferSize=std::nullopt, std::optional< std::size_t > sendBufferSize=std::nullopt, std::optional< std::size_t > internalBufferSize=std::nullopt, bool reuseAddress=true, int soRecvTimeoutMillis=-1, int soSendTimeoutMillis=-1, bool nonBlocking=false, bool dualStack=true, bool autoBind=true)
Constructs a fully configurable multicast socket for receiving and sending datagrams.
Definition MulticastSocket.cpp:7
std::string getMulticastInterface() const
Get the currently selected egress interface for multicast transmissions.
Definition MulticastSocket.hpp:478
void setLoopbackMode(bool enable)
Enable or disable multicast loopback for this socket.
Definition MulticastSocket.cpp:79
void setMulticastInterface(const std::string &iface)
Select the default outgoing interface for multicast transmissions.
Definition MulticastSocket.cpp:20
void leaveGroup(const std::string &groupAddr, const std::string &iface="")
Leave a multicast group on an optional interface (string-friendly).
Definition MulticastSocket.cpp:249
A C++ socket library providing Java-style networking interfaces.
Definition BufferView.hpp:16