Source code

Revision control

Other Tools

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
6
#ifndef nsUDPSocket_h__
7
#define nsUDPSocket_h__
8
9
#include "nsIUDPSocket.h"
10
#include "mozilla/Mutex.h"
11
#include "mozilla/net/DNS.h"
12
#include "nsIOutputStream.h"
13
#include "nsAutoPtr.h"
14
#include "nsCycleCollectionParticipant.h"
15
16
//-----------------------------------------------------------------------------
17
18
namespace mozilla {
19
namespace net {
20
21
class nsUDPSocket final : public nsASocketHandler, public nsIUDPSocket {
22
public:
23
NS_DECL_THREADSAFE_ISUPPORTS
24
NS_DECL_NSIUDPSOCKET
25
26
// nsASocketHandler methods:
27
virtual void OnSocketReady(PRFileDesc* fd, int16_t outFlags) override;
28
virtual void OnSocketDetached(PRFileDesc* fd) override;
29
virtual void IsLocal(bool* aIsLocal) override;
30
31
uint64_t ByteCountSent() override { return mByteWriteCount; }
32
uint64_t ByteCountReceived() override { return mByteReadCount; }
33
34
void AddOutputBytes(uint64_t aBytes);
35
36
nsUDPSocket();
37
38
private:
39
virtual ~nsUDPSocket();
40
41
void OnMsgClose();
42
void OnMsgAttach();
43
44
// try attaching our socket (mFD) to the STS's poll list.
45
nsresult TryAttach();
46
47
friend class SetSocketOptionRunnable;
48
nsresult SetSocketOption(const PRSocketOptionData& aOpt);
49
nsresult JoinMulticastInternal(const PRNetAddr& aAddr,
50
const PRNetAddr& aIface);
51
nsresult LeaveMulticastInternal(const PRNetAddr& aAddr,
52
const PRNetAddr& aIface);
53
nsresult SetMulticastInterfaceInternal(const PRNetAddr& aIface);
54
55
void CloseSocket();
56
57
// lock protects access to mListener;
58
// so mListener is not cleared while being used/locked.
59
Mutex mLock;
60
PRFileDesc* mFD;
61
NetAddr mAddr;
62
OriginAttributes mOriginAttributes;
63
nsCOMPtr<nsIUDPSocketListener> mListener;
64
nsCOMPtr<nsIEventTarget> mListenerTarget;
65
bool mAttached;
66
RefPtr<nsSocketTransportService> mSts;
67
68
uint64_t mByteReadCount;
69
uint64_t mByteWriteCount;
70
};
71
72
//-----------------------------------------------------------------------------
73
74
class nsUDPMessage : public nsIUDPMessage {
75
public:
76
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
77
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsUDPMessage)
78
NS_DECL_NSIUDPMESSAGE
79
80
nsUDPMessage(NetAddr* aAddr, nsIOutputStream* aOutputStream,
81
FallibleTArray<uint8_t>& aData);
82
83
private:
84
virtual ~nsUDPMessage();
85
86
NetAddr mAddr;
87
nsCOMPtr<nsIOutputStream> mOutputStream;
88
FallibleTArray<uint8_t> mData;
89
JS::Heap<JSObject*> mJsobj;
90
};
91
92
//-----------------------------------------------------------------------------
93
94
class nsUDPOutputStream : public nsIOutputStream {
95
public:
96
NS_DECL_THREADSAFE_ISUPPORTS
97
NS_DECL_NSIOUTPUTSTREAM
98
99
nsUDPOutputStream(nsUDPSocket* aSocket, PRFileDesc* aFD,
100
PRNetAddr& aPrClientAddr);
101
102
private:
103
virtual ~nsUDPOutputStream() = default;
104
105
RefPtr<nsUDPSocket> mSocket;
106
PRFileDesc* mFD;
107
PRNetAddr mPrClientAddr;
108
bool mIsClosed;
109
};
110
111
} // namespace net
112
} // namespace mozilla
113
114
#endif // nsUDPSocket_h__