henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2012, Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | // This file contains interfaces for DataChannels |
| 29 | // http://dev.w3.org/2011/webrtc/editor/webrtc.html#rtcdatachannel |
| 30 | |
| 31 | #ifndef TALK_APP_WEBRTC_DATACHANNELINTERFACE_H_ |
| 32 | #define TALK_APP_WEBRTC_DATACHANNELINTERFACE_H_ |
| 33 | |
| 34 | #include <string> |
| 35 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 36 | #include "webrtc/base/basictypes.h" |
| 37 | #include "webrtc/base/buffer.h" |
decurtis@webrtc.org | 487a444 | 2015-01-15 22:55:07 +0000 | [diff] [blame] | 38 | #include "webrtc/base/checks.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 39 | #include "webrtc/base/refcount.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 40 | |
| 41 | |
| 42 | namespace webrtc { |
| 43 | |
| 44 | struct DataChannelInit { |
| 45 | DataChannelInit() |
| 46 | : reliable(false), |
| 47 | ordered(true), |
| 48 | maxRetransmitTime(-1), |
| 49 | maxRetransmits(-1), |
| 50 | negotiated(false), |
| 51 | id(-1) { |
| 52 | } |
| 53 | |
| 54 | bool reliable; // Deprecated. |
| 55 | bool ordered; // True if ordered delivery is required. |
| 56 | int maxRetransmitTime; // The max period of time in milliseconds in which |
| 57 | // retransmissions will be sent. After this time, no |
| 58 | // more retransmissions will be sent. -1 if unset. |
| 59 | int maxRetransmits; // The max number of retransmissions. -1 if unset. |
| 60 | std::string protocol; // This is set by the application and opaque to the |
| 61 | // WebRTC implementation. |
| 62 | bool negotiated; // True if the channel has been externally negotiated |
| 63 | // and we do not send an in-band signalling in the |
| 64 | // form of an "open" message. |
| 65 | int id; // The stream id, or SID, for SCTP data channels. -1 |
| 66 | // if unset. |
| 67 | }; |
| 68 | |
| 69 | struct DataBuffer { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 70 | DataBuffer(const rtc::Buffer& data, bool binary) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 71 | : data(data), |
| 72 | binary(binary) { |
| 73 | } |
| 74 | // For convenience for unit tests. |
| 75 | explicit DataBuffer(const std::string& text) |
| 76 | : data(text.data(), text.length()), |
| 77 | binary(false) { |
| 78 | } |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 79 | size_t size() const { return data.length(); } |
| 80 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 81 | rtc::Buffer data; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 82 | // Indicates if the received data contains UTF-8 or binary data. |
| 83 | // Note that the upper layers are left to verify the UTF-8 encoding. |
| 84 | // TODO(jiayl): prefer to use an enum instead of a bool. |
| 85 | bool binary; |
| 86 | }; |
| 87 | |
| 88 | class DataChannelObserver { |
| 89 | public: |
| 90 | // The data channel state have changed. |
| 91 | virtual void OnStateChange() = 0; |
| 92 | // A data buffer was successfully received. |
| 93 | virtual void OnMessage(const DataBuffer& buffer) = 0; |
| 94 | |
| 95 | protected: |
| 96 | virtual ~DataChannelObserver() {} |
| 97 | }; |
| 98 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 99 | class DataChannelInterface : public rtc::RefCountInterface { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 100 | public: |
tkchin@webrtc.org | ff27332 | 2014-04-30 18:32:33 +0000 | [diff] [blame] | 101 | // Keep in sync with DataChannel.java:State and |
| 102 | // RTCDataChannel.h:RTCDataChannelState. |
| 103 | enum DataState { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 104 | kConnecting, |
| 105 | kOpen, // The DataChannel is ready to send data. |
| 106 | kClosing, |
| 107 | kClosed |
| 108 | }; |
| 109 | |
decurtis@webrtc.org | 487a444 | 2015-01-15 22:55:07 +0000 | [diff] [blame] | 110 | static const char* DataStateString(DataState state) { |
| 111 | switch (state) { |
| 112 | case kConnecting: |
| 113 | return "connecting"; |
| 114 | case kOpen: |
| 115 | return "open"; |
| 116 | case kClosing: |
| 117 | return "closing"; |
| 118 | case kClosed: |
| 119 | return "closed"; |
| 120 | } |
| 121 | CHECK(false) << "Unknown DataChannel state: " << state; |
| 122 | return ""; |
| 123 | } |
| 124 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 125 | virtual void RegisterObserver(DataChannelObserver* observer) = 0; |
| 126 | virtual void UnregisterObserver() = 0; |
| 127 | // The label attribute represents a label that can be used to distinguish this |
| 128 | // DataChannel object from other DataChannel objects. |
| 129 | virtual std::string label() const = 0; |
| 130 | virtual bool reliable() const = 0; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 131 | |
| 132 | // TODO(tommyw): Remove these dummy implementations when all classes have |
| 133 | // implemented these APIs. They should all just return the values the |
| 134 | // DataChannel was created with. |
| 135 | virtual bool ordered() const { return false; } |
| 136 | virtual uint16 maxRetransmitTime() const { return 0; } |
| 137 | virtual uint16 maxRetransmits() const { return 0; } |
| 138 | virtual std::string protocol() const { return std::string(); } |
| 139 | virtual bool negotiated() const { return false; } |
| 140 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 141 | virtual int id() const = 0; |
| 142 | virtual DataState state() const = 0; |
| 143 | // The buffered_amount returns the number of bytes of application data |
| 144 | // (UTF-8 text and binary data) that have been queued using SendBuffer but |
| 145 | // have not yet been transmitted to the network. |
| 146 | virtual uint64 buffered_amount() const = 0; |
| 147 | virtual void Close() = 0; |
| 148 | // Sends |data| to the remote peer. |
| 149 | virtual bool Send(const DataBuffer& buffer) = 0; |
| 150 | |
| 151 | protected: |
| 152 | virtual ~DataChannelInterface() {} |
| 153 | }; |
| 154 | |
| 155 | } // namespace webrtc |
| 156 | |
| 157 | #endif // TALK_APP_WEBRTC_DATACHANNELINTERFACE_H_ |