WebTransportStreamBase.cpp (2225B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 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 #include "WebTransportStreamBase.h" 7 8 #include "nsIPipe.h" 9 #include "nsIOService.h" 10 #include "nsSocketTransportService2.h" 11 12 namespace mozilla::net { 13 14 WebTransportStreamBase::WebTransportStreamBase( 15 uint64_t aSessionId, 16 std::function<void(Result<RefPtr<WebTransportStreamBase>, nsresult>&&)>&& 17 aCallback) 18 : mSessionId(aSessionId), mStreamReadyCallback(std::move(aCallback)) {} 19 20 WebTransportStreamBase::~WebTransportStreamBase() = default; 21 22 nsresult WebTransportStreamBase::InitOutputPipe() { 23 nsCOMPtr<nsIAsyncOutputStream> out; 24 nsCOMPtr<nsIAsyncInputStream> in; 25 NS_NewPipe2(getter_AddRefs(in), getter_AddRefs(out), true, true, 26 nsIOService::gDefaultSegmentSize, 27 nsIOService::gDefaultSegmentCount); 28 29 { 30 MutexAutoLock lock(mMutex); 31 mSendStreamPipeIn = std::move(in); 32 mSendStreamPipeOut = std::move(out); 33 } 34 35 nsresult rv = 36 mSendStreamPipeIn->AsyncWait(this, 0, 0, gSocketTransportService); 37 if (NS_FAILED(rv)) { 38 return rv; 39 } 40 41 mSendState = WAITING_DATA; 42 return NS_OK; 43 } 44 45 nsresult WebTransportStreamBase::InitInputPipe() { 46 nsCOMPtr<nsIAsyncOutputStream> out; 47 nsCOMPtr<nsIAsyncInputStream> in; 48 NS_NewPipe2(getter_AddRefs(in), getter_AddRefs(out), true, true, 49 nsIOService::gDefaultSegmentSize, 50 nsIOService::gDefaultSegmentCount); 51 52 { 53 MutexAutoLock lock(mMutex); 54 mReceiveStreamPipeIn = std::move(in); 55 mReceiveStreamPipeOut = std::move(out); 56 } 57 58 mRecvState = READING; 59 return NS_OK; 60 } 61 62 void WebTransportStreamBase::GetWriterAndReader( 63 nsIAsyncOutputStream** aOutOutputStream, 64 nsIAsyncInputStream** aOutInputStream) { 65 nsCOMPtr<nsIAsyncOutputStream> output; 66 nsCOMPtr<nsIAsyncInputStream> input; 67 { 68 MutexAutoLock lock(mMutex); 69 output = mSendStreamPipeOut; 70 input = mReceiveStreamPipeIn; 71 } 72 73 output.forget(aOutOutputStream); 74 input.forget(aOutInputStream); 75 } 76 77 } // namespace mozilla::net