media/mtransport/transportlayerprsock.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/mtransport/transportlayerprsock.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,116 @@
     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 +#include "logging.h"
    1.13 +#include "nspr.h"
    1.14 +#include "prerror.h"
    1.15 +#include "prio.h"
    1.16 +
    1.17 +#include "nsCOMPtr.h"
    1.18 +#include "nsASocketHandler.h"
    1.19 +#include "nsISocketTransportService.h"
    1.20 +#include "nsNetCID.h"
    1.21 +#include "nsServiceManagerUtils.h"
    1.22 +#include "nsXPCOM.h"
    1.23 +
    1.24 +#include "transportflow.h"
    1.25 +#include "transportlayerprsock.h"
    1.26 +
    1.27 +namespace mozilla {
    1.28 +
    1.29 +MOZ_MTLOG_MODULE("mtransport")
    1.30 +
    1.31 +nsresult TransportLayerPrsock::InitInternal() {
    1.32 +  // Get the transport service as a transport service
    1.33 +  nsresult rv;
    1.34 +  stservice_ = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
    1.35 +
    1.36 +  if (!NS_SUCCEEDED(rv)) {
    1.37 +    MOZ_MTLOG(ML_ERROR, "Couldn't get socket transport service");
    1.38 +    return rv;
    1.39 +  }
    1.40 +
    1.41 +  return NS_OK;
    1.42 +}
    1.43 +
    1.44 +void TransportLayerPrsock::Import(PRFileDesc *fd, nsresult *result) {
    1.45 +  if (state_ != TS_INIT) {
    1.46 +    *result = NS_ERROR_NOT_INITIALIZED;
    1.47 +    return;
    1.48 +  }
    1.49 +
    1.50 +  MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Importing()");
    1.51 +  fd_ = fd;
    1.52 +  handler_ = new SocketHandler(this, fd);
    1.53 +
    1.54 +  nsresult rv = stservice_->AttachSocket(fd_, handler_);
    1.55 +  if (!NS_SUCCEEDED(rv)) {
    1.56 +    *result = rv;
    1.57 +    return;
    1.58 +  }
    1.59 +
    1.60 +  TL_SET_STATE(TS_OPEN);
    1.61 +
    1.62 +  *result = NS_OK;
    1.63 +}
    1.64 +
    1.65 +int TransportLayerPrsock::SendPacket(const unsigned char *data, size_t len) {
    1.66 +  MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "SendPacket(" << len << ")");
    1.67 +  if (state_ != TS_OPEN) {
    1.68 +    MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Can't send packet on closed interface");
    1.69 +    return TE_INTERNAL;
    1.70 +  }
    1.71 +
    1.72 +  int32_t status;
    1.73 +  status = PR_Write(fd_, data, len);
    1.74 +  if (status >= 0) {
    1.75 +    MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Wrote " << len << " bytes");
    1.76 +    return status;
    1.77 +  }
    1.78 +
    1.79 +  PRErrorCode err = PR_GetError();
    1.80 +  if (err == PR_WOULD_BLOCK_ERROR) {
    1.81 +    MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Write blocked");
    1.82 +    return TE_WOULDBLOCK;
    1.83 +  }
    1.84 +
    1.85 +
    1.86 +  MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Write error; channel closed");
    1.87 +  TL_SET_STATE(TS_ERROR);
    1.88 +  return TE_ERROR;
    1.89 +}
    1.90 +
    1.91 +void TransportLayerPrsock::OnSocketReady(PRFileDesc *fd, int16_t outflags) {
    1.92 +  if (!(outflags & PR_POLL_READ)) {
    1.93 +    return;
    1.94 +  }
    1.95 +
    1.96 +  MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "OnSocketReady(flags=" << outflags << ")");
    1.97 +
    1.98 +  unsigned char buf[1600];
    1.99 +  int32_t rv = PR_Read(fd, buf, sizeof(buf));
   1.100 +
   1.101 +  if (rv > 0) {
   1.102 +    // Successful read
   1.103 +    MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Read " << rv << " bytes");
   1.104 +    SignalPacketReceived(this, buf, rv);
   1.105 +  } else if (rv == 0) {
   1.106 +    MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Read 0 bytes; channel closed");
   1.107 +    TL_SET_STATE(TS_CLOSED);
   1.108 +  } else {
   1.109 +    PRErrorCode err = PR_GetError();
   1.110 +
   1.111 +    if (err != PR_WOULD_BLOCK_ERROR) {
   1.112 +      MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Read error; channel closed");
   1.113 +      TL_SET_STATE(TS_ERROR);
   1.114 +    }
   1.115 +  }
   1.116 +}
   1.117 +
   1.118 +NS_IMPL_ISUPPORTS0(TransportLayerPrsock::SocketHandler)
   1.119 +}  // close namespace

mercurial