blob: e93d6aa3a02fcd2414090b27842ae9ccf3a00abe [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 09:11:00 -080011#include "rtc_base/socket_stream.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/checks.h"
Yves Gerey988cc082018-10-23 12:03:01 +020014#include "rtc_base/socket.h"
nisseede5da42017-01-12 05:15:36 -080015
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016namespace rtc {
17
deadbeef37f5ecf2017-02-27 14:06:41 -080018SocketStream::SocketStream(AsyncSocket* socket) : socket_(nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019 Attach(socket);
20}
21
22SocketStream::~SocketStream() {
23 delete socket_;
24}
25
26void SocketStream::Attach(AsyncSocket* socket) {
27 if (socket_)
28 delete socket_;
29 socket_ = socket;
30 if (socket_) {
31 socket_->SignalConnectEvent.connect(this, &SocketStream::OnConnectEvent);
Yves Gerey665174f2018-06-19 15:03:05 +020032 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:26 +000035 }
36}
37
38AsyncSocket* 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);
deadbeef37f5ecf2017-02-27 14:06:41 -080045 socket_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046 }
47 return socket;
48}
49
50StreamState SocketStream::GetState() const {
deadbeef37f5ecf2017-02-27 14:06:41 -080051 RTC_DCHECK(socket_ != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000052 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 Gerey665174f2018-06-19 15:03:05 +020063StreamResult SocketStream::Read(void* buffer,
64 size_t buffer_len,
65 size_t* read,
66 int* error) {
deadbeef37f5ecf2017-02-27 14:06:41 -080067 RTC_DCHECK(socket_ != nullptr);
Stefan Holmer9131efd2016-05-23 18:19:26 +020068 int result = socket_->Recv(buffer, buffer_len, nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069 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 Gerey665174f2018-06-19 15:03:05 +020084StreamResult SocketStream::Write(const void* data,
85 size_t data_len,
86 size_t* written,
87 int* error) {
deadbeef37f5ecf2017-02-27 14:06:41 -080088 RTC_DCHECK(socket_ != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000089 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
102void SocketStream::Close() {
deadbeef37f5ecf2017-02-27 14:06:41 -0800103 RTC_DCHECK(socket_ != nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000104 socket_->Close();
105}
106
107void SocketStream::OnConnectEvent(AsyncSocket* socket) {
nisseede5da42017-01-12 05:15:36 -0800108 RTC_DCHECK(socket == socket_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000109 SignalEvent(this, SE_OPEN | SE_READ | SE_WRITE, 0);
110}
111
112void SocketStream::OnReadEvent(AsyncSocket* socket) {
nisseede5da42017-01-12 05:15:36 -0800113 RTC_DCHECK(socket == socket_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000114 SignalEvent(this, SE_READ, 0);
115}
116
117void SocketStream::OnWriteEvent(AsyncSocket* socket) {
nisseede5da42017-01-12 05:15:36 -0800118 RTC_DCHECK(socket == socket_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000119 SignalEvent(this, SE_WRITE, 0);
120}
121
122void SocketStream::OnCloseEvent(AsyncSocket* socket, int err) {
nisseede5da42017-01-12 05:15:36 -0800123 RTC_DCHECK(socket == socket_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000124 SignalEvent(this, SE_CLOSE, err);
125}
126
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000127} // namespace rtc