2013-03-03 00:04:38 -08:00
|
|
|
/* vim:set ts=2 sw=2 et cindent: */
|
|
|
|
/* 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/. */
|
|
|
|
|
2013-05-10 05:00:22 -07:00
|
|
|
#ifndef nsUDPSocket_h__
|
|
|
|
#define nsUDPSocket_h__
|
2013-03-03 00:04:38 -08:00
|
|
|
|
2013-05-10 05:00:22 -07:00
|
|
|
#include "nsIUDPSocket.h"
|
2013-03-03 00:04:38 -08:00
|
|
|
#include "mozilla/Mutex.h"
|
|
|
|
#include "nsIOutputStream.h"
|
2013-09-19 06:54:39 -07:00
|
|
|
#include "nsAutoPtr.h"
|
2014-03-12 22:43:36 -07:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2013-03-03 00:04:38 -08:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
class nsUDPSocket final : public nsASocketHandler
|
2015-03-27 11:52:19 -07:00
|
|
|
, public nsIUDPSocket
|
2013-03-03 00:04:38 -08:00
|
|
|
{
|
|
|
|
public:
|
2013-07-18 19:24:13 -07:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2013-05-10 05:00:22 -07:00
|
|
|
NS_DECL_NSIUDPSOCKET
|
2013-03-03 00:04:38 -08:00
|
|
|
|
|
|
|
// nsASocketHandler methods:
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual void OnSocketReady(PRFileDesc* fd, int16_t outFlags) override;
|
|
|
|
virtual void OnSocketDetached(PRFileDesc* fd) override;
|
|
|
|
virtual void IsLocal(bool* aIsLocal) override;
|
2013-03-03 00:04:38 -08:00
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
uint64_t ByteCountSent() override { return mByteWriteCount; }
|
|
|
|
uint64_t ByteCountReceived() override { return mByteReadCount; }
|
2013-03-03 00:04:38 -08:00
|
|
|
|
|
|
|
void AddOutputBytes(uint64_t aBytes);
|
|
|
|
|
2013-05-10 05:00:22 -07:00
|
|
|
nsUDPSocket();
|
2013-03-03 00:04:38 -08:00
|
|
|
|
2014-06-24 09:36:44 -07:00
|
|
|
private:
|
2013-05-10 05:00:22 -07:00
|
|
|
virtual ~nsUDPSocket();
|
2013-03-03 00:04:38 -08:00
|
|
|
|
|
|
|
void OnMsgClose();
|
|
|
|
void OnMsgAttach();
|
|
|
|
|
|
|
|
// try attaching our socket (mFD) to the STS's poll list.
|
|
|
|
nsresult TryAttach();
|
|
|
|
|
2014-04-29 12:35:00 -07:00
|
|
|
friend class SetSocketOptionRunnable;
|
|
|
|
nsresult SetSocketOption(const PRSocketOptionData& aOpt);
|
|
|
|
nsresult JoinMulticastInternal(const PRNetAddr& aAddr,
|
|
|
|
const PRNetAddr& aIface);
|
|
|
|
nsresult LeaveMulticastInternal(const PRNetAddr& aAddr,
|
|
|
|
const PRNetAddr& aIface);
|
|
|
|
nsresult SetMulticastInterfaceInternal(const PRNetAddr& aIface);
|
|
|
|
|
2013-03-03 00:04:38 -08:00
|
|
|
// lock protects access to mListener;
|
|
|
|
// so mListener is not cleared while being used/locked.
|
|
|
|
mozilla::Mutex mLock;
|
|
|
|
PRFileDesc *mFD;
|
|
|
|
mozilla::net::NetAddr mAddr;
|
2013-05-10 05:00:22 -07:00
|
|
|
nsCOMPtr<nsIUDPSocketListener> mListener;
|
2013-03-03 00:04:38 -08:00
|
|
|
nsCOMPtr<nsIEventTarget> mListenerTarget;
|
|
|
|
bool mAttached;
|
|
|
|
nsRefPtr<nsSocketTransportService> mSts;
|
|
|
|
|
|
|
|
uint64_t mByteReadCount;
|
|
|
|
uint64_t mByteWriteCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class nsUDPMessage : public nsIUDPMessage
|
|
|
|
{
|
|
|
|
public:
|
2014-03-12 22:43:36 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsUDPMessage)
|
2013-03-03 00:04:38 -08:00
|
|
|
NS_DECL_NSIUDPMESSAGE
|
|
|
|
|
2014-03-12 22:43:36 -07:00
|
|
|
nsUDPMessage(mozilla::net::NetAddr* aAddr,
|
2013-03-03 00:04:38 -08:00
|
|
|
nsIOutputStream* aOutputStream,
|
2014-03-12 22:43:36 -07:00
|
|
|
FallibleTArray<uint8_t>& aData);
|
2013-03-03 00:04:38 -08:00
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~nsUDPMessage();
|
|
|
|
|
2014-03-12 22:43:36 -07:00
|
|
|
mozilla::net::NetAddr mAddr;
|
2013-03-03 00:04:38 -08:00
|
|
|
nsCOMPtr<nsIOutputStream> mOutputStream;
|
2014-03-12 22:43:36 -07:00
|
|
|
FallibleTArray<uint8_t> mData;
|
|
|
|
JS::Heap<JSObject*> mJsobj;
|
2013-03-03 00:04:38 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class nsUDPOutputStream : public nsIOutputStream
|
|
|
|
{
|
|
|
|
public:
|
2013-07-18 19:24:13 -07:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2013-03-03 00:04:38 -08:00
|
|
|
NS_DECL_NSIOUTPUTSTREAM
|
|
|
|
|
2013-05-10 05:00:22 -07:00
|
|
|
nsUDPOutputStream(nsUDPSocket* aSocket,
|
2013-03-03 00:04:38 -08:00
|
|
|
PRFileDesc* aFD,
|
|
|
|
PRNetAddr& aPrClientAddr);
|
|
|
|
|
|
|
|
private:
|
2014-06-24 09:36:44 -07:00
|
|
|
virtual ~nsUDPOutputStream();
|
|
|
|
|
2013-05-10 05:00:22 -07:00
|
|
|
nsRefPtr<nsUDPSocket> mSocket;
|
2013-03-03 00:04:38 -08:00
|
|
|
PRFileDesc *mFD;
|
|
|
|
PRNetAddr mPrClientAddr;
|
|
|
|
bool mIsClosed;
|
|
|
|
};
|
|
|
|
|
2013-05-10 05:00:22 -07:00
|
|
|
#endif // nsUDPSocket_h__
|