You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 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. #include "rtc_base/stream.h"
  11. #include <errno.h>
  12. #include <string.h>
  13. #include <algorithm>
  14. #include <string>
  15. #include "rtc_base/checks.h"
  16. #include "rtc_base/location.h"
  17. #include "rtc_base/message_queue.h"
  18. #include "rtc_base/thread.h"
  19. namespace rtc {
  20. ///////////////////////////////////////////////////////////////////////////////
  21. // StreamInterface
  22. ///////////////////////////////////////////////////////////////////////////////
  23. StreamInterface::~StreamInterface() {}
  24. StreamResult StreamInterface::WriteAll(const void* data,
  25. size_t data_len,
  26. size_t* written,
  27. int* error) {
  28. StreamResult result = SR_SUCCESS;
  29. size_t total_written = 0, current_written;
  30. while (total_written < data_len) {
  31. result = Write(static_cast<const char*>(data) + total_written,
  32. data_len - total_written, &current_written, error);
  33. if (result != SR_SUCCESS)
  34. break;
  35. total_written += current_written;
  36. }
  37. if (written)
  38. *written = total_written;
  39. return result;
  40. }
  41. void StreamInterface::PostEvent(Thread* t, int events, int err) {
  42. t->Post(RTC_FROM_HERE, this, MSG_POST_EVENT,
  43. new StreamEventData(events, err));
  44. }
  45. void StreamInterface::PostEvent(int events, int err) {
  46. PostEvent(Thread::Current(), events, err);
  47. }
  48. bool StreamInterface::Flush() {
  49. return false;
  50. }
  51. StreamInterface::StreamInterface() {}
  52. void StreamInterface::OnMessage(Message* msg) {
  53. if (MSG_POST_EVENT == msg->message_id) {
  54. StreamEventData* pe = static_cast<StreamEventData*>(msg->pdata);
  55. SignalEvent(this, pe->events, pe->error);
  56. delete msg->pdata;
  57. }
  58. }
  59. ///////////////////////////////////////////////////////////////////////////////
  60. // StreamAdapterInterface
  61. ///////////////////////////////////////////////////////////////////////////////
  62. StreamAdapterInterface::StreamAdapterInterface(StreamInterface* stream,
  63. bool owned)
  64. : stream_(stream), owned_(owned) {
  65. if (nullptr != stream_)
  66. stream_->SignalEvent.connect(this, &StreamAdapterInterface::OnEvent);
  67. }
  68. StreamState StreamAdapterInterface::GetState() const {
  69. return stream_->GetState();
  70. }
  71. StreamResult StreamAdapterInterface::Read(void* buffer,
  72. size_t buffer_len,
  73. size_t* read,
  74. int* error) {
  75. return stream_->Read(buffer, buffer_len, read, error);
  76. }
  77. StreamResult StreamAdapterInterface::Write(const void* data,
  78. size_t data_len,
  79. size_t* written,
  80. int* error) {
  81. return stream_->Write(data, data_len, written, error);
  82. }
  83. void StreamAdapterInterface::Close() {
  84. stream_->Close();
  85. }
  86. bool StreamAdapterInterface::Flush() {
  87. return stream_->Flush();
  88. }
  89. void StreamAdapterInterface::Attach(StreamInterface* stream, bool owned) {
  90. if (nullptr != stream_)
  91. stream_->SignalEvent.disconnect(this);
  92. if (owned_)
  93. delete stream_;
  94. stream_ = stream;
  95. owned_ = owned;
  96. if (nullptr != stream_)
  97. stream_->SignalEvent.connect(this, &StreamAdapterInterface::OnEvent);
  98. }
  99. StreamInterface* StreamAdapterInterface::Detach() {
  100. if (nullptr != stream_)
  101. stream_->SignalEvent.disconnect(this);
  102. StreamInterface* stream = stream_;
  103. stream_ = nullptr;
  104. return stream;
  105. }
  106. StreamAdapterInterface::~StreamAdapterInterface() {
  107. if (owned_)
  108. delete stream_;
  109. }
  110. void StreamAdapterInterface::OnEvent(StreamInterface* stream,
  111. int events,
  112. int err) {
  113. SignalEvent(this, events, err);
  114. }
  115. } // namespace rtc