diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Channel.cpp | 2 | ||||
-rw-r--r-- | src/main.cpp | 23 |
2 files changed, 12 insertions, 13 deletions
diff --git a/src/Channel.cpp b/src/Channel.cpp index 501e5bc..6107a22 100644 --- a/src/Channel.cpp +++ b/src/Channel.cpp @@ -10,7 +10,7 @@ namespace dchat { static Channel *currentChannel; - Channel::Channel(const string &_name, const odhtdb::DatabaseNode &_databaseNodeInfo, User *_localUser, odhtdb::Database *_database) : + Channel::Channel(const string &_name, const odhtdb::DatabaseNode &_databaseNodeInfo, User *_localUser, std::shared_ptr<odhtdb::Database> _database) : database(_database), databaseNodeInfo(_databaseNodeInfo), name(_name), diff --git a/src/main.cpp b/src/main.cpp index afdff99..e60f77c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -294,13 +294,13 @@ int main(int argc, char **argv) bool loggedIn = false; sf::Clock lastFocusedTimer; - odhtdb::Database *database = nullptr; + shared_ptr<odhtdb::Database> database = nullptr; odhtdb::DatabaseCallbackFuncs callbackFuncs; using LocalUserMessageCallback = function<void(const odhtdb::DatabaseAddNodeRequest &request)>; LocalUserMessageCallback onMessageByLocalUser = nullptr; - callbackFuncs.createNodeCallbackFunc = [&waitingToJoinChannels, &database, &channels, &channelMessageMutex, &waitingToJoin, &localNodeUsers, &lastFocusedTimer](const odhtdb::DatabaseCreateNodeRequest &request) + callbackFuncs.createNodeCallbackFunc = [&waitingToJoinChannels, database, &channels, &channelMessageMutex, &waitingToJoin, &localNodeUsers, &lastFocusedTimer](const odhtdb::DatabaseCreateNodeRequest &request) { lock_guard<recursive_mutex> lock(channelMessageMutex); printf("Create node callback func %s\n", request.nodeHash->toString().c_str()); @@ -398,7 +398,7 @@ int main(int argc, char **argv) } } }; - database = new odhtdb::Database("83.252.53.188", 27130, Cache::getDchatDir(), callbackFuncs); + database = odhtdb::Database::connect("83.252.53.188", 27130, Cache::getDchatDir(), callbackFuncs).get(); auto addSystemMessage = [&lastFocusedTimer](const std::string &msg, bool plainText = true) { @@ -407,7 +407,7 @@ int main(int argc, char **argv) }; // Login to account - Command::add("login", [&localNodeUsers, &database, &channels, &loggedIn, &channelMessageMutex, ¤tUsername, ¤tPassword](const vector<string> &args) + Command::add("login", [&localNodeUsers, database, &channels, &loggedIn, &channelMessageMutex, ¤tUsername, ¤tPassword](const vector<string> &args) { lock_guard<recursive_mutex> lock(channelMessageMutex); if(args.size() != 2) @@ -445,7 +445,7 @@ int main(int argc, char **argv) }); // Register account - Command::add("register", [&localNodeUsers, &database, &channels, &loggedIn, &channelMessageMutex, ¤tUsername, ¤tPassword](const vector<string> &args) + Command::add("register", [&localNodeUsers, database, &channels, &loggedIn, &channelMessageMutex, ¤tUsername, ¤tPassword](const vector<string> &args) { lock_guard<recursive_mutex> lock(channelMessageMutex); if(args.size() != 2) @@ -482,7 +482,7 @@ int main(int argc, char **argv) // TODO: Use database->addData to change channel name // Create channel - Command::add("cc", [&database, &channels, &channelMessageMutex, &loggedIn, &localNodeUsers, ¤tUsername, ¤tPassword, &lastFocusedTimer, addSystemMessage](const vector<string> &args) + Command::add("cc", [database, &channels, &channelMessageMutex, &loggedIn, &localNodeUsers, ¤tUsername, ¤tPassword, &lastFocusedTimer, addSystemMessage](const vector<string> &args) { lock_guard<recursive_mutex> lock(channelMessageMutex); if(args.size() != 1) @@ -515,7 +515,7 @@ int main(int argc, char **argv) }); // Create invite key - Command::add("invite", [&channelMessageMutex, &offlineChannel, &database, addSystemMessage](const vector<string> &args) + Command::add("invite", [&channelMessageMutex, &offlineChannel, database, addSystemMessage](const vector<string> &args) { lock_guard<recursive_mutex> lock(channelMessageMutex); if(args.size() != 0) @@ -552,7 +552,7 @@ int main(int argc, char **argv) keySerializer.add((const u8*)channelNodeHash->getData(), channelNodeHash->getSize()); keySerializer.add((const u8*)encryptionKey->data, odhtdb::ENCRYPTION_KEY_BYTE_SIZE); odhtdb::InfoHash key = odhtdb::Database::getInfoHash(keySerializer.getBuffer().data(), keySerializer.getBuffer().size()); - database->receiveCustomMessage(key, [&channelMessageMutex, encryptionKey, channelEncryptionKey, currentChannel, &database](const void *data, usize size) + database->receiveCustomMessage(key, [&channelMessageMutex, encryptionKey, channelEncryptionKey, currentChannel, database](const void *data, usize size) { // TODO: User can remove channel @currentChannel before we get here, meaning @currentChannel is deleted and would be invalid; causing the program to crash try @@ -599,7 +599,7 @@ int main(int argc, char **argv) }); // Join channel using invite key - Command::add("jc", [&loggedIn, &database, &localNodeUsers, &channelMessageMutex, &waitingToJoin, &waitingToJoinChannels, ¤tUsername, ¤tPassword](const vector<string> &args) + Command::add("jc", [&loggedIn, database, &localNodeUsers, &channelMessageMutex, &waitingToJoin, &waitingToJoinChannels, ¤tUsername, ¤tPassword](const vector<string> &args) { lock_guard<recursive_mutex> lock(channelMessageMutex); if(args.size() != 1) @@ -651,7 +651,7 @@ int main(int argc, char **argv) keySerializer.add((const u8*)encryptionKeyBinRaw.data(), odhtdb::ENCRYPTION_KEY_BYTE_SIZE); odhtdb::InfoHash key = odhtdb::Database::getInfoHash(keySerializer.getBuffer().data(), keySerializer.getBuffer().size()); database->sendCustomMessage(key, serializer.getBuffer().data(), serializer.getBuffer().size(), - [&database, nodeHash, encryptionKey, &waitingToJoinChannels, &channelMessageMutex, keyPair, ¤tUsername, ¤tPassword, &localNodeUsers] + [database, nodeHash, encryptionKey, &waitingToJoinChannels, &channelMessageMutex, keyPair, ¤tUsername, ¤tPassword, &localNodeUsers] (bool gotResponse, const void *data, usize size) { if(!gotResponse) @@ -918,7 +918,7 @@ int main(int argc, char **argv) addSystemMessage(msg); }); - Command::add("clearcache", [&database](const vector<string> &args) + Command::add("clearcache", [database](const vector<string> &args) { printf("Cleared cache (%d bytes)\n", database->clearCache()); }); @@ -1296,7 +1296,6 @@ int main(int argc, char **argv) // We need to wait until our `ping` packet for disconnecting is sent to all channels printf("Shutting down...\n"); this_thread::sleep_for(chrono::seconds(3)); - delete database; return 0; } |