OpenShot Audio Library | OpenShotAudio  0.6.0
juce_Socket.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2022 - Raw Material Software Limited
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  The code included in this file is provided under the terms of the ISC license
11  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12  To use, copy, modify, and/or distribute this software for any purpose with or
13  without fee is hereby granted provided that the above copyright notice and
14  this permission notice appear in all copies.
15 
16  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18  DISCLAIMED.
19 
20  ==============================================================================
21 */
22 
23 namespace juce
24 {
25 
34 class JUCE_API SocketOptions
35 {
36 public:
44  [[nodiscard]] SocketOptions withReceiveBufferSize (int size) const
45  {
46  return withMember (*this, &SocketOptions::receiveBufferSize, size);
47  }
48 
54  [[nodiscard]] SocketOptions withSendBufferSize (int size) const
55  {
56  return withMember (*this, &SocketOptions::sendBufferSize, size);
57  }
58 
60  [[nodiscard]] auto getReceiveBufferSize() const { return receiveBufferSize; }
61 
63  [[nodiscard]] auto getSendBufferSize() const { return sendBufferSize; }
64 
65 private:
66  std::optional<int> receiveBufferSize;
67  std::optional<int> sendBufferSize;
68 };
69 
70 //==============================================================================
81 class JUCE_API StreamingSocket final
82 {
83 public:
84  using Options = SocketOptions;
85 
86  //==============================================================================
97 
108  explicit StreamingSocket (const SocketOptions& optionsIn)
109  : options { optionsIn }
110  {}
111 
113  ~StreamingSocket();
114 
115  //==============================================================================
121  bool bindToPort (int localPortNumber);
122 
134  bool bindToPort (int localPortNumber, const String& localAddress);
135 
144  int getBoundPort() const noexcept;
145 
154  bool connect (const String& remoteHostname,
155  int remotePortNumber,
156  int timeOutMillisecs = 3000);
157 
159  bool isConnected() const noexcept { return connected; }
160 
162  void close();
163 
165  const String& getHostName() const noexcept { return hostName; }
166 
168  int getPort() const noexcept { return portNumber; }
169 
171  bool isLocal() const noexcept;
172 
174  int getRawSocketHandle() const noexcept { return handle; }
175 
176  //==============================================================================
188  int waitUntilReady (bool readyForReading, int timeoutMsecs);
189 
200  int read (void* destBuffer, int maxBytesToRead,
201  bool blockUntilSpecifiedAmountHasArrived);
202 
210  int write (const void* sourceBuffer, int numBytesToWrite);
211 
212  //==============================================================================
226  bool createListener (int portNumber, const String& localHostName = String());
227 
237  StreamingSocket* waitForNextConnection() const;
238 
239 private:
240  //==============================================================================
241  SocketOptions options;
242  String hostName;
243  std::atomic<int> portNumber { 0 }, handle { -1 };
244  std::atomic<bool> connected { false }, isListener { false };
245  mutable CriticalSection readLock;
246 
247  StreamingSocket (const String& hostname, int portNumber, int handle, const SocketOptions& options);
248 
249  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (StreamingSocket)
250 };
251 
252 
253 //==============================================================================
264 class JUCE_API DatagramSocket final
265 {
266 public:
267  using Options = SocketOptions;
268 
269  //==============================================================================
279  DatagramSocket (bool enableBroadcasting, const SocketOptions& optionsIn);
280 
289  explicit DatagramSocket (bool enableBroadcasting)
290  : DatagramSocket (enableBroadcasting, SocketOptions{})
291  {}
292 
301  {}
302 
304  ~DatagramSocket();
305 
306  //==============================================================================
315  bool bindToPort (int localPortNumber);
316 
328  bool bindToPort (int localPortNumber, const String& localAddress);
329 
337  int getBoundPort() const noexcept;
338 
340  int getRawSocketHandle() const noexcept { return handle; }
341 
342  //==============================================================================
354  int waitUntilReady (bool readyForReading, int timeoutMsecs);
355 
366  int read (void* destBuffer, int maxBytesToRead,
367  bool blockUntilSpecifiedAmountHasArrived);
368 
380  int read (void* destBuffer, int maxBytesToRead,
381  bool blockUntilSpecifiedAmountHasArrived,
382  String& senderIPAddress, int& senderPortNumber);
383 
391  int write (const String& remoteHostname, int remotePortNumber,
392  const void* sourceBuffer, int numBytesToWrite);
393 
408  void shutdown();
409 
410  //==============================================================================
415  bool joinMulticast (const String& multicastIPAddress);
416 
421  bool leaveMulticast (const String& multicastIPAddress);
422 
427  bool setMulticastLoopbackEnabled (bool enableLoopback);
428 
429  //==============================================================================
438  bool setEnablePortReuse (bool enabled);
439 
440 private:
441  //==============================================================================
442  SocketOptions options;
443  std::atomic<int> handle { -1 };
444  bool isBound = false;
445  String lastBindAddress, lastServerHost;
446  int lastServerPort = -1;
447  void* lastServerAddress = nullptr;
448  mutable CriticalSection readLock;
449 
450  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DatagramSocket)
451 };
452 
453 } // namespace juce
DatagramSocket(bool enableBroadcasting)
Definition: juce_Socket.h:289
SocketOptions withSendBufferSize(int size) const
Definition: juce_Socket.h:54
auto getSendBufferSize() const
Definition: juce_Socket.h:63
SocketOptions withReceiveBufferSize(int size) const
Definition: juce_Socket.h:44
auto getReceiveBufferSize() const
Definition: juce_Socket.h:60
const String & getHostName() const noexcept
Definition: juce_Socket.h:165
int getPort() const noexcept
Definition: juce_Socket.h:168
StreamingSocket(const SocketOptions &optionsIn)
Definition: juce_Socket.h:108