blob: 5c993ea233ea1c9e9eaa33a89d3bfea027ceca43 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 Anton10542f22019-01-11 17:11:0011#include "rtc_base/socket_stream.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2612
Mirko Bonadei92ea95e2017-09-15 04:47:3113#include "rtc_base/checks.h"
Yves Gerey988cc082018-10-23 10:03:0114#include "rtc_base/socket.h"
nisseede5da42017-01-12 13:15:3615
henrike@webrtc.orgf0488722014-05-13 18:00:2616namespace rtc {
17
Niels Möllerd0b88792021-08-12 08:32:3018SocketStream::SocketStream(Socket* socket) : socket_(nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:2619 Attach(socket);
20}
21
22SocketStream::~SocketStream() {
23 delete socket_;
24}
25
Niels Möllerd0b88792021-08-12 08:32:3026void SocketStream::Attach(Socket* socket) {
henrike@webrtc.orgf0488722014-05-13 18:00:2627 if (socket_)
28 delete socket_;
29 socket_ = socket;
30 if (socket_) {
31 socket_->SignalConnectEvent.connect(this, &SocketStream::OnConnectEvent);
Yves Gerey665174f2018-06-19 13:03:0532 socket_->SignalReadEvent.connect(this, &SocketStream::OnReadEvent);
33 socket_->SignalWriteEvent.connect(this, &SocketStream::OnWriteEvent);
34 socket_->SignalCloseEvent.connect(this, &SocketStream::OnCloseEvent);
henrike@webrtc.orgf0488722014-05-13 18:00:2635 }
36}
37
Niels Möllerd0b88792021-08-12 08:32:3038Socket* SocketStream::Detach() {
39 Socket* socket = socket_;
henrike@webrtc.orgf0488722014-05-13 18:00:2640 if (socket_) {
41 socket_->SignalConnectEvent.disconnect(this);
42 socket_->SignalReadEvent.disconnect(this);
43 socket_->SignalWriteEvent.disconnect(this);
44 socket_->SignalCloseEvent.disconnect(this);
deadbeef37f5ecf2017-02-27 22:06:4145 socket_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:2646 }
47 return socket;
48}
49
50StreamState SocketStream::GetState() const {
deadbeef37f5ecf2017-02-27 22:06:4151 RTC_DCHECK(socket_ != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:2652 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 Alvestrand7ccd88f2022-11-16 08:59:5863StreamResult SocketStream::Read(rtc::ArrayView<uint8_t> buffer,
64 size_t& read,
65 int& error) {
deadbeef37f5ecf2017-02-27 22:06:4166 RTC_DCHECK(socket_ != nullptr);
Harald Alvestrand7ccd88f2022-11-16 08:59:5867 int result = socket_->Recv(buffer.data(), buffer.size(), nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:2668 if (result < 0) {
69 if (socket_->IsBlocking())
70 return SR_BLOCK;
Harald Alvestrand7ccd88f2022-11-16 08:59:5871 error = socket_->GetError();
henrike@webrtc.orgf0488722014-05-13 18:00:2672 return SR_ERROR;
73 }
Harald Alvestrand7ccd88f2022-11-16 08:59:5874 if ((result > 0) || (buffer.size() == 0)) {
75 read = result;
henrike@webrtc.orgf0488722014-05-13 18:00:2676 return SR_SUCCESS;
77 }
78 return SR_EOS;
79}
80
Harald Alvestrand7ccd88f2022-11-16 08:59:5881StreamResult SocketStream::Write(rtc::ArrayView<const uint8_t> data,
82 size_t& written,
83 int& error) {
deadbeef37f5ecf2017-02-27 22:06:4184 RTC_DCHECK(socket_ != nullptr);
Harald Alvestrand7ccd88f2022-11-16 08:59:5885 int result = socket_->Send(data.data(), data.size());
henrike@webrtc.orgf0488722014-05-13 18:00:2686 if (result < 0) {
87 if (socket_->IsBlocking())
88 return SR_BLOCK;
Harald Alvestrand7ccd88f2022-11-16 08:59:5889 error = socket_->GetError();
henrike@webrtc.orgf0488722014-05-13 18:00:2690 return SR_ERROR;
91 }
Harald Alvestrand7ccd88f2022-11-16 08:59:5892 written = result;
henrike@webrtc.orgf0488722014-05-13 18:00:2693 return SR_SUCCESS;
94}
95
96void SocketStream::Close() {
deadbeef37f5ecf2017-02-27 22:06:4197 RTC_DCHECK(socket_ != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:2698 socket_->Close();
99}
100
Niels Möllerd0b88792021-08-12 08:32:30101void SocketStream::OnConnectEvent(Socket* socket) {
nisseede5da42017-01-12 13:15:36102 RTC_DCHECK(socket == socket_);
henrike@webrtc.orgf0488722014-05-13 18:00:26103 SignalEvent(this, SE_OPEN | SE_READ | SE_WRITE, 0);
104}
105
Niels Möllerd0b88792021-08-12 08:32:30106void SocketStream::OnReadEvent(Socket* socket) {
nisseede5da42017-01-12 13:15:36107 RTC_DCHECK(socket == socket_);
henrike@webrtc.orgf0488722014-05-13 18:00:26108 SignalEvent(this, SE_READ, 0);
109}
110
Niels Möllerd0b88792021-08-12 08:32:30111void SocketStream::OnWriteEvent(Socket* socket) {
nisseede5da42017-01-12 13:15:36112 RTC_DCHECK(socket == socket_);
henrike@webrtc.orgf0488722014-05-13 18:00:26113 SignalEvent(this, SE_WRITE, 0);
114}
115
Niels Möllerd0b88792021-08-12 08:32:30116void SocketStream::OnCloseEvent(Socket* socket, int err) {
nisseede5da42017-01-12 13:15:36117 RTC_DCHECK(socket == socket_);
henrike@webrtc.orgf0488722014-05-13 18:00:26118 SignalEvent(this, SE_CLOSE, err);
119}
120
henrike@webrtc.orgf0488722014-05-13 18:00:26121} // namespace rtc