Made pqiperson more readable evidence some strange code

This commit is contained in:
Gio 2015-12-19 22:10:07 +01:00
parent 874f304cd2
commit b2c27a1fed
2 changed files with 274 additions and 303 deletions

View File

@ -38,23 +38,16 @@ const int pqipersonzone = 82371;
* #define PERSON_DEBUG 1
****/
pqiperson::pqiperson(const RsPeerId& id, pqipersongrp *pg)
:PQInterface(id), mNotifyMtx("pqiperson-notify"), mPersonMtx("pqiperson"),
active(false), activepqi(NULL),
inConnectAttempt(false), waittimes(0),
pqipg(pg)
{
/* must check id! */
return;
}
pqiperson::pqiperson(const RsPeerId& id, pqipersongrp *pg) :
PQInterface(id), mNotifyMtx("pqiperson-notify"), mPersonMtx("pqiperson"),
active(false), activepqi(NULL), inConnectAttempt(false), waittimes(0),
pqipg(pg) {} // TODO: must check id!
pqiperson::~pqiperson()
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
// clean up the children.
// clean up the childrens
std::map<uint32_t, pqiconnect *>::iterator it;
for(it = kids.begin(); it != kids.end(); ++it)
{
@ -64,11 +57,9 @@ pqiperson::~pqiperson()
kids.clear();
}
// The PQInterface interface.
int pqiperson::SendItem(RsItem *i,uint32_t& serialized_size)
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
if(active)
{
@ -103,7 +94,7 @@ int pqiperson::SendItem(RsItem *i,uint32_t& serialized_size)
RsItem *pqiperson::GetItem()
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
if (active)
return activepqi->GetItem();
@ -114,8 +105,7 @@ RsItem *pqiperson::GetItem()
bool pqiperson::RecvItem(RsItem *item)
{
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::RecvItem()";
std::cerr << std::endl;
std::cerr << "pqiperson::RecvItem()" << std::endl;
#endif
return pqipg->recvItem((RsRawItem *) item);
@ -124,7 +114,7 @@ bool pqiperson::RecvItem(RsItem *item)
int pqiperson::status()
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
if (active)
return activepqi -> status();
@ -133,39 +123,45 @@ int pqiperson::status()
int pqiperson::receiveHeartbeat()
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::receiveHeartbeat() from peer : "
<< PeerId().toStdString() << std::endl;
#endif
pqioutput(PQL_WARNING, pqipersonzone, "pqiperson::receiveHeartbeat() from peer : " + PeerId().toStdString());
RS_STACK_MUTEX(mPersonMtx);
lastHeartbeatReceived = time(NULL);
return true ;
return 1;
}
// tick......
int pqiperson::tick()
{
int activeTick = 0;
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
//if lastHeartbeatReceived is 0, it might be not activated so don't do a net reset.
if (active && (lastHeartbeatReceived != 0) &&
(time(NULL) - lastHeartbeatReceived) > HEARTBEAT_REPEAT_TIME * 5)
if ( active && (lastHeartbeatReceived != 0)
&& (time(NULL) - lastHeartbeatReceived) > HEARTBEAT_REPEAT_TIME * 5)
{
int ageLastIncoming = time(NULL) - activepqi->getLastIncomingTS();
std::string out = "pqiperson::tick() WARNING No heartbeat from: " + PeerId().toStdString();
//out << " assume dead. calling pqissl::reset(), LastHeartbeat was: ";
rs_sprintf_append(out, " LastHeartbeat was: %ld secs ago", time(NULL) - lastHeartbeatReceived);
rs_sprintf_append(out, " LastIncoming was: %d secs ago", ageLastIncoming);
pqioutput(PQL_WARNING, pqipersonzone, out);
#define NO_PACKET_TIMEOUT 60
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::tick() WARNING No heartbeat from: "
<< PeerId().toStdString() << " LastHeartbeat was: "
<< time(NULL) - lastHeartbeatReceived
<< "secs ago LastIncoming was: " << ageLastIncoming
<< "secs ago" << std::endl;
#endif
if (ageLastIncoming > NO_PACKET_TIMEOUT)
if (ageLastIncoming > 60) // Check timeout
{
out = "pqiperson::tick() " + PeerId().toStdString();
out += " No Heartbeat & No Packets -> assume dead. calling pqissl::reset()";
pqioutput(PQL_WARNING, pqipersonzone, out);
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::tick() " << PeerId().toStdString()
<< " No Heartbeat & No Packets -> assume dead."
<< "calling pqissl::reset()" << std::endl;
#endif
this->reset_locked();
}
@ -174,34 +170,31 @@ int pqiperson::tick()
{
std::string out = "pqiperson::tick() Id: " + PeerId().toStdString() + " ";
#ifdef PERSON_DEBUG
std::string statusStr = " inactive ";
if (active)
out += "***Active***";
else
out += ">>InActive<<";
out += "\n";
rs_sprintf_append(out, "Activepqi: %p inConnectAttempt: ", activepqi);
statusStr = " active ";
std::string connectStr = " Not Connecting ";
if (inConnectAttempt)
out += "In Connection Attempt";
else
out += " Not Connecting ";
out += "\n";
connectStr = " In Connection Attempt ";
std::cerr << "pqiperson::tick() Id: " << PeerId().toStdString()
<< "activepqi: " << activepqi << " inConnectAttempt:"
<< connectStr << std::endl;
#endif
// tick the children.
std::map<uint32_t, pqiconnect *>::iterator it;
for(it = kids.begin(); it != kids.end(); ++it)
{
if (0 < (it->second)->tick())
{
activeTick = 1;
#ifdef PERSON_DEBUG
std::cerr << "\tTicking Child: "<< it->first << std::endl;
#endif
}
rs_sprintf_append(out, "\tTicking Child: %d\n", it->first);
}
pqioutput(PQL_DEBUG_ALL, pqipersonzone, out);
} // end of pqioutput.
}
// handle Notify Events that were generated.
@ -214,44 +207,41 @@ int pqiperson::tick()
// This is only used for out-of-band info....
// otherwise could get dangerous loops.
// - Actually, now we have - must store and process later.
int pqiperson::notifyEvent(NetInterface *ni, int newState, const struct sockaddr_storage &remote_peer_address)
int pqiperson::notifyEvent(NetInterface *ni, int newState,
const sockaddr_storage &remote_peer_address)
{
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::notifyEvent() adding event to Queue. newState=" << newState << " from IP = " << sockaddr_storage_tostring(remote_peer_address) << std::endl;
std::cerr << "pqiperson::notifyEvent() adding event to Queue. newState="
<< newState << " from IP = "
<< sockaddr_storage_tostring(remote_peer_address) << std::endl;
#endif
if (mPersonMtx.trylock())
{
handleNotifyEvent_locked(ni, newState, remote_peer_address);
mPersonMtx.unlock();
return 1;
}
RsStackMutex stack(mNotifyMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mNotifyMtx);
mNotifyQueue.push_back(NotifyData(ni, newState, remote_peer_address));
return 1;
}
void pqiperson::processNotifyEvents()
{
NetInterface *ni;
int state;
struct sockaddr_storage addr;
sockaddr_storage addr;
while(1)
while(1) // While there is notification to handle
{
{
RsStackMutex stack(mNotifyMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mNotifyMtx);
if(mNotifyQueue.empty())
{
return;
}
NotifyData &data = mNotifyQueue.front();
ni = data.mNi;
state = data.mState;
@ -260,23 +250,21 @@ void pqiperson::processNotifyEvents()
mNotifyQueue.pop_front();
}
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
handleNotifyEvent_locked(ni, state, addr);
}
return;
}
int pqiperson::handleNotifyEvent_locked(NetInterface *ni, int newState, const struct sockaddr_storage &remote_peer_address)
int pqiperson::handleNotifyEvent_locked(NetInterface *ni, int newState,
const sockaddr_storage &remote_peer_address)
{
{
std::string out = "pqiperson::notifyEvent() Id: " + PeerId().toStdString() + "\n";
rs_sprintf_append(out, "Message: %d from: %p\n", newState, ni);
rs_sprintf_append(out, "Active pqi : %p", activepqi);
pqioutput(PQL_DEBUG_BASIC, pqipersonzone, out);
}
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::handleNotifyEvent_locked() Id: "
<< PeerId().toStdString() << " Message: " << newState
<< " from: " << ni << std::endl;
int i = 0;
#endif
/* find the pqi, */
pqiconnect *pqi = NULL;
@ -284,14 +272,14 @@ int pqiperson::handleNotifyEvent_locked(NetInterface *ni, int newState, const s
std::map<uint32_t, pqiconnect *>::iterator it;
/* start again */
int i = 0;
for(it = kids.begin(); it != kids.end(); ++it)
{
std::string out;
rs_sprintf(out, "pqiperson::connectattempt() Kid# %d of %u\n", i, kids.size());
rs_sprintf_append(out, " type: %u in_ni: %p", it->first, ni);
pqioutput(PQL_DEBUG_BASIC, pqipersonzone, out);
i++;
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::handleNotifyEvent_locked() Kid# " << i
<< " of " << kids.size() << " type: " << it->first
<< " in_ni: " << ni << std::endl;
++i;
#endif
if ((it->second)->thisNetInterface(ni))
{
@ -302,7 +290,8 @@ int pqiperson::handleNotifyEvent_locked(NetInterface *ni, int newState, const s
if (!pqi)
{
pqioutput(PQL_WARNING, pqipersonzone, "Unknown notfyEvent Source!");
std::cerr << "pqiperson::handleNotifyEvent_locked Unknown Event Source!"
<< std::endl;
return -1;
}
@ -311,12 +300,13 @@ int pqiperson::handleNotifyEvent_locked(NetInterface *ni, int newState, const s
{
case CONNECT_RECEIVED:
case CONNECT_SUCCESS:
{
/* notify */
if (pqipg)
{
pqissl *ssl = dynamic_cast<pqissl*>(ni);
if(ssl != NULL)
if(ssl)
pqipg->notifyConnect(PeerId(), type, true, ssl->actAsServer(), remote_peer_address);
else
pqipg->notifyConnect(PeerId(), type, true, false, remote_peer_address);
@ -324,7 +314,12 @@ int pqiperson::handleNotifyEvent_locked(NetInterface *ni, int newState, const s
if ((active) && (activepqi != pqi)) // already connected - trouble
{
pqioutput(PQL_WARNING, pqipersonzone, "pqiperson::notifyEvent() Id: " + PeerId().toStdString() + " CONNECT_SUCCESS+active-> activing new connection, shutting others");
// TODO: 2015/12/19 Is this block dead code?
std::cerr << "pqiperson::handleNotifyEvent_locked Id: "
<< PeerId().toStdString() << " CONNECT_SUCCESS+active->"
<< "activing new connection, shutting others"
<< std::endl;
// This is the RESET that's killing the connections.....
//activepqi -> reset();
@ -335,8 +330,11 @@ int pqiperson::handleNotifyEvent_locked(NetInterface *ni, int newState, const s
/* now install a new one. */
{
pqioutput(PQL_WARNING, pqipersonzone, "pqiperson::notifyEvent() Id: " + PeerId().toStdString() + " CONNECT_SUCCESS->marking so! (resetting others)");
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::handleNotifyEvent_locked Id: "
<< PeerId().toStdString() << " CONNECT_SUCCESS->marking "
<< "so! (resetting others)" << std::endl;
#endif
// mark as active.
active = true;
@ -346,74 +344,62 @@ int pqiperson::handleNotifyEvent_locked(NetInterface *ni, int newState, const s
activepqi->start(); // STARTUP THREAD.
/* reset all other children? (clear up long UDP attempt) */
// reset all other children (clear up long UDP attempt)
for(it = kids.begin(); it != kids.end(); ++it)
{
if (!(it->second)->thisNetInterface(ni))
{
std::string out;
rs_sprintf(out, "Resetting pqi ref : %p", &(it->second));
pqioutput(PQL_DEBUG_BASIC, pqipersonzone, out);
it->second->reset();
} else {
//std::cerr << "Active pqi : not resetting." << std::endl;
}
}
return 1;
}
break;
}
case CONNECT_UNREACHABLE:
case CONNECT_FIREWALLED:
case CONNECT_FAILED:
if (active)
{
if (activepqi == pqi)
if (active && (activepqi == pqi))
{
pqioutput(PQL_WARNING, pqipersonzone, "pqiperson::notifyEvent() Id: " + PeerId().toStdString() + " CONNECT_FAILED->marking so!");
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::handleNotifyEvent_locked Id: "
<< PeerId().toStdString()
<< " CONNECT_FAILED->marking so!" << std::endl;
#endif
activepqi->shutdown(); // STOP THREAD.
active = false;
activepqi = NULL;
}
#ifdef PERSON_DEBUG
else
{
pqioutput(PQL_WARNING, pqipersonzone, "pqiperson::notifyEvent() Id: " + PeerId().toStdString() + " CONNECT_FAILED-> from an unactive connection, don't flag the peer as not connected, just try next attempt !");
}
}
else
{
pqioutput(PQL_WARNING, pqipersonzone, "pqiperson::notifyEvent() Id: " + PeerId().toStdString() + " CONNECT_FAILED+NOT active -> try connect again");
}
std::cerr << "pqiperson::handleNotifyEvent_locked Id: "
<< PeerId().toStdString() + " CONNECT_FAILED-> from "
<< "an unactive connection, don't flag the peer as "
<< "not connected, just try next attempt !" << std::endl;
#endif
/* notify up */
if (pqipg)
{
pqipg->notifyConnect(PeerId(), type, false, false, remote_peer_address);
}
return 1;
break;
default:
break;
}
default:
return -1;
}
}
/***************** Not PQInterface Fns ***********************/
int pqiperson::reset()
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
return reset_locked();
}
int pqiperson::reset_locked()
{
pqioutput(PQL_WARNING, pqipersonzone, "pqiperson::reset() resetting all pqiconnect for Id: " + PeerId().toStdString());
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::reset_locked() resetting all pqiconnect for Id: "
<< PeerId().toStdString() << std::endl;
#endif
std::map<uint32_t, pqiconnect *>::iterator it;
for(it = kids.begin(); it != kids.end(); ++it)
@ -431,15 +417,16 @@ int pqiperson::reset_locked()
int pqiperson::fullstopthreads()
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::fullstopthreads() for Id: "
<< PeerId().toStdString() << std::endl;
#endif
pqioutput(PQL_WARNING, pqipersonzone, "pqiperson::fullstopthreads() for Id: " + PeerId().toStdString());
RS_STACK_MUTEX(mPersonMtx);
std::map<uint32_t, pqiconnect *>::iterator it;
for(it = kids.begin(); it != kids.end(); ++it)
{
(it->second)->fullstop(); // WAIT FOR THREAD TO STOP.
}
activepqi = NULL;
active = false;
@ -450,13 +437,12 @@ int pqiperson::fullstopthreads()
int pqiperson::addChildInterface(uint32_t type, pqiconnect *pqi)
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::addChildInterface() : Id "
<< PeerId().toStdString() << " " << type << std::endl;
#endif
{
std::string out;
rs_sprintf(out, "pqiperson::addChildInterface() : Id %s %u", PeerId().toStdString().c_str(), type);
pqioutput(PQL_DEBUG_BASIC, pqipersonzone, out);
}
RS_STACK_MUTEX(mPersonMtx);
kids[type] = pqi;
return 1;
@ -468,89 +454,76 @@ int pqiperson::addChildInterface(uint32_t type, pqiconnect *pqi)
int pqiperson::listen()
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::listen() Id: " + PeerId().toStdString() << std::endl;
#endif
pqioutput(PQL_DEBUG_BASIC, pqipersonzone, "pqiperson::listen() Id: " + PeerId().toStdString());
RS_STACK_MUTEX(mPersonMtx);
if (!active)
{
std::map<uint32_t, pqiconnect *>::iterator it;
for(it = kids.begin(); it != kids.end(); ++it)
{
// set them all listening.
(it->second)->listen();
}
}
return 1;
}
int pqiperson::stoplistening()
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::stoplistening() Id: " + PeerId().toStdString()
<< std::endl;
#endif
pqioutput(PQL_DEBUG_BASIC, pqipersonzone, "pqiperson::stoplistening() Id: " + PeerId().toStdString());
RS_STACK_MUTEX(mPersonMtx);
std::map<uint32_t, pqiconnect *>::iterator it;
for(it = kids.begin(); it != kids.end(); ++it)
{
// set them all listening.
(it->second)->stoplistening();
}
return 1;
}
int pqiperson::connect(uint32_t type, const struct sockaddr_storage &raddr,
const struct sockaddr_storage &proxyaddr, const struct sockaddr_storage &srcaddr,
uint32_t delay, uint32_t period, uint32_t timeout, uint32_t flags, uint32_t bandwidth,
int pqiperson::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)
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::connect() Id: " << PeerId().toStdString()
<< " type: " << type << " addr: "
<< sockaddr_storage_tostring(raddr) << " proxyaddr: "
<< sockaddr_storage_tostring(proxyaddr) << " srcaddr: "
<< sockaddr_storage_tostring(srcaddr) << " delay: " << delay
<< " period: " << period << " timeout: " << timeout << " flags: "
<< flags << " bandwidth: " << bandwidth << std::endl;
#endif
{
std::string out = "pqiperson::connect() Id: " + PeerId().toStdString();
rs_sprintf_append(out, " type: %u", type);
out += " addr: ";
out += sockaddr_storage_tostring(raddr);
out += " proxyaddr: ";
out += sockaddr_storage_tostring(proxyaddr);
out += " srcaddr: ";
out += sockaddr_storage_tostring(srcaddr);
rs_sprintf_append(out, " delay: %u", delay);
rs_sprintf_append(out, " period: %u", period);
rs_sprintf_append(out, " timeout: %u", timeout);
rs_sprintf_append(out, " flags: %u", flags);
rs_sprintf_append(out, " bandwidth: %u", bandwidth);
//std::cerr << out.str();
pqioutput(PQL_WARNING, pqipersonzone, out);
}
RS_STACK_MUTEX(mPersonMtx);
std::map<uint32_t, pqiconnect *>::iterator it;
it = kids.find(type);
if (it == kids.end())
{
#ifdef PERSON_DEBUG
//pqioutput(PQL_DEBUG_BASIC, pqipersonzone, "pqiperson::connect() missing pqiconnect");
#endif
/* notify of fail! */
pqipg->notifyConnect(PeerId(), type, false, false, raddr);
return 0;
}
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::connect() WARNING, resetting for new connection attempt" << std::endl;
std::cerr << "pqiperson::connect() resetting for new connection attempt" << std::endl;
#endif
/* set the parameters */
pqioutput(PQL_WARNING, pqipersonzone, "pqiperson::connect reset() before connection attempt");
(it->second)->reset();
#ifdef PERSON_DEBUG
std::cerr << "pqiperson::connect() WARNING, clearing rate cap" << std::endl;
std::cerr << "pqiperson::connect() clearing rate cap" << std::endl;
#endif
setRateCap_locked(0,0);
@ -585,16 +558,19 @@ int pqiperson::connect(uint32_t type, const struct sockaddr_storage &raddr,
void pqiperson::getRates(RsBwRates &rates)
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
// get the rate from the active one.
if ((!active) || (activepqi == NULL))
return;
activepqi->getRates(rates);
}
int pqiperson::gatherStatistics(std::list<RSTrafficClue>& out_lst,std::list<RSTrafficClue>& in_lst)
int pqiperson::gatherStatistics(std::list<RSTrafficClue>& out_lst,
std::list<RSTrafficClue>& in_lst)
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
// get the rate from the active one.
if ((!active) || (activepqi == NULL))
@ -602,19 +578,21 @@ int pqiperson::gatherStatistics(std::list<RSTrafficClue>& out_lst,std::list<
return activepqi->gatherStatistics(out_lst, in_lst);
}
int pqiperson::getQueueSize(bool in)
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
// get the rate from the active one.
if ((!active) || (activepqi == NULL))
return 0;
return activepqi->getQueueSize(in);
}
bool pqiperson::getCryptoParams(RsPeerCryptoParams & params)
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
if(active && activepqi != NULL)
return activepqi->getCryptoParams(params);
@ -652,27 +630,26 @@ bool pqiconnect::getCryptoParams(RsPeerCryptoParams& params)
float pqiperson::getRate(bool in)
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
// get the rate from the active one.
if ((!active) || (activepqi == NULL))
return 0;
return activepqi -> getRate(in);
}
void pqiperson::setMaxRate(bool in, float val)
{
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
RS_STACK_MUTEX(mPersonMtx);
// set to all of them. (and us)
PQInterface::setMaxRate(in, val);
// clean up the children.
std::map<uint32_t, pqiconnect *>::iterator it;
for(it = kids.begin(); it != kids.end(); ++it)
{
(it->second) -> setMaxRate(in, val);
}
}
void pqiperson::setRateCap(float val_in, float val_out)
{
@ -686,13 +663,20 @@ void pqiperson::setRateCap(float val_in, float val_out)
// The lock cannot be locked by the same thread between the first test and
// the "else" statement, so there is no possibility for this code to fail.
//
// We could actually put that code in RsMutex::lock()
// We could actually put that code in RsMutex::lock()?
// TODO: 2015/12/19 This code is already in RsMutex::lock() but is guarded
// by RSTHREAD_SELF_LOCKING_GUARD which is specifically unset in the header
// Why is that code guarded? Do it have an impact on performance?
// Or we should not get in the situation of trying to relock the mutex on
// the same thread NEVER?
if(pthread_equal(mPersonMtx.owner(),pthread_self())) // 1 - unlocked, or already locked by same thread
return setRateCap_locked(val_in, val_out); // -> do nothing
if(pthread_equal(mPersonMtx.owner(), pthread_self()))
// Unlocked, or already locked by same thread
setRateCap_locked(val_in, val_out);
else
{ // 2 - lock was free or locked by different thread => wait.
RsStackMutex stack(mPersonMtx); /**** LOCK MUTEX ****/
{
// Lock was free or locked by different thread => wait.
RS_STACK_MUTEX(mPersonMtx);
setRateCap_locked(val_in, val_out);
}
}
@ -704,10 +688,5 @@ void pqiperson::setRateCap_locked(float val_in, float val_out)
// clean up the children.
std::map<uint32_t, pqiconnect *>::iterator it;
for(it = kids.begin(); it != kids.end(); ++it)
{
(it->second)->setRateCap(val_in, val_out);
}
}

View File

@ -83,22 +83,20 @@ protected:
class pqipersongrp;
class NotifyData
{
public:
NotifyData()
:mNi(NULL), mState(0)
NotifyData() : mNi(NULL), mState(0)
{
sockaddr_storage_clear(mAddr);
}
NotifyData(NetInterface *ni, int state, const struct sockaddr_storage &addr)
:mNi(ni), mState(state), mAddr(addr) { return; }
NotifyData(NetInterface *ni, int state, const sockaddr_storage &addr) :
mNi(ni), mState(state), mAddr(addr) {}
NetInterface *mNi;
int mState;
struct sockaddr_storage mAddr;
sockaddr_storage mAddr;
};
@ -113,14 +111,14 @@ int reset();
int listen();
int stoplistening();
int connect(uint32_t type, const struct sockaddr_storage &raddr,
const struct sockaddr_storage &proxyaddr, const struct 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);
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);
int fullstopthreads();
int receiveHeartbeat();
// add in connection method.
int addChildInterface(uint32_t type, pqiconnect *pqi);
@ -130,10 +128,13 @@ virtual bool getCryptoParams(RsPeerCryptoParams&) ;
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;
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);
}
virtual RsItem *GetItem();
virtual bool RecvItem(RsItem *item);
@ -149,20 +150,18 @@ 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) ;
virtual int gatherStatistics(std::list<RSTrafficClue>& outqueue_lst,
std::list<RSTrafficClue>& inqueue_lst);
private:
void processNotifyEvents();
int handleNotifyEvent_locked(NetInterface *ni, int event, const struct sockaddr_storage &addr);
RsMutex mNotifyMtx; /**** LOCKS Notify Queue ****/
int handleNotifyEvent_locked(NetInterface *ni, int event,
const sockaddr_storage &addr);
RsMutex mNotifyMtx; // LOCKS Notify Queue
std::list<NotifyData> mNotifyQueue;
RsMutex mPersonMtx; /**** LOCKS below ****/
RsMutex mPersonMtx; // LOCKS below
int reset_locked();
@ -174,14 +173,7 @@ void setRateCap_locked(float val_in, float val_out);
bool inConnectAttempt;
int waittimes;
time_t lastHeartbeatReceived; // use to track connection failure
private: /* Helper functions */
pqipersongrp *pqipg; /* parent for callback */
};
#endif