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

0001 /*
0002     SPDX-FileCopyrightText: 2009 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef NET_REVERSERESOLVER_H
0008 #define NET_REVERSERESOLVER_H
0009 
0010 #include <QMutex>
0011 #include <QObject>
0012 #include <QThread>
0013 #include <QWaitCondition>
0014 #include <ktorrent_export.h>
0015 #include <net/address.h>
0016 
0017 namespace net
0018 {
0019 class ReverseResolverThread;
0020 
0021 /**
0022     Resolve an IP address into a hostname
0023     This should be threated as fire and forget objects, when using them asynchronously.
0024     The worker thread will delete them, when they are done.
0025 */
0026 class ReverseResolver : public QObject
0027 {
0028     Q_OBJECT
0029 public:
0030     ReverseResolver(QObject *parent = nullptr);
0031     ~ReverseResolver() override;
0032 
0033     /**
0034         Resolve an ip address asynchronously, uses the worker thread
0035         Connecting to the resolved signal should be done with Qt::QueuedConnection, seeing
0036         that it will be emitted from the worker thread.
0037         @param addr The address
0038     */
0039     void resolveAsync(const net::Address &addr);
0040 
0041     /**
0042         Resolve an ip address synchronously.
0043     */
0044     QString resolve(const net::Address &addr);
0045 
0046     /**
0047         Run the actual resolve and emit the signal when done
0048     */
0049     void run();
0050 
0051     /// Shutdown the worker thread
0052     static void shutdown();
0053 
0054 Q_SIGNALS:
0055     /// Emitted when the resolution is complete
0056     void resolved(const QString &host);
0057 
0058 private:
0059     static ReverseResolverThread *worker;
0060     net::Address addr_to_resolve;
0061 };
0062 
0063 class ReverseResolverThread : public QThread
0064 {
0065     Q_OBJECT
0066 public:
0067     ReverseResolverThread();
0068     ~ReverseResolverThread() override;
0069 
0070     /// Add a ReverseResolver to the todo list
0071     void add(ReverseResolver *rr);
0072 
0073     /// Run the thread
0074     void run() override;
0075 
0076     /// Stop the thread
0077     void stop();
0078 
0079 private:
0080     QMutex mutex;
0081     QWaitCondition more_data;
0082     QList<ReverseResolver *> todo_list;
0083     bool stopped;
0084 };
0085 
0086 }
0087 
0088 #endif // NET_REVERSERESOLVER_H