2007-11-14 22:18:48 -05:00
|
|
|
/*
|
|
|
|
* "$Id: pqistreamer.cc,v 1.19 2007-02-18 21:46:50 rmf24 Exp $"
|
|
|
|
*
|
|
|
|
* 3P/PQI network interface for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2004-2006 by Robert Fernie.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2008-07-10 12:29:18 -04:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
2013-10-21 07:00:49 -04:00
|
|
|
#include <time.h>
|
2008-07-10 12:29:18 -04:00
|
|
|
#include "util/rsdebug.h"
|
2012-04-14 18:38:24 -04:00
|
|
|
#include "util/rsstring.h"
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
#include "pqi/pqistreamer.h"
|
2014-01-07 17:51:22 -05:00
|
|
|
#include "rsserver/p3face.h"
|
2008-07-10 12:29:18 -04:00
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
#include "serialiser/rsserial.h"
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
const int pqistreamerzone = 8221;
|
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
const int PQISTREAM_ABS_MAX = 100000000; /* 100 MB/sec (actually per loop) */
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2008-04-03 08:51:28 -04:00
|
|
|
/* This removes the print statements (which hammer pqidebug) */
|
|
|
|
/***
|
|
|
|
#define RSITEM_DEBUG 1
|
2010-11-13 06:46:17 -05:00
|
|
|
#define DEBUG_TRANSFERS 1
|
2011-06-24 17:44:29 -04:00
|
|
|
#define DEBUG_PQISTREAMER 1
|
2008-04-03 08:51:28 -04:00
|
|
|
***/
|
|
|
|
|
2010-11-13 06:46:17 -05:00
|
|
|
#ifdef DEBUG_TRANSFERS
|
2010-11-11 18:59:04 -05:00
|
|
|
#include "util/rsprint.h"
|
|
|
|
#endif
|
|
|
|
|
2008-04-03 08:51:28 -04:00
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
pqistreamer::pqistreamer(RsSerialiser *rss, const RsPeerId& id, BinInterface *bio_in, int bio_flags_in)
|
2013-10-01 06:11:34 -04:00
|
|
|
:PQInterface(id), mStreamerMtx("pqistreamer"),
|
2013-10-01 23:21:04 -04:00
|
|
|
mBio(bio_in), mBio_flags(bio_flags_in), mRsSerialiser(rss),
|
2013-10-01 06:11:34 -04:00
|
|
|
mPkt_wpending(NULL),
|
|
|
|
mTotalRead(0), mTotalSent(0),
|
|
|
|
mCurrRead(0), mCurrSent(0),
|
|
|
|
mAvgReadCount(0), mAvgSentCount(0)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2013-10-01 06:11:34 -04:00
|
|
|
RsStackMutex stack(mStreamerMtx); /**** LOCKED MUTEX ****/
|
|
|
|
|
|
|
|
mAvgLastUpdate = mCurrReadTS = mCurrSentTS = time(NULL);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
/* allocated once */
|
2013-10-01 06:11:34 -04:00
|
|
|
mPkt_rpend_size = getRsPktMaxSize();
|
|
|
|
mPkt_rpending = malloc(mPkt_rpend_size);
|
|
|
|
mReading_state = reading_state_initial ;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-02-15 15:22:34 -05:00
|
|
|
// avoid uninitialized (and random) memory read.
|
2013-10-01 06:11:34 -04:00
|
|
|
memset(mPkt_rpending,0,mPkt_rpend_size) ;
|
2009-02-15 15:22:34 -05:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
// 100 B/s (minimal)
|
|
|
|
setMaxRate(true, 0.1);
|
|
|
|
setMaxRate(false, 0.1);
|
|
|
|
setRate(true, 0);
|
|
|
|
setRate(false, 0);
|
|
|
|
|
2012-04-14 18:38:24 -04:00
|
|
|
pqioutput(PQL_DEBUG_ALL, pqistreamerzone, "pqistreamer::pqistreamer() Initialisation!");
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
if (!bio_in)
|
2009-03-03 14:40:42 -05:00
|
|
|
{
|
2012-04-14 18:38:24 -04:00
|
|
|
pqioutput(PQL_ALERT, pqistreamerzone, "pqistreamer::pqistreamer() NULL bio, FATAL ERROR!");
|
2009-03-03 14:40:42 -05:00
|
|
|
exit(1);
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
mFailed_read_attempts = 0; // reset failed read, as no packet is still read.
|
2009-03-09 10:10:24 -04:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pqistreamer::~pqistreamer()
|
|
|
|
{
|
2013-10-01 06:11:34 -04:00
|
|
|
RsStackMutex stack(mStreamerMtx); /**** LOCKED MUTEX ****/
|
2009-03-11 16:36:51 -04:00
|
|
|
|
2012-04-14 18:38:24 -04:00
|
|
|
pqioutput(PQL_DEBUG_ALL, pqistreamerzone, "pqistreamer::~pqistreamer() Destruction!");
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
if (mBio_flags & BIN_FLAGS_NO_CLOSE)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2012-04-14 18:38:24 -04:00
|
|
|
pqioutput(PQL_DEBUG_ALL, pqistreamerzone, "pqistreamer::~pqistreamer() Not Closing BinInterface!");
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2013-10-01 06:11:34 -04:00
|
|
|
else if (mBio)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2012-04-14 18:38:24 -04:00
|
|
|
pqioutput(PQL_DEBUG_ALL, pqistreamerzone, "pqistreamer::~pqistreamer() Deleting BinInterface!");
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
delete mBio;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2008-04-02 09:55:45 -04:00
|
|
|
/* clean up serialiser */
|
2013-10-01 06:11:34 -04:00
|
|
|
if (mRsSerialiser)
|
|
|
|
delete mRsSerialiser;
|
2008-04-02 09:55:45 -04:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
// clean up outgoing. (cntrl packets)
|
2012-06-23 08:10:41 -04:00
|
|
|
locked_clear_out_queue() ;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
if (mPkt_wpending)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2013-10-01 06:11:34 -04:00
|
|
|
free(mPkt_wpending);
|
|
|
|
mPkt_wpending = NULL;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
free(mPkt_rpending);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2012-06-23 08:10:41 -04:00
|
|
|
// clean up incoming.
|
2013-10-01 06:11:34 -04:00
|
|
|
while(mIncoming.size() > 0)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2013-10-01 06:11:34 -04:00
|
|
|
RsItem *i = mIncoming.front();
|
|
|
|
mIncoming.pop_front();
|
2007-11-14 22:18:48 -05:00
|
|
|
delete i;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get/Send Items.
|
2011-09-04 16:01:30 -04:00
|
|
|
int pqistreamer::SendItem(RsItem *si,uint32_t& out_size)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2008-04-03 08:51:28 -04:00
|
|
|
#ifdef RSITEM_DEBUG
|
2012-04-14 18:38:24 -04:00
|
|
|
{
|
|
|
|
std::string out = "pqistreamer::SendItem():\n";
|
|
|
|
si -> print_string(out);
|
|
|
|
pqioutput(PQL_DEBUG_ALL, pqistreamerzone, out);
|
|
|
|
std::cerr << out;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2008-04-03 08:51:28 -04:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
RsStackMutex stack(mStreamerMtx); /**** LOCKED MUTEX ****/
|
|
|
|
|
|
|
|
return queue_outpqi_locked(si,out_size);
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
RsItem *pqistreamer::GetItem()
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2012-09-17 16:25:22 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2012-04-14 18:38:24 -04:00
|
|
|
pqioutput(PQL_DEBUG_ALL, pqistreamerzone, "pqistreamer::GetItem()");
|
2012-09-17 16:25:22 -04:00
|
|
|
#endif
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
RsStackMutex stack(mStreamerMtx); /**** LOCKED MUTEX ****/
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
if(mIncoming.empty())
|
2011-04-08 16:57:16 -04:00
|
|
|
return NULL;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
RsItem *osr = mIncoming.front() ;
|
|
|
|
mIncoming.pop_front() ;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
return osr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// // PQInterface
|
|
|
|
int pqistreamer::tick()
|
|
|
|
{
|
2013-10-01 06:11:34 -04:00
|
|
|
|
2011-09-04 16:01:30 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
|
|
|
{
|
2013-10-01 23:21:04 -04:00
|
|
|
RsStackMutex stack(mStreamerMtx); /**** LOCKED MUTEX ****/
|
2012-04-14 18:38:24 -04:00
|
|
|
std::string out = "pqistreamer::tick()\n" + PeerId();
|
2013-10-01 06:11:34 -04:00
|
|
|
rs_sprintf_append(out, ": currRead/Sent: %d/%d", mCurrRead, mCurrSent);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2012-04-14 18:38:24 -04:00
|
|
|
pqioutput(PQL_DEBUG_ALL, pqistreamerzone, out);
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2011-09-04 16:01:30 -04:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2013-10-01 23:21:04 -04:00
|
|
|
if (!tick_bio())
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-01 23:21:04 -04:00
|
|
|
tick_recv(0);
|
|
|
|
tick_send(0);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2013-10-01 23:21:04 -04:00
|
|
|
RsStackMutex stack(mStreamerMtx); /**** LOCKED MUTEX ****/
|
2011-09-04 16:01:30 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2007-11-14 22:18:48 -05:00
|
|
|
/* give details of the packets */
|
|
|
|
{
|
2011-09-04 16:01:30 -04:00
|
|
|
std::list<void *>::iterator it;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2012-04-14 18:38:24 -04:00
|
|
|
std::string out = "pqistreamer::tick() Queued Data: for " + PeerId();
|
2007-12-11 20:29:14 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
if (mBio->isactive())
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2012-04-14 18:38:24 -04:00
|
|
|
out += " (active)";
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-14 18:38:24 -04:00
|
|
|
out += " (waiting)";
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2012-04-14 18:38:24 -04:00
|
|
|
out += "\n";
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
{
|
2013-10-01 06:11:34 -04:00
|
|
|
int total = locked_compute_out_pkt_size() ;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
rs_sprintf_append(out, "\t Out Packets [%d] => %d bytes\n", locked_out_queue_size(), total);
|
|
|
|
rs_sprintf_append(out, "\t Incoming [%d]\n", mIncoming.size());
|
2009-03-11 16:36:51 -04:00
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2012-04-14 18:38:24 -04:00
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqistreamerzone, out);
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2011-09-04 16:01:30 -04:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
/* if there is more stuff in the queues */
|
2013-10-01 06:11:34 -04:00
|
|
|
if ((mIncoming.size() > 0) || (locked_out_queue_size() > 0))
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-01 23:21:04 -04:00
|
|
|
int pqistreamer::tick_bio()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mStreamerMtx); /**** LOCKED MUTEX ****/
|
|
|
|
mBio->tick();
|
|
|
|
|
|
|
|
/* short circuit everything is bio isn't active */
|
|
|
|
if (!(mBio->isactive()))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int pqistreamer::tick_recv(uint32_t timeout)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mStreamerMtx); /**** LOCKED MUTEX ****/
|
|
|
|
|
|
|
|
if (mBio->moretoread(timeout))
|
|
|
|
{
|
|
|
|
handleincoming_locked();
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int pqistreamer::tick_send(uint32_t timeout)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(mStreamerMtx); /**** LOCKED MUTEX ****/
|
|
|
|
|
|
|
|
/* short circuit everything is bio isn't active */
|
|
|
|
if (!(mBio->isactive()))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mBio->cansend(timeout))
|
|
|
|
{
|
|
|
|
handleoutgoing_locked();
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
int pqistreamer::status()
|
|
|
|
{
|
2013-10-01 06:11:34 -04:00
|
|
|
RsStackMutex stack(mStreamerMtx); /**** LOCKED MUTEX ****/
|
|
|
|
|
|
|
|
|
2012-09-17 16:25:22 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2012-04-14 18:38:24 -04:00
|
|
|
pqioutput(PQL_DEBUG_ALL, pqistreamerzone, "pqistreamer::status()");
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
if (mBio->isactive())
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2012-04-14 18:38:24 -04:00
|
|
|
std::string out;
|
2013-10-01 06:11:34 -04:00
|
|
|
rs_sprintf(out, "Data in:%d out:%d", mTotalRead, mTotalSent);
|
2012-04-14 18:38:24 -04:00
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqistreamerzone, out);
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2012-09-17 16:25:22 -04:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-23 08:10:41 -04:00
|
|
|
void pqistreamer::locked_storeInOutputQueue(void *ptr,int)
|
|
|
|
{
|
2013-10-01 06:11:34 -04:00
|
|
|
mOutPkts.push_back(ptr);
|
2012-06-23 08:10:41 -04:00
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
//
|
|
|
|
/**************** HANDLE OUTGOING TRANSLATION + TRANSMISSION ******/
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
int pqistreamer::queue_outpqi_locked(RsItem *pqi,uint32_t& pktsize)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2011-09-04 16:01:30 -04:00
|
|
|
pktsize = 0 ;
|
2009-12-13 16:59:26 -05:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
|
|
|
std::cerr << "pqistreamer::queue_outpqi() called." << std::endl;
|
|
|
|
#endif
|
2009-03-11 16:36:51 -04:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
/* decide which type of packet it is */
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
pktsize = mRsSerialiser->size(pqi);
|
2007-12-11 20:29:14 -05:00
|
|
|
void *ptr = malloc(pktsize);
|
2009-02-15 15:22:34 -05:00
|
|
|
|
2009-12-13 16:59:26 -05:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2011-09-04 16:01:30 -04:00
|
|
|
std::cerr << "pqistreamer::queue_outpqi() serializing packet with packet size : " << pktsize << std::endl;
|
2009-12-13 16:59:26 -05:00
|
|
|
#endif
|
2013-10-01 06:11:34 -04:00
|
|
|
if (mRsSerialiser->serialise(pqi, ptr, &pktsize))
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2012-06-23 08:10:41 -04:00
|
|
|
locked_storeInOutputQueue(ptr,pqi->priority_level()) ;
|
2011-09-04 16:01:30 -04:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
if (!(mBio_flags & BIN_FLAGS_NO_DELETE))
|
2008-01-21 04:22:42 -05:00
|
|
|
{
|
|
|
|
delete pqi;
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
return 1;
|
|
|
|
}
|
2007-12-11 20:29:14 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* cleanup serialiser */
|
|
|
|
free(ptr);
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2012-04-14 18:38:24 -04:00
|
|
|
std::string out = "pqistreamer::queue_outpqi() Null Pkt generated!\nCaused By:\n";
|
|
|
|
pqi -> print_string(out);
|
|
|
|
pqioutput(PQL_ALERT, pqistreamerzone, out);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
if (!(mBio_flags & BIN_FLAGS_NO_DELETE))
|
2008-01-21 04:22:42 -05:00
|
|
|
{
|
|
|
|
delete pqi;
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
return 1; // keep error internal.
|
|
|
|
}
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
int pqistreamer::handleincomingitem_locked(RsItem *pqi)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2013-10-01 06:11:34 -04:00
|
|
|
|
2011-09-04 16:01:30 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2013-10-01 06:11:34 -04:00
|
|
|
pqioutput(PQL_DEBUG_ALL, pqistreamerzone, "pqistreamer::handleincomingitem_locked()");
|
2011-09-04 16:01:30 -04:00
|
|
|
#endif
|
2012-01-19 11:23:57 -05:00
|
|
|
// timestamp last received packet.
|
|
|
|
mLastIncomingTs = time(NULL);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
// Use overloaded Contact function
|
2007-12-11 20:29:14 -05:00
|
|
|
pqi -> PeerId(PeerId());
|
2013-10-01 06:11:34 -04:00
|
|
|
mIncoming.push_back(pqi);
|
2007-11-14 22:18:48 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-01-19 11:23:57 -05:00
|
|
|
time_t pqistreamer::getLastIncomingTS()
|
|
|
|
{
|
2013-10-01 06:11:34 -04:00
|
|
|
RsStackMutex stack(mStreamerMtx); /**** LOCKED MUTEX ****/
|
|
|
|
|
2012-01-19 11:23:57 -05:00
|
|
|
return mLastIncomingTs;
|
|
|
|
}
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
int pqistreamer::handleoutgoing_locked()
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2011-09-04 16:01:30 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2013-10-01 06:11:34 -04:00
|
|
|
pqioutput(PQL_DEBUG_ALL, pqistreamerzone, "pqistreamer::handleoutgoing_locked()");
|
2011-09-04 16:01:30 -04:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
int maxbytes = outAllowedBytes_locked();
|
2007-11-14 22:18:48 -05:00
|
|
|
int sentbytes = 0;
|
|
|
|
int len;
|
|
|
|
int ss;
|
2009-03-03 14:40:42 -05:00
|
|
|
// std::cerr << "pqistreamer: maxbytes=" << maxbytes<< std::endl ;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
std::list<void *>::iterator it;
|
|
|
|
|
|
|
|
// if not connection, or cannot send anything... pause.
|
2013-10-01 06:11:34 -04:00
|
|
|
if (!(mBio->isactive()))
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
/* if we are not active - clear anything in the queues. */
|
2012-06-23 08:10:41 -04:00
|
|
|
locked_clear_out_queue() ;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
/* also remove the pending packets */
|
2013-10-01 06:11:34 -04:00
|
|
|
if (mPkt_wpending)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2013-10-01 06:11:34 -04:00
|
|
|
free(mPkt_wpending);
|
|
|
|
mPkt_wpending = NULL;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
outSentBytes_locked(sentbytes);
|
2007-11-14 22:18:48 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2009-03-03 14:40:42 -05:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
// a very simple round robin
|
|
|
|
|
|
|
|
bool sent = true;
|
|
|
|
while(sent) // catch if all items sent.
|
|
|
|
{
|
|
|
|
sent = false;
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
if ((!(mBio->cansend(0))) || (maxbytes < sentbytes))
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2010-11-11 18:59:04 -05:00
|
|
|
|
2010-11-13 06:46:17 -05:00
|
|
|
#ifdef DEBUG_TRANSFERS
|
2010-11-11 18:59:04 -05:00
|
|
|
if (maxbytes < sentbytes)
|
|
|
|
{
|
2013-10-01 06:11:34 -04:00
|
|
|
std::cerr << "pqistreamer::handleoutgoing_locked() Stopped sending sentbytes > maxbytes. Sent " << sentbytes << " bytes ";
|
2010-11-11 18:59:04 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-01 06:11:34 -04:00
|
|
|
std::cerr << "pqistreamer::handleoutgoing_locked() Stopped sending at cansend() is false";
|
2010-11-11 18:59:04 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
outSentBytes_locked(sentbytes);
|
2007-11-14 22:18:48 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// send a out_pkt., else send out_data. unless
|
|
|
|
// there is a pending packet.
|
2013-10-01 06:11:34 -04:00
|
|
|
if (!mPkt_wpending)
|
|
|
|
mPkt_wpending = locked_pop_out_data() ;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
if (mPkt_wpending)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
// write packet.
|
2013-10-01 06:11:34 -04:00
|
|
|
len = getRsItemSize(mPkt_wpending);
|
2009-02-26 09:04:48 -05:00
|
|
|
|
2009-12-13 16:59:26 -05:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
|
|
|
std::cout << "Sending Out Pkt of size " << len << " !" << std::endl;
|
|
|
|
#endif
|
2009-02-26 09:04:48 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
if (len != (ss = mBio->senddata(mPkt_wpending, len)))
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2012-09-17 16:25:22 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2012-04-14 18:38:24 -04:00
|
|
|
std::string out;
|
|
|
|
rs_sprintf(out, "Problems with Send Data! (only %d bytes sent, total pkt size=%d)", ss, len);
|
|
|
|
// std::cerr << out << std::endl ;
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqistreamerzone, out);
|
2012-09-17 16:25:22 -04:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
outSentBytes_locked(sentbytes);
|
2009-03-03 14:40:42 -05:00
|
|
|
// pkt_wpending will kept til next time.
|
2007-11-14 22:18:48 -05:00
|
|
|
// ensuring exactly the same data is written (openSSL requirement).
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-11-13 06:46:17 -05:00
|
|
|
#ifdef DEBUG_TRANSFERS
|
2013-10-01 06:11:34 -04:00
|
|
|
std::cerr << "pqistreamer::handleoutgoing_locked() Sent Packet len: " << len << " @ " << RsUtil::AccurateTimeString();
|
2010-11-11 18:59:04 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
free(mPkt_wpending);
|
|
|
|
mPkt_wpending = NULL;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
sentbytes += len;
|
|
|
|
sent = true;
|
|
|
|
}
|
|
|
|
}
|
2013-10-01 06:11:34 -04:00
|
|
|
outSentBytes_locked(sentbytes);
|
2007-11-14 22:18:48 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Handles reading from input stream.
|
|
|
|
*/
|
2013-10-01 06:11:34 -04:00
|
|
|
int pqistreamer::handleincoming_locked()
|
2009-03-03 14:40:42 -05:00
|
|
|
{
|
|
|
|
int readbytes = 0;
|
2009-03-20 19:37:06 -04:00
|
|
|
static const int max_failed_read_attempts = 2000 ;
|
2009-03-03 14:40:42 -05:00
|
|
|
|
2011-09-04 16:01:30 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2013-10-01 06:11:34 -04:00
|
|
|
pqioutput(PQL_DEBUG_ALL, pqistreamerzone, "pqistreamer::handleincoming_locked()");
|
2011-09-04 16:01:30 -04:00
|
|
|
#endif
|
2009-03-03 14:40:42 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
if(!(mBio->isactive()))
|
2009-03-03 14:40:42 -05:00
|
|
|
{
|
2013-10-01 06:11:34 -04:00
|
|
|
mReading_state = reading_state_initial ;
|
|
|
|
inReadBytes_locked(readbytes);
|
2009-03-03 14:40:42 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// enough space to read any packet.
|
2013-10-01 06:11:34 -04:00
|
|
|
int maxlen = mPkt_rpend_size;
|
|
|
|
void *block = mPkt_rpending;
|
2009-03-03 14:40:42 -05:00
|
|
|
|
|
|
|
// initial read size: basic packet.
|
|
|
|
int blen = getRsPktBaseSize();
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
int maxin = inAllowedBytes_locked();
|
2009-03-03 14:40:42 -05:00
|
|
|
|
2009-07-21 16:14:31 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2013-10-01 06:11:34 -04:00
|
|
|
std::cerr << "[" << (void*)pthread_self() << "] " << "reading state = " << mReading_state << std::endl ;
|
2009-07-21 16:14:31 -04:00
|
|
|
#endif
|
2013-10-01 06:11:34 -04:00
|
|
|
switch(mReading_state)
|
2009-03-03 14:40:42 -05:00
|
|
|
{
|
2009-03-09 15:38:00 -04:00
|
|
|
case reading_state_initial: /*std::cerr << "jumping to start" << std::endl; */ goto start_packet_read ;
|
|
|
|
case reading_state_packet_started: /*std::cerr << "jumping to middle" << std::endl;*/ goto continue_packet ;
|
2009-03-03 14:40:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
start_packet_read:
|
|
|
|
{ // scope to ensure variable visibility
|
|
|
|
// read the basic block (minimum packet size)
|
|
|
|
int tmplen;
|
2009-07-21 16:14:31 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
|
|
|
std::cerr << "[" << (void*)pthread_self() << "] " << "starting packet" << std::endl ;
|
|
|
|
#endif
|
2009-03-09 15:38:00 -04:00
|
|
|
memset(block,0,blen) ; // reset the block, to avoid uninitialized memory reads.
|
2009-03-03 14:40:42 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
if (blen != (tmplen = mBio->readdata(block, blen)))
|
2009-03-03 14:40:42 -05:00
|
|
|
{
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqistreamerzone, "pqistreamer::handleincoming() Didn't read BasePkt!");
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
inReadBytes_locked(readbytes);
|
2009-03-09 15:38:00 -04:00
|
|
|
|
2009-03-03 14:40:42 -05:00
|
|
|
// error.... (either blocked or failure)
|
|
|
|
if (tmplen == 0)
|
|
|
|
{
|
2012-09-17 16:25:22 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2009-03-03 14:40:42 -05:00
|
|
|
// most likely blocked!
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqistreamerzone, "pqistreamer::handleincoming() read blocked");
|
2009-07-21 16:14:31 -04:00
|
|
|
std::cerr << "[" << (void*)pthread_self() << "] " << "given up 1" << std::endl ;
|
|
|
|
#endif
|
2009-03-03 14:40:42 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (tmplen < 0)
|
|
|
|
{
|
|
|
|
// Most likely it is that the packet is pending but could not be read by pqissl because of stream flow.
|
|
|
|
// So we return without an error, and leave the machine state in 'start_read'.
|
|
|
|
//
|
2009-03-20 19:37:06 -04:00
|
|
|
//pqioutput(PQL_WARNING, pqistreamerzone, "pqistreamer::handleincoming() Error in bio read");
|
2009-07-21 16:14:31 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2013-10-01 06:11:34 -04:00
|
|
|
std::cerr << "[" << (void*)pthread_self() << "] " << "given up 2, state = " << mReading_state << std::endl ;
|
2009-07-21 16:14:31 -04:00
|
|
|
#endif
|
2009-03-03 14:40:42 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else // tmplen > 0
|
|
|
|
{
|
|
|
|
// strange case....This should never happen as partial reads are handled by pqissl below.
|
2012-09-17 16:25:22 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2012-04-14 18:38:24 -04:00
|
|
|
std::string out = "pqistreamer::handleincoming() Incomplete ";
|
|
|
|
rs_sprintf_append(out, "(Strange) read of %d bytes", tmplen);
|
|
|
|
pqioutput(PQL_ALERT, pqistreamerzone, out);
|
2012-09-17 16:25:22 -04:00
|
|
|
|
2009-07-21 16:14:31 -04:00
|
|
|
std::cerr << "[" << (void*)pthread_self() << "] " << "given up 3" << std::endl ;
|
|
|
|
#endif
|
2009-03-03 14:40:42 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2009-07-21 16:14:31 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2012-04-14 18:38:24 -04:00
|
|
|
std::cerr << "[" << (void*)pthread_self() << "] " << "block 0 : " << (int)(((unsigned char*)block)[0]) << " " << (int)(((unsigned char*)block)[1]) << " " << (int)(((unsigned char*)block)[2]) << " "
|
2009-07-21 16:14:31 -04:00
|
|
|
<< (int)(((unsigned char*)block)[3]) << " "
|
2012-04-14 18:38:24 -04:00
|
|
|
<< (int)(((unsigned char*)block)[4]) << " "
|
|
|
|
<< (int)(((unsigned char*)block)[5]) << " "
|
|
|
|
<< (int)(((unsigned char*)block)[6]) << " "
|
2009-07-21 16:14:31 -04:00
|
|
|
<< (int)(((unsigned char*)block)[7]) << " " << std::endl ;
|
|
|
|
#endif
|
2009-03-03 14:40:42 -05:00
|
|
|
|
|
|
|
readbytes += blen;
|
2013-10-01 06:11:34 -04:00
|
|
|
mReading_state = reading_state_packet_started ;
|
|
|
|
mFailed_read_attempts = 0 ; // reset failed read, as the packet has been totally read.
|
2009-03-03 14:40:42 -05:00
|
|
|
}
|
|
|
|
continue_packet:
|
|
|
|
{
|
|
|
|
// workout how much more to read.
|
|
|
|
int extralen = getRsItemSize(block) - blen;
|
|
|
|
|
2009-07-21 16:14:31 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2009-12-13 16:59:26 -05:00
|
|
|
std::cerr << "[" << (void*)pthread_self() << "] " << "continuing packet getRsItemSize(block) = " << getRsItemSize(block) << std::endl ;
|
|
|
|
std::cerr << "[" << (void*)pthread_self() << "] " << "continuing packet extralen = " << extralen << std::endl ;
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
std::cerr << "[" << (void*)pthread_self() << "] " << "continuing packet state=" << mReading_state << std::endl ;
|
2009-07-21 16:14:31 -04:00
|
|
|
std::cerr << "[" << (void*)pthread_self() << "] " << "block 1 : " << (int)(((unsigned char*)block)[0]) << " " << (int)(((unsigned char*)block)[1]) << " " << (int)(((unsigned char*)block)[2]) << " " << (int)(((unsigned char*)block)[3]) << " "
|
2012-04-14 18:38:24 -04:00
|
|
|
<< (int)(((unsigned char*)block)[4]) << " "
|
|
|
|
<< (int)(((unsigned char*)block)[5]) << " "
|
|
|
|
<< (int)(((unsigned char*)block)[6]) << " "
|
2009-07-21 16:14:31 -04:00
|
|
|
<< (int)(((unsigned char*)block)[7]) << " " << std::endl ;
|
|
|
|
#endif
|
2009-03-03 14:40:42 -05:00
|
|
|
if (extralen > maxlen - blen)
|
|
|
|
{
|
|
|
|
pqioutput(PQL_ALERT, pqistreamerzone, "ERROR: Read Packet too Big!");
|
|
|
|
|
2014-01-07 17:51:22 -05:00
|
|
|
p3Notify *notify = RsServer::notify();
|
2009-03-03 14:40:42 -05:00
|
|
|
if (notify)
|
|
|
|
{
|
|
|
|
std::string title =
|
|
|
|
"Warning: Bad Packet Read";
|
|
|
|
|
2012-04-14 18:38:24 -04:00
|
|
|
std::string msg;
|
|
|
|
msg = " **** WARNING **** \n";
|
|
|
|
msg += "Retroshare has caught a BAD Packet Read";
|
|
|
|
msg += "\n";
|
|
|
|
msg += "This is normally caused by connecting to an";
|
|
|
|
msg += " OLD version of Retroshare";
|
|
|
|
msg += "\n";
|
|
|
|
rs_sprintf_append(msg, "(M:%d B:%d E:%d)\n", maxlen, blen, extralen);
|
|
|
|
msg += "\n";
|
|
|
|
rs_sprintf_append(msg, "block = %d %d %d %d %d %d %d %d\n",
|
|
|
|
(int)(((unsigned char*)block)[0]),
|
|
|
|
(int)(((unsigned char*)block)[1]),
|
|
|
|
(int)(((unsigned char*)block)[2]),
|
|
|
|
(int)(((unsigned char*)block)[3]),
|
|
|
|
(int)(((unsigned char*)block)[4]),
|
|
|
|
(int)(((unsigned char*)block)[5]),
|
|
|
|
(int)(((unsigned char*)block)[6]),
|
|
|
|
(int)(((unsigned char*)block)[7])) ;
|
|
|
|
msg += "\n";
|
|
|
|
msg += "Please get your friends to upgrade to the latest version";
|
|
|
|
msg += "\n";
|
|
|
|
msg += "\n";
|
|
|
|
msg += "If you are sure the error was not caused by an old version";
|
|
|
|
msg += "\n";
|
|
|
|
msg += "Please report the problem to Retroshare's developers";
|
|
|
|
msg += "\n";
|
|
|
|
|
2009-05-05 09:35:36 -04:00
|
|
|
notify->AddLogMessage(0, RS_SYS_WARNING, title, msg);
|
2011-07-29 11:20:40 -04:00
|
|
|
|
|
|
|
std::cerr << "pqistreamer::handle_incoming() ERROR: Read Packet too Big" << std::endl;
|
2012-04-14 18:38:24 -04:00
|
|
|
std::cerr << msg;
|
2011-07-29 11:20:40 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2009-03-03 14:40:42 -05:00
|
|
|
}
|
2013-10-01 06:11:34 -04:00
|
|
|
mBio->close();
|
|
|
|
mReading_state = reading_state_initial ; // restart at state 1.
|
|
|
|
mFailed_read_attempts = 0 ;
|
2009-03-03 14:40:42 -05:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
// Used to exit now! exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (extralen > 0)
|
|
|
|
{
|
|
|
|
void *extradata = (void *) (((char *) block) + blen);
|
|
|
|
int tmplen ;
|
2011-06-24 17:44:29 -04:00
|
|
|
|
|
|
|
// Don't reset the block now! If pqissl is in the middle of a multiple-chunk
|
|
|
|
// packet (larger than 16384 bytes), and pqistreamer jumped directly yo
|
|
|
|
// continue_packet:, then readdata is going to write after the beginning of
|
|
|
|
// extradata, yet not exactly at start -> the start of the packet would be wiped out.
|
|
|
|
//
|
|
|
|
// so, don't do that:
|
|
|
|
// memset( extradata,0,extralen ) ;
|
2009-03-09 10:10:24 -04:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
if (extralen != (tmplen = mBio->readdata(extradata, extralen)))
|
2009-03-09 10:10:24 -04:00
|
|
|
{
|
2009-07-21 16:57:18 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2009-03-09 10:10:24 -04:00
|
|
|
if(tmplen > 0)
|
2009-07-21 16:14:31 -04:00
|
|
|
std::cerr << "[" << (void*)pthread_self() << "] " << "Incomplete packet read ! This is a real problem ;-)" << std::endl ;
|
2009-07-21 16:57:18 -04:00
|
|
|
#endif
|
2009-03-09 10:10:24 -04:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
if(++mFailed_read_attempts > max_failed_read_attempts)
|
2009-03-03 14:40:42 -05:00
|
|
|
{
|
2012-04-14 18:38:24 -04:00
|
|
|
std::string out;
|
|
|
|
rs_sprintf(out, "Error Completing Read (read %d/%d)", tmplen, extralen);
|
|
|
|
std::cerr << out << std::endl ;
|
|
|
|
pqioutput(PQL_ALERT, pqistreamerzone, out);
|
2009-03-03 14:40:42 -05:00
|
|
|
|
2014-01-07 17:51:22 -05:00
|
|
|
p3Notify *notify = RsServer::notify();
|
2009-03-03 14:40:42 -05:00
|
|
|
if (notify)
|
|
|
|
{
|
|
|
|
std::string title = "Warning: Error Completing Read";
|
|
|
|
|
2012-04-14 18:38:24 -04:00
|
|
|
std::string msgout;
|
|
|
|
msgout = " **** WARNING **** \n";
|
|
|
|
msgout += "Retroshare has experienced an unexpected Read ERROR";
|
|
|
|
msgout += "\n";
|
|
|
|
rs_sprintf_append(msgout, "(M:%d B:%d E:%d R:%d)\n", maxlen, blen, extralen, tmplen);
|
|
|
|
msgout += "\n";
|
|
|
|
msgout += "Note: this error might as well happen (rarely) when a peer disconnects in between a transmission of a large packet.\n";
|
|
|
|
msgout += "If it happens manny time, please contact the developers, and send them these numbers:";
|
|
|
|
msgout += "\n";
|
|
|
|
|
|
|
|
rs_sprintf_append(msgout, "block = %d %d %d %d %d %d %d %d\n",
|
|
|
|
(int)(((unsigned char*)block)[0]),
|
|
|
|
(int)(((unsigned char*)block)[1]),
|
|
|
|
(int)(((unsigned char*)block)[2]),
|
|
|
|
(int)(((unsigned char*)block)[3]),
|
|
|
|
(int)(((unsigned char*)block)[4]),
|
|
|
|
(int)(((unsigned char*)block)[5]),
|
|
|
|
(int)(((unsigned char*)block)[6]),
|
|
|
|
(int)(((unsigned char*)block)[7]));
|
2011-02-23 14:32:51 -05:00
|
|
|
|
|
|
|
//notify->AddSysMessage(0, RS_SYS_WARNING, title, msgout.str());
|
|
|
|
|
2012-04-14 18:38:24 -04:00
|
|
|
std::cerr << msgout << std::endl;
|
2009-03-03 14:40:42 -05:00
|
|
|
}
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
mBio->close();
|
|
|
|
mReading_state = reading_state_initial ; // restart at state 1.
|
|
|
|
mFailed_read_attempts = 0 ;
|
2009-03-03 14:40:42 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
2009-03-09 15:38:00 -04:00
|
|
|
{
|
2009-07-21 16:14:31 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2013-10-01 06:11:34 -04:00
|
|
|
std::cerr << "[" << (void*)pthread_self() << "] " << "given up 5, state = " << mReading_state << std::endl ;
|
2009-07-21 16:14:31 -04:00
|
|
|
#endif
|
2009-03-03 14:40:42 -05:00
|
|
|
return 0 ; // this is just a SSL_WANT_READ error. Don't panic, we'll re-try the read soon.
|
|
|
|
// we assume readdata() returned either -1 or the complete read size.
|
2009-03-09 15:38:00 -04:00
|
|
|
}
|
2009-03-09 10:10:24 -04:00
|
|
|
}
|
2009-07-21 16:14:31 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2013-10-01 06:11:34 -04:00
|
|
|
std::cerr << "[" << (void*)pthread_self() << "] " << "continuing packet state=" << mReading_state << std::endl ;
|
2009-07-21 16:14:31 -04:00
|
|
|
std::cerr << "[" << (void*)pthread_self() << "] " << "block 2 : " << (int)(((unsigned char*)extradata)[0]) << " " << (int)(((unsigned char*)extradata)[1]) << " " << (int)(((unsigned char*)extradata)[2]) << " " << (int)(((unsigned char*)extradata)[3]) << " "
|
2012-04-14 18:38:24 -04:00
|
|
|
<< (int)(((unsigned char*)extradata)[4]) << " "
|
|
|
|
<< (int)(((unsigned char*)extradata)[5]) << " "
|
|
|
|
<< (int)(((unsigned char*)extradata)[6]) << " "
|
2009-07-21 16:14:31 -04:00
|
|
|
<< (int)(((unsigned char*)extradata)[7]) << " " << std::endl ;
|
|
|
|
#endif
|
2009-03-03 14:40:42 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
mFailed_read_attempts = 0 ;
|
2009-03-03 14:40:42 -05:00
|
|
|
readbytes += extralen;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create packet, based on header.
|
2012-09-17 16:25:22 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2009-03-03 14:40:42 -05:00
|
|
|
{
|
2012-04-14 18:38:24 -04:00
|
|
|
std::string out;
|
|
|
|
rs_sprintf(out, "Read Data Block -> Incoming Pkt(%d)", blen + extralen);
|
|
|
|
//std::cerr << out ;
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqistreamerzone, out);
|
2009-03-03 14:40:42 -05:00
|
|
|
}
|
2012-09-17 16:25:22 -04:00
|
|
|
#endif
|
2009-03-03 14:40:42 -05:00
|
|
|
|
2009-12-13 16:59:26 -05:00
|
|
|
// std::cerr << "Deserializing packet of size " << pktlen <<std::endl ;
|
2009-03-03 14:40:42 -05:00
|
|
|
|
|
|
|
uint32_t pktlen = blen+extralen ;
|
2009-07-21 16:14:31 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
|
|
|
std::cerr << "[" << (void*)pthread_self() << "] " << "deserializing. Size=" << pktlen << std::endl ;
|
|
|
|
#endif
|
2009-03-09 15:38:00 -04:00
|
|
|
|
2009-07-20 15:28:16 -04:00
|
|
|
// if(pktlen == 17306)
|
|
|
|
// {
|
2011-04-03 19:11:38 -04:00
|
|
|
// FILE *f = RsDirUtil::rs_fopen("dbug.packet.bin","w");
|
2009-07-20 15:28:16 -04:00
|
|
|
// fwrite(block,pktlen,1,f) ;
|
|
|
|
// fclose(f) ;
|
|
|
|
// exit(-1) ;
|
|
|
|
// }
|
2013-10-01 06:11:34 -04:00
|
|
|
RsItem *pkt = mRsSerialiser->deserialise(block, &pktlen);
|
2009-03-03 14:40:42 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
if ((pkt != NULL) && (0 < handleincomingitem_locked(pkt)))
|
2012-09-17 16:25:22 -04:00
|
|
|
{
|
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2009-03-03 14:40:42 -05:00
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqistreamerzone, "Successfully Read a Packet!");
|
2012-09-17 16:25:22 -04:00
|
|
|
#endif
|
|
|
|
}
|
2009-03-03 14:40:42 -05:00
|
|
|
else
|
2012-09-17 16:25:22 -04:00
|
|
|
{
|
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2009-03-03 14:40:42 -05:00
|
|
|
pqioutput(PQL_ALERT, pqistreamerzone, "Failed to handle Packet!");
|
2012-09-17 16:25:22 -04:00
|
|
|
#endif
|
|
|
|
}
|
2009-03-03 14:40:42 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
mReading_state = reading_state_initial ; // restart at state 1.
|
|
|
|
mFailed_read_attempts = 0 ; // reset failed read, as the packet has been totally read.
|
2009-03-03 14:40:42 -05:00
|
|
|
}
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
if(maxin > readbytes && mBio->moretoread(0))
|
2009-03-03 14:40:42 -05:00
|
|
|
goto start_packet_read ;
|
|
|
|
|
2010-11-13 06:46:17 -05:00
|
|
|
#ifdef DEBUG_TRANSFERS
|
2010-11-11 18:59:04 -05:00
|
|
|
if (readbytes >= maxin)
|
|
|
|
{
|
|
|
|
std::cerr << "pqistreamer::handleincoming() Stopped reading as readbytes >= maxin. Read " << readbytes << " bytes ";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
inReadBytes_locked(readbytes);
|
2009-03-03 14:40:42 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
|
|
|
|
/* BandWidth Management Assistance */
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
float pqistreamer::outTimeSlice_locked()
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2012-09-17 16:25:22 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2012-04-14 18:38:24 -04:00
|
|
|
pqioutput(PQL_DEBUG_ALL, pqistreamerzone, "pqistreamer::outTimeSlice()");
|
2012-09-17 16:25:22 -04:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
//fixme("pqistreamer::outTimeSlice()", 1);
|
2007-11-14 22:18:48 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// very simple.....
|
2013-10-01 06:11:34 -04:00
|
|
|
int pqistreamer::outAllowedBytes_locked()
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
int t = time(NULL); // get current timestep.
|
|
|
|
|
|
|
|
/* allow a lot if not bandwidthLimited */
|
2013-10-01 06:11:34 -04:00
|
|
|
if (!mBio->bandwidthLimited())
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2013-10-01 06:11:34 -04:00
|
|
|
mCurrSent = 0;
|
|
|
|
mCurrSentTS = t;
|
2007-11-14 22:18:48 -05:00
|
|
|
return PQISTREAM_ABS_MAX;
|
|
|
|
}
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
int dt = t - mCurrSentTS;
|
2007-11-14 22:18:48 -05:00
|
|
|
// limiter -> for when currSentTs -> 0.
|
|
|
|
if (dt > 5)
|
|
|
|
dt = 5;
|
|
|
|
|
|
|
|
int maxout = (int) (getMaxRate(false) * 1000.0);
|
2013-10-01 06:11:34 -04:00
|
|
|
mCurrSent -= dt * maxout;
|
|
|
|
if (mCurrSent < 0)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2013-10-01 06:11:34 -04:00
|
|
|
mCurrSent = 0;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
mCurrSentTS = t;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2012-09-17 16:25:22 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2012-04-14 18:38:24 -04:00
|
|
|
{
|
|
|
|
std::string out;
|
2013-10-01 06:11:34 -04:00
|
|
|
rs_sprintf(out, "pqistreamer::outAllowedBytes() is %d/%d", maxout - mCurrSent, maxout);
|
2012-04-14 18:38:24 -04:00
|
|
|
pqioutput(PQL_DEBUG_ALL, pqistreamerzone, out);
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2012-09-17 16:25:22 -04:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
return maxout - mCurrSent;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
int pqistreamer::inAllowedBytes_locked()
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
int t = time(NULL); // get current timestep.
|
|
|
|
|
|
|
|
/* allow a lot if not bandwidthLimited */
|
2013-10-01 06:11:34 -04:00
|
|
|
if (!mBio->bandwidthLimited())
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2013-10-01 06:11:34 -04:00
|
|
|
mCurrReadTS = t;
|
|
|
|
mCurrRead = 0;
|
2007-11-14 22:18:48 -05:00
|
|
|
return PQISTREAM_ABS_MAX;
|
|
|
|
}
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
int dt = t - mCurrReadTS;
|
2007-11-14 22:18:48 -05:00
|
|
|
// limiter -> for when currReadTs -> 0.
|
|
|
|
if (dt > 5)
|
|
|
|
dt = 5;
|
|
|
|
|
|
|
|
int maxin = (int) (getMaxRate(true) * 1000.0);
|
2013-10-01 06:11:34 -04:00
|
|
|
mCurrRead -= dt * maxin;
|
|
|
|
if (mCurrRead < 0)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2013-10-01 06:11:34 -04:00
|
|
|
mCurrRead = 0;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
mCurrReadTS = t;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2012-09-17 16:25:22 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2012-04-14 18:38:24 -04:00
|
|
|
{
|
|
|
|
std::string out;
|
2013-10-01 06:11:34 -04:00
|
|
|
rs_sprintf(out, "pqistreamer::inAllowedBytes() is %d/%d", maxin - mCurrRead, maxin);
|
2012-04-14 18:38:24 -04:00
|
|
|
pqioutput(PQL_DEBUG_ALL, pqistreamerzone, out);
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2012-09-17 16:25:22 -04:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
return maxin - mCurrRead;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const float AVG_PERIOD = 5; // sec
|
|
|
|
static const float AVG_FRAC = 0.8; // for low pass filter.
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
void pqistreamer::outSentBytes_locked(int outb)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2012-09-17 16:25:22 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2012-04-14 18:38:24 -04:00
|
|
|
{
|
|
|
|
std::string out;
|
|
|
|
rs_sprintf(out, "pqistreamer::outSentBytes(): %d@%gkB/s", outb, getRate(false));
|
|
|
|
pqioutput(PQL_DEBUG_ALL, pqistreamerzone, out);
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2012-09-17 16:25:22 -04:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2012-06-21 19:23:46 -04:00
|
|
|
/*** One theory for the massive delays - is that the queue here is filling up ****/
|
|
|
|
//#define DEBUG_LAG 1
|
|
|
|
#ifdef DEBUG_LAG
|
|
|
|
|
|
|
|
#define MIN_PKTS_FOR_MSG 100
|
2012-06-23 08:10:41 -04:00
|
|
|
if (out_queue_size() > MIN_PKTS_FOR_MSG)
|
2012-06-21 19:23:46 -04:00
|
|
|
{
|
|
|
|
std::cerr << "pqistreamer::outSentBytes() for: " << PeerId();
|
2012-06-23 08:10:41 -04:00
|
|
|
std::cerr << " End of Write and still " << out_queue_size() << " pkts left";
|
2012-06-21 19:23:46 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
mTotalSent += outb;
|
|
|
|
mCurrSent += outb;
|
|
|
|
mAvgSentCount += outb;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
int t = time(NULL); // get current timestep.
|
2013-10-01 06:11:34 -04:00
|
|
|
if (t - mAvgLastUpdate > AVG_PERIOD)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
float avgReadpSec = getRate(true);
|
|
|
|
float avgSentpSec = getRate(false);
|
|
|
|
|
|
|
|
avgReadpSec *= AVG_FRAC;
|
2013-10-01 06:11:34 -04:00
|
|
|
avgReadpSec += (1.0 - AVG_FRAC) * mAvgReadCount /
|
|
|
|
(1000.0 * (t - mAvgLastUpdate));
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
avgSentpSec *= AVG_FRAC;
|
2013-10-01 06:11:34 -04:00
|
|
|
avgSentpSec += (1.0 - AVG_FRAC) * mAvgSentCount /
|
|
|
|
(1000.0 * (t - mAvgLastUpdate));
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
|
|
|
|
/* pretend our rate is zero if we are
|
|
|
|
* not bandwidthLimited().
|
|
|
|
*/
|
2013-10-01 06:11:34 -04:00
|
|
|
if (mBio->bandwidthLimited())
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
setRate(true, avgReadpSec);
|
|
|
|
setRate(false, avgSentpSec);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setRate(true, 0);
|
|
|
|
setRate(false, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
mAvgLastUpdate = t;
|
|
|
|
mAvgReadCount = 0;
|
|
|
|
mAvgSentCount = 0;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
void pqistreamer::inReadBytes_locked(int inb)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2012-09-17 16:25:22 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2012-04-14 18:38:24 -04:00
|
|
|
{
|
|
|
|
std::string out;
|
|
|
|
rs_sprintf(out, "pqistreamer::inReadBytes(): %d@%gkB/s", inb, getRate(true));
|
|
|
|
pqioutput(PQL_DEBUG_ALL, pqistreamerzone, out);
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2012-09-17 16:25:22 -04:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
mTotalRead += inb;
|
|
|
|
mCurrRead += inb;
|
|
|
|
mAvgReadCount += inb;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-21 21:35:32 -04:00
|
|
|
int pqistreamer::getQueueSize(bool in)
|
|
|
|
{
|
2013-10-01 06:11:34 -04:00
|
|
|
RsStackMutex stack(mStreamerMtx); /**** LOCKED MUTEX ****/
|
|
|
|
|
2012-06-21 21:35:32 -04:00
|
|
|
if (in)
|
2013-10-01 06:11:34 -04:00
|
|
|
return mIncoming.size();
|
|
|
|
return locked_out_queue_size();
|
2012-06-21 21:35:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void pqistreamer::getRates(RsBwRates &rates)
|
|
|
|
{
|
|
|
|
RateInterface::getRates(rates);
|
2013-10-01 06:11:34 -04:00
|
|
|
|
|
|
|
RsStackMutex stack(mStreamerMtx); /**** LOCKED MUTEX ****/
|
|
|
|
|
|
|
|
rates.mQueueIn = mIncoming.size();
|
|
|
|
rates.mQueueOut = locked_out_queue_size();
|
2012-06-21 21:35:32 -04:00
|
|
|
}
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
int pqistreamer::locked_out_queue_size() const
|
2012-06-23 08:10:41 -04:00
|
|
|
{
|
|
|
|
// Warning: because out_pkt is a list, calling size
|
|
|
|
// is O(n) ! Makign algorithms pretty inefficient. We should record how many
|
|
|
|
// items get stored and discarded to have a proper size value at any time
|
|
|
|
//
|
2013-10-01 06:11:34 -04:00
|
|
|
return mOutPkts.size() ;
|
2012-06-23 08:10:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void pqistreamer::locked_clear_out_queue()
|
|
|
|
{
|
2013-10-01 06:11:34 -04:00
|
|
|
for(std::list<void*>::iterator it = mOutPkts.begin(); it != mOutPkts.end(); )
|
2012-06-23 08:10:41 -04:00
|
|
|
{
|
|
|
|
free(*it);
|
2013-10-01 06:11:34 -04:00
|
|
|
it = mOutPkts.erase(it);
|
2012-06-23 08:10:41 -04:00
|
|
|
#ifdef DEBUG_PQISTREAMER
|
2013-10-01 06:11:34 -04:00
|
|
|
std::string out = "pqistreamer::locked_clear_out_queue() Not active -> Clearing Pkt!";
|
|
|
|
std::cerr << out << std::endl;
|
2012-06-23 08:10:41 -04:00
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqistreamerzone, out);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int pqistreamer::locked_compute_out_pkt_size() const
|
|
|
|
{
|
|
|
|
int total = 0 ;
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
for(std::list<void*>::const_iterator it = mOutPkts.begin(); it != mOutPkts.end(); ++it)
|
2012-06-23 08:10:41 -04:00
|
|
|
total += getRsItemSize(*it);
|
2012-06-21 21:35:32 -04:00
|
|
|
|
2012-06-23 08:10:41 -04:00
|
|
|
return total ;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *pqistreamer::locked_pop_out_data()
|
|
|
|
{
|
|
|
|
void *res = NULL ;
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
if (!mOutPkts.empty())
|
2012-06-23 08:10:41 -04:00
|
|
|
{
|
2013-10-01 06:11:34 -04:00
|
|
|
res = *(mOutPkts.begin());
|
|
|
|
mOutPkts.pop_front();
|
2012-06-23 08:10:41 -04:00
|
|
|
#ifdef DEBUG_TRANSFERS
|
2013-10-01 06:11:34 -04:00
|
|
|
std::cerr << "pqistreamer::locked_pop_out_data() getting next pkt from mOutPkts queue";
|
2012-06-23 08:10:41 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return res ;
|
|
|
|
}
|