summaryrefslogtreecommitdiffstats
path: root/media/mtransport/transportlayerprsock.cpp
blob: ccaff32d16a40d7b72dc30a8eedab16a8128c151 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

// Original author: ekr@rtfm.com

#include "logging.h"
#include "nspr.h"
#include "prerror.h"
#include "prio.h"

#include "nsCOMPtr.h"
#include "nsASocketHandler.h"
#include "nsISocketTransportService.h"
#include "nsNetCID.h"
#include "nsServiceManagerUtils.h"
#include "nsXPCOM.h"

#include "transportflow.h"
#include "transportlayerprsock.h"

namespace mozilla {

MOZ_MTLOG_MODULE("mtransport")

nsresult TransportLayerPrsock::InitInternal() {
  // Get the transport service as a transport service
  nsresult rv;
  stservice_ = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);

  if (!NS_SUCCEEDED(rv)) {
    MOZ_MTLOG(ML_ERROR, "Couldn't get socket transport service");
    return rv;
  }

  return NS_OK;
}

void TransportLayerPrsock::Import(PRFileDesc *fd, nsresult *result) {
  if (state_ != TS_INIT) {
    *result = NS_ERROR_NOT_INITIALIZED;
    return;
  }

  MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Importing()");
  fd_ = fd;
  handler_ = new SocketHandler(this, fd);

  nsresult rv = stservice_->AttachSocket(fd_, handler_);
  if (!NS_SUCCEEDED(rv)) {
    *result = rv;
    return;
  }

  TL_SET_STATE(TS_OPEN);

  *result = NS_OK;
}

int TransportLayerPrsock::SendPacket(const unsigned char *data, size_t len) {
  MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "SendPacket(" << len << ")");
  if (state_ != TS_OPEN) {
    MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Can't send packet on closed interface");
    return TE_INTERNAL;
  }

  int32_t status;
  status = PR_Write(fd_, data, len);
  if (status >= 0) {
    MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Wrote " << len << " bytes");
    return status;
  }

  PRErrorCode err = PR_GetError();
  if (err == PR_WOULD_BLOCK_ERROR) {
    MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Write blocked");
    return TE_WOULDBLOCK;
  }


  MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Write error; channel closed");
  TL_SET_STATE(TS_ERROR);
  return TE_ERROR;
}

void TransportLayerPrsock::OnSocketReady(PRFileDesc *fd, int16_t outflags) {
  if (!(outflags & PR_POLL_READ)) {
    return;
  }

  MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "OnSocketReady(flags=" << outflags << ")");

  unsigned char buf[1600];
  int32_t rv = PR_Read(fd, buf, sizeof(buf));

  if (rv > 0) {
    // Successful read
    MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Read " << rv << " bytes");
    SignalPacketReceived(this, buf, rv);
  } else if (rv == 0) {
    MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Read 0 bytes; channel closed");
    TL_SET_STATE(TS_CLOSED);
  } else {
    PRErrorCode err = PR_GetError();

    if (err != PR_WOULD_BLOCK_ERROR) {
      MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Read error; channel closed");
      TL_SET_STATE(TS_ERROR);
    }
  }
}

NS_IMPL_ISUPPORTS0(TransportLayerPrsock::SocketHandler)
}  // close namespace