OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2010 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2010 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/base/socketstream.h" | 11 #include "webrtc/base/socketstream.h" |
12 | 12 |
13 #include "webrtc/base/checks.h" | 13 #include "webrtc/base/checks.h" |
14 | 14 |
15 namespace rtc { | 15 namespace rtc { |
16 | 16 |
17 SocketStream::SocketStream(AsyncSocket* socket) : socket_(NULL) { | 17 SocketStream::SocketStream(AsyncSocket* socket) : socket_(nullptr) { |
18 Attach(socket); | 18 Attach(socket); |
19 } | 19 } |
20 | 20 |
21 SocketStream::~SocketStream() { | 21 SocketStream::~SocketStream() { |
22 delete socket_; | 22 delete socket_; |
23 } | 23 } |
24 | 24 |
25 void SocketStream::Attach(AsyncSocket* socket) { | 25 void SocketStream::Attach(AsyncSocket* socket) { |
26 if (socket_) | 26 if (socket_) |
27 delete socket_; | 27 delete socket_; |
28 socket_ = socket; | 28 socket_ = socket; |
29 if (socket_) { | 29 if (socket_) { |
30 socket_->SignalConnectEvent.connect(this, &SocketStream::OnConnectEvent); | 30 socket_->SignalConnectEvent.connect(this, &SocketStream::OnConnectEvent); |
31 socket_->SignalReadEvent.connect(this, &SocketStream::OnReadEvent); | 31 socket_->SignalReadEvent.connect(this, &SocketStream::OnReadEvent); |
32 socket_->SignalWriteEvent.connect(this, &SocketStream::OnWriteEvent); | 32 socket_->SignalWriteEvent.connect(this, &SocketStream::OnWriteEvent); |
33 socket_->SignalCloseEvent.connect(this, &SocketStream::OnCloseEvent); | 33 socket_->SignalCloseEvent.connect(this, &SocketStream::OnCloseEvent); |
34 } | 34 } |
35 } | 35 } |
36 | 36 |
37 AsyncSocket* SocketStream::Detach() { | 37 AsyncSocket* SocketStream::Detach() { |
38 AsyncSocket* socket = socket_; | 38 AsyncSocket* socket = socket_; |
39 if (socket_) { | 39 if (socket_) { |
40 socket_->SignalConnectEvent.disconnect(this); | 40 socket_->SignalConnectEvent.disconnect(this); |
41 socket_->SignalReadEvent.disconnect(this); | 41 socket_->SignalReadEvent.disconnect(this); |
42 socket_->SignalWriteEvent.disconnect(this); | 42 socket_->SignalWriteEvent.disconnect(this); |
43 socket_->SignalCloseEvent.disconnect(this); | 43 socket_->SignalCloseEvent.disconnect(this); |
44 socket_ = NULL; | 44 socket_ = nullptr; |
45 } | 45 } |
46 return socket; | 46 return socket; |
47 } | 47 } |
48 | 48 |
49 StreamState SocketStream::GetState() const { | 49 StreamState SocketStream::GetState() const { |
50 RTC_DCHECK(socket_ != NULL); | 50 RTC_DCHECK(socket_ != nullptr); |
51 switch (socket_->GetState()) { | 51 switch (socket_->GetState()) { |
52 case Socket::CS_CONNECTED: | 52 case Socket::CS_CONNECTED: |
53 return SS_OPEN; | 53 return SS_OPEN; |
54 case Socket::CS_CONNECTING: | 54 case Socket::CS_CONNECTING: |
55 return SS_OPENING; | 55 return SS_OPENING; |
56 case Socket::CS_CLOSED: | 56 case Socket::CS_CLOSED: |
57 default: | 57 default: |
58 return SS_CLOSED; | 58 return SS_CLOSED; |
59 } | 59 } |
60 } | 60 } |
61 | 61 |
62 StreamResult SocketStream::Read(void* buffer, size_t buffer_len, | 62 StreamResult SocketStream::Read(void* buffer, size_t buffer_len, |
63 size_t* read, int* error) { | 63 size_t* read, int* error) { |
64 RTC_DCHECK(socket_ != NULL); | 64 RTC_DCHECK(socket_ != nullptr); |
65 int result = socket_->Recv(buffer, buffer_len, nullptr); | 65 int result = socket_->Recv(buffer, buffer_len, nullptr); |
66 if (result < 0) { | 66 if (result < 0) { |
67 if (socket_->IsBlocking()) | 67 if (socket_->IsBlocking()) |
68 return SR_BLOCK; | 68 return SR_BLOCK; |
69 if (error) | 69 if (error) |
70 *error = socket_->GetError(); | 70 *error = socket_->GetError(); |
71 return SR_ERROR; | 71 return SR_ERROR; |
72 } | 72 } |
73 if ((result > 0) || (buffer_len == 0)) { | 73 if ((result > 0) || (buffer_len == 0)) { |
74 if (read) | 74 if (read) |
75 *read = result; | 75 *read = result; |
76 return SR_SUCCESS; | 76 return SR_SUCCESS; |
77 } | 77 } |
78 return SR_EOS; | 78 return SR_EOS; |
79 } | 79 } |
80 | 80 |
81 StreamResult SocketStream::Write(const void* data, size_t data_len, | 81 StreamResult SocketStream::Write(const void* data, size_t data_len, |
82 size_t* written, int* error) { | 82 size_t* written, int* error) { |
83 RTC_DCHECK(socket_ != NULL); | 83 RTC_DCHECK(socket_ != nullptr); |
84 int result = socket_->Send(data, data_len); | 84 int result = socket_->Send(data, data_len); |
85 if (result < 0) { | 85 if (result < 0) { |
86 if (socket_->IsBlocking()) | 86 if (socket_->IsBlocking()) |
87 return SR_BLOCK; | 87 return SR_BLOCK; |
88 if (error) | 88 if (error) |
89 *error = socket_->GetError(); | 89 *error = socket_->GetError(); |
90 return SR_ERROR; | 90 return SR_ERROR; |
91 } | 91 } |
92 if (written) | 92 if (written) |
93 *written = result; | 93 *written = result; |
94 return SR_SUCCESS; | 94 return SR_SUCCESS; |
95 } | 95 } |
96 | 96 |
97 void SocketStream::Close() { | 97 void SocketStream::Close() { |
98 RTC_DCHECK(socket_ != NULL); | 98 RTC_DCHECK(socket_ != nullptr); |
99 socket_->Close(); | 99 socket_->Close(); |
100 } | 100 } |
101 | 101 |
102 void SocketStream::OnConnectEvent(AsyncSocket* socket) { | 102 void SocketStream::OnConnectEvent(AsyncSocket* socket) { |
103 RTC_DCHECK(socket == socket_); | 103 RTC_DCHECK(socket == socket_); |
104 SignalEvent(this, SE_OPEN | SE_READ | SE_WRITE, 0); | 104 SignalEvent(this, SE_OPEN | SE_READ | SE_WRITE, 0); |
105 } | 105 } |
106 | 106 |
107 void SocketStream::OnReadEvent(AsyncSocket* socket) { | 107 void SocketStream::OnReadEvent(AsyncSocket* socket) { |
108 RTC_DCHECK(socket == socket_); | 108 RTC_DCHECK(socket == socket_); |
109 SignalEvent(this, SE_READ, 0); | 109 SignalEvent(this, SE_READ, 0); |
110 } | 110 } |
111 | 111 |
112 void SocketStream::OnWriteEvent(AsyncSocket* socket) { | 112 void SocketStream::OnWriteEvent(AsyncSocket* socket) { |
113 RTC_DCHECK(socket == socket_); | 113 RTC_DCHECK(socket == socket_); |
114 SignalEvent(this, SE_WRITE, 0); | 114 SignalEvent(this, SE_WRITE, 0); |
115 } | 115 } |
116 | 116 |
117 void SocketStream::OnCloseEvent(AsyncSocket* socket, int err) { | 117 void SocketStream::OnCloseEvent(AsyncSocket* socket, int err) { |
118 RTC_DCHECK(socket == socket_); | 118 RTC_DCHECK(socket == socket_); |
119 SignalEvent(this, SE_CLOSE, err); | 119 SignalEvent(this, SE_CLOSE, err); |
120 } | 120 } |
121 | 121 |
122 | 122 |
123 } // namespace rtc | 123 } // namespace rtc |
OLD | NEW |