2013-10-01 23:21:04 -04:00
|
|
|
/*
|
|
|
|
* pqithreadstreamer.cc
|
|
|
|
*
|
|
|
|
* 3P/PQI network interface for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2004-2013 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.1 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".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "pqi/pqithreadstreamer.h"
|
2014-01-19 18:38:48 -05:00
|
|
|
#include <unistd.h>
|
2013-10-01 23:21:04 -04:00
|
|
|
|
|
|
|
#define DEFAULT_STREAMER_TIMEOUT 10000 // 10 ms.
|
|
|
|
#define DEFAULT_STREAMER_SLEEP 1000 // 1 ms.
|
|
|
|
#define DEFAULT_STREAMER_IDLE_SLEEP 1000000 // 1 sec
|
|
|
|
|
2015-04-21 16:09:20 -04:00
|
|
|
//#define PQISTREAMER_DEBUG
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
pqithreadstreamer::pqithreadstreamer(PQInterface *parent, RsSerialiser *rss, const RsPeerId& id, BinInterface *bio_in, int bio_flags_in)
|
2016-06-02 08:44:33 -04:00
|
|
|
:pqistreamer(rss, id, bio_in, bio_flags_in), mParent(parent), mTimeout(0), mThreadMutex("pqithreadstreamer")
|
2013-10-01 23:21:04 -04:00
|
|
|
{
|
2015-05-22 14:16:49 -04:00
|
|
|
mTimeout = DEFAULT_STREAMER_TIMEOUT;
|
2015-03-10 17:16:41 -04:00
|
|
|
mSleepPeriod = DEFAULT_STREAMER_SLEEP;
|
2013-10-01 23:21:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool pqithreadstreamer::RecvItem(RsItem *item)
|
|
|
|
{
|
|
|
|
return mParent->RecvItem(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
int pqithreadstreamer::tick()
|
|
|
|
{
|
2014-12-08 17:17:06 -05:00
|
|
|
RsStackMutex stack(mThreadMutex);
|
|
|
|
tick_bio();
|
2013-10-01 23:21:04 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-22 16:54:38 -04:00
|
|
|
void pqithreadstreamer::data_tick()
|
2013-10-01 23:21:04 -04:00
|
|
|
{
|
2015-05-22 14:16:49 -04:00
|
|
|
uint32_t recv_timeout = 0;
|
|
|
|
uint32_t sleep_period = 0;
|
|
|
|
bool isactive = false;
|
2014-12-16 16:46:07 -05:00
|
|
|
{
|
2015-05-22 14:16:49 -04:00
|
|
|
RsStackMutex stack(mStreamerMtx);
|
|
|
|
recv_timeout = mTimeout;
|
|
|
|
sleep_period = mSleepPeriod;
|
|
|
|
isactive = mBio->isactive();
|
2014-12-16 16:46:07 -05:00
|
|
|
}
|
2016-04-09 14:48:05 -04:00
|
|
|
|
|
|
|
updateRates() ;
|
2013-10-01 23:21:04 -04:00
|
|
|
|
2015-05-22 14:16:49 -04:00
|
|
|
if (!isactive)
|
2015-03-05 15:53:32 -05:00
|
|
|
{
|
2015-05-22 14:16:49 -04:00
|
|
|
usleep(DEFAULT_STREAMER_IDLE_SLEEP);
|
2015-05-22 16:54:38 -04:00
|
|
|
return ;
|
2015-03-10 17:16:41 -04:00
|
|
|
}
|
2015-03-05 15:53:32 -05:00
|
|
|
|
2014-12-08 17:17:06 -05:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mThreadMutex);
|
2015-05-22 14:16:49 -04:00
|
|
|
tick_recv(recv_timeout);
|
2014-12-08 17:17:06 -05:00
|
|
|
}
|
2013-10-01 23:21:04 -04:00
|
|
|
|
2015-05-22 14:16:49 -04:00
|
|
|
// Push Items, Outside of Mutex.
|
|
|
|
RsItem *incoming = NULL;
|
|
|
|
while((incoming = GetItem()))
|
|
|
|
{
|
|
|
|
RecvItem(incoming);
|
|
|
|
}
|
2013-10-01 23:21:04 -04:00
|
|
|
|
2014-12-08 17:17:06 -05:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mThreadMutex);
|
2015-05-22 14:16:49 -04:00
|
|
|
tick_send(0);
|
2014-12-08 17:17:06 -05:00
|
|
|
}
|
2013-10-01 23:21:04 -04:00
|
|
|
|
2015-05-22 14:16:49 -04:00
|
|
|
if (sleep_period)
|
|
|
|
{
|
|
|
|
usleep(sleep_period);
|
|
|
|
}
|
2013-10-01 23:21:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|