michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // Original author: ekr@rtfm.com michael@0: michael@0: #ifndef transportlayerprsock_h__ michael@0: #define transportlayerprsock_h__ michael@0: michael@0: #include "nspr.h" michael@0: #include "prio.h" michael@0: michael@0: #include "nsASocketHandler.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsISocketTransportService.h" michael@0: #include "nsXPCOM.h" michael@0: michael@0: #include "m_cpp_utils.h" michael@0: #include "transportflow.h" michael@0: #include "transportlayer.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: class TransportLayerPrsock : public TransportLayer { michael@0: public: michael@0: TransportLayerPrsock() : fd_(nullptr), handler_() {} michael@0: michael@0: virtual ~TransportLayerPrsock() { michael@0: Detach(); michael@0: } michael@0: michael@0: michael@0: // Internal initializer michael@0: virtual nsresult InitInternal(); michael@0: michael@0: void Import(PRFileDesc *fd, nsresult *result); michael@0: michael@0: void Detach() { michael@0: handler_->Detach(); michael@0: } michael@0: michael@0: // Implement TransportLayer michael@0: virtual TransportResult SendPacket(const unsigned char *data, size_t len); michael@0: michael@0: TRANSPORT_LAYER_ID("prsock") michael@0: michael@0: private: michael@0: DISALLOW_COPY_ASSIGN(TransportLayerPrsock); michael@0: michael@0: // Inner class michael@0: class SocketHandler : public nsASocketHandler { michael@0: public: michael@0: SocketHandler(TransportLayerPrsock *prsock, PRFileDesc *fd) : michael@0: prsock_(prsock), fd_(fd) { michael@0: mPollFlags = PR_POLL_READ; michael@0: } michael@0: virtual ~SocketHandler() {} michael@0: michael@0: void Detach() { michael@0: mCondition = NS_BASE_STREAM_CLOSED; michael@0: prsock_ = nullptr; michael@0: } michael@0: michael@0: // Implement nsASocket michael@0: virtual void OnSocketReady(PRFileDesc *fd, int16_t outflags) { michael@0: if (prsock_) { michael@0: prsock_->OnSocketReady(fd, outflags); michael@0: } michael@0: } michael@0: michael@0: virtual void OnSocketDetached(PRFileDesc *fd) { michael@0: if (prsock_) { michael@0: prsock_->OnSocketDetached(fd); michael@0: } michael@0: PR_Close(fd_); michael@0: } michael@0: michael@0: virtual void IsLocal(bool *aIsLocal) { michael@0: // TODO(jesup): better check? Does it matter? (likely no) michael@0: *aIsLocal = false; michael@0: } michael@0: michael@0: virtual uint64_t ByteCountSent() { return 0; } michael@0: virtual uint64_t ByteCountReceived() { return 0; } michael@0: michael@0: // nsISupports methods michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: michael@0: private: michael@0: TransportLayerPrsock *prsock_; michael@0: PRFileDesc *fd_; michael@0: private: michael@0: DISALLOW_COPY_ASSIGN(SocketHandler); michael@0: }; michael@0: michael@0: // Allow SocketHandler to talk to our APIs michael@0: friend class SocketHandler; michael@0: michael@0: // Functions to be called by SocketHandler michael@0: void OnSocketReady(PRFileDesc *fd, int16_t outflags); michael@0: void OnSocketDetached(PRFileDesc *fd) { michael@0: TL_SET_STATE(TS_CLOSED); michael@0: } michael@0: void IsLocal(bool *aIsLocal) { michael@0: // TODO(jesup): better check? Does it matter? (likely no) michael@0: *aIsLocal = false; michael@0: } michael@0: michael@0: PRFileDesc *fd_; michael@0: nsRefPtr handler_; michael@0: nsCOMPtr stservice_; michael@0: michael@0: }; michael@0: michael@0: michael@0: michael@0: } // close namespace michael@0: #endif