mirror of
https://github.com/kunkundi/crossdesk-server.git
synced 2026-03-18 13:07:50 +08:00
[fix] create db file if it does not exist
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <openssl/sha.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <filesystem>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
@@ -11,6 +12,14 @@
|
||||
#include "log.h"
|
||||
|
||||
DeviceDBManager::DeviceDBManager(const std::string& db_path) : db_(nullptr) {
|
||||
try {
|
||||
std::filesystem::path path(db_path);
|
||||
std::filesystem::create_directories(path.parent_path());
|
||||
} catch (const std::exception& e) {
|
||||
throw std::runtime_error("Failed to create parent directory for DB: " +
|
||||
std::string(e.what()));
|
||||
}
|
||||
|
||||
if (sqlite3_open(db_path.c_str(), &db_) != SQLITE_OK) {
|
||||
LOG_ERROR("Failed to open database, {}", sqlite3_errmsg(db_));
|
||||
}
|
||||
|
||||
@@ -36,8 +36,9 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
InitLogger(log_dir);
|
||||
|
||||
SignalServer s;
|
||||
s.Run(std::stoi(port), certs_dir, db_path);
|
||||
SignalServer s(std::stoi(port), certs_dir, db_path);
|
||||
s.Run();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -37,6 +37,44 @@ SignalServer::SignalServer() {
|
||||
std::placeholders::_2));
|
||||
}
|
||||
|
||||
SignalServer::SignalServer(uint16_t port, std::string certs_dir,
|
||||
std::string db_path)
|
||||
: port_(port), certs_dir_(certs_dir), db_path_(db_path) {
|
||||
LOG_INFO(
|
||||
"Starting CrossDesk Signaling Server on port {}, certs_dir: {}, "
|
||||
"db_path: {}",
|
||||
port_, certs_dir_, db_path_);
|
||||
|
||||
server_.set_error_channels(websocketpp::log::elevel::none);
|
||||
server_.set_access_channels(websocketpp::log::alevel::none);
|
||||
server_.init_asio();
|
||||
|
||||
server_.set_open_handler(
|
||||
std::bind(&SignalServer::OnOpen, this, std::placeholders::_1));
|
||||
server_.set_close_handler(
|
||||
std::bind(&SignalServer::OnClose, this, std::placeholders::_1));
|
||||
server_.set_fail_handler(
|
||||
std::bind(&SignalServer::OnFail, this, std::placeholders::_1));
|
||||
server_.set_message_handler(std::bind(&SignalServer::OnMessage, this,
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2));
|
||||
server_.set_tls_init_handler(
|
||||
std::bind(&SignalServer::OnTlsInit, this, std::placeholders::_1));
|
||||
server_.set_ping_handler(std::bind(&SignalServer::OnPing, this,
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2));
|
||||
server_.set_pong_handler(std::bind(&SignalServer::OnPing, this,
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2));
|
||||
|
||||
transmission_manager_ = std::make_shared<TransmissionManager>();
|
||||
signal_negotiation_ =
|
||||
std::make_unique<SignalNegotiation>(transmission_manager_, db_path_);
|
||||
signal_negotiation_->SetSendMsgCallback(std::bind(&SignalServer::SendMsg,
|
||||
this, std::placeholders::_1,
|
||||
std::placeholders::_2));
|
||||
}
|
||||
|
||||
SignalServer::~SignalServer() {}
|
||||
|
||||
bool SignalServer::OnOpen(websocketpp::connection_hdl hdl) {
|
||||
@@ -98,20 +136,16 @@ bool SignalServer::OnPong(websocketpp::connection_hdl hdl, std::string s) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void SignalServer::Run(uint16_t port, std::string certs_dir,
|
||||
std::string db_path) {
|
||||
certs_dir_ = certs_dir;
|
||||
db_path_ = db_path;
|
||||
|
||||
void SignalServer::Run() {
|
||||
if (!std::filesystem::exists(certs_dir_)) {
|
||||
LOG_ERROR("Certs dir [{}] not exist", certs_dir_);
|
||||
return;
|
||||
}
|
||||
|
||||
server_.set_reuse_addr(true);
|
||||
LOG_INFO("Signal server runs on port [{}]", port);
|
||||
LOG_INFO("Signal server runs on port [{}]", port_);
|
||||
|
||||
server_.listen(port);
|
||||
server_.listen(port_);
|
||||
server_.start_accept();
|
||||
server_.run();
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ typedef unsigned int connection_id;
|
||||
class SignalServer {
|
||||
public:
|
||||
SignalServer();
|
||||
SignalServer(uint16_t port, std::string certs_dir, std::string db_path);
|
||||
~SignalServer();
|
||||
|
||||
bool OnOpen(websocketpp::connection_hdl hdl);
|
||||
@@ -35,12 +36,13 @@ class SignalServer {
|
||||
bool OnPing(websocketpp::connection_hdl hdl, std::string s);
|
||||
bool OnPong(websocketpp::connection_hdl hdl, std::string s);
|
||||
|
||||
void Run(uint16_t port, std::string certs_dir, std::string db_path);
|
||||
void Run();
|
||||
void SendMsg(websocketpp::connection_hdl hdl, json message);
|
||||
void OnMessage(websocketpp::connection_hdl hdl, server::message_ptr msg);
|
||||
|
||||
private:
|
||||
server server_;
|
||||
uint16_t port_;
|
||||
std::string certs_dir_;
|
||||
std::string db_path_;
|
||||
std::map<websocketpp::connection_hdl, connection_id,
|
||||
|
||||
Reference in New Issue
Block a user