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

Merge pull request #346 from dubslow/dhtconnect

Mostly fix #340
This commit is contained in:
Tux3 / Mlkj / !Lev.uXFMLA 2014-09-29 23:37:58 +02:00
commit 06e3ce61bc
3 changed files with 68 additions and 93 deletions

156
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++)
@ -222,18 +216,75 @@ void Core::start()
}
else
qDebug() << "Core: Error loading self avatar";
process(); // starts its own timer
}
bootstrapDht();
void Core::process()
{
if (!tox)
return;
static int retries = 0;
tox_do(tox);
#ifdef DEBUG
//we want to see the debug messages immediately
fflush(stdout);
#endif
if (checkConnection())
retries = 0;
else if (retries < 2)
{
retries++;
bootstrapDht();
}
toxTimer->start(tox_do_interval(tox));
}
void Core::onBootstrapTimer()
bool Core::checkConnection()
{
if (!tox)
return;
if(!tox_isconnected(tox))
bootstrapDht();
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 < 2)
{
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)
@ -512,20 +563,13 @@ void Core::onAvatarInfoCallback(Tox*, int32_t friendnumber, uint8_t format,
Core* core = static_cast<Core*>(_core);
if (format == TOX_AVATAR_FORMAT_NONE)
{
qDebug() << "Core: Got null avatar info from" << friendnumber;
emit core->friendAvatarRemoved(friendnumber);
}
else
{
QByteArray oldHash = Settings::getInstance().getAvatarHash(core->getFriendAddress(friendnumber));
if (QByteArray((char*)hash, TOX_HASH_LENGTH) != oldHash) // comparison failed miserably if I didn't convert hash to QByteArray
{
qDebug() << "Core: got different avatar hash from" << friendnumber;
if (QByteArray((char*)hash, TOX_HASH_LENGTH) != oldHash)
// comparison failed miserably if I didn't convert hash to QByteArray
tox_request_avatar_data(core->tox, friendnumber);
}
else
qDebug() << "Core: Got old avatar info from" << friendnumber;
}
}
@ -534,11 +578,8 @@ void Core::onAvatarDataCallback(Tox*, int32_t friendnumber, uint8_t,
{
QPixmap pic;
pic.loadFromData((uchar*)data, datalen);
if (pic.isNull())
qDebug() << "Core: Got null avatar from "<<friendnumber;
else
if (!pic.isNull())
{
qDebug() << "Core: Got avatar data from "<<friendnumber<<", size:"<<pic.size();
Settings::getInstance().saveAvatar(pic, static_cast<Core*>(core)->getFriendAddress(friendnumber));
Settings::getInstance().saveAvatarHash(QByteArray((char*)hash, TOX_HASH_LENGTH), static_cast<Core*>(core)->getFriendAddress(friendnumber));
emit static_cast<Core*>(core)->friendAvatarChanged(friendnumber, pic);
@ -927,69 +968,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);

3
core.h
View File

@ -207,8 +207,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();

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