2013-03-03 12:04:38 +04: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 16:00:22 +04:00
|
|
|
#ifndef nsUDPSocket_h__
|
|
|
|
#define nsUDPSocket_h__
|
2013-03-03 12:04:38 +04:00
|
|
|
|
2013-05-10 16:00:22 +04:00
|
|
|
#include "nsIUDPSocket.h"
|
2013-03-03 12:04:38 +04:00
|
|
|
#include "mozilla/Mutex.h"
|
2018-12-17 05:36:32 +03:00
|
|
|
#include "mozilla/net/DNS.h"
|
2013-03-03 12:04:38 +04:00
|
|
|
#include "nsIOutputStream.h"
|
2013-09-19 17:54:39 +04:00
|
|
|
#include "nsAutoPtr.h"
|
2014-03-13 09:43:36 +04:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2013-03-03 12:04:38 +04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2016-05-19 05:02:57 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsUDPSocket final : public nsASocketHandler, public nsIUDPSocket {
|
2013-03-03 12:04:38 +04:00
|
|
|
public:
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2013-05-10 16:00:22 +04:00
|
|
|
NS_DECL_NSIUDPSOCKET
|
2013-03-03 12:04:38 +04:00
|
|
|
|
|
|
|
// nsASocketHandler methods:
|
2015-03-21 19:28:04 +03: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 12:04:38 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
uint64_t ByteCountSent() override { return mByteWriteCount; }
|
|
|
|
uint64_t ByteCountReceived() override { return mByteReadCount; }
|
2013-03-03 12:04:38 +04:00
|
|
|
|
|
|
|
void AddOutputBytes(uint64_t aBytes);
|
|
|
|
|
2013-05-10 16:00:22 +04:00
|
|
|
nsUDPSocket();
|
2013-03-03 12:04:38 +04:00
|
|
|
|
2014-06-24 20:36:44 +04:00
|
|
|
private:
|
2013-05-10 16:00:22 +04:00
|
|
|
virtual ~nsUDPSocket();
|
2013-03-03 12:04:38 +04:00
|
|
|
|
|
|
|
void OnMsgClose();
|
|
|
|
void OnMsgAttach();
|
|
|
|
|
|
|
|
// try attaching our socket (mFD) to the STS's poll list.
|
|
|
|
nsresult TryAttach();
|
|
|
|
|
2014-04-29 23:35:00 +04: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);
|
|
|
|
|
2016-01-21 00:01:00 +03:00
|
|
|
void CloseSocket();
|
|
|
|
|
2013-03-03 12:04:38 +04:00
|
|
|
// lock protects access to mListener;
|
|
|
|
// so mListener is not cleared while being used/locked.
|
2016-05-19 05:02:57 +03:00
|
|
|
Mutex mLock;
|
|
|
|
PRFileDesc* mFD;
|
|
|
|
NetAddr mAddr;
|
2017-02-15 12:48:15 +03:00
|
|
|
OriginAttributes mOriginAttributes;
|
2013-05-10 16:00:22 +04:00
|
|
|
nsCOMPtr<nsIUDPSocketListener> mListener;
|
2013-03-03 12:04:38 +04:00
|
|
|
nsCOMPtr<nsIEventTarget> mListenerTarget;
|
|
|
|
bool mAttached;
|
2016-05-19 05:02:57 +03:00
|
|
|
RefPtr<nsSocketTransportService> mSts;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-03-03 12:04:38 +04:00
|
|
|
uint64_t mByteReadCount;
|
|
|
|
uint64_t mByteWriteCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class nsUDPMessage : public nsIUDPMessage {
|
|
|
|
public:
|
2014-03-13 09:43:36 +04:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsUDPMessage)
|
2013-03-03 12:04:38 +04:00
|
|
|
NS_DECL_NSIUDPMESSAGE
|
|
|
|
|
|
|
|
nsUDPMessage(NetAddr* aAddr, nsIOutputStream* aOutputStream,
|
2014-03-13 09:43:36 +04:00
|
|
|
FallibleTArray<uint8_t>& aData);
|
2013-03-03 12:04:38 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~nsUDPMessage();
|
|
|
|
|
2016-05-19 05:02:57 +03:00
|
|
|
NetAddr mAddr;
|
2013-03-03 12:04:38 +04:00
|
|
|
nsCOMPtr<nsIOutputStream> mOutputStream;
|
2014-03-13 09:43:36 +04:00
|
|
|
FallibleTArray<uint8_t> mData;
|
|
|
|
JS::Heap<JSObject*> mJsobj;
|
2013-03-03 12:04:38 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class nsUDPOutputStream : public nsIOutputStream {
|
|
|
|
public:
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2013-03-03 12:04:38 +04:00
|
|
|
NS_DECL_NSIOUTPUTSTREAM
|
|
|
|
|
2013-05-10 16:00:22 +04:00
|
|
|
nsUDPOutputStream(nsUDPSocket* aSocket, PRFileDesc* aFD,
|
2013-03-03 12:04:38 +04:00
|
|
|
PRNetAddr& aPrClientAddr);
|
|
|
|
|
|
|
|
private:
|
2018-04-30 19:46:04 +03:00
|
|
|
virtual ~nsUDPOutputStream() = default;
|
2014-06-24 20:36:44 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsUDPSocket> mSocket;
|
2013-03-03 12:04:38 +04:00
|
|
|
PRFileDesc* mFD;
|
|
|
|
PRNetAddr mPrClientAddr;
|
|
|
|
bool mIsClosed;
|
|
|
|
};
|
|
|
|
|
2016-05-19 05:02:57 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2013-05-10 16:00:22 +04:00
|
|
|
#endif // nsUDPSocket_h__
|