henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 11 | #include "rtc_base/async_socket.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 12 | #include "rtc_base/checks.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
| 14 | namespace rtc { |
| 15 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 16 | AsyncSocket::AsyncSocket() {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 18 | AsyncSocket::~AsyncSocket() {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 19 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 20 | AsyncSocketAdapter::AsyncSocketAdapter(AsyncSocket* socket) : socket_(nullptr) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 21 | Attach(socket); |
| 22 | } |
| 23 | |
| 24 | AsyncSocketAdapter::~AsyncSocketAdapter() { |
| 25 | delete socket_; |
| 26 | } |
| 27 | |
| 28 | void AsyncSocketAdapter::Attach(AsyncSocket* socket) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 29 | RTC_DCHECK(!socket_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 30 | socket_ = socket; |
| 31 | if (socket_) { |
| 32 | socket_->SignalConnectEvent.connect(this, |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 33 | &AsyncSocketAdapter::OnConnectEvent); |
| 34 | socket_->SignalReadEvent.connect(this, &AsyncSocketAdapter::OnReadEvent); |
| 35 | socket_->SignalWriteEvent.connect(this, &AsyncSocketAdapter::OnWriteEvent); |
| 36 | socket_->SignalCloseEvent.connect(this, &AsyncSocketAdapter::OnCloseEvent); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 40 | SocketAddress AsyncSocketAdapter::GetLocalAddress() const { |
| 41 | return socket_->GetLocalAddress(); |
| 42 | } |
| 43 | |
| 44 | SocketAddress AsyncSocketAdapter::GetRemoteAddress() const { |
| 45 | return socket_->GetRemoteAddress(); |
| 46 | } |
| 47 | |
| 48 | int AsyncSocketAdapter::Bind(const SocketAddress& addr) { |
| 49 | return socket_->Bind(addr); |
| 50 | } |
| 51 | |
| 52 | int AsyncSocketAdapter::Connect(const SocketAddress& addr) { |
| 53 | return socket_->Connect(addr); |
| 54 | } |
| 55 | |
| 56 | int AsyncSocketAdapter::Send(const void* pv, size_t cb) { |
| 57 | return socket_->Send(pv, cb); |
| 58 | } |
| 59 | |
| 60 | int AsyncSocketAdapter::SendTo(const void* pv, |
| 61 | size_t cb, |
| 62 | const SocketAddress& addr) { |
| 63 | return socket_->SendTo(pv, cb, addr); |
| 64 | } |
| 65 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 66 | int AsyncSocketAdapter::Recv(void* pv, size_t cb, int64_t* timestamp) { |
| 67 | return socket_->Recv(pv, cb, timestamp); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 70 | int AsyncSocketAdapter::RecvFrom(void* pv, |
| 71 | size_t cb, |
| 72 | SocketAddress* paddr, |
| 73 | int64_t* timestamp) { |
| 74 | return socket_->RecvFrom(pv, cb, paddr, timestamp); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | int AsyncSocketAdapter::Listen(int backlog) { |
| 78 | return socket_->Listen(backlog); |
| 79 | } |
| 80 | |
| 81 | AsyncSocket* AsyncSocketAdapter::Accept(SocketAddress* paddr) { |
| 82 | return socket_->Accept(paddr); |
| 83 | } |
| 84 | |
| 85 | int AsyncSocketAdapter::Close() { |
| 86 | return socket_->Close(); |
| 87 | } |
| 88 | |
| 89 | int AsyncSocketAdapter::GetError() const { |
| 90 | return socket_->GetError(); |
| 91 | } |
| 92 | |
| 93 | void AsyncSocketAdapter::SetError(int error) { |
| 94 | return socket_->SetError(error); |
| 95 | } |
| 96 | |
| 97 | AsyncSocket::ConnState AsyncSocketAdapter::GetState() const { |
| 98 | return socket_->GetState(); |
| 99 | } |
| 100 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 101 | int AsyncSocketAdapter::GetOption(Option opt, int* value) { |
| 102 | return socket_->GetOption(opt, value); |
| 103 | } |
| 104 | |
| 105 | int AsyncSocketAdapter::SetOption(Option opt, int value) { |
| 106 | return socket_->SetOption(opt, value); |
| 107 | } |
| 108 | |
| 109 | void AsyncSocketAdapter::OnConnectEvent(AsyncSocket* socket) { |
| 110 | SignalConnectEvent(this); |
| 111 | } |
| 112 | |
| 113 | void AsyncSocketAdapter::OnReadEvent(AsyncSocket* socket) { |
| 114 | SignalReadEvent(this); |
| 115 | } |
| 116 | |
| 117 | void AsyncSocketAdapter::OnWriteEvent(AsyncSocket* socket) { |
| 118 | SignalWriteEvent(this); |
| 119 | } |
| 120 | |
| 121 | void AsyncSocketAdapter::OnCloseEvent(AsyncSocket* socket, int err) { |
| 122 | SignalCloseEvent(this, err); |
| 123 | } |
| 124 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 125 | } // namespace rtc |