henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [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 17:11:00 | [diff] [blame] | 11 | #include "rtc_base/async_socket.h" |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 13 | #include "rtc_base/checks.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 14 | |
| 15 | namespace rtc { |
| 16 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 17 | AsyncSocket::AsyncSocket() {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 18 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 19 | AsyncSocket::~AsyncSocket() {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 20 | |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 21 | AsyncSocketAdapter::AsyncSocketAdapter(AsyncSocket* socket) : socket_(nullptr) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 22 | Attach(socket); |
| 23 | } |
| 24 | |
| 25 | AsyncSocketAdapter::~AsyncSocketAdapter() { |
| 26 | delete socket_; |
| 27 | } |
| 28 | |
| 29 | void AsyncSocketAdapter::Attach(AsyncSocket* socket) { |
nisse | ede5da4 | 2017-01-12 13:15:36 | [diff] [blame] | 30 | RTC_DCHECK(!socket_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 31 | socket_ = socket; |
| 32 | if (socket_) { |
| 33 | socket_->SignalConnectEvent.connect(this, |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 | [diff] [blame] | 34 | &AsyncSocketAdapter::OnConnectEvent); |
| 35 | socket_->SignalReadEvent.connect(this, &AsyncSocketAdapter::OnReadEvent); |
| 36 | socket_->SignalWriteEvent.connect(this, &AsyncSocketAdapter::OnWriteEvent); |
| 37 | socket_->SignalCloseEvent.connect(this, &AsyncSocketAdapter::OnCloseEvent); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 | [diff] [blame] | 41 | SocketAddress AsyncSocketAdapter::GetLocalAddress() const { |
| 42 | return socket_->GetLocalAddress(); |
| 43 | } |
| 44 | |
| 45 | SocketAddress AsyncSocketAdapter::GetRemoteAddress() const { |
| 46 | return socket_->GetRemoteAddress(); |
| 47 | } |
| 48 | |
| 49 | int AsyncSocketAdapter::Bind(const SocketAddress& addr) { |
| 50 | return socket_->Bind(addr); |
| 51 | } |
| 52 | |
| 53 | int AsyncSocketAdapter::Connect(const SocketAddress& addr) { |
| 54 | return socket_->Connect(addr); |
| 55 | } |
| 56 | |
| 57 | int AsyncSocketAdapter::Send(const void* pv, size_t cb) { |
| 58 | return socket_->Send(pv, cb); |
| 59 | } |
| 60 | |
| 61 | int AsyncSocketAdapter::SendTo(const void* pv, |
| 62 | size_t cb, |
| 63 | const SocketAddress& addr) { |
| 64 | return socket_->SendTo(pv, cb, addr); |
| 65 | } |
| 66 | |
Stefan Holmer | 9131efd | 2016-05-23 16:19:26 | [diff] [blame] | 67 | int AsyncSocketAdapter::Recv(void* pv, size_t cb, int64_t* timestamp) { |
| 68 | return socket_->Recv(pv, cb, timestamp); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 | [diff] [blame] | 69 | } |
| 70 | |
Stefan Holmer | 9131efd | 2016-05-23 16:19:26 | [diff] [blame] | 71 | int AsyncSocketAdapter::RecvFrom(void* pv, |
| 72 | size_t cb, |
| 73 | SocketAddress* paddr, |
| 74 | int64_t* timestamp) { |
| 75 | return socket_->RecvFrom(pv, cb, paddr, timestamp); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | int AsyncSocketAdapter::Listen(int backlog) { |
| 79 | return socket_->Listen(backlog); |
| 80 | } |
| 81 | |
| 82 | AsyncSocket* AsyncSocketAdapter::Accept(SocketAddress* paddr) { |
| 83 | return socket_->Accept(paddr); |
| 84 | } |
| 85 | |
| 86 | int AsyncSocketAdapter::Close() { |
| 87 | return socket_->Close(); |
| 88 | } |
| 89 | |
| 90 | int AsyncSocketAdapter::GetError() const { |
| 91 | return socket_->GetError(); |
| 92 | } |
| 93 | |
| 94 | void AsyncSocketAdapter::SetError(int error) { |
| 95 | return socket_->SetError(error); |
| 96 | } |
| 97 | |
| 98 | AsyncSocket::ConnState AsyncSocketAdapter::GetState() const { |
| 99 | return socket_->GetState(); |
| 100 | } |
| 101 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 | [diff] [blame] | 102 | int AsyncSocketAdapter::GetOption(Option opt, int* value) { |
| 103 | return socket_->GetOption(opt, value); |
| 104 | } |
| 105 | |
| 106 | int AsyncSocketAdapter::SetOption(Option opt, int value) { |
| 107 | return socket_->SetOption(opt, value); |
| 108 | } |
| 109 | |
| 110 | void AsyncSocketAdapter::OnConnectEvent(AsyncSocket* socket) { |
| 111 | SignalConnectEvent(this); |
| 112 | } |
| 113 | |
| 114 | void AsyncSocketAdapter::OnReadEvent(AsyncSocket* socket) { |
| 115 | SignalReadEvent(this); |
| 116 | } |
| 117 | |
| 118 | void AsyncSocketAdapter::OnWriteEvent(AsyncSocket* socket) { |
| 119 | SignalWriteEvent(this); |
| 120 | } |
| 121 | |
| 122 | void AsyncSocketAdapter::OnCloseEvent(AsyncSocket* socket, int err) { |
| 123 | SignalCloseEvent(this, err); |
| 124 | } |
| 125 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 126 | } // namespace rtc |