1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 ts=8 et tw=80 : */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_net_WebSocketConnection_h
8 #define mozilla_net_WebSocketConnection_h
12 #include "nsIStreamListener.h"
13 #include "nsIAsyncInputStream.h"
14 #include "nsIAsyncOutputStream.h"
15 #include "mozilla/net/WebSocketConnectionBase.h"
17 #include "nsISocketTransport.h"
19 class nsISocketTransport
;
24 class WebSocketConnectionListener
;
26 class WebSocketConnection
: public nsIInputStreamCallback
,
27 public nsIOutputStreamCallback
,
28 public WebSocketConnectionBase
{
30 NS_DECL_THREADSAFE_ISUPPORTS
31 NS_DECL_NSIINPUTSTREAMCALLBACK
32 NS_DECL_NSIOUTPUTSTREAMCALLBACK
34 explicit WebSocketConnection(nsISocketTransport
* aTransport
,
35 nsIAsyncInputStream
* aInputStream
,
36 nsIAsyncOutputStream
* aOutputStream
);
38 nsresult
Init(WebSocketConnectionListener
* aListener
) override
;
39 void GetIoTarget(nsIEventTarget
** aTarget
) override
;
40 void Close() override
;
41 nsresult
WriteOutputData(const uint8_t* aHdrBuf
, uint32_t aHdrBufLength
,
42 const uint8_t* aPayloadBuf
,
43 uint32_t aPayloadBufLength
) override
;
44 nsresult
WriteOutputData(nsTArray
<uint8_t>&& aData
);
45 nsresult
StartReading() override
;
46 void DrainSocketData() override
;
47 nsresult
GetSecurityInfo(nsITransportSecurityInfo
** aSecurityInfo
) override
;
50 virtual ~WebSocketConnection();
54 explicit OutputData(nsTArray
<uint8_t>&& aData
) : mData(std::move(aData
)) {
55 MOZ_COUNT_CTOR(OutputData
);
58 ~OutputData() { MOZ_COUNT_DTOR(OutputData
); }
60 const nsTArray
<uint8_t>& GetData() const { return mData
; }
63 nsTArray
<uint8_t> mData
;
66 RefPtr
<WebSocketConnectionListener
> mListener
;
67 nsCOMPtr
<nsISocketTransport
> mTransport
;
68 nsCOMPtr
<nsIAsyncInputStream
> mSocketIn
;
69 nsCOMPtr
<nsIAsyncOutputStream
> mSocketOut
;
70 nsCOMPtr
<nsIEventTarget
> mSocketThread
;
71 size_t mWriteOffset
{0};
72 std::list
<OutputData
> mOutputQueue
;
73 bool mStartReadingCalled
{false};
77 } // namespace mozilla
79 #endif // mozilla_net_WebSocketConnection_h