2018-05-28 16:03:39 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
* libretroshare/src/pqi: pqistreamer.h *
|
|
|
|
* *
|
|
|
|
* libretroshare: retroshare core library *
|
|
|
|
* *
|
|
|
|
* Copyright 2004-2008 by Robert Fernie <retroshare@lunamutt.com> *
|
|
|
|
* *
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU Lesser General Public License as *
|
|
|
|
* published by the Free Software Foundation, either version 3 of the *
|
|
|
|
* License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program 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 Lesser General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
|
|
|
* *
|
|
|
|
*******************************************************************************/
|
2007-11-14 22:18:48 -05:00
|
|
|
#ifndef MRK_PQI_STREAMER_HEADER
|
|
|
|
#define MRK_PQI_STREAMER_HEADER
|
|
|
|
|
2016-08-25 05:33:11 -04:00
|
|
|
#include <stdint.h> // for uint32_t
|
|
|
|
#include <time.h> // for time_t
|
|
|
|
#include <iostream> // for operator<<, basic_ostream, cerr, endl
|
|
|
|
#include <list> // for list
|
|
|
|
#include <map> // for map
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2016-08-25 05:33:11 -04:00
|
|
|
#include "pqi/pqi_base.h" // for BinInterface (ptr only), PQInterface
|
|
|
|
#include "retroshare/rsconfig.h" // for RSTrafficClue
|
|
|
|
#include "retroshare/rstypes.h" // for RsPeerId
|
|
|
|
#include "util/rsthreads.h" // for RsMutex
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2018-01-22 09:02:33 -05:00
|
|
|
struct RsItem;
|
2016-08-25 05:33:11 -04:00
|
|
|
class RsSerialiser;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2016-04-24 13:43:34 -04:00
|
|
|
struct PartialPacketRecord
|
|
|
|
{
|
|
|
|
void *mem ;
|
|
|
|
uint32_t size ;
|
|
|
|
};
|
|
|
|
|
2016-08-25 05:33:11 -04:00
|
|
|
/**
|
|
|
|
* @brief Fully implements the PQInterface and communicates with peer etc via
|
|
|
|
* the BinInterface.
|
|
|
|
* The interface does not handle connection, just communication.
|
|
|
|
* Possible BIN_FLAGS: BIN_FLAGS_NO_CLOSE | BIN_FLAGS_NO_DELETE
|
|
|
|
*/
|
2007-11-14 22:18:48 -05:00
|
|
|
class pqistreamer: public PQInterface
|
|
|
|
{
|
2011-09-04 16:01:30 -04:00
|
|
|
public:
|
2014-03-17 16:56:06 -04:00
|
|
|
pqistreamer(RsSerialiser *rss, const RsPeerId& peerid, BinInterface *bio_in, int bio_flagsin);
|
2011-09-04 16:01:30 -04:00
|
|
|
virtual ~pqistreamer();
|
|
|
|
|
|
|
|
// PQInterface
|
|
|
|
virtual int SendItem(RsItem *item)
|
|
|
|
{
|
|
|
|
std::cerr << "Warning pqistreamer::sendItem(RsItem*) should not be called. Plz call SendItem(RsItem *,uint32_t& serialized_size) instead." << std::endl;
|
|
|
|
uint32_t serialized_size ;
|
|
|
|
return SendItem(item,serialized_size) ;
|
|
|
|
}
|
|
|
|
virtual int SendItem(RsItem *,uint32_t& serialized_size);
|
|
|
|
virtual RsItem *GetItem();
|
|
|
|
virtual int status();
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2012-01-19 11:23:57 -05:00
|
|
|
time_t getLastIncomingTS(); // Time of last data packet, for checking a connection is alive.
|
2012-06-21 21:35:32 -04:00
|
|
|
virtual void getRates(RsBwRates &rates);
|
|
|
|
virtual int getQueueSize(bool in); // extracting data.
|
2015-07-12 00:04:18 -04:00
|
|
|
virtual int gatherStatistics(std::list<RSTrafficClue>& outqueue_stats,std::list<RSTrafficClue>& inqueue_stats); // extracting data.
|
2016-06-11 09:33:11 -04:00
|
|
|
|
|
|
|
// mutex protected versions of RateInterface calls.
|
|
|
|
virtual void setRate(bool b,float f) ;
|
|
|
|
virtual void setMaxRate(bool b,float f) ;
|
|
|
|
virtual float getRate(bool b) ;
|
|
|
|
|
2014-10-31 17:24:42 -04:00
|
|
|
protected:
|
2016-06-25 14:12:35 -04:00
|
|
|
virtual int reset() ;
|
2013-10-01 23:21:04 -04:00
|
|
|
|
|
|
|
int tick_bio();
|
|
|
|
int tick_send(uint32_t timeout);
|
|
|
|
int tick_recv(uint32_t timeout);
|
|
|
|
|
2011-09-04 16:01:30 -04:00
|
|
|
/* Implementation */
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2012-06-23 08:10:41 -04:00
|
|
|
// These methods are redefined in pqiQoSstreamer
|
|
|
|
//
|
2016-04-23 17:10:25 -04:00
|
|
|
virtual void locked_storeInOutputQueue(void *ptr, int size, int priority) ;
|
2013-10-01 06:11:34 -04:00
|
|
|
virtual int locked_out_queue_size() const ;
|
2012-06-23 08:10:41 -04:00
|
|
|
virtual void locked_clear_out_queue() ;
|
|
|
|
virtual int locked_compute_out_pkt_size() const ;
|
2016-04-25 23:37:02 -04:00
|
|
|
virtual void *locked_pop_out_data(uint32_t max_slice_size,uint32_t& size,bool& starts,bool& ends,uint32_t& packet_id);
|
2016-04-23 17:10:25 -04:00
|
|
|
virtual int locked_gatherStatistics(std::list<RSTrafficClue>& outqueue_stats,std::list<RSTrafficClue>& inqueue_stats); // extracting data.
|
2012-06-23 08:10:41 -04:00
|
|
|
|
2016-04-09 14:48:05 -04:00
|
|
|
void updateRates() ;
|
2016-06-11 09:33:11 -04:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
protected:
|
|
|
|
RsMutex mStreamerMtx ; // Protects data, fns below, protected so pqiqos can use it too.
|
|
|
|
|
2013-10-01 23:21:04 -04:00
|
|
|
// Binary Interface for IO, initialisated at startup.
|
|
|
|
BinInterface *mBio;
|
|
|
|
unsigned int mBio_flags; // BIN_FLAGS_NO_CLOSE | BIN_FLAGS_NO_DELETE
|
|
|
|
|
2012-06-23 08:10:41 -04:00
|
|
|
private:
|
2013-10-01 06:11:34 -04:00
|
|
|
int queue_outpqi_locked(RsItem *i,uint32_t& serialized_size);
|
2015-07-12 00:04:18 -04:00
|
|
|
int handleincomingitem_locked(RsItem *i, int len);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2011-09-04 16:01:30 -04:00
|
|
|
// ticked regularly (manages out queues and sending
|
|
|
|
// via above interfaces.
|
2013-10-01 06:11:34 -04:00
|
|
|
virtual int handleoutgoing_locked();
|
|
|
|
virtual int handleincoming_locked();
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2011-09-04 16:01:30 -04:00
|
|
|
// Bandwidth/Streaming Management.
|
2013-10-01 06:11:34 -04:00
|
|
|
float outTimeSlice_locked();
|
|
|
|
|
|
|
|
int outAllowedBytes_locked();
|
2016-03-02 19:00:51 -05:00
|
|
|
void outSentBytes_locked(uint32_t );
|
2013-10-01 06:11:34 -04:00
|
|
|
|
|
|
|
int inAllowedBytes_locked();
|
2016-03-02 19:00:51 -05:00
|
|
|
void inReadBytes_locked(uint32_t );
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2016-06-25 14:12:35 -04:00
|
|
|
// cleans up everything that's pending / half finished.
|
|
|
|
void free_pend_locked();
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2011-09-04 16:01:30 -04:00
|
|
|
// RsSerialiser - determines which packets can be serialised.
|
2013-10-01 06:11:34 -04:00
|
|
|
RsSerialiser *mRsSerialiser;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
void *mPkt_wpending; // storage for pending packet to write.
|
2015-12-12 23:07:33 -05:00
|
|
|
uint32_t mPkt_wpending_size; // ... and its size.
|
2015-04-04 05:58:53 -04:00
|
|
|
|
|
|
|
void allocate_rpend_locked(); // use these two functions to allocate/free the buffer below
|
2016-06-25 14:12:35 -04:00
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
int mPkt_rpend_size; // size of pkt_rpending.
|
|
|
|
void *mPkt_rpending; // storage for read in pending packets.
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2011-09-04 16:01:30 -04:00
|
|
|
enum {reading_state_packet_started=1,
|
2009-03-03 14:40:42 -05:00
|
|
|
reading_state_initial=0 } ;
|
|
|
|
|
2013-10-01 06:11:34 -04:00
|
|
|
int mReading_state ;
|
|
|
|
int mFailed_read_attempts ;
|
2009-03-03 14:40:42 -05:00
|
|
|
|
2011-09-04 16:01:30 -04:00
|
|
|
// Temp Storage for transient data.....
|
2013-10-01 06:11:34 -04:00
|
|
|
std::list<void *> mOutPkts; // Cntrl / Search / Results queue
|
|
|
|
std::list<RsItem *> mIncoming;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2014-10-31 17:24:42 -04:00
|
|
|
uint32_t mIncomingSize; // size of mIncoming. To avoid calling linear cost std::list::size()
|
|
|
|
|
2011-09-04 16:01:30 -04:00
|
|
|
// data for network stats.
|
2013-10-01 06:11:34 -04:00
|
|
|
int mTotalRead;
|
|
|
|
int mTotalSent;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2011-09-04 16:01:30 -04:00
|
|
|
// these are representative (but not exact)
|
2013-10-01 06:11:34 -04:00
|
|
|
int mCurrRead;
|
|
|
|
int mCurrSent;
|
2016-04-09 14:48:05 -04:00
|
|
|
|
2016-10-01 16:14:50 -04:00
|
|
|
double mCurrReadTS; // TS from which these are measured.
|
|
|
|
double mCurrSentTS;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2017-01-18 07:25:45 -05:00
|
|
|
double mAvgLastUpdate; // TS from which these are measured.
|
2016-06-11 17:21:04 -04:00
|
|
|
uint32_t mAvgReadCount;
|
|
|
|
uint32_t mAvgSentCount;
|
2009-03-11 16:36:51 -04:00
|
|
|
|
2017-01-18 07:25:45 -05:00
|
|
|
double mAvgDtOut; // average time diff between 2 rounds of sending data
|
|
|
|
double mAvgDtIn; // average time diff between 2 rounds of receiving data
|
|
|
|
|
2012-01-19 11:23:57 -05:00
|
|
|
time_t mLastIncomingTs;
|
|
|
|
|
2015-07-12 00:04:18 -04:00
|
|
|
// traffic statistics
|
2012-01-19 11:23:57 -05:00
|
|
|
|
2015-07-12 00:04:18 -04:00
|
|
|
std::list<RSTrafficClue> mPreviousStatsChunk_In ;
|
|
|
|
std::list<RSTrafficClue> mPreviousStatsChunk_Out ;
|
|
|
|
std::list<RSTrafficClue> mCurrentStatsChunk_In ;
|
|
|
|
std::list<RSTrafficClue> mCurrentStatsChunk_Out ;
|
|
|
|
time_t mStatisticsTimeStamp ;
|
|
|
|
|
2016-04-26 09:22:24 -04:00
|
|
|
bool mAcceptsPacketSlicing ;
|
|
|
|
time_t mLastSentPacketSlicingProbe ;
|
2016-04-23 17:10:25 -04:00
|
|
|
void locked_addTrafficClue(const RsItem *pqi, uint32_t pktsize, std::list<RSTrafficClue> &lst);
|
2016-06-25 14:12:35 -04:00
|
|
|
RsItem *addPartialPacket_locked(const void *block, uint32_t len, uint32_t slice_packet_id,bool packet_starting,bool packet_ending,uint32_t& total_len);
|
2016-04-24 13:43:34 -04:00
|
|
|
|
|
|
|
std::map<uint32_t,PartialPacketRecord> mPartialPackets ;
|
2007-11-14 22:18:48 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif //MRK_PQI_STREAMER_HEADER
|