2012-10-02 13:04:58 -07:00
|
|
|
/* -*- 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
|
|
|
|
|
2013-05-20 19:11:01 -07:00
|
|
|
#include "logging.h"
|
2012-10-02 13:04:58 -07:00
|
|
|
#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 {
|
|
|
|
|
2012-11-15 17:50:26 -08:00
|
|
|
MOZ_MTLOG_MODULE("mtransport")
|
2012-10-02 13:04:58 -07:00
|
|
|
|
|
|
|
nsresult TransportLayerPrsock::InitInternal() {
|
|
|
|
// Get the transport service as a transport service
|
|
|
|
nsresult rv;
|
|
|
|
stservice_ = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
|
|
|
|
|
|
|
|
if (!NS_SUCCEEDED(rv)) {
|
2013-07-03 11:40:36 -07:00
|
|
|
MOZ_MTLOG(ML_ERROR, "Couldn't get socket transport service");
|
2012-10-02 13:04:58 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransportLayerPrsock::Import(PRFileDesc *fd, nsresult *result) {
|
|
|
|
if (state_ != TS_INIT) {
|
|
|
|
*result = NS_ERROR_NOT_INITIALIZED;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-03 11:40:36 -07:00
|
|
|
MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Importing()");
|
2012-10-02 13:04:58 -07:00
|
|
|
fd_ = fd;
|
|
|
|
handler_ = new SocketHandler(this, fd);
|
|
|
|
|
|
|
|
nsresult rv = stservice_->AttachSocket(fd_, handler_);
|
|
|
|
if (!NS_SUCCEEDED(rv)) {
|
|
|
|
*result = rv;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-23 12:21:24 -08:00
|
|
|
TL_SET_STATE(TS_OPEN);
|
2012-10-02 13:04:58 -07:00
|
|
|
|
|
|
|
*result = NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TransportLayerPrsock::SendPacket(const unsigned char *data, size_t len) {
|
2013-07-03 11:40:36 -07:00
|
|
|
MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "SendPacket(" << len << ")");
|
2012-10-02 13:04:58 -07:00
|
|
|
if (state_ != TS_OPEN) {
|
2013-07-03 11:40:36 -07:00
|
|
|
MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Can't send packet on closed interface");
|
2012-10-02 13:04:58 -07:00
|
|
|
return TE_INTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t status;
|
|
|
|
status = PR_Write(fd_, data, len);
|
|
|
|
if (status >= 0) {
|
2013-07-03 11:40:36 -07:00
|
|
|
MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Wrote " << len << " bytes");
|
2012-10-02 13:04:58 -07:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRErrorCode err = PR_GetError();
|
|
|
|
if (err == PR_WOULD_BLOCK_ERROR) {
|
2013-07-03 11:40:36 -07:00
|
|
|
MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Write blocked");
|
2012-10-02 13:04:58 -07:00
|
|
|
return TE_WOULDBLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-03 11:40:36 -07:00
|
|
|
MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Write error; channel closed");
|
2014-01-23 12:21:24 -08:00
|
|
|
TL_SET_STATE(TS_ERROR);
|
2012-10-02 13:04:58 -07:00
|
|
|
return TE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransportLayerPrsock::OnSocketReady(PRFileDesc *fd, int16_t outflags) {
|
|
|
|
if (!(outflags & PR_POLL_READ)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-03 11:40:36 -07:00
|
|
|
MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "OnSocketReady(flags=" << outflags << ")");
|
2012-10-02 13:04:58 -07:00
|
|
|
|
|
|
|
unsigned char buf[1600];
|
|
|
|
int32_t rv = PR_Read(fd, buf, sizeof(buf));
|
|
|
|
|
|
|
|
if (rv > 0) {
|
|
|
|
// Successful read
|
2013-07-03 11:40:36 -07:00
|
|
|
MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Read " << rv << " bytes");
|
2012-10-02 13:04:58 -07:00
|
|
|
SignalPacketReceived(this, buf, rv);
|
|
|
|
} else if (rv == 0) {
|
2013-07-03 11:40:36 -07:00
|
|
|
MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Read 0 bytes; channel closed");
|
2014-01-23 12:21:24 -08:00
|
|
|
TL_SET_STATE(TS_CLOSED);
|
2012-10-02 13:04:58 -07:00
|
|
|
} else {
|
|
|
|
PRErrorCode err = PR_GetError();
|
|
|
|
|
|
|
|
if (err != PR_WOULD_BLOCK_ERROR) {
|
2013-07-03 11:40:36 -07:00
|
|
|
MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "Read error; channel closed");
|
2014-01-23 12:21:24 -08:00
|
|
|
TL_SET_STATE(TS_ERROR);
|
2012-10-02 13:04:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-18 19:23:32 -07:00
|
|
|
NS_IMPL_ISUPPORTS0(TransportLayerPrsock::SocketHandler)
|
2012-10-02 13:04:58 -07:00
|
|
|
} // close namespace
|