Loading...
Searching...
No Matches
juce_Socket.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
class JUCE_API SocketOptions
{
public:
[[nodiscard]] SocketOptions withReceiveBufferSize (int size) const
{
return withMember (*this, &SocketOptions::receiveBufferSize, size);
}
[[nodiscard]] SocketOptions withSendBufferSize (int size) const
{
return withMember (*this, &SocketOptions::sendBufferSize, size);
}
[[nodiscard]] auto getReceiveBufferSize() const { return receiveBufferSize; }
[[nodiscard]] auto getSendBufferSize() const { return sendBufferSize; }
private:
std::optional<int> receiveBufferSize;
std::optional<int> sendBufferSize;
};
//==============================================================================
class JUCE_API StreamingSocket final
{
public:
using Options = SocketOptions;
//==============================================================================
StreamingSocket();
explicit StreamingSocket (const SocketOptions& optionsIn)
: options { optionsIn }
{}
~StreamingSocket();
//==============================================================================
bool bindToPort (int localPortNumber);
bool bindToPort (int localPortNumber, const String& localAddress);
int getBoundPort() const noexcept;
bool connect (const String& remoteHostname,
int remotePortNumber,
int timeOutMillisecs = 3000);
bool isConnected() const noexcept { return connected; }
void close();
const String& getHostName() const noexcept { return hostName; }
int getPort() const noexcept { return portNumber; }
bool isLocal() const noexcept;
int getRawSocketHandle() const noexcept { return handle; }
//==============================================================================
int waitUntilReady (bool readyForReading, int timeoutMsecs);
int read (void* destBuffer, int maxBytesToRead,
bool blockUntilSpecifiedAmountHasArrived);
int write (const void* sourceBuffer, int numBytesToWrite);
//==============================================================================
bool createListener (int portNumber, const String& localHostName = String());
StreamingSocket* waitForNextConnection() const;
private:
//==============================================================================
SocketOptions options;
String hostName;
std::atomic<int> portNumber { 0 }, handle { -1 };
std::atomic<bool> connected { false }, isListener { false };
mutable CriticalSection readLock;
StreamingSocket (const String& hostname, int portNumber, int handle, const SocketOptions& options);
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (StreamingSocket)
};
//==============================================================================
class JUCE_API DatagramSocket final
{
public:
using Options = SocketOptions;
//==============================================================================
DatagramSocket (bool enableBroadcasting, const SocketOptions& optionsIn);
explicit DatagramSocket (bool enableBroadcasting)
: DatagramSocket (enableBroadcasting, SocketOptions{})
{}
DatagramSocket() : DatagramSocket (false)
{}
~DatagramSocket();
//==============================================================================
bool bindToPort (int localPortNumber);
bool bindToPort (int localPortNumber, const String& localAddress);
int getBoundPort() const noexcept;
int getRawSocketHandle() const noexcept { return handle; }
//==============================================================================
int waitUntilReady (bool readyForReading, int timeoutMsecs);
int read (void* destBuffer, int maxBytesToRead,
bool blockUntilSpecifiedAmountHasArrived);
int read (void* destBuffer, int maxBytesToRead,
bool blockUntilSpecifiedAmountHasArrived,
String& senderIPAddress, int& senderPortNumber);
int write (const String& remoteHostname, int remotePortNumber,
const void* sourceBuffer, int numBytesToWrite);
void shutdown();
//==============================================================================
bool joinMulticast (const String& multicastIPAddress);
bool leaveMulticast (const String& multicastIPAddress);
bool setMulticastLoopbackEnabled (bool enableLoopback);
//==============================================================================
bool setEnablePortReuse (bool enabled);
private:
//==============================================================================
SocketOptions options;
std::atomic<int> handle { -1 };
bool isBound = false;
String lastBindAddress, lastServerHost;
int lastServerPort = -1;
void* lastServerAddress = nullptr;
mutable CriticalSection readLock;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DatagramSocket)
};
} // namespace juce
juce::DatagramSocket
Definition juce_Socket.h:265
juce::DatagramSocket::DatagramSocket
DatagramSocket(bool enableBroadcasting)
Definition juce_Socket.h:289
juce::DatagramSocket::getRawSocketHandle
int getRawSocketHandle() const noexcept
Definition juce_Socket.h:340
juce::DatagramSocket::DatagramSocket
DatagramSocket(bool enableBroadcasting, const SocketOptions &optionsIn)
Definition juce_Socket.cpp:661
juce::DatagramSocket::DatagramSocket
DatagramSocket()
Definition juce_Socket.h:300
juce::SocketOptions
Definition juce_Socket.h:35
juce::SocketOptions::withSendBufferSize
SocketOptions withSendBufferSize(int size) const
Definition juce_Socket.h:54
juce::SocketOptions::getSendBufferSize
auto getSendBufferSize() const
Definition juce_Socket.h:63
juce::SocketOptions::withReceiveBufferSize
SocketOptions withReceiveBufferSize(int size) const
Definition juce_Socket.h:44
juce::SocketOptions::getReceiveBufferSize
auto getReceiveBufferSize() const
Definition juce_Socket.h:60
juce::StreamingSocket
Definition juce_Socket.h:82
juce::StreamingSocket::getPort
int getPort() const noexcept
Definition juce_Socket.h:168
juce::StreamingSocket::getHostName
const String & getHostName() const noexcept
Definition juce_Socket.h:165
juce::StreamingSocket::StreamingSocket
StreamingSocket(const SocketOptions &optionsIn)
Definition juce_Socket.h:108
juce::StreamingSocket::StreamingSocket
StreamingSocket()
Definition juce_Socket.cpp:484
juce::StreamingSocket::isConnected
bool isConnected() const noexcept
Definition juce_Socket.h:159
juce::StreamingSocket::getRawSocketHandle
int getRawSocketHandle() const noexcept
Definition juce_Socket.h:174
juce::String
Definition juce_String.h:53