WebSocketConnection.h (2534B)
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/. */ 6 7 #ifndef mozilla_net_WebSocketConnection_h 8 #define mozilla_net_WebSocketConnection_h 9 10 #include <list> 11 12 #include "nsIStreamListener.h" 13 #include "nsIAsyncInputStream.h" 14 #include "nsIAsyncOutputStream.h" 15 #include "mozilla/net/WebSocketConnectionBase.h" 16 #include "nsTArray.h" 17 #include "nsISocketTransport.h" 18 19 class nsISocketTransport; 20 21 namespace mozilla { 22 namespace net { 23 24 class WebSocketConnectionListener; 25 26 class WebSocketConnection : public nsIInputStreamCallback, 27 public nsIOutputStreamCallback, 28 public WebSocketConnectionBase { 29 public: 30 NS_DECL_THREADSAFE_ISUPPORTS 31 NS_DECL_NSIINPUTSTREAMCALLBACK 32 NS_DECL_NSIOUTPUTSTREAMCALLBACK 33 34 explicit WebSocketConnection(nsISocketTransport* aTransport, 35 nsIAsyncInputStream* aInputStream, 36 nsIAsyncOutputStream* aOutputStream); 37 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; 48 49 private: 50 virtual ~WebSocketConnection(); 51 52 class OutputData { 53 public: 54 explicit OutputData(nsTArray<uint8_t>&& aData) : mData(std::move(aData)) { 55 MOZ_COUNT_CTOR(OutputData); 56 } 57 58 ~OutputData() { MOZ_COUNT_DTOR(OutputData); } 59 60 const nsTArray<uint8_t>& GetData() const { return mData; } 61 62 private: 63 nsTArray<uint8_t> mData; 64 }; 65 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}; 74 }; 75 76 } // namespace net 77 } // namespace mozilla 78 79 #endif // mozilla_net_WebSocketConnection_h