Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 5 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef NETWERK_SCTP_DATACHANNEL_DATACHANNEL_H_ |
michael@0 | 8 | #define NETWERK_SCTP_DATACHANNEL_DATACHANNEL_H_ |
michael@0 | 9 | |
michael@0 | 10 | #ifdef MOZ_WEBRTC_SIGNALING |
michael@0 | 11 | #define SCTP_DTLS_SUPPORTED 1 |
michael@0 | 12 | #endif |
michael@0 | 13 | |
michael@0 | 14 | #include <string> |
michael@0 | 15 | #include <errno.h> |
michael@0 | 16 | #include "nsISupports.h" |
michael@0 | 17 | #include "nsCOMPtr.h" |
michael@0 | 18 | #include "mozilla/WeakPtr.h" |
michael@0 | 19 | #include "nsString.h" |
michael@0 | 20 | #include "nsThreadUtils.h" |
michael@0 | 21 | #include "nsTArray.h" |
michael@0 | 22 | #include "nsDeque.h" |
michael@0 | 23 | #include "nsIInputStream.h" |
michael@0 | 24 | #include "nsITimer.h" |
michael@0 | 25 | #include "mozilla/Mutex.h" |
michael@0 | 26 | #include "DataChannelProtocol.h" |
michael@0 | 27 | #include "DataChannelListener.h" |
michael@0 | 28 | #ifdef SCTP_DTLS_SUPPORTED |
michael@0 | 29 | #include "mtransport/sigslot.h" |
michael@0 | 30 | #include "mtransport/transportflow.h" |
michael@0 | 31 | #include "mtransport/transportlayer.h" |
michael@0 | 32 | #include "mtransport/transportlayerdtls.h" |
michael@0 | 33 | #include "mtransport/transportlayerprsock.h" |
michael@0 | 34 | #endif |
michael@0 | 35 | |
michael@0 | 36 | #ifndef DATACHANNEL_LOG |
michael@0 | 37 | #define DATACHANNEL_LOG(args) |
michael@0 | 38 | #endif |
michael@0 | 39 | |
michael@0 | 40 | #ifndef EALREADY |
michael@0 | 41 | #define EALREADY WSAEALREADY |
michael@0 | 42 | #endif |
michael@0 | 43 | |
michael@0 | 44 | extern "C" { |
michael@0 | 45 | struct socket; |
michael@0 | 46 | struct sctp_rcvinfo; |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | namespace mozilla { |
michael@0 | 50 | |
michael@0 | 51 | class DTLSConnection; |
michael@0 | 52 | class DataChannelConnection; |
michael@0 | 53 | class DataChannel; |
michael@0 | 54 | class DataChannelOnMessageAvailable; |
michael@0 | 55 | |
michael@0 | 56 | // For queuing outgoing messages |
michael@0 | 57 | class BufferedMsg |
michael@0 | 58 | { |
michael@0 | 59 | public: |
michael@0 | 60 | BufferedMsg(struct sctp_sendv_spa &spa,const char *data, |
michael@0 | 61 | uint32_t length); |
michael@0 | 62 | ~BufferedMsg(); |
michael@0 | 63 | |
michael@0 | 64 | struct sctp_sendv_spa *mSpa; |
michael@0 | 65 | const char *mData; |
michael@0 | 66 | uint32_t mLength; |
michael@0 | 67 | }; |
michael@0 | 68 | |
michael@0 | 69 | // for queuing incoming data messages before the Open or |
michael@0 | 70 | // external negotiation is indicated to us |
michael@0 | 71 | class QueuedDataMessage |
michael@0 | 72 | { |
michael@0 | 73 | public: |
michael@0 | 74 | QueuedDataMessage(uint16_t stream, uint32_t ppid, |
michael@0 | 75 | const void *data, size_t length) |
michael@0 | 76 | : mStream(stream) |
michael@0 | 77 | , mPpid(ppid) |
michael@0 | 78 | , mLength(length) |
michael@0 | 79 | { |
michael@0 | 80 | mData = static_cast<char *>(moz_xmalloc(length)); // infallible |
michael@0 | 81 | memcpy(mData, data, length); |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | ~QueuedDataMessage() |
michael@0 | 85 | { |
michael@0 | 86 | moz_free(mData); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | uint16_t mStream; |
michael@0 | 90 | uint32_t mPpid; |
michael@0 | 91 | size_t mLength; |
michael@0 | 92 | char *mData; |
michael@0 | 93 | }; |
michael@0 | 94 | |
michael@0 | 95 | // One per PeerConnection |
michael@0 | 96 | class DataChannelConnection: public nsITimerCallback |
michael@0 | 97 | #ifdef SCTP_DTLS_SUPPORTED |
michael@0 | 98 | , public sigslot::has_slots<> |
michael@0 | 99 | #endif |
michael@0 | 100 | { |
michael@0 | 101 | public: |
michael@0 | 102 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 103 | NS_DECL_NSITIMERCALLBACK |
michael@0 | 104 | |
michael@0 | 105 | class DataConnectionListener : public SupportsWeakPtr<DataConnectionListener> |
michael@0 | 106 | { |
michael@0 | 107 | public: |
michael@0 | 108 | MOZ_DECLARE_REFCOUNTED_TYPENAME(DataChannelConnection::DataConnectionListener) |
michael@0 | 109 | virtual ~DataConnectionListener() {} |
michael@0 | 110 | |
michael@0 | 111 | // Called when a the connection is open |
michael@0 | 112 | virtual void NotifyConnection() = 0; |
michael@0 | 113 | |
michael@0 | 114 | // Called when a the connection is lost/closed |
michael@0 | 115 | virtual void NotifyClosedConnection() = 0; |
michael@0 | 116 | |
michael@0 | 117 | // Called when a new DataChannel has been opened by the other side. |
michael@0 | 118 | virtual void NotifyDataChannel(already_AddRefed<DataChannel> channel) = 0; |
michael@0 | 119 | }; |
michael@0 | 120 | |
michael@0 | 121 | DataChannelConnection(DataConnectionListener *listener); |
michael@0 | 122 | virtual ~DataChannelConnection(); |
michael@0 | 123 | |
michael@0 | 124 | bool Init(unsigned short aPort, uint16_t aNumStreams, bool aUsingDtls); |
michael@0 | 125 | void Destroy(); // So we can spawn refs tied to runnables in shutdown |
michael@0 | 126 | // Finish Destroy on STS to avoid SCTP race condition with ABORT from far end |
michael@0 | 127 | void DestroyOnSTS(struct socket *aMasterSocket, |
michael@0 | 128 | struct socket *aSocket); |
michael@0 | 129 | |
michael@0 | 130 | #ifdef ALLOW_DIRECT_SCTP_LISTEN_CONNECT |
michael@0 | 131 | // These block; they require something to decide on listener/connector |
michael@0 | 132 | // (though you can do simultaneous Connect()). Do not call these from |
michael@0 | 133 | // the main thread! |
michael@0 | 134 | bool Listen(unsigned short port); |
michael@0 | 135 | bool Connect(const char *addr, unsigned short port); |
michael@0 | 136 | #endif |
michael@0 | 137 | |
michael@0 | 138 | #ifdef SCTP_DTLS_SUPPORTED |
michael@0 | 139 | // Connect using a TransportFlow (DTLS) channel |
michael@0 | 140 | void SetEvenOdd(); |
michael@0 | 141 | bool ConnectViaTransportFlow(TransportFlow *aFlow, uint16_t localport, uint16_t remoteport); |
michael@0 | 142 | void CompleteConnect(TransportFlow *flow, TransportLayer::State state); |
michael@0 | 143 | void SetSignals(); |
michael@0 | 144 | #endif |
michael@0 | 145 | |
michael@0 | 146 | typedef enum { |
michael@0 | 147 | RELIABLE=0, |
michael@0 | 148 | PARTIAL_RELIABLE_REXMIT = 1, |
michael@0 | 149 | PARTIAL_RELIABLE_TIMED = 2 |
michael@0 | 150 | } Type; |
michael@0 | 151 | |
michael@0 | 152 | already_AddRefed<DataChannel> Open(const nsACString& label, |
michael@0 | 153 | const nsACString& protocol, |
michael@0 | 154 | Type type, bool inOrder, |
michael@0 | 155 | uint32_t prValue, |
michael@0 | 156 | DataChannelListener *aListener, |
michael@0 | 157 | nsISupports *aContext, |
michael@0 | 158 | bool aExternalNegotiated, |
michael@0 | 159 | uint16_t aStream) NS_WARN_UNUSED_RESULT; |
michael@0 | 160 | |
michael@0 | 161 | void Close(DataChannel *aChannel); |
michael@0 | 162 | // CloseInt() must be called with mLock held |
michael@0 | 163 | void CloseInt(DataChannel *aChannel); |
michael@0 | 164 | void CloseAll(); |
michael@0 | 165 | |
michael@0 | 166 | int32_t SendMsg(uint16_t stream, const nsACString &aMsg) |
michael@0 | 167 | { |
michael@0 | 168 | return SendMsgCommon(stream, aMsg, false); |
michael@0 | 169 | } |
michael@0 | 170 | int32_t SendBinaryMsg(uint16_t stream, const nsACString &aMsg) |
michael@0 | 171 | { |
michael@0 | 172 | return SendMsgCommon(stream, aMsg, true); |
michael@0 | 173 | } |
michael@0 | 174 | int32_t SendBlob(uint16_t stream, nsIInputStream *aBlob); |
michael@0 | 175 | |
michael@0 | 176 | // Called on data reception from the SCTP library |
michael@0 | 177 | // must(?) be public so my c->c++ trampoline can call it |
michael@0 | 178 | int ReceiveCallback(struct socket* sock, void *data, size_t datalen, |
michael@0 | 179 | struct sctp_rcvinfo rcv, int32_t flags); |
michael@0 | 180 | |
michael@0 | 181 | // Find out state |
michael@0 | 182 | enum { |
michael@0 | 183 | CONNECTING = 0U, |
michael@0 | 184 | OPEN = 1U, |
michael@0 | 185 | CLOSING = 2U, |
michael@0 | 186 | CLOSED = 3U |
michael@0 | 187 | }; |
michael@0 | 188 | uint16_t GetReadyState() { MutexAutoLock lock(mLock); return mState; } |
michael@0 | 189 | |
michael@0 | 190 | friend class DataChannel; |
michael@0 | 191 | Mutex mLock; |
michael@0 | 192 | |
michael@0 | 193 | void ReadBlob(already_AddRefed<DataChannelConnection> aThis, uint16_t aStream, nsIInputStream* aBlob); |
michael@0 | 194 | |
michael@0 | 195 | void GetStreamIds(std::vector<uint16_t>* aStreamList); |
michael@0 | 196 | |
michael@0 | 197 | protected: |
michael@0 | 198 | friend class DataChannelOnMessageAvailable; |
michael@0 | 199 | // Avoid cycles with PeerConnectionImpl |
michael@0 | 200 | // Use from main thread only as WeakPtr is not threadsafe |
michael@0 | 201 | WeakPtr<DataConnectionListener> mListener; |
michael@0 | 202 | |
michael@0 | 203 | private: |
michael@0 | 204 | friend class DataChannelConnectRunnable; |
michael@0 | 205 | |
michael@0 | 206 | #ifdef SCTP_DTLS_SUPPORTED |
michael@0 | 207 | static void DTLSConnectThread(void *data); |
michael@0 | 208 | int SendPacket(const unsigned char* data, size_t len, bool release); |
michael@0 | 209 | void SctpDtlsInput(TransportFlow *flow, const unsigned char *data, size_t len); |
michael@0 | 210 | static int SctpDtlsOutput(void *addr, void *buffer, size_t length, uint8_t tos, uint8_t set_df); |
michael@0 | 211 | #endif |
michael@0 | 212 | DataChannel* FindChannelByStream(uint16_t stream); |
michael@0 | 213 | uint16_t FindFreeStream(); |
michael@0 | 214 | bool RequestMoreStreams(int32_t aNeeded = 16); |
michael@0 | 215 | int32_t SendControlMessage(void *msg, uint32_t len, uint16_t stream); |
michael@0 | 216 | int32_t SendOpenRequestMessage(const nsACString& label, const nsACString& protocol, |
michael@0 | 217 | uint16_t stream, |
michael@0 | 218 | bool unordered, uint16_t prPolicy, uint32_t prValue); |
michael@0 | 219 | int32_t SendOpenAckMessage(uint16_t stream); |
michael@0 | 220 | int32_t SendMsgInternal(DataChannel *channel, const char *data, |
michael@0 | 221 | uint32_t length, uint32_t ppid); |
michael@0 | 222 | int32_t SendBinary(DataChannel *channel, const char *data, |
michael@0 | 223 | uint32_t len, uint32_t ppid_partial, uint32_t ppid_final); |
michael@0 | 224 | int32_t SendMsgCommon(uint16_t stream, const nsACString &aMsg, bool isBinary); |
michael@0 | 225 | |
michael@0 | 226 | void DeliverQueuedData(uint16_t stream); |
michael@0 | 227 | |
michael@0 | 228 | already_AddRefed<DataChannel> OpenFinish(already_AddRefed<DataChannel>&& aChannel); |
michael@0 | 229 | |
michael@0 | 230 | void StartDefer(); |
michael@0 | 231 | bool SendDeferredMessages(); |
michael@0 | 232 | void ProcessQueuedOpens(); |
michael@0 | 233 | void ClearResets(); |
michael@0 | 234 | void SendOutgoingStreamReset(); |
michael@0 | 235 | void ResetOutgoingStream(uint16_t stream); |
michael@0 | 236 | void HandleOpenRequestMessage(const struct rtcweb_datachannel_open_request *req, |
michael@0 | 237 | size_t length, |
michael@0 | 238 | uint16_t stream); |
michael@0 | 239 | void HandleOpenAckMessage(const struct rtcweb_datachannel_ack *ack, |
michael@0 | 240 | size_t length, uint16_t stream); |
michael@0 | 241 | void HandleUnknownMessage(uint32_t ppid, size_t length, uint16_t stream); |
michael@0 | 242 | void HandleDataMessage(uint32_t ppid, const void *buffer, size_t length, uint16_t stream); |
michael@0 | 243 | void HandleMessage(const void *buffer, size_t length, uint32_t ppid, uint16_t stream); |
michael@0 | 244 | void HandleAssociationChangeEvent(const struct sctp_assoc_change *sac); |
michael@0 | 245 | void HandlePeerAddressChangeEvent(const struct sctp_paddr_change *spc); |
michael@0 | 246 | void HandleRemoteErrorEvent(const struct sctp_remote_error *sre); |
michael@0 | 247 | void HandleShutdownEvent(const struct sctp_shutdown_event *sse); |
michael@0 | 248 | void HandleAdaptationIndication(const struct sctp_adaptation_event *sai); |
michael@0 | 249 | void HandleSendFailedEvent(const struct sctp_send_failed_event *ssfe); |
michael@0 | 250 | void HandleStreamResetEvent(const struct sctp_stream_reset_event *strrst); |
michael@0 | 251 | void HandleStreamChangeEvent(const struct sctp_stream_change_event *strchg); |
michael@0 | 252 | void HandleNotification(const union sctp_notification *notif, size_t n); |
michael@0 | 253 | |
michael@0 | 254 | #ifdef SCTP_DTLS_SUPPORTED |
michael@0 | 255 | bool IsSTSThread() { |
michael@0 | 256 | bool on = false; |
michael@0 | 257 | if (mSTS) { |
michael@0 | 258 | mSTS->IsOnCurrentThread(&on); |
michael@0 | 259 | } |
michael@0 | 260 | return on; |
michael@0 | 261 | } |
michael@0 | 262 | #endif |
michael@0 | 263 | |
michael@0 | 264 | // Exists solely for proxying release of the TransportFlow to the STS thread |
michael@0 | 265 | static void ReleaseTransportFlow(nsRefPtr<TransportFlow> aFlow) {} |
michael@0 | 266 | |
michael@0 | 267 | // Data: |
michael@0 | 268 | // NOTE: while this array will auto-expand, increases in the number of |
michael@0 | 269 | // channels available from the stack must be negotiated! |
michael@0 | 270 | bool mAllocateEven; |
michael@0 | 271 | nsAutoTArray<nsRefPtr<DataChannel>,16> mStreams; |
michael@0 | 272 | nsDeque mPending; // Holds already_AddRefed<DataChannel>s -- careful! |
michael@0 | 273 | // holds data that's come in before a channel is open |
michael@0 | 274 | nsTArray<nsAutoPtr<QueuedDataMessage> > mQueuedData; |
michael@0 | 275 | |
michael@0 | 276 | // Streams pending reset |
michael@0 | 277 | nsAutoTArray<uint16_t,4> mStreamsResetting; |
michael@0 | 278 | |
michael@0 | 279 | struct socket *mMasterSocket; // accessed from STS thread |
michael@0 | 280 | struct socket *mSocket; // cloned from mMasterSocket on successful Connect on STS thread |
michael@0 | 281 | uint16_t mState; // Protected with mLock |
michael@0 | 282 | |
michael@0 | 283 | #ifdef SCTP_DTLS_SUPPORTED |
michael@0 | 284 | nsRefPtr<TransportFlow> mTransportFlow; |
michael@0 | 285 | nsCOMPtr<nsIEventTarget> mSTS; |
michael@0 | 286 | #endif |
michael@0 | 287 | uint16_t mLocalPort; // Accessed from connect thread |
michael@0 | 288 | uint16_t mRemotePort; |
michael@0 | 289 | bool mUsingDtls; |
michael@0 | 290 | |
michael@0 | 291 | // Timer to control when we try to resend blocked messages |
michael@0 | 292 | nsCOMPtr<nsITimer> mDeferredTimer; |
michael@0 | 293 | uint32_t mDeferTimeout; // in ms |
michael@0 | 294 | bool mTimerRunning; |
michael@0 | 295 | nsCOMPtr<nsIThread> mInternalIOThread; |
michael@0 | 296 | }; |
michael@0 | 297 | |
michael@0 | 298 | #define ENSURE_DATACONNECTION \ |
michael@0 | 299 | do { if (!mConnection) { DATACHANNEL_LOG(("%s: %p no connection!",__FUNCTION__, this)); return; } } while (0) |
michael@0 | 300 | |
michael@0 | 301 | #define ENSURE_DATACONNECTION_RET(x) \ |
michael@0 | 302 | do { if (!mConnection) { DATACHANNEL_LOG(("%s: %p no connection!",__FUNCTION__, this)); return (x); } } while (0) |
michael@0 | 303 | |
michael@0 | 304 | class DataChannel { |
michael@0 | 305 | public: |
michael@0 | 306 | enum { |
michael@0 | 307 | CONNECTING = 0U, |
michael@0 | 308 | OPEN = 1U, |
michael@0 | 309 | CLOSING = 2U, |
michael@0 | 310 | CLOSED = 3U, |
michael@0 | 311 | WAITING_TO_OPEN = 4U |
michael@0 | 312 | }; |
michael@0 | 313 | |
michael@0 | 314 | DataChannel(DataChannelConnection *connection, |
michael@0 | 315 | uint16_t stream, |
michael@0 | 316 | uint16_t state, |
michael@0 | 317 | const nsACString& label, |
michael@0 | 318 | const nsACString& protocol, |
michael@0 | 319 | uint16_t policy, uint32_t value, |
michael@0 | 320 | uint32_t flags, |
michael@0 | 321 | DataChannelListener *aListener, |
michael@0 | 322 | nsISupports *aContext) |
michael@0 | 323 | : mListenerLock("netwerk::sctp::DataChannel") |
michael@0 | 324 | , mListener(aListener) |
michael@0 | 325 | , mContext(aContext) |
michael@0 | 326 | , mConnection(connection) |
michael@0 | 327 | , mLabel(label) |
michael@0 | 328 | , mProtocol(protocol) |
michael@0 | 329 | , mState(state) |
michael@0 | 330 | , mReady(false) |
michael@0 | 331 | , mStream(stream) |
michael@0 | 332 | , mPrPolicy(policy) |
michael@0 | 333 | , mPrValue(value) |
michael@0 | 334 | , mFlags(flags) |
michael@0 | 335 | , mIsRecvBinary(false) |
michael@0 | 336 | { |
michael@0 | 337 | NS_ASSERTION(mConnection,"NULL connection"); |
michael@0 | 338 | } |
michael@0 | 339 | |
michael@0 | 340 | ~DataChannel(); |
michael@0 | 341 | void Destroy(); // when we disconnect from the connection after stream RESET |
michael@0 | 342 | |
michael@0 | 343 | NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DataChannel) |
michael@0 | 344 | |
michael@0 | 345 | // Close this DataChannel. Can be called multiple times. MUST be called |
michael@0 | 346 | // before destroying the DataChannel (state must be CLOSED or CLOSING). |
michael@0 | 347 | void Close(); |
michael@0 | 348 | |
michael@0 | 349 | // Set the listener (especially for channels created from the other side) |
michael@0 | 350 | void SetListener(DataChannelListener *aListener, nsISupports *aContext); |
michael@0 | 351 | |
michael@0 | 352 | // Send a string |
michael@0 | 353 | bool SendMsg(const nsACString &aMsg) |
michael@0 | 354 | { |
michael@0 | 355 | ENSURE_DATACONNECTION_RET(false); |
michael@0 | 356 | |
michael@0 | 357 | if (mStream != INVALID_STREAM) |
michael@0 | 358 | return (mConnection->SendMsg(mStream, aMsg) > 0); |
michael@0 | 359 | else |
michael@0 | 360 | return false; |
michael@0 | 361 | } |
michael@0 | 362 | |
michael@0 | 363 | // Send a binary message (TypedArray) |
michael@0 | 364 | bool SendBinaryMsg(const nsACString &aMsg) |
michael@0 | 365 | { |
michael@0 | 366 | ENSURE_DATACONNECTION_RET(false); |
michael@0 | 367 | |
michael@0 | 368 | if (mStream != INVALID_STREAM) |
michael@0 | 369 | return (mConnection->SendBinaryMsg(mStream, aMsg) > 0); |
michael@0 | 370 | else |
michael@0 | 371 | return false; |
michael@0 | 372 | } |
michael@0 | 373 | |
michael@0 | 374 | // Send a binary blob |
michael@0 | 375 | bool SendBinaryStream(nsIInputStream *aBlob, uint32_t msgLen) |
michael@0 | 376 | { |
michael@0 | 377 | ENSURE_DATACONNECTION_RET(false); |
michael@0 | 378 | |
michael@0 | 379 | if (mStream != INVALID_STREAM) |
michael@0 | 380 | return (mConnection->SendBlob(mStream, aBlob) > 0); |
michael@0 | 381 | else |
michael@0 | 382 | return false; |
michael@0 | 383 | } |
michael@0 | 384 | |
michael@0 | 385 | uint16_t GetType() { return mPrPolicy; } |
michael@0 | 386 | |
michael@0 | 387 | bool GetOrdered() { return !(mFlags & DATA_CHANNEL_FLAGS_OUT_OF_ORDER_ALLOWED); } |
michael@0 | 388 | |
michael@0 | 389 | // Amount of data buffered to send |
michael@0 | 390 | uint32_t GetBufferedAmount(); |
michael@0 | 391 | |
michael@0 | 392 | // Find out state |
michael@0 | 393 | uint16_t GetReadyState() |
michael@0 | 394 | { |
michael@0 | 395 | if (mConnection) { |
michael@0 | 396 | MutexAutoLock lock(mConnection->mLock); |
michael@0 | 397 | if (mState == WAITING_TO_OPEN) |
michael@0 | 398 | return CONNECTING; |
michael@0 | 399 | return mState; |
michael@0 | 400 | } |
michael@0 | 401 | return CLOSED; |
michael@0 | 402 | } |
michael@0 | 403 | |
michael@0 | 404 | void GetLabel(nsAString& aLabel) { CopyUTF8toUTF16(mLabel, aLabel); } |
michael@0 | 405 | void GetProtocol(nsAString& aProtocol) { CopyUTF8toUTF16(mProtocol, aProtocol); } |
michael@0 | 406 | uint16_t GetStream() { return mStream; } |
michael@0 | 407 | |
michael@0 | 408 | void AppReady(); |
michael@0 | 409 | |
michael@0 | 410 | void SendOrQueue(DataChannelOnMessageAvailable *aMessage); |
michael@0 | 411 | |
michael@0 | 412 | protected: |
michael@0 | 413 | Mutex mListenerLock; // protects mListener and mContext |
michael@0 | 414 | DataChannelListener *mListener; |
michael@0 | 415 | nsCOMPtr<nsISupports> mContext; |
michael@0 | 416 | |
michael@0 | 417 | private: |
michael@0 | 418 | friend class DataChannelOnMessageAvailable; |
michael@0 | 419 | friend class DataChannelConnection; |
michael@0 | 420 | |
michael@0 | 421 | nsresult AddDataToBinaryMsg(const char *data, uint32_t size); |
michael@0 | 422 | |
michael@0 | 423 | nsRefPtr<DataChannelConnection> mConnection; |
michael@0 | 424 | nsCString mLabel; |
michael@0 | 425 | nsCString mProtocol; |
michael@0 | 426 | uint16_t mState; |
michael@0 | 427 | bool mReady; |
michael@0 | 428 | uint16_t mStream; |
michael@0 | 429 | uint16_t mPrPolicy; |
michael@0 | 430 | uint32_t mPrValue; |
michael@0 | 431 | uint32_t mFlags; |
michael@0 | 432 | uint32_t mId; |
michael@0 | 433 | bool mIsRecvBinary; |
michael@0 | 434 | nsCString mRecvBuffer; |
michael@0 | 435 | nsTArray<nsAutoPtr<BufferedMsg> > mBufferedData; |
michael@0 | 436 | nsTArray<nsCOMPtr<nsIRunnable> > mQueuedMessages; |
michael@0 | 437 | }; |
michael@0 | 438 | |
michael@0 | 439 | // used to dispatch notifications of incoming data to the main thread |
michael@0 | 440 | // Patterned on CallOnMessageAvailable in WebSockets |
michael@0 | 441 | // Also used to proxy other items to MainThread |
michael@0 | 442 | class DataChannelOnMessageAvailable : public nsRunnable |
michael@0 | 443 | { |
michael@0 | 444 | public: |
michael@0 | 445 | enum { |
michael@0 | 446 | ON_CONNECTION, |
michael@0 | 447 | ON_DISCONNECTED, |
michael@0 | 448 | ON_CHANNEL_CREATED, |
michael@0 | 449 | ON_CHANNEL_OPEN, |
michael@0 | 450 | ON_CHANNEL_CLOSED, |
michael@0 | 451 | ON_DATA, |
michael@0 | 452 | START_DEFER, |
michael@0 | 453 | }; /* types */ |
michael@0 | 454 | |
michael@0 | 455 | DataChannelOnMessageAvailable(int32_t aType, |
michael@0 | 456 | DataChannelConnection *aConnection, |
michael@0 | 457 | DataChannel *aChannel, |
michael@0 | 458 | nsCString &aData, // XXX this causes inefficiency |
michael@0 | 459 | int32_t aLen) |
michael@0 | 460 | : mType(aType), |
michael@0 | 461 | mChannel(aChannel), |
michael@0 | 462 | mConnection(aConnection), |
michael@0 | 463 | mData(aData), |
michael@0 | 464 | mLen(aLen) {} |
michael@0 | 465 | |
michael@0 | 466 | DataChannelOnMessageAvailable(int32_t aType, |
michael@0 | 467 | DataChannel *aChannel) |
michael@0 | 468 | : mType(aType), |
michael@0 | 469 | mChannel(aChannel) {} |
michael@0 | 470 | // XXX is it safe to leave mData/mLen uninitialized? This should only be |
michael@0 | 471 | // used for notifications that don't use them, but I'd like more |
michael@0 | 472 | // bulletproof compile-time checking. |
michael@0 | 473 | |
michael@0 | 474 | DataChannelOnMessageAvailable(int32_t aType, |
michael@0 | 475 | DataChannelConnection *aConnection, |
michael@0 | 476 | DataChannel *aChannel) |
michael@0 | 477 | : mType(aType), |
michael@0 | 478 | mChannel(aChannel), |
michael@0 | 479 | mConnection(aConnection) {} |
michael@0 | 480 | |
michael@0 | 481 | // for ON_CONNECTION/ON_DISCONNECTED |
michael@0 | 482 | DataChannelOnMessageAvailable(int32_t aType, |
michael@0 | 483 | DataChannelConnection *aConnection, |
michael@0 | 484 | bool aResult = true) |
michael@0 | 485 | : mType(aType), |
michael@0 | 486 | mConnection(aConnection), |
michael@0 | 487 | mResult(aResult) {} |
michael@0 | 488 | |
michael@0 | 489 | NS_IMETHOD Run() |
michael@0 | 490 | { |
michael@0 | 491 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 492 | switch (mType) { |
michael@0 | 493 | case ON_DATA: |
michael@0 | 494 | case ON_CHANNEL_OPEN: |
michael@0 | 495 | case ON_CHANNEL_CLOSED: |
michael@0 | 496 | { |
michael@0 | 497 | MutexAutoLock lock(mChannel->mListenerLock); |
michael@0 | 498 | if (!mChannel->mListener) { |
michael@0 | 499 | DATACHANNEL_LOG(("DataChannelOnMessageAvailable (%d) with null Listener!",mType)); |
michael@0 | 500 | return NS_OK; |
michael@0 | 501 | } |
michael@0 | 502 | |
michael@0 | 503 | switch (mType) { |
michael@0 | 504 | case ON_DATA: |
michael@0 | 505 | if (mLen < 0) { |
michael@0 | 506 | mChannel->mListener->OnMessageAvailable(mChannel->mContext, mData); |
michael@0 | 507 | } else { |
michael@0 | 508 | mChannel->mListener->OnBinaryMessageAvailable(mChannel->mContext, mData); |
michael@0 | 509 | } |
michael@0 | 510 | break; |
michael@0 | 511 | case ON_CHANNEL_OPEN: |
michael@0 | 512 | mChannel->mListener->OnChannelConnected(mChannel->mContext); |
michael@0 | 513 | break; |
michael@0 | 514 | case ON_CHANNEL_CLOSED: |
michael@0 | 515 | mChannel->mListener->OnChannelClosed(mChannel->mContext); |
michael@0 | 516 | break; |
michael@0 | 517 | } |
michael@0 | 518 | break; |
michael@0 | 519 | } |
michael@0 | 520 | case ON_DISCONNECTED: |
michael@0 | 521 | // If we've disconnected, make sure we close all the streams - from mainthread! |
michael@0 | 522 | mConnection->CloseAll(); |
michael@0 | 523 | // fall through |
michael@0 | 524 | case ON_CHANNEL_CREATED: |
michael@0 | 525 | case ON_CONNECTION: |
michael@0 | 526 | // WeakPtr - only used/modified/nulled from MainThread so we can use a WeakPtr here |
michael@0 | 527 | if (!mConnection->mListener) { |
michael@0 | 528 | DATACHANNEL_LOG(("DataChannelOnMessageAvailable (%d) with null Listener",mType)); |
michael@0 | 529 | return NS_OK; |
michael@0 | 530 | } |
michael@0 | 531 | switch (mType) { |
michael@0 | 532 | case ON_CHANNEL_CREATED: |
michael@0 | 533 | // important to give it an already_AddRefed pointer! |
michael@0 | 534 | mConnection->mListener->NotifyDataChannel(mChannel.forget()); |
michael@0 | 535 | break; |
michael@0 | 536 | case ON_CONNECTION: |
michael@0 | 537 | if (mResult) { |
michael@0 | 538 | mConnection->mListener->NotifyConnection(); |
michael@0 | 539 | } |
michael@0 | 540 | // FIX - on mResult false (failure) we should do something. Needs spec work here |
michael@0 | 541 | break; |
michael@0 | 542 | case ON_DISCONNECTED: |
michael@0 | 543 | mConnection->mListener->NotifyClosedConnection(); |
michael@0 | 544 | break; |
michael@0 | 545 | } |
michael@0 | 546 | break; |
michael@0 | 547 | case START_DEFER: |
michael@0 | 548 | mConnection->StartDefer(); |
michael@0 | 549 | break; |
michael@0 | 550 | } |
michael@0 | 551 | return NS_OK; |
michael@0 | 552 | } |
michael@0 | 553 | |
michael@0 | 554 | private: |
michael@0 | 555 | ~DataChannelOnMessageAvailable() {} |
michael@0 | 556 | |
michael@0 | 557 | int32_t mType; |
michael@0 | 558 | // XXX should use union |
michael@0 | 559 | nsRefPtr<DataChannel> mChannel; |
michael@0 | 560 | nsRefPtr<DataChannelConnection> mConnection; |
michael@0 | 561 | nsCString mData; |
michael@0 | 562 | int32_t mLen; |
michael@0 | 563 | bool mResult; |
michael@0 | 564 | }; |
michael@0 | 565 | |
michael@0 | 566 | } |
michael@0 | 567 | |
michael@0 | 568 | #endif // NETWERK_SCTP_DATACHANNEL_DATACHANNEL_H_ |