OpenTera WebRTC API (C++) 1.2.5
DataChannelClient.h
1 #ifndef OPENTERA_WEBRTC_NATIVE_CLIENT_DATA_CHANNEL_CLIENT_H
2 #define OPENTERA_WEBRTC_NATIVE_CLIENT_DATA_CHANNEL_CLIENT_H
3 
4 #include <OpenteraWebrtcNativeClient/WebrtcClient.h>
5 #include <OpenteraWebrtcNativeClient/Utils/ClassMacro.h>
6 #include <OpenteraWebrtcNativeClient/Configurations/DataChannelConfiguration.h>
7 
8 #include <api/data_channel_interface.h>
9 
10 namespace opentera
11 {
16  {
17  DataChannelConfiguration m_dataChannelConfiguration;
18 
19  std::function<void(const Client&)> m_onDataChannelOpened;
20  std::function<void(const Client&)> m_onDataChannelClosed;
21  std::function<void(const Client&, const std::string&)> m_onDataChannelError;
22  std::function<void(const Client&, const uint8_t*, std::size_t)> m_onDataChannelMessageBinary;
23  std::function<void(const Client&, const std::string&)> m_onDataChannelMessageString;
24 
25  public:
27  SignalingServerConfiguration signalingServerConfiguration,
28  WebrtcConfiguration webrtcConfiguration,
29  DataChannelConfiguration dataChannelConfiguration);
30  ~DataChannelClient() override = default;
31 
32  DECLARE_NOT_COPYABLE(DataChannelClient);
33  DECLARE_NOT_MOVABLE(DataChannelClient);
34 
35  bool sendTo(const uint8_t* data, std::size_t size, const std::vector<std::string>& ids);
36  bool sendTo(const std::string& message, const std::vector<std::string>& ids);
37  bool sendToAll(const uint8_t* data, std::size_t size);
38  bool sendToAll(const std::string& message);
39 
40  void setOnDataChannelOpened(const std::function<void(const Client&)>& callback);
41  void setOnDataChannelClosed(const std::function<void(const Client&)>& callback);
42  void setOnDataChannelError(const std::function<void(const Client&, const std::string&)>& callback);
44  const std::function<void(const Client&, const uint8_t*, std::size_t)>& callback);
45  void setOnDataChannelMessageString(const std::function<void(const Client&, const std::string&)>& callback);
46 
47  protected:
48  bool sendTo(const webrtc::DataBuffer& buffer, const std::vector<std::string>& ids);
49  bool sendToAll(const webrtc::DataBuffer& buffer);
50 
51  std::unique_ptr<PeerConnectionHandler>
52  createPeerConnectionHandler(const std::string& id, const Client& peerClient, bool isCaller) override;
53  };
54 
62  inline bool DataChannelClient::sendTo(const uint8_t* data, size_t size, const std::vector<std::string>& ids)
63  {
64  return sendTo(webrtc::DataBuffer(rtc::CopyOnWriteBuffer(data, size), true), ids);
65  }
66 
73  inline bool DataChannelClient::sendTo(const std::string& message, const std::vector<std::string>& ids)
74  {
75  return sendTo(webrtc::DataBuffer(message), ids);
76  }
77 
84  inline bool DataChannelClient::sendToAll(const uint8_t* data, size_t size)
85  {
86  return sendToAll(webrtc::DataBuffer(rtc::CopyOnWriteBuffer(data, size), true));
87  }
88 
94  inline bool DataChannelClient::sendToAll(const std::string& message)
95  {
96  return sendToAll(webrtc::DataBuffer(message));
97  }
98 
111  inline void DataChannelClient::setOnDataChannelOpened(const std::function<void(const Client&)>& callback)
112  {
113  callSync(getInternalClientThread(), [this, &callback]() { m_onDataChannelOpened = callback; });
114  }
115 
128  inline void DataChannelClient::setOnDataChannelClosed(const std::function<void(const Client&)>& callback)
129  {
130  callSync(getInternalClientThread(), [this, &callback]() { m_onDataChannelClosed = callback; });
131  }
132 
146  inline void
147  DataChannelClient::setOnDataChannelError(const std::function<void(const Client&, const std::string&)>& callback)
148  {
149  callSync(getInternalClientThread(), [this, &callback]() { m_onDataChannelError = callback; });
150  }
151 
167  const std::function<void(const Client&, const uint8_t*, std::size_t)>& callback)
168  {
169  callSync(getInternalClientThread(), [this, &callback]() { m_onDataChannelMessageBinary = callback; });
170  }
171 
186  const std::function<void(const Client&, const std::string&)>& callback)
187  {
188  callSync(getInternalClientThread(), [this, &callback]() { m_onDataChannelMessageString = callback; });
189  }
190 }
191 
192 #endif
Represents a peer client.
Definition: Client.h:14
Represents a client for data channel communication.
Definition: DataChannelClient.h:16
void setOnDataChannelError(const std::function< void(const Client &, const std::string &)> &callback)
Sets the callback that is called when a data channel error occurs.
Definition: DataChannelClient.h:147
DataChannelClient(SignalingServerConfiguration signalingServerConfiguration, WebrtcConfiguration webrtcConfiguration, DataChannelConfiguration dataChannelConfiguration)
Creates a data channel client with the specified configurations.
Definition: DataChannelClient.cpp:14
void setOnDataChannelMessageBinary(const std::function< void(const Client &, const uint8_t *, std::size_t)> &callback)
Sets the callback that is called when binary data are received.
Definition: DataChannelClient.h:166
void setOnDataChannelClosed(const std::function< void(const Client &)> &callback)
Sets the callback that is called when a data channel closes.
Definition: DataChannelClient.h:128
void setOnDataChannelOpened(const std::function< void(const Client &)> &callback)
Sets the callback that is called when a data channel opens.
Definition: DataChannelClient.h:111
void setOnDataChannelMessageString(const std::function< void(const Client &, const std::string &)> &callback)
Sets the callback that is called when a string message is received.
Definition: DataChannelClient.h:185
Represents a data channel configuration.
Definition: DataChannelConfiguration.h:14
Represents a signaling server configuration.
Definition: SignalingServerConfiguration.h:14
Represents the base class of DataChannelClient and StreamClient.
Definition: WebrtcClient.h:29
Represents a WebRTC peer connection configuration.
Definition: WebrtcConfiguration.h:14