File indexing completed on 2025-01-05 04:37:24

0001 /*
0002     SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "globals.h"
0007 
0008 #include "server.h"
0009 #include <dht/dht.h>
0010 #include <net/portlist.h>
0011 #include <net/reverseresolver.h>
0012 #include <utp/utpserver.h>
0013 
0014 namespace bt
0015 {
0016 Globals *Globals::inst = nullptr;
0017 
0018 Globals::Globals()
0019 {
0020     plist = new net::PortList();
0021     tcp_server = nullptr;
0022     utp_server = nullptr;
0023     dh_table = new dht::DHT();
0024 }
0025 
0026 Globals::~Globals()
0027 {
0028     // shutdown the reverse resolver thread
0029     net::ReverseResolver::shutdown();
0030     shutdownUTPServer();
0031     delete tcp_server;
0032     delete dh_table;
0033     delete plist;
0034 }
0035 
0036 Globals &Globals::instance()
0037 {
0038     if (!inst)
0039         inst = new Globals();
0040     return *inst;
0041 }
0042 
0043 void Globals::cleanup()
0044 {
0045     delete inst;
0046     inst = nullptr;
0047 }
0048 
0049 bool Globals::initTCPServer(Uint16 port)
0050 {
0051     if (tcp_server)
0052         shutdownTCPServer();
0053 
0054     tcp_server = new Server();
0055     if (!tcp_server->changePort(port))
0056         return false;
0057 
0058     return true;
0059 }
0060 
0061 void Globals::shutdownTCPServer()
0062 {
0063     delete tcp_server;
0064     tcp_server = nullptr;
0065 }
0066 
0067 bool Globals::initUTPServer(Uint16 port)
0068 {
0069     if (utp_server)
0070         shutdownUTPServer();
0071 
0072     utp_server = new utp::UTPServer();
0073     if (!utp_server->changePort(port))
0074         return false;
0075 
0076     utp_server->start();
0077     return true;
0078 }
0079 
0080 void Globals::shutdownUTPServer()
0081 {
0082     if (utp_server) {
0083         utp_server->stop();
0084         delete utp_server;
0085         utp_server = nullptr;
0086     }
0087 }
0088 
0089 }