2018-05-28 16:03:39 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
* libretroshare/src/pqi: pqiperson.h *
|
|
|
|
* *
|
|
|
|
* libretroshare: retroshare core library *
|
|
|
|
* *
|
|
|
|
* Copyright (C) 2004-2006 Robert Fernie. *
|
|
|
|
* *
|
|
|
|
* 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_PERSON_HEADER
|
|
|
|
#define MRK_PQI_PERSON_HEADER
|
|
|
|
|
|
|
|
|
2017-03-16 12:50:25 -04:00
|
|
|
#include <string>
|
2007-11-14 22:18:48 -05:00
|
|
|
#include "pqi/pqi.h"
|
2013-10-01 23:21:04 -04:00
|
|
|
#include "util/rsnet.h"
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
class pqiperson;
|
2013-09-10 15:31:44 -04:00
|
|
|
class RsPeerCryptoParams ;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
static const int CONNECT_RECEIVED = 1;
|
|
|
|
static const int CONNECT_SUCCESS = 2;
|
|
|
|
static const int CONNECT_UNREACHABLE = 3;
|
|
|
|
static const int CONNECT_FIREWALLED = 4;
|
|
|
|
static const int CONNECT_FAILED = 5;
|
|
|
|
|
2016-04-09 14:48:05 -04:00
|
|
|
static const time_t HEARTBEAT_REPEAT_TIME = 5;
|
2009-12-22 05:41:19 -05:00
|
|
|
|
2012-06-23 08:10:41 -04:00
|
|
|
#include "pqi/pqiqosstreamer.h"
|
2013-10-01 23:21:04 -04:00
|
|
|
#include "pqi/pqithreadstreamer.h"
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2015-09-27 14:15:41 -04:00
|
|
|
class pqiconnect : public pqiQoSstreamer, public NetInterface
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
public:
|
2015-09-27 14:15:41 -04:00
|
|
|
pqiconnect(PQInterface *parent, RsSerialiser *rss, NetBinInterface *ni_in) :
|
|
|
|
pqiQoSstreamer(parent, rss, ni_in->PeerId(), ni_in, 0), // pqistreamer will cleanup NetInterface.
|
|
|
|
NetInterface(NULL, ni_in->PeerId()), // No need for callback
|
|
|
|
ni(ni_in) {}
|
2011-07-14 10:56:33 -04:00
|
|
|
|
2015-09-27 14:15:41 -04:00
|
|
|
virtual ~pqiconnect() {}
|
|
|
|
virtual bool getCryptoParams(RsPeerCryptoParams& params);
|
2011-07-14 10:56:33 -04:00
|
|
|
|
2015-09-27 14:15:41 -04:00
|
|
|
// presents a virtual NetInterface -> passes to ni.
|
|
|
|
virtual int connect(const struct sockaddr_storage &raddr) { return ni->connect(raddr); }
|
|
|
|
virtual int listen() { return ni->listen(); }
|
|
|
|
virtual int stoplistening() { return ni->stoplistening(); }
|
2016-06-25 14:12:35 -04:00
|
|
|
virtual int reset() { pqistreamer::reset(); return ni->reset(); }
|
|
|
|
virtual int disconnect() { return reset() ; }
|
2015-09-27 14:15:41 -04:00
|
|
|
virtual bool connect_parameter(uint32_t type, uint32_t value) { return ni->connect_parameter(type, value);}
|
2017-03-16 12:50:25 -04:00
|
|
|
virtual bool connect_parameter(uint32_t type, const std::string &value) { return ni->connect_parameter(type, value);}
|
2015-09-27 14:15:41 -04:00
|
|
|
virtual bool connect_additional_address(uint32_t type, const struct sockaddr_storage &addr) { return ni->connect_additional_address(type, addr); }
|
|
|
|
virtual int getConnectAddress(struct sockaddr_storage &raddr){ return ni->getConnectAddress(raddr); }
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
// get the contact from the net side!
|
2015-09-27 14:15:41 -04:00
|
|
|
virtual const RsPeerId& PeerId() { return ni->PeerId(); }
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
// to check if our interface.
|
2015-09-27 14:15:41 -04:00
|
|
|
virtual bool thisNetInterface(NetInterface *ni_in) { return (ni_in == ni); }
|
|
|
|
|
2012-01-08 11:28:44 -05:00
|
|
|
protected:
|
2007-11-14 22:18:48 -05:00
|
|
|
NetBinInterface *ni;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
class pqipersongrp;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2013-10-01 23:21:04 -04:00
|
|
|
class NotifyData
|
|
|
|
{
|
2015-12-19 16:10:07 -05:00
|
|
|
public:
|
|
|
|
NotifyData() : mNi(NULL), mState(0)
|
2013-10-01 23:21:04 -04:00
|
|
|
{
|
|
|
|
sockaddr_storage_clear(mAddr);
|
|
|
|
}
|
|
|
|
|
2015-12-19 16:10:07 -05:00
|
|
|
NotifyData(NetInterface *ni, int state, const sockaddr_storage &addr) :
|
|
|
|
mNi(ni), mState(state), mAddr(addr) {}
|
2013-10-01 23:21:04 -04:00
|
|
|
|
|
|
|
NetInterface *mNi;
|
|
|
|
int mState;
|
2015-12-19 16:10:07 -05:00
|
|
|
sockaddr_storage mAddr;
|
2013-10-01 23:21:04 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
class pqiperson: public PQInterface
|
|
|
|
{
|
|
|
|
public:
|
2014-03-17 16:56:06 -04:00
|
|
|
pqiperson(const RsPeerId& id, pqipersongrp *ppg);
|
2015-12-19 16:10:07 -05:00
|
|
|
virtual ~pqiperson(); // must clean up children.
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
// control of the connection.
|
2015-12-19 16:10:07 -05:00
|
|
|
int reset();
|
|
|
|
int listen();
|
|
|
|
int stoplistening();
|
2011-07-14 10:56:33 -04:00
|
|
|
|
2015-12-19 16:10:07 -05:00
|
|
|
int connect(uint32_t type, const sockaddr_storage &raddr,
|
|
|
|
const sockaddr_storage &proxyaddr, const sockaddr_storage &srcaddr,
|
|
|
|
uint32_t delay, uint32_t period, uint32_t timeout, uint32_t flags,
|
|
|
|
uint32_t bandwidth, const std::string &domain_addr, uint16_t domain_port);
|
2013-09-03 09:35:39 -04:00
|
|
|
|
2015-12-19 16:10:07 -05:00
|
|
|
int fullstopthreads();
|
|
|
|
int receiveHeartbeat();
|
2013-10-01 23:21:04 -04:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
// add in connection method.
|
2015-12-19 16:10:07 -05:00
|
|
|
int addChildInterface(uint32_t type, pqiconnect *pqi);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2015-12-19 16:10:07 -05:00
|
|
|
virtual bool getCryptoParams(RsPeerCryptoParams&);
|
2013-09-10 15:31:44 -04:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
// The PQInterface interface.
|
2015-12-19 16:10:07 -05:00
|
|
|
virtual int SendItem(RsItem *,uint32_t& serialized_size);
|
|
|
|
virtual int SendItem(RsItem *item)
|
|
|
|
{
|
|
|
|
std::cerr << "Warning pqiperson::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);
|
|
|
|
}
|
2013-10-01 23:21:04 -04:00
|
|
|
|
2015-12-19 16:10:07 -05:00
|
|
|
virtual RsItem *GetItem();
|
|
|
|
virtual bool RecvItem(RsItem *item);
|
|
|
|
|
|
|
|
virtual int status();
|
|
|
|
virtual int tick();
|
|
|
|
|
|
|
|
// overloaded callback function for the child - notify of a change.
|
|
|
|
virtual int notifyEvent(NetInterface *ni, int event, const struct sockaddr_storage &addr);
|
|
|
|
|
|
|
|
// PQInterface for rate control overloaded....
|
|
|
|
virtual int getQueueSize(bool in);
|
|
|
|
virtual void getRates(RsBwRates &rates);
|
|
|
|
virtual float getRate(bool in);
|
|
|
|
virtual void setMaxRate(bool in, float val);
|
|
|
|
virtual void setRateCap(float val_in, float val_out);
|
|
|
|
virtual int gatherStatistics(std::list<RSTrafficClue>& outqueue_lst,
|
|
|
|
std::list<RSTrafficClue>& inqueue_lst);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void processNotifyEvents();
|
|
|
|
int handleNotifyEvent_locked(NetInterface *ni, int event,
|
|
|
|
const sockaddr_storage &addr);
|
|
|
|
|
|
|
|
RsMutex mNotifyMtx; // LOCKS Notify Queue
|
2013-10-01 23:21:04 -04:00
|
|
|
std::list<NotifyData> mNotifyQueue;
|
|
|
|
|
2015-12-19 16:10:07 -05:00
|
|
|
RsMutex mPersonMtx; // LOCKS below
|
2013-10-01 23:21:04 -04:00
|
|
|
|
2015-12-19 16:10:07 -05:00
|
|
|
int reset_locked();
|
2014-01-20 06:42:27 -05:00
|
|
|
|
2015-12-19 16:10:07 -05:00
|
|
|
void setRateCap_locked(float val_in, float val_out);
|
2013-10-01 23:21:04 -04:00
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
std::map<uint32_t, pqiconnect *> kids;
|
2007-11-14 22:18:48 -05:00
|
|
|
bool active;
|
|
|
|
pqiconnect *activepqi;
|
|
|
|
bool inConnectAttempt;
|
2017-03-16 17:01:22 -04:00
|
|
|
//int waittimes;
|
2015-12-19 16:10:07 -05:00
|
|
|
time_t lastHeartbeatReceived; // use to track connection failure
|
2008-01-25 01:36:40 -05:00
|
|
|
pqipersongrp *pqipg; /* parent for callback */
|
2007-11-14 22:18:48 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|