blob: ed026836ccf35eccfe4b21c9265f8797b54d37c2 [file] [log] [blame]
Ryan Keanedeb48b32019-06-28 16:24:40 -07001// 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 Keanea4dfaa12019-08-19 10:30:20 -07007#include "platform/api/task_runner.h"
8
Ryan Keanedeb48b32019-06-28 16:24:40 -07009namespace openscreen {
10namespace platform {
11
Ryan Keanea4dfaa12019-08-19 10:30:20 -070012UdpSocket::UdpSocket(TaskRunner* task_runner, Client* client)
13 : client_(client), task_runner_(task_runner) {
14 OSP_CHECK(task_runner_);
Ryan Keanedeb48b32019-06-28 16:24:40 -070015 deletion_callback_ = [](UdpSocket* socket) {};
16}
17
18UdpSocket::~UdpSocket() {
19 deletion_callback_(this);
20}
21
22void UdpSocket::SetDeletionCallback(std::function<void(UdpSocket*)> callback) {
23 deletion_callback_ = callback;
24}
25
Ryan Keane63fbedd2019-08-19 12:33:41 -070026void 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}
35void 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}
44void 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 Keanedeb48b32019-06-28 16:24:40 -070054} // namespace platform
55} // namespace openscreen