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/socket_stream.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 13 | #include "rtc_base/checks.h" |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 14 | #include "rtc_base/socket.h" |
nisse | ede5da4 | 2017-01-12 13:15:36 | [diff] [blame] | 15 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 16 | namespace rtc { |
| 17 | |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 18 | SocketStream::SocketStream(Socket* socket) : socket_(nullptr) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 19 | Attach(socket); |
| 20 | } |
| 21 | |
| 22 | SocketStream::~SocketStream() { |
| 23 | delete socket_; |
| 24 | } |
| 25 | |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 26 | void SocketStream::Attach(Socket* socket) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 27 | if (socket_) |
| 28 | delete socket_; |
| 29 | socket_ = socket; |
| 30 | if (socket_) { |
| 31 | socket_->SignalConnectEvent.connect(this, &SocketStream::OnConnectEvent); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 32 | socket_->SignalReadEvent.connect(this, &SocketStream::OnReadEvent); |
| 33 | socket_->SignalWriteEvent.connect(this, &SocketStream::OnWriteEvent); |
| 34 | socket_->SignalCloseEvent.connect(this, &SocketStream::OnCloseEvent); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 38 | Socket* SocketStream::Detach() { |
| 39 | Socket* socket = socket_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 40 | if (socket_) { |
| 41 | socket_->SignalConnectEvent.disconnect(this); |
| 42 | socket_->SignalReadEvent.disconnect(this); |
| 43 | socket_->SignalWriteEvent.disconnect(this); |
| 44 | socket_->SignalCloseEvent.disconnect(this); |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 45 | socket_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 46 | } |
| 47 | return socket; |
| 48 | } |
| 49 | |
| 50 | StreamState SocketStream::GetState() const { |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 51 | RTC_DCHECK(socket_ != nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 52 | switch (socket_->GetState()) { |
| 53 | case Socket::CS_CONNECTED: |
| 54 | return SS_OPEN; |
| 55 | case Socket::CS_CONNECTING: |
| 56 | return SS_OPENING; |
| 57 | case Socket::CS_CLOSED: |
| 58 | default: |
| 59 | return SS_CLOSED; |
| 60 | } |
| 61 | } |
| 62 | |
Harald Alvestrand | 7ccd88f | 2022-11-16 08:59:58 | [diff] [blame] | 63 | StreamResult SocketStream::Read(rtc::ArrayView<uint8_t> buffer, |
| 64 | size_t& read, |
| 65 | int& error) { |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 66 | RTC_DCHECK(socket_ != nullptr); |
Harald Alvestrand | 7ccd88f | 2022-11-16 08:59:58 | [diff] [blame] | 67 | int result = socket_->Recv(buffer.data(), buffer.size(), nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 68 | if (result < 0) { |
| 69 | if (socket_->IsBlocking()) |
| 70 | return SR_BLOCK; |
Harald Alvestrand | 7ccd88f | 2022-11-16 08:59:58 | [diff] [blame] | 71 | error = socket_->GetError(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 72 | return SR_ERROR; |
| 73 | } |
Harald Alvestrand | 7ccd88f | 2022-11-16 08:59:58 | [diff] [blame] | 74 | if ((result > 0) || (buffer.size() == 0)) { |
| 75 | read = result; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 76 | return SR_SUCCESS; |
| 77 | } |
| 78 | return SR_EOS; |
| 79 | } |
| 80 | |
Harald Alvestrand | 7ccd88f | 2022-11-16 08:59:58 | [diff] [blame] | 81 | StreamResult SocketStream::Write(rtc::ArrayView<const uint8_t> data, |
| 82 | size_t& written, |
| 83 | int& error) { |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 84 | RTC_DCHECK(socket_ != nullptr); |
Harald Alvestrand | 7ccd88f | 2022-11-16 08:59:58 | [diff] [blame] | 85 | int result = socket_->Send(data.data(), data.size()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 86 | if (result < 0) { |
| 87 | if (socket_->IsBlocking()) |
| 88 | return SR_BLOCK; |
Harald Alvestrand | 7ccd88f | 2022-11-16 08:59:58 | [diff] [blame] | 89 | error = socket_->GetError(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 90 | return SR_ERROR; |
| 91 | } |
Harald Alvestrand | 7ccd88f | 2022-11-16 08:59:58 | [diff] [blame] | 92 | written = result; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 93 | return SR_SUCCESS; |
| 94 | } |
| 95 | |
| 96 | void SocketStream::Close() { |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 97 | RTC_DCHECK(socket_ != nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 98 | socket_->Close(); |
| 99 | } |
| 100 | |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 101 | void SocketStream::OnConnectEvent(Socket* socket) { |
nisse | ede5da4 | 2017-01-12 13:15:36 | [diff] [blame] | 102 | RTC_DCHECK(socket == socket_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 103 | SignalEvent(this, SE_OPEN | SE_READ | SE_WRITE, 0); |
| 104 | } |
| 105 | |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 106 | void SocketStream::OnReadEvent(Socket* socket) { |
nisse | ede5da4 | 2017-01-12 13:15:36 | [diff] [blame] | 107 | RTC_DCHECK(socket == socket_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 108 | SignalEvent(this, SE_READ, 0); |
| 109 | } |
| 110 | |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 111 | void SocketStream::OnWriteEvent(Socket* socket) { |
nisse | ede5da4 | 2017-01-12 13:15:36 | [diff] [blame] | 112 | RTC_DCHECK(socket == socket_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 113 | SignalEvent(this, SE_WRITE, 0); |
| 114 | } |
| 115 | |
Niels Möller | d0b8879 | 2021-08-12 08:32:30 | [diff] [blame] | 116 | void SocketStream::OnCloseEvent(Socket* socket, int err) { |
nisse | ede5da4 | 2017-01-12 13:15:36 | [diff] [blame] | 117 | RTC_DCHECK(socket == socket_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 118 | SignalEvent(this, SE_CLOSE, err); |
| 119 | } |
| 120 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 121 | } // namespace rtc |