#include "pch.h" #include "DataChannelObject.h" namespace unity { namespace webrtc { DataChannelObject::DataChannelObject( rtc::scoped_refptr channel, PeerConnectionObject& pc) : dataChannel(channel) { dataChannel->RegisterObserver(this); } DataChannelObject::~DataChannelObject() { dataChannel->UnregisterObserver(); auto state = dataChannel->state(); if (state == webrtc::DataChannelInterface::kOpen) { dataChannel->Close(); } dataChannel = nullptr; onClose = nullptr; onOpen = nullptr; onMessage = nullptr; } void DataChannelObject::OnStateChange() { auto state = dataChannel->state(); switch (state) { case webrtc::DataChannelInterface::kOpen: if (onOpen != nullptr) { onOpen(this->dataChannel); } break; case webrtc::DataChannelInterface::kClosed: if (onClose != nullptr) { onClose(this->dataChannel); } break; case webrtc::DataChannelInterface::kConnecting: case webrtc::DataChannelInterface::kClosing: break; } } void DataChannelObject::OnMessage(const webrtc::DataBuffer& buffer) { if (onMessage != nullptr) { size_t size = buffer.data.size(); if (onMessage != nullptr) { onMessage(this->dataChannel, buffer.data.data(), static_cast(size)); } } } } // end namespace webrtc } // end namespace unity