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