media/mtransport/transportlayerprsock.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* vim: set ts=2 et sw=2 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 file,
     5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 // Original author: ekr@rtfm.com
     9 #ifndef transportlayerprsock_h__
    10 #define transportlayerprsock_h__
    12 #include "nspr.h"
    13 #include "prio.h"
    15 #include "nsASocketHandler.h"
    16 #include "nsCOMPtr.h"
    17 #include "nsISocketTransportService.h"
    18 #include "nsXPCOM.h"
    20 #include "m_cpp_utils.h"
    21 #include "transportflow.h"
    22 #include "transportlayer.h"
    24 namespace mozilla {
    26 class TransportLayerPrsock : public TransportLayer {
    27  public:
    28   TransportLayerPrsock() : fd_(nullptr), handler_() {}
    30   virtual ~TransportLayerPrsock() {
    31     Detach();
    32   }
    35   // Internal initializer
    36   virtual nsresult InitInternal();
    38   void Import(PRFileDesc *fd, nsresult *result);
    40   void Detach() {
    41     handler_->Detach();
    42   }
    44   // Implement TransportLayer
    45   virtual TransportResult SendPacket(const unsigned char *data, size_t len);
    47   TRANSPORT_LAYER_ID("prsock")
    49  private:
    50   DISALLOW_COPY_ASSIGN(TransportLayerPrsock);
    52   // Inner class
    53   class SocketHandler : public nsASocketHandler {
    54    public:
    55       SocketHandler(TransportLayerPrsock *prsock, PRFileDesc *fd) :
    56         prsock_(prsock), fd_(fd) {
    57         mPollFlags = PR_POLL_READ;
    58       }
    59       virtual ~SocketHandler() {}
    61       void Detach() {
    62         mCondition = NS_BASE_STREAM_CLOSED;
    63         prsock_ = nullptr;
    64       }
    66       // Implement nsASocket
    67       virtual void OnSocketReady(PRFileDesc *fd, int16_t outflags) {
    68         if (prsock_) {
    69           prsock_->OnSocketReady(fd, outflags);
    70         }
    71       }
    73       virtual void OnSocketDetached(PRFileDesc *fd) {
    74         if (prsock_) {
    75           prsock_->OnSocketDetached(fd);
    76         }
    77         PR_Close(fd_);
    78       }
    80       virtual void IsLocal(bool *aIsLocal) {
    81         // TODO(jesup): better check? Does it matter? (likely no)
    82         *aIsLocal = false;
    83       }
    85       virtual uint64_t ByteCountSent() { return 0; }
    86       virtual uint64_t ByteCountReceived() { return 0; }
    88       // nsISupports methods
    89       NS_DECL_THREADSAFE_ISUPPORTS
    91       private:
    92       TransportLayerPrsock *prsock_;
    93       PRFileDesc *fd_;
    94    private:
    95     DISALLOW_COPY_ASSIGN(SocketHandler);
    96   };
    98   // Allow SocketHandler to talk to our APIs
    99   friend class SocketHandler;
   101   // Functions to be called by SocketHandler
   102   void OnSocketReady(PRFileDesc *fd, int16_t outflags);
   103   void OnSocketDetached(PRFileDesc *fd) {
   104     TL_SET_STATE(TS_CLOSED);
   105   }
   106   void IsLocal(bool *aIsLocal) {
   107     // TODO(jesup): better check? Does it matter? (likely no)
   108     *aIsLocal = false;
   109   }
   111   PRFileDesc *fd_;
   112   nsRefPtr<SocketHandler> handler_;
   113   nsCOMPtr<nsISocketTransportService> stservice_;
   115 };
   119 }  // close namespace
   120 #endif

mercurial