Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file |
| 4 | |
| 5 | #include "platform/api/udp_socket.h" |
| 6 | |
Ryan Keane | a4dfaa1 | 2019-08-19 10:30:20 -0700 | [diff] [blame] | 7 | #include "platform/api/task_runner.h" |
| 8 | |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 9 | namespace openscreen { |
| 10 | namespace platform { |
| 11 | |
Ryan Keane | a4dfaa1 | 2019-08-19 10:30:20 -0700 | [diff] [blame] | 12 | UdpSocket::UdpSocket(TaskRunner* task_runner, Client* client) |
| 13 | : client_(client), task_runner_(task_runner) { |
| 14 | OSP_CHECK(task_runner_); |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 15 | deletion_callback_ = [](UdpSocket* socket) {}; |
| 16 | } |
| 17 | |
| 18 | UdpSocket::~UdpSocket() { |
| 19 | deletion_callback_(this); |
| 20 | } |
| 21 | |
| 22 | void UdpSocket::SetDeletionCallback(std::function<void(UdpSocket*)> callback) { |
| 23 | deletion_callback_ = callback; |
| 24 | } |
| 25 | |
Ryan Keane | 63fbedd | 2019-08-19 12:33:41 -0700 | [diff] [blame^] | 26 | void UdpSocket::OnError(Error error) { |
| 27 | if (!client_) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | task_runner_->PostTask([e = std::move(error), this]() mutable { |
| 32 | this->client_->OnError(this, std::move(e)); |
| 33 | }); |
| 34 | } |
| 35 | void UdpSocket::OnSendError(Error error) { |
| 36 | if (!client_) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | task_runner_->PostTask([e = std::move(error), this]() mutable { |
| 41 | this->client_->OnSendError(this, std::move(e)); |
| 42 | }); |
| 43 | } |
| 44 | void UdpSocket::OnRead(ErrorOr<UdpPacket> read_data) { |
| 45 | if (!client_) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | task_runner_->PostTask([data = std::move(read_data), this]() mutable { |
| 50 | this->client_->OnRead(this, std::move(data)); |
| 51 | }); |
| 52 | } |
| 53 | |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 54 | } // namespace platform |
| 55 | } // namespace openscreen |