1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

work torwards #340, CAUTION

spams bootstrapDht() until connection is confirmed, so it's not quite right...
This commit is contained in:
dubslow 2014-09-28 18:08:07 -05:00
parent 84f56b81a7
commit c037edd033
3 changed files with 59 additions and 79 deletions

132
core.cpp
View File

@ -49,15 +49,9 @@ Core::Core(Camera* cam, QThread *coreThread) :
toxTimer = new QTimer(this);
toxTimer->setSingleShot(true);
//saveTimer = new QTimer(this);
//saveTimer->start(TOX_SAVE_INTERVAL);
bootstrapTimer = new QTimer(this);
bootstrapTimer->start(TOX_BOOTSTRAP_INTERVAL);
connect(toxTimer, &QTimer::timeout, this, &Core::process);
//connect(saveTimer, &QTimer::timeout, this, &Core::saveConfiguration); //Disable save timer in favor of saving on events
//connect(fileTimer, &QTimer::timeout, this, &Core::fileHeartbeat);
connect(bootstrapTimer, &QTimer::timeout, this, &Core::onBootstrapTimer);
connect(&Settings::getInstance(), &Settings::dhtServerListChanged, this, &Core::bootstrapDht);
connect(&Settings::getInstance(), &Settings::dhtServerListChanged, this, &Core::process);
connect(this, SIGNAL(fileTransferFinished(ToxFile)), this, SLOT(onFileTransferFinished(ToxFile)));
for (int i=0; i<TOXAV_MAX_CALLS;i++)
@ -223,17 +217,68 @@ void Core::start()
else
qDebug() << "Core: Error loading self avatar";
bootstrapDht();
toxTimer->start(tox_do_interval(tox));
TOX_DO_INTERVAL = tox_do_interval(tox);
process(); // starts its own timer
}
void Core::onBootstrapTimer()
void Core::process()
{
if (!tox)
return;
if(!tox_isconnected(tox))
tox_do(tox);
#ifdef DEBUG
//we want to see the debug messages immediately
fflush(stdout);
#endif
if (!checkConnection())
bootstrapDht();
toxTimer->start(TOX_DO_INTERVAL);
}
bool Core::checkConnection()
{
static bool isConnected = false;
bool toxConnected = tox_isconnected(tox);
if (toxConnected && !isConnected) {
qDebug() << "Core: Connected to DHT";
emit connected();
isConnected = true;
} else if (!toxConnected && isConnected) {
qDebug() << "Core: Disconnected to DHT";
emit disconnected();
isConnected = false;
}
return isConnected;
}
void Core::bootstrapDht()
{
const Settings& s = Settings::getInstance();
QList<Settings::DhtServer> dhtServerList = s.getDhtServerList();
int listSize = dhtServerList.size();
static int j = qrand() % listSize;
qDebug() << "Core: Bootstraping to the DHT ...";
int i=0;
while (i < 3)
{
const Settings::DhtServer& dhtServer = dhtServerList[j % listSize];
if (tox_bootstrap_from_address(tox, dhtServer.address.toLatin1().data(),
dhtServer.port, CUserId(dhtServer.userId).data()) == 1)
qDebug() << QString("Core: Bootstraping from ")+dhtServer.name+QString(", addr ")+dhtServer.address.toLatin1().data()
+QString(", port ")+QString().setNum(dhtServer.port);
else
qDebug() << "Core: Error bootstraping from "+dhtServer.name;
j++;
i++;
}
}
void Core::onFriendRequest(Tox*/* tox*/, const uint8_t* cUserId, const uint8_t* cMessage, uint16_t cMessageSize, void* core)
@ -861,69 +906,6 @@ void Core::onFileTransferFinished(ToxFile file)
emit fileDownloadFinished(file.filePath);
}
void Core::bootstrapDht()
{
const Settings& s = Settings::getInstance();
QList<Settings::DhtServer> dhtServerList = s.getDhtServerList();
int listSize = dhtServerList.size();
static int j = qrand() % listSize, n=0;
// We couldn't connect after trying 6 different nodes, let's try something else
if (n>3)
{
qDebug() << "Core: We're having trouble connecting to the DHT, slowing down";
bootstrapTimer->setInterval(TOX_BOOTSTRAP_INTERVAL*(n-1));
}
else
qDebug() << "Core: Connecting to the DHT ...";
int i=0;
while (i < (2 - (n>3)))
{
const Settings::DhtServer& dhtServer = dhtServerList[j % listSize];
if (tox_bootstrap_from_address(tox, dhtServer.address.toLatin1().data(),
dhtServer.port, CUserId(dhtServer.userId).data()) == 1)
qDebug() << QString("Core: Bootstraping from ")+dhtServer.name+QString(", addr ")+dhtServer.address.toLatin1().data()
+QString(", port ")+QString().setNum(dhtServer.port);
else
qDebug() << "Core: Error bootstraping from "+dhtServer.name;
tox_do(tox);
j++;
i++;
n++;
}
}
void Core::process()
{
tox_do(tox);
#ifdef DEBUG
//we want to see the debug messages immediately
fflush(stdout);
#endif
checkConnection();
//int toxInterval = tox_do_interval(tox);
//qDebug() << QString("Tox interval %1").arg(toxInterval);
toxTimer->start(50);
}
void Core::checkConnection()
{
static bool isConnected = false;
if (tox_isconnected(tox) && !isConnected) {
qDebug() << "Core: Connected to DHT";
emit connected();
isConnected = true;
} else if (!tox_isconnected(tox) && isConnected) {
qDebug() << "Core: Disconnected to DHT";
emit disconnected();
isConnected = false;
}
}
void Core::loadConfiguration()
{
QString path = QDir(Settings::getSettingsDirPath()).filePath(CONFIG_FILE_NAME);

4
core.h
View File

@ -206,8 +206,7 @@ private:
static void playCallVideo(ToxAv* toxav, int32_t callId, vpx_image_t* img, void *user_data);
void sendCallVideo(int callId);
void checkConnection();
void onBootstrapTimer();
bool checkConnection();
void loadConfiguration();
void loadFriends();
@ -236,6 +235,7 @@ private:
static const int videobufsize;
static uint8_t* videobuf;
static int videoBusyness; // Used to know when to drop frames
int TOX_DO_INTERVAL;
static ALCdevice* alOutDev, *alInDev;
static ALCcontext* alContext;

View File

@ -3,9 +3,7 @@
#define TOXAV_MAX_CALLS 16
#define GROUPCHAT_MAX_SIZE 32
#define TOX_SAVE_INTERVAL 30*1000
#define TOX_FILE_INTERVAL 0
#define TOX_BOOTSTRAP_INTERVAL 5*1000
#define TOXAV_RINGING_TIME 15
// TODO: Put that in the settings