nsUDPSocket.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* vim:set ts=2 sw=2 et cindent: */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef nsUDPSocket_h__
  6. #define nsUDPSocket_h__
  7. #include "nsIUDPSocket.h"
  8. #include "mozilla/Mutex.h"
  9. #include "nsIOutputStream.h"
  10. #include "nsAutoPtr.h"
  11. #include "nsCycleCollectionParticipant.h"
  12. //-----------------------------------------------------------------------------
  13. namespace mozilla {
  14. namespace net {
  15. class nsUDPSocket final : public nsASocketHandler
  16. , public nsIUDPSocket
  17. {
  18. public:
  19. NS_DECL_THREADSAFE_ISUPPORTS
  20. NS_DECL_NSIUDPSOCKET
  21. // nsASocketHandler methods:
  22. virtual void OnSocketReady(PRFileDesc* fd, int16_t outFlags) override;
  23. virtual void OnSocketDetached(PRFileDesc* fd) override;
  24. virtual void IsLocal(bool* aIsLocal) override;
  25. uint64_t ByteCountSent() override { return mByteWriteCount; }
  26. uint64_t ByteCountReceived() override { return mByteReadCount; }
  27. void AddOutputBytes(uint64_t aBytes);
  28. nsUDPSocket();
  29. private:
  30. virtual ~nsUDPSocket();
  31. void OnMsgClose();
  32. void OnMsgAttach();
  33. // try attaching our socket (mFD) to the STS's poll list.
  34. nsresult TryAttach();
  35. friend class SetSocketOptionRunnable;
  36. nsresult SetSocketOption(const PRSocketOptionData& aOpt);
  37. nsresult JoinMulticastInternal(const PRNetAddr& aAddr,
  38. const PRNetAddr& aIface);
  39. nsresult LeaveMulticastInternal(const PRNetAddr& aAddr,
  40. const PRNetAddr& aIface);
  41. nsresult SetMulticastInterfaceInternal(const PRNetAddr& aIface);
  42. void CloseSocket();
  43. // lock protects access to mListener;
  44. // so mListener is not cleared while being used/locked.
  45. Mutex mLock;
  46. PRFileDesc *mFD;
  47. NetAddr mAddr;
  48. uint32_t mAppId;
  49. bool mIsInIsolatedMozBrowserElement;
  50. nsCOMPtr<nsIUDPSocketListener> mListener;
  51. nsCOMPtr<nsIEventTarget> mListenerTarget;
  52. bool mAttached;
  53. RefPtr<nsSocketTransportService> mSts;
  54. uint64_t mByteReadCount;
  55. uint64_t mByteWriteCount;
  56. };
  57. //-----------------------------------------------------------------------------
  58. class nsUDPMessage : public nsIUDPMessage
  59. {
  60. public:
  61. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  62. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsUDPMessage)
  63. NS_DECL_NSIUDPMESSAGE
  64. nsUDPMessage(NetAddr* aAddr,
  65. nsIOutputStream* aOutputStream,
  66. FallibleTArray<uint8_t>& aData);
  67. private:
  68. virtual ~nsUDPMessage();
  69. NetAddr mAddr;
  70. nsCOMPtr<nsIOutputStream> mOutputStream;
  71. FallibleTArray<uint8_t> mData;
  72. JS::Heap<JSObject*> mJsobj;
  73. };
  74. //-----------------------------------------------------------------------------
  75. class nsUDPOutputStream : public nsIOutputStream
  76. {
  77. public:
  78. NS_DECL_THREADSAFE_ISUPPORTS
  79. NS_DECL_NSIOUTPUTSTREAM
  80. nsUDPOutputStream(nsUDPSocket* aSocket,
  81. PRFileDesc* aFD,
  82. PRNetAddr& aPrClientAddr);
  83. private:
  84. virtual ~nsUDPOutputStream();
  85. RefPtr<nsUDPSocket> mSocket;
  86. PRFileDesc *mFD;
  87. PRNetAddr mPrClientAddr;
  88. bool mIsClosed;
  89. };
  90. } // namespace net
  91. } // namespace mozilla
  92. #endif // nsUDPSocket_h__