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/socket_stream.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "rtc_base/checks.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include "rtc_base/socket.h" |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 15 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 16 | namespace rtc { |
| 17 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 18 | SocketStream::SocketStream(AsyncSocket* socket) : socket_(nullptr) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 19 | Attach(socket); |
| 20 | } |
| 21 | |
| 22 | SocketStream::~SocketStream() { |
| 23 | delete socket_; |
| 24 | } |
| 25 | |
| 26 | void SocketStream::Attach(AsyncSocket* socket) { |
| 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 15:03:05 +0200 | [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 +0000 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
| 38 | AsyncSocket* SocketStream::Detach() { |
| 39 | AsyncSocket* socket = socket_; |
| 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 14:06:41 -0800 | [diff] [blame] | 45 | socket_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 46 | } |
| 47 | return socket; |
| 48 | } |
| 49 | |
| 50 | StreamState SocketStream::GetState() const { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 51 | RTC_DCHECK(socket_ != nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [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 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 63 | StreamResult SocketStream::Read(void* buffer, |
| 64 | size_t buffer_len, |
| 65 | size_t* read, |
| 66 | int* error) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 67 | RTC_DCHECK(socket_ != nullptr); |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 68 | int result = socket_->Recv(buffer, buffer_len, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 69 | if (result < 0) { |
| 70 | if (socket_->IsBlocking()) |
| 71 | return SR_BLOCK; |
| 72 | if (error) |
| 73 | *error = socket_->GetError(); |
| 74 | return SR_ERROR; |
| 75 | } |
| 76 | if ((result > 0) || (buffer_len == 0)) { |
| 77 | if (read) |
| 78 | *read = result; |
| 79 | return SR_SUCCESS; |
| 80 | } |
| 81 | return SR_EOS; |
| 82 | } |
| 83 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 84 | StreamResult SocketStream::Write(const void* data, |
| 85 | size_t data_len, |
| 86 | size_t* written, |
| 87 | int* error) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 88 | RTC_DCHECK(socket_ != nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 89 | int result = socket_->Send(data, data_len); |
| 90 | if (result < 0) { |
| 91 | if (socket_->IsBlocking()) |
| 92 | return SR_BLOCK; |
| 93 | if (error) |
| 94 | *error = socket_->GetError(); |
| 95 | return SR_ERROR; |
| 96 | } |
| 97 | if (written) |
| 98 | *written = result; |
| 99 | return SR_SUCCESS; |
| 100 | } |
| 101 | |
| 102 | void SocketStream::Close() { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 103 | RTC_DCHECK(socket_ != nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 104 | socket_->Close(); |
| 105 | } |
| 106 | |
| 107 | void SocketStream::OnConnectEvent(AsyncSocket* socket) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 108 | RTC_DCHECK(socket == socket_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 109 | SignalEvent(this, SE_OPEN | SE_READ | SE_WRITE, 0); |
| 110 | } |
| 111 | |
| 112 | void SocketStream::OnReadEvent(AsyncSocket* socket) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 113 | RTC_DCHECK(socket == socket_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 114 | SignalEvent(this, SE_READ, 0); |
| 115 | } |
| 116 | |
| 117 | void SocketStream::OnWriteEvent(AsyncSocket* socket) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 118 | RTC_DCHECK(socket == socket_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 119 | SignalEvent(this, SE_WRITE, 0); |
| 120 | } |
| 121 | |
| 122 | void SocketStream::OnCloseEvent(AsyncSocket* socket, int err) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 123 | RTC_DCHECK(socket == socket_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 124 | SignalEvent(this, SE_CLOSE, err); |
| 125 | } |
| 126 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 127 | } // namespace rtc |