2012-10-03 23:51:23 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef NETWERK_SCTP_DATACHANNEL_DATACHANNEL_H_
|
|
|
|
#define NETWERK_SCTP_DATACHANNEL_DATACHANNEL_H_
|
|
|
|
|
2012-10-02 20:04:58 +00:00
|
|
|
#ifdef MOZ_WEBRTC_SIGNALING
|
|
|
|
#define SCTP_DTLS_SUPPORTED 1
|
|
|
|
#endif
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
#include <string>
|
2012-10-05 20:54:51 +00:00
|
|
|
#include <errno.h>
|
2012-10-03 23:51:23 +00:00
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2013-02-12 21:32:43 +00:00
|
|
|
#include "mozilla/WeakPtr.h"
|
2012-10-03 23:51:23 +00:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsDeque.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "mozilla/Mutex.h"
|
|
|
|
#include "DataChannelProtocol.h"
|
2013-10-10 21:04:28 +00:00
|
|
|
#include "DataChannelListener.h"
|
2012-10-03 23:51:23 +00:00
|
|
|
#ifdef SCTP_DTLS_SUPPORTED
|
2012-10-02 20:04:58 +00:00
|
|
|
#include "mtransport/sigslot.h"
|
2012-10-03 23:51:23 +00:00
|
|
|
#include "mtransport/transportflow.h"
|
|
|
|
#include "mtransport/transportlayer.h"
|
2013-04-01 01:09:26 +00:00
|
|
|
#include "mtransport/transportlayerdtls.h"
|
2012-10-03 23:51:23 +00:00
|
|
|
#include "mtransport/transportlayerprsock.h"
|
|
|
|
#endif
|
|
|
|
|
2013-01-07 03:01:23 +00:00
|
|
|
#ifndef DATACHANNEL_LOG
|
|
|
|
#define DATACHANNEL_LOG(args)
|
|
|
|
#endif
|
|
|
|
|
2012-10-05 20:54:51 +00:00
|
|
|
#ifndef EALREADY
|
|
|
|
#define EALREADY WSAEALREADY
|
|
|
|
#endif
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
extern "C" {
|
|
|
|
struct socket;
|
|
|
|
struct sctp_rcvinfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class DTLSConnection;
|
|
|
|
class DataChannelConnection;
|
|
|
|
class DataChannel;
|
|
|
|
class DataChannelOnMessageAvailable;
|
|
|
|
|
2013-04-01 01:09:25 +00:00
|
|
|
// For queuing outgoing messages
|
2012-10-03 23:51:23 +00:00
|
|
|
class BufferedMsg
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BufferedMsg(struct sctp_sendv_spa &spa,const char *data,
|
|
|
|
uint32_t length);
|
|
|
|
~BufferedMsg();
|
|
|
|
|
|
|
|
struct sctp_sendv_spa *mSpa;
|
|
|
|
const char *mData;
|
|
|
|
uint32_t mLength;
|
|
|
|
};
|
|
|
|
|
2013-04-01 01:09:25 +00:00
|
|
|
// for queuing incoming data messages before the Open or
|
|
|
|
// external negotiation is indicated to us
|
|
|
|
class QueuedDataMessage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
QueuedDataMessage(uint16_t stream, uint32_t ppid,
|
|
|
|
const void *data, size_t length)
|
|
|
|
: mStream(stream)
|
|
|
|
, mPpid(ppid)
|
|
|
|
, mLength(length)
|
|
|
|
{
|
|
|
|
mData = static_cast<char *>(moz_xmalloc(length)); // infallible
|
|
|
|
memcpy(mData, data, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
~QueuedDataMessage()
|
|
|
|
{
|
|
|
|
moz_free(mData);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t mStream;
|
|
|
|
uint32_t mPpid;
|
|
|
|
size_t mLength;
|
|
|
|
char *mData;
|
|
|
|
};
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
// One per PeerConnection
|
|
|
|
class DataChannelConnection: public nsITimerCallback
|
|
|
|
#ifdef SCTP_DTLS_SUPPORTED
|
|
|
|
, public sigslot::has_slots<>
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
public:
|
2013-07-19 02:24:13 +00:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2012-10-03 23:51:23 +00:00
|
|
|
NS_DECL_NSITIMERCALLBACK
|
|
|
|
|
2013-02-12 21:32:43 +00:00
|
|
|
class DataConnectionListener : public SupportsWeakPtr<DataConnectionListener>
|
|
|
|
{
|
2012-10-03 23:51:23 +00:00
|
|
|
public:
|
2014-02-21 02:33:49 +00:00
|
|
|
MOZ_DECLARE_REFCOUNTED_TYPENAME(DataChannelConnection::DataConnectionListener)
|
2012-10-03 23:51:23 +00:00
|
|
|
virtual ~DataConnectionListener() {}
|
|
|
|
|
|
|
|
// Called when a the connection is open
|
|
|
|
virtual void NotifyConnection() = 0;
|
|
|
|
|
|
|
|
// Called when a the connection is lost/closed
|
|
|
|
virtual void NotifyClosedConnection() = 0;
|
|
|
|
|
|
|
|
// Called when a new DataChannel has been opened by the other side.
|
2013-01-07 03:01:23 +00:00
|
|
|
virtual void NotifyDataChannel(already_AddRefed<DataChannel> channel) = 0;
|
2012-10-03 23:51:23 +00:00
|
|
|
};
|
|
|
|
|
2013-04-01 01:09:26 +00:00
|
|
|
DataChannelConnection(DataConnectionListener *listener);
|
2012-10-03 23:51:23 +00:00
|
|
|
virtual ~DataChannelConnection();
|
|
|
|
|
|
|
|
bool Init(unsigned short aPort, uint16_t aNumStreams, bool aUsingDtls);
|
2012-12-14 04:30:11 +00:00
|
|
|
void Destroy(); // So we can spawn refs tied to runnables in shutdown
|
2014-03-15 19:15:17 +00:00
|
|
|
// Finish Destroy on STS to avoid SCTP race condition with ABORT from far end
|
|
|
|
void DestroyOnSTS(struct socket *aMasterSocket,
|
|
|
|
struct socket *aSocket);
|
2012-10-03 23:51:23 +00:00
|
|
|
|
2013-04-01 01:09:26 +00:00
|
|
|
#ifdef ALLOW_DIRECT_SCTP_LISTEN_CONNECT
|
2012-10-03 23:51:23 +00:00
|
|
|
// These block; they require something to decide on listener/connector
|
|
|
|
// (though you can do simultaneous Connect()). Do not call these from
|
|
|
|
// the main thread!
|
|
|
|
bool Listen(unsigned short port);
|
|
|
|
bool Connect(const char *addr, unsigned short port);
|
2013-04-01 01:09:26 +00:00
|
|
|
#endif
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
#ifdef SCTP_DTLS_SUPPORTED
|
|
|
|
// Connect using a TransportFlow (DTLS) channel
|
2013-04-01 01:09:26 +00:00
|
|
|
void SetEvenOdd();
|
|
|
|
bool ConnectViaTransportFlow(TransportFlow *aFlow, uint16_t localport, uint16_t remoteport);
|
|
|
|
void CompleteConnect(TransportFlow *flow, TransportLayer::State state);
|
|
|
|
void SetSignals();
|
2012-10-03 23:51:23 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
RELIABLE=0,
|
|
|
|
PARTIAL_RELIABLE_REXMIT = 1,
|
|
|
|
PARTIAL_RELIABLE_TIMED = 2
|
|
|
|
} Type;
|
2013-01-07 03:01:23 +00:00
|
|
|
|
2012-11-02 19:28:13 +00:00
|
|
|
already_AddRefed<DataChannel> Open(const nsACString& label,
|
2013-04-01 01:09:26 +00:00
|
|
|
const nsACString& protocol,
|
2012-11-02 19:28:13 +00:00
|
|
|
Type type, bool inOrder,
|
|
|
|
uint32_t prValue,
|
|
|
|
DataChannelListener *aListener,
|
2013-04-01 01:09:26 +00:00
|
|
|
nsISupports *aContext,
|
|
|
|
bool aExternalNegotiated,
|
2013-04-17 03:58:09 +00:00
|
|
|
uint16_t aStream) NS_WARN_UNUSED_RESULT;
|
2012-10-03 23:51:23 +00:00
|
|
|
|
2013-01-17 10:26:40 +00:00
|
|
|
void Close(DataChannel *aChannel);
|
2013-03-27 23:46:48 +00:00
|
|
|
// CloseInt() must be called with mLock held
|
|
|
|
void CloseInt(DataChannel *aChannel);
|
2012-10-03 23:51:23 +00:00
|
|
|
void CloseAll();
|
|
|
|
|
|
|
|
int32_t SendMsg(uint16_t stream, const nsACString &aMsg)
|
|
|
|
{
|
|
|
|
return SendMsgCommon(stream, aMsg, false);
|
|
|
|
}
|
|
|
|
int32_t SendBinaryMsg(uint16_t stream, const nsACString &aMsg)
|
|
|
|
{
|
|
|
|
return SendMsgCommon(stream, aMsg, true);
|
|
|
|
}
|
|
|
|
int32_t SendBlob(uint16_t stream, nsIInputStream *aBlob);
|
|
|
|
|
|
|
|
// Called on data reception from the SCTP library
|
2013-01-07 03:01:23 +00:00
|
|
|
// must(?) be public so my c->c++ trampoline can call it
|
|
|
|
int ReceiveCallback(struct socket* sock, void *data, size_t datalen,
|
2012-10-03 23:51:23 +00:00
|
|
|
struct sctp_rcvinfo rcv, int32_t flags);
|
|
|
|
|
|
|
|
// Find out state
|
|
|
|
enum {
|
|
|
|
CONNECTING = 0U,
|
|
|
|
OPEN = 1U,
|
|
|
|
CLOSING = 2U,
|
|
|
|
CLOSED = 3U
|
|
|
|
};
|
2013-04-01 01:09:26 +00:00
|
|
|
uint16_t GetReadyState() { MutexAutoLock lock(mLock); return mState; }
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
friend class DataChannel;
|
|
|
|
Mutex mLock;
|
|
|
|
|
2014-02-01 13:09:00 +00:00
|
|
|
void ReadBlob(already_AddRefed<DataChannelConnection> aThis, uint16_t aStream, nsIInputStream* aBlob);
|
2014-01-23 07:41:29 +00:00
|
|
|
|
2014-03-10 15:41:35 +00:00
|
|
|
void GetStreamIds(std::vector<uint16_t>* aStreamList);
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
protected:
|
|
|
|
friend class DataChannelOnMessageAvailable;
|
2013-02-12 21:32:43 +00:00
|
|
|
// Avoid cycles with PeerConnectionImpl
|
|
|
|
// Use from main thread only as WeakPtr is not threadsafe
|
|
|
|
WeakPtr<DataConnectionListener> mListener;
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
private:
|
2012-12-14 04:30:11 +00:00
|
|
|
friend class DataChannelConnectRunnable;
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
#ifdef SCTP_DTLS_SUPPORTED
|
|
|
|
static void DTLSConnectThread(void *data);
|
2012-11-02 19:28:11 +00:00
|
|
|
int SendPacket(const unsigned char* data, size_t len, bool release);
|
|
|
|
void SctpDtlsInput(TransportFlow *flow, const unsigned char *data, size_t len);
|
2012-10-03 23:51:23 +00:00
|
|
|
static int SctpDtlsOutput(void *addr, void *buffer, size_t length, uint8_t tos, uint8_t set_df);
|
|
|
|
#endif
|
2013-04-01 01:09:25 +00:00
|
|
|
DataChannel* FindChannelByStream(uint16_t stream);
|
|
|
|
uint16_t FindFreeStream();
|
|
|
|
bool RequestMoreStreams(int32_t aNeeded = 16);
|
2013-10-25 20:08:18 +00:00
|
|
|
int32_t SendControlMessage(void *msg, uint32_t len, uint16_t stream);
|
2013-04-01 01:09:26 +00:00
|
|
|
int32_t SendOpenRequestMessage(const nsACString& label, const nsACString& protocol,
|
2013-10-25 20:08:18 +00:00
|
|
|
uint16_t stream,
|
2012-10-03 23:51:23 +00:00
|
|
|
bool unordered, uint16_t prPolicy, uint32_t prValue);
|
2013-10-25 20:08:18 +00:00
|
|
|
int32_t SendOpenAckMessage(uint16_t stream);
|
2012-10-03 23:51:23 +00:00
|
|
|
int32_t SendMsgInternal(DataChannel *channel, const char *data,
|
|
|
|
uint32_t length, uint32_t ppid);
|
|
|
|
int32_t SendBinary(DataChannel *channel, const char *data,
|
2013-04-12 14:08:49 +00:00
|
|
|
uint32_t len, uint32_t ppid_partial, uint32_t ppid_final);
|
2012-10-03 23:51:23 +00:00
|
|
|
int32_t SendMsgCommon(uint16_t stream, const nsACString &aMsg, bool isBinary);
|
|
|
|
|
2013-04-01 01:09:25 +00:00
|
|
|
void DeliverQueuedData(uint16_t stream);
|
|
|
|
|
2014-03-15 19:00:17 +00:00
|
|
|
already_AddRefed<DataChannel> OpenFinish(already_AddRefed<DataChannel>&& aChannel);
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
void StartDefer();
|
|
|
|
bool SendDeferredMessages();
|
2013-04-01 01:09:26 +00:00
|
|
|
void ProcessQueuedOpens();
|
2013-06-02 13:48:17 +00:00
|
|
|
void ClearResets();
|
2012-10-03 23:51:23 +00:00
|
|
|
void SendOutgoingStreamReset();
|
2013-10-25 20:08:18 +00:00
|
|
|
void ResetOutgoingStream(uint16_t stream);
|
2012-10-03 23:51:23 +00:00
|
|
|
void HandleOpenRequestMessage(const struct rtcweb_datachannel_open_request *req,
|
|
|
|
size_t length,
|
2013-10-25 20:08:18 +00:00
|
|
|
uint16_t stream);
|
|
|
|
void HandleOpenAckMessage(const struct rtcweb_datachannel_ack *ack,
|
|
|
|
size_t length, uint16_t stream);
|
|
|
|
void HandleUnknownMessage(uint32_t ppid, size_t length, uint16_t stream);
|
|
|
|
void HandleDataMessage(uint32_t ppid, const void *buffer, size_t length, uint16_t stream);
|
|
|
|
void HandleMessage(const void *buffer, size_t length, uint32_t ppid, uint16_t stream);
|
2012-10-03 23:51:23 +00:00
|
|
|
void HandleAssociationChangeEvent(const struct sctp_assoc_change *sac);
|
|
|
|
void HandlePeerAddressChangeEvent(const struct sctp_paddr_change *spc);
|
|
|
|
void HandleRemoteErrorEvent(const struct sctp_remote_error *sre);
|
|
|
|
void HandleShutdownEvent(const struct sctp_shutdown_event *sse);
|
|
|
|
void HandleAdaptationIndication(const struct sctp_adaptation_event *sai);
|
|
|
|
void HandleSendFailedEvent(const struct sctp_send_failed_event *ssfe);
|
|
|
|
void HandleStreamResetEvent(const struct sctp_stream_reset_event *strrst);
|
|
|
|
void HandleStreamChangeEvent(const struct sctp_stream_change_event *strchg);
|
|
|
|
void HandleNotification(const union sctp_notification *notif, size_t n);
|
|
|
|
|
2012-10-29 03:48:36 +00:00
|
|
|
#ifdef SCTP_DTLS_SUPPORTED
|
|
|
|
bool IsSTSThread() {
|
|
|
|
bool on = false;
|
|
|
|
if (mSTS) {
|
|
|
|
mSTS->IsOnCurrentThread(&on);
|
|
|
|
}
|
|
|
|
return on;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-01-12 10:40:22 +00:00
|
|
|
// Exists solely for proxying release of the TransportFlow to the STS thread
|
|
|
|
static void ReleaseTransportFlow(nsRefPtr<TransportFlow> aFlow) {}
|
|
|
|
|
|
|
|
// Data:
|
2013-04-01 01:09:25 +00:00
|
|
|
// NOTE: while this array will auto-expand, increases in the number of
|
2012-10-03 23:51:23 +00:00
|
|
|
// channels available from the stack must be negotiated!
|
2013-04-01 01:09:25 +00:00
|
|
|
bool mAllocateEven;
|
|
|
|
nsAutoTArray<nsRefPtr<DataChannel>,16> mStreams;
|
2012-11-02 19:28:13 +00:00
|
|
|
nsDeque mPending; // Holds already_AddRefed<DataChannel>s -- careful!
|
2013-04-01 01:09:25 +00:00
|
|
|
// holds data that's come in before a channel is open
|
|
|
|
nsTArray<nsAutoPtr<QueuedDataMessage> > mQueuedData;
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
// Streams pending reset
|
|
|
|
nsAutoTArray<uint16_t,4> mStreamsResetting;
|
|
|
|
|
2013-04-01 01:09:26 +00:00
|
|
|
struct socket *mMasterSocket; // accessed from STS thread
|
|
|
|
struct socket *mSocket; // cloned from mMasterSocket on successful Connect on STS thread
|
|
|
|
uint16_t mState; // Protected with mLock
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
#ifdef SCTP_DTLS_SUPPORTED
|
|
|
|
nsRefPtr<TransportFlow> mTransportFlow;
|
2012-10-29 03:48:36 +00:00
|
|
|
nsCOMPtr<nsIEventTarget> mSTS;
|
2012-10-03 23:51:23 +00:00
|
|
|
#endif
|
2012-12-14 04:30:11 +00:00
|
|
|
uint16_t mLocalPort; // Accessed from connect thread
|
2012-10-03 23:51:23 +00:00
|
|
|
uint16_t mRemotePort;
|
2013-04-01 01:09:24 +00:00
|
|
|
bool mUsingDtls;
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
// Timer to control when we try to resend blocked messages
|
|
|
|
nsCOMPtr<nsITimer> mDeferredTimer;
|
|
|
|
uint32_t mDeferTimeout; // in ms
|
|
|
|
bool mTimerRunning;
|
2014-01-23 07:41:29 +00:00
|
|
|
nsCOMPtr<nsIThread> mInternalIOThread;
|
2012-10-03 23:51:23 +00:00
|
|
|
};
|
|
|
|
|
2013-01-07 03:01:23 +00:00
|
|
|
#define ENSURE_DATACONNECTION \
|
|
|
|
do { if (!mConnection) { DATACHANNEL_LOG(("%s: %p no connection!",__FUNCTION__, this)); return; } } while (0)
|
|
|
|
|
|
|
|
#define ENSURE_DATACONNECTION_RET(x) \
|
|
|
|
do { if (!mConnection) { DATACHANNEL_LOG(("%s: %p no connection!",__FUNCTION__, this)); return (x); } } while (0)
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
class DataChannel {
|
|
|
|
public:
|
|
|
|
enum {
|
|
|
|
CONNECTING = 0U,
|
|
|
|
OPEN = 1U,
|
|
|
|
CLOSING = 2U,
|
|
|
|
CLOSED = 3U,
|
|
|
|
WAITING_TO_OPEN = 4U
|
|
|
|
};
|
|
|
|
|
|
|
|
DataChannel(DataChannelConnection *connection,
|
2013-04-01 01:09:25 +00:00
|
|
|
uint16_t stream,
|
2012-10-03 23:51:23 +00:00
|
|
|
uint16_t state,
|
|
|
|
const nsACString& label,
|
2013-04-01 01:09:26 +00:00
|
|
|
const nsACString& protocol,
|
2012-10-03 23:51:23 +00:00
|
|
|
uint16_t policy, uint32_t value,
|
|
|
|
uint32_t flags,
|
|
|
|
DataChannelListener *aListener,
|
|
|
|
nsISupports *aContext)
|
2013-02-12 21:32:43 +00:00
|
|
|
: mListenerLock("netwerk::sctp::DataChannel")
|
|
|
|
, mListener(aListener)
|
|
|
|
, mContext(aContext)
|
2012-10-03 23:51:23 +00:00
|
|
|
, mConnection(connection)
|
|
|
|
, mLabel(label)
|
2013-04-01 01:09:26 +00:00
|
|
|
, mProtocol(protocol)
|
2012-10-03 23:51:23 +00:00
|
|
|
, mState(state)
|
|
|
|
, mReady(false)
|
2013-04-01 01:09:25 +00:00
|
|
|
, mStream(stream)
|
2012-10-03 23:51:23 +00:00
|
|
|
, mPrPolicy(policy)
|
|
|
|
, mPrValue(value)
|
2013-08-12 20:27:14 +00:00
|
|
|
, mFlags(flags)
|
2013-04-12 14:08:49 +00:00
|
|
|
, mIsRecvBinary(false)
|
2012-10-03 23:51:23 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mConnection,"NULL connection");
|
|
|
|
}
|
|
|
|
|
2012-11-02 19:28:13 +00:00
|
|
|
~DataChannel();
|
2012-12-14 04:30:11 +00:00
|
|
|
void Destroy(); // when we disconnect from the connection after stream RESET
|
2012-11-02 19:28:13 +00:00
|
|
|
|
2012-11-16 01:50:27 +00:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DataChannel)
|
2012-10-03 23:51:23 +00:00
|
|
|
|
2013-03-27 23:46:48 +00:00
|
|
|
// Close this DataChannel. Can be called multiple times. MUST be called
|
|
|
|
// before destroying the DataChannel (state must be CLOSED or CLOSING).
|
2012-10-03 23:51:23 +00:00
|
|
|
void Close();
|
|
|
|
|
|
|
|
// Set the listener (especially for channels created from the other side)
|
|
|
|
void SetListener(DataChannelListener *aListener, nsISupports *aContext);
|
|
|
|
|
|
|
|
// Send a string
|
|
|
|
bool SendMsg(const nsACString &aMsg)
|
|
|
|
{
|
2013-01-07 03:01:23 +00:00
|
|
|
ENSURE_DATACONNECTION_RET(false);
|
|
|
|
|
2013-04-01 01:09:25 +00:00
|
|
|
if (mStream != INVALID_STREAM)
|
|
|
|
return (mConnection->SendMsg(mStream, aMsg) > 0);
|
2012-10-03 23:51:23 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a binary message (TypedArray)
|
|
|
|
bool SendBinaryMsg(const nsACString &aMsg)
|
|
|
|
{
|
2013-01-07 03:01:23 +00:00
|
|
|
ENSURE_DATACONNECTION_RET(false);
|
|
|
|
|
2013-04-01 01:09:25 +00:00
|
|
|
if (mStream != INVALID_STREAM)
|
|
|
|
return (mConnection->SendBinaryMsg(mStream, aMsg) > 0);
|
2012-10-03 23:51:23 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a binary blob
|
|
|
|
bool SendBinaryStream(nsIInputStream *aBlob, uint32_t msgLen)
|
|
|
|
{
|
2013-01-07 03:01:23 +00:00
|
|
|
ENSURE_DATACONNECTION_RET(false);
|
|
|
|
|
2013-04-01 01:09:25 +00:00
|
|
|
if (mStream != INVALID_STREAM)
|
|
|
|
return (mConnection->SendBlob(mStream, aBlob) > 0);
|
2012-10-03 23:51:23 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t GetType() { return mPrPolicy; }
|
|
|
|
|
2013-04-01 01:09:26 +00:00
|
|
|
bool GetOrdered() { return !(mFlags & DATA_CHANNEL_FLAGS_OUT_OF_ORDER_ALLOWED); }
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
// Amount of data buffered to send
|
|
|
|
uint32_t GetBufferedAmount();
|
|
|
|
|
|
|
|
// Find out state
|
|
|
|
uint16_t GetReadyState()
|
|
|
|
{
|
2013-06-28 21:56:45 +00:00
|
|
|
if (mConnection) {
|
|
|
|
MutexAutoLock lock(mConnection->mLock);
|
|
|
|
if (mState == WAITING_TO_OPEN)
|
|
|
|
return CONNECTING;
|
|
|
|
return mState;
|
|
|
|
}
|
|
|
|
return CLOSED;
|
2012-10-03 23:51:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GetLabel(nsAString& aLabel) { CopyUTF8toUTF16(mLabel, aLabel); }
|
2013-04-01 01:09:26 +00:00
|
|
|
void GetProtocol(nsAString& aProtocol) { CopyUTF8toUTF16(mProtocol, aProtocol); }
|
2013-05-20 07:19:45 +00:00
|
|
|
uint16_t GetStream() { return mStream; }
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
void AppReady();
|
|
|
|
|
2012-11-02 19:28:13 +00:00
|
|
|
void SendOrQueue(DataChannelOnMessageAvailable *aMessage);
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
protected:
|
2013-02-12 21:32:43 +00:00
|
|
|
Mutex mListenerLock; // protects mListener and mContext
|
2012-10-03 23:51:23 +00:00
|
|
|
DataChannelListener *mListener;
|
2013-02-12 21:32:43 +00:00
|
|
|
nsCOMPtr<nsISupports> mContext;
|
2012-10-03 23:51:23 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class DataChannelOnMessageAvailable;
|
|
|
|
friend class DataChannelConnection;
|
|
|
|
|
|
|
|
nsresult AddDataToBinaryMsg(const char *data, uint32_t size);
|
|
|
|
|
|
|
|
nsRefPtr<DataChannelConnection> mConnection;
|
|
|
|
nsCString mLabel;
|
2013-04-01 01:09:26 +00:00
|
|
|
nsCString mProtocol;
|
2012-10-03 23:51:23 +00:00
|
|
|
uint16_t mState;
|
|
|
|
bool mReady;
|
2013-04-01 01:09:25 +00:00
|
|
|
uint16_t mStream;
|
2012-10-03 23:51:23 +00:00
|
|
|
uint16_t mPrPolicy;
|
|
|
|
uint32_t mPrValue;
|
|
|
|
uint32_t mFlags;
|
|
|
|
uint32_t mId;
|
2013-04-12 14:08:49 +00:00
|
|
|
bool mIsRecvBinary;
|
|
|
|
nsCString mRecvBuffer;
|
2012-10-03 23:51:23 +00:00
|
|
|
nsTArray<nsAutoPtr<BufferedMsg> > mBufferedData;
|
|
|
|
nsTArray<nsCOMPtr<nsIRunnable> > mQueuedMessages;
|
|
|
|
};
|
|
|
|
|
|
|
|
// used to dispatch notifications of incoming data to the main thread
|
|
|
|
// Patterned on CallOnMessageAvailable in WebSockets
|
|
|
|
// Also used to proxy other items to MainThread
|
|
|
|
class DataChannelOnMessageAvailable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum {
|
|
|
|
ON_CONNECTION,
|
|
|
|
ON_DISCONNECTED,
|
|
|
|
ON_CHANNEL_CREATED,
|
|
|
|
ON_CHANNEL_OPEN,
|
|
|
|
ON_CHANNEL_CLOSED,
|
|
|
|
ON_DATA,
|
|
|
|
START_DEFER,
|
|
|
|
}; /* types */
|
|
|
|
|
|
|
|
DataChannelOnMessageAvailable(int32_t aType,
|
|
|
|
DataChannelConnection *aConnection,
|
|
|
|
DataChannel *aChannel,
|
|
|
|
nsCString &aData, // XXX this causes inefficiency
|
|
|
|
int32_t aLen)
|
|
|
|
: mType(aType),
|
|
|
|
mChannel(aChannel),
|
2013-01-07 03:01:23 +00:00
|
|
|
mConnection(aConnection),
|
2012-10-03 23:51:23 +00:00
|
|
|
mData(aData),
|
|
|
|
mLen(aLen) {}
|
|
|
|
|
|
|
|
DataChannelOnMessageAvailable(int32_t aType,
|
|
|
|
DataChannel *aChannel)
|
|
|
|
: mType(aType),
|
|
|
|
mChannel(aChannel) {}
|
|
|
|
// XXX is it safe to leave mData/mLen uninitialized? This should only be
|
|
|
|
// used for notifications that don't use them, but I'd like more
|
|
|
|
// bulletproof compile-time checking.
|
|
|
|
|
|
|
|
DataChannelOnMessageAvailable(int32_t aType,
|
|
|
|
DataChannelConnection *aConnection,
|
|
|
|
DataChannel *aChannel)
|
|
|
|
: mType(aType),
|
|
|
|
mChannel(aChannel),
|
|
|
|
mConnection(aConnection) {}
|
|
|
|
|
2013-03-05 16:17:07 +00:00
|
|
|
// for ON_CONNECTION/ON_DISCONNECTED
|
2012-12-14 04:30:11 +00:00
|
|
|
DataChannelOnMessageAvailable(int32_t aType,
|
|
|
|
DataChannelConnection *aConnection,
|
2013-03-05 16:17:07 +00:00
|
|
|
bool aResult = true)
|
2012-12-14 04:30:11 +00:00
|
|
|
: mType(aType),
|
|
|
|
mConnection(aConnection),
|
|
|
|
mResult(aResult) {}
|
|
|
|
|
2012-10-03 23:51:23 +00:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2013-02-12 21:32:43 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-10-03 23:51:23 +00:00
|
|
|
switch (mType) {
|
|
|
|
case ON_DATA:
|
|
|
|
case ON_CHANNEL_OPEN:
|
|
|
|
case ON_CHANNEL_CLOSED:
|
2013-02-12 21:32:43 +00:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mChannel->mListenerLock);
|
|
|
|
if (!mChannel->mListener) {
|
|
|
|
DATACHANNEL_LOG(("DataChannelOnMessageAvailable (%d) with null Listener!",mType));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (mType) {
|
|
|
|
case ON_DATA:
|
|
|
|
if (mLen < 0) {
|
|
|
|
mChannel->mListener->OnMessageAvailable(mChannel->mContext, mData);
|
|
|
|
} else {
|
|
|
|
mChannel->mListener->OnBinaryMessageAvailable(mChannel->mContext, mData);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ON_CHANNEL_OPEN:
|
|
|
|
mChannel->mListener->OnChannelConnected(mChannel->mContext);
|
|
|
|
break;
|
|
|
|
case ON_CHANNEL_CLOSED:
|
|
|
|
mChannel->mListener->OnChannelClosed(mChannel->mContext);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-06-02 13:48:17 +00:00
|
|
|
case ON_DISCONNECTED:
|
|
|
|
// If we've disconnected, make sure we close all the streams - from mainthread!
|
|
|
|
mConnection->CloseAll();
|
|
|
|
// fall through
|
2012-10-03 23:51:23 +00:00
|
|
|
case ON_CHANNEL_CREATED:
|
|
|
|
case ON_CONNECTION:
|
2013-02-12 21:32:43 +00:00
|
|
|
// WeakPtr - only used/modified/nulled from MainThread so we can use a WeakPtr here
|
|
|
|
if (!mConnection->mListener) {
|
|
|
|
DATACHANNEL_LOG(("DataChannelOnMessageAvailable (%d) with null Listener",mType));
|
2012-10-03 23:51:23 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-02-12 21:32:43 +00:00
|
|
|
switch (mType) {
|
|
|
|
case ON_CHANNEL_CREATED:
|
|
|
|
// important to give it an already_AddRefed pointer!
|
|
|
|
mConnection->mListener->NotifyDataChannel(mChannel.forget());
|
|
|
|
break;
|
|
|
|
case ON_CONNECTION:
|
|
|
|
if (mResult) {
|
|
|
|
mConnection->mListener->NotifyConnection();
|
|
|
|
}
|
2013-03-05 16:17:07 +00:00
|
|
|
// FIX - on mResult false (failure) we should do something. Needs spec work here
|
2013-02-12 21:32:43 +00:00
|
|
|
break;
|
|
|
|
case ON_DISCONNECTED:
|
|
|
|
mConnection->mListener->NotifyClosedConnection();
|
|
|
|
break;
|
2012-12-14 04:30:11 +00:00
|
|
|
}
|
2012-10-03 23:51:23 +00:00
|
|
|
break;
|
|
|
|
case START_DEFER:
|
|
|
|
mConnection->StartDefer();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~DataChannelOnMessageAvailable() {}
|
|
|
|
|
|
|
|
int32_t mType;
|
|
|
|
// XXX should use union
|
2012-11-02 19:28:13 +00:00
|
|
|
nsRefPtr<DataChannel> mChannel;
|
2012-10-03 23:51:23 +00:00
|
|
|
nsRefPtr<DataChannelConnection> mConnection;
|
|
|
|
nsCString mData;
|
|
|
|
int32_t mLen;
|
2012-12-14 04:30:11 +00:00
|
|
|
bool mResult;
|
2012-10-03 23:51:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // NETWERK_SCTP_DATACHANNEL_DATACHANNEL_H_
|