1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/mtransport/transportlayerprsock.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,120 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +// Original author: ekr@rtfm.com 1.11 + 1.12 +#ifndef transportlayerprsock_h__ 1.13 +#define transportlayerprsock_h__ 1.14 + 1.15 +#include "nspr.h" 1.16 +#include "prio.h" 1.17 + 1.18 +#include "nsASocketHandler.h" 1.19 +#include "nsCOMPtr.h" 1.20 +#include "nsISocketTransportService.h" 1.21 +#include "nsXPCOM.h" 1.22 + 1.23 +#include "m_cpp_utils.h" 1.24 +#include "transportflow.h" 1.25 +#include "transportlayer.h" 1.26 + 1.27 +namespace mozilla { 1.28 + 1.29 +class TransportLayerPrsock : public TransportLayer { 1.30 + public: 1.31 + TransportLayerPrsock() : fd_(nullptr), handler_() {} 1.32 + 1.33 + virtual ~TransportLayerPrsock() { 1.34 + Detach(); 1.35 + } 1.36 + 1.37 + 1.38 + // Internal initializer 1.39 + virtual nsresult InitInternal(); 1.40 + 1.41 + void Import(PRFileDesc *fd, nsresult *result); 1.42 + 1.43 + void Detach() { 1.44 + handler_->Detach(); 1.45 + } 1.46 + 1.47 + // Implement TransportLayer 1.48 + virtual TransportResult SendPacket(const unsigned char *data, size_t len); 1.49 + 1.50 + TRANSPORT_LAYER_ID("prsock") 1.51 + 1.52 + private: 1.53 + DISALLOW_COPY_ASSIGN(TransportLayerPrsock); 1.54 + 1.55 + // Inner class 1.56 + class SocketHandler : public nsASocketHandler { 1.57 + public: 1.58 + SocketHandler(TransportLayerPrsock *prsock, PRFileDesc *fd) : 1.59 + prsock_(prsock), fd_(fd) { 1.60 + mPollFlags = PR_POLL_READ; 1.61 + } 1.62 + virtual ~SocketHandler() {} 1.63 + 1.64 + void Detach() { 1.65 + mCondition = NS_BASE_STREAM_CLOSED; 1.66 + prsock_ = nullptr; 1.67 + } 1.68 + 1.69 + // Implement nsASocket 1.70 + virtual void OnSocketReady(PRFileDesc *fd, int16_t outflags) { 1.71 + if (prsock_) { 1.72 + prsock_->OnSocketReady(fd, outflags); 1.73 + } 1.74 + } 1.75 + 1.76 + virtual void OnSocketDetached(PRFileDesc *fd) { 1.77 + if (prsock_) { 1.78 + prsock_->OnSocketDetached(fd); 1.79 + } 1.80 + PR_Close(fd_); 1.81 + } 1.82 + 1.83 + virtual void IsLocal(bool *aIsLocal) { 1.84 + // TODO(jesup): better check? Does it matter? (likely no) 1.85 + *aIsLocal = false; 1.86 + } 1.87 + 1.88 + virtual uint64_t ByteCountSent() { return 0; } 1.89 + virtual uint64_t ByteCountReceived() { return 0; } 1.90 + 1.91 + // nsISupports methods 1.92 + NS_DECL_THREADSAFE_ISUPPORTS 1.93 + 1.94 + private: 1.95 + TransportLayerPrsock *prsock_; 1.96 + PRFileDesc *fd_; 1.97 + private: 1.98 + DISALLOW_COPY_ASSIGN(SocketHandler); 1.99 + }; 1.100 + 1.101 + // Allow SocketHandler to talk to our APIs 1.102 + friend class SocketHandler; 1.103 + 1.104 + // Functions to be called by SocketHandler 1.105 + void OnSocketReady(PRFileDesc *fd, int16_t outflags); 1.106 + void OnSocketDetached(PRFileDesc *fd) { 1.107 + TL_SET_STATE(TS_CLOSED); 1.108 + } 1.109 + void IsLocal(bool *aIsLocal) { 1.110 + // TODO(jesup): better check? Does it matter? (likely no) 1.111 + *aIsLocal = false; 1.112 + } 1.113 + 1.114 + PRFileDesc *fd_; 1.115 + nsRefPtr<SocketHandler> handler_; 1.116 + nsCOMPtr<nsISocketTransportService> stservice_; 1.117 + 1.118 +}; 1.119 + 1.120 + 1.121 + 1.122 +} // close namespace 1.123 +#endif