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

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 UTP_UTPSERVER_P_H
0008 #define UTP_UTPSERVER_P_H
0009 
0010 #include "connection.h"
0011 #include "outputqueue.h"
0012 #include "pollpipe.h"
0013 #include "utpserver.h"
0014 #include "utpsocket.h"
0015 
0016 #include <QMap>
0017 #include <QRecursiveMutex>
0018 #include <QSocketNotifier>
0019 #include <QTimer>
0020 
0021 #include <ktorrent_export.h>
0022 #include <net/poll.h>
0023 #include <net/serversocket.h>
0024 #include <net/socket.h>
0025 #include <net/wakeuppipe.h>
0026 #include <util/ptrmap.h>
0027 
0028 namespace utp
0029 {
0030 class UTPServerThread;
0031 
0032 /**
0033     Utility class used by UTPServer to make sure that ServerInterface::newConnection is called
0034     from the main thread and not from UTP thread (which is dangerous).
0035 */
0036 class MainThreadCall : public QObject
0037 {
0038     Q_OBJECT
0039 public:
0040     MainThreadCall(UTPServer *server);
0041     ~MainThreadCall() override;
0042 
0043 public Q_SLOTS:
0044     /**
0045         Calls UTPServer::handlePendingConnections, this should be executed in
0046         the main thread.
0047     */
0048     void handlePendingConnections();
0049 
0050 private:
0051     UTPServer *server;
0052 };
0053 
0054 typedef bt::PtrMap<quint16, Connection>::iterator ConItr;
0055 
0056 struct PollPipePair {
0057     PollPipe::Ptr read_pipe;
0058     PollPipe::Ptr write_pipe;
0059 
0060     PollPipePair();
0061 
0062     bool testRead(ConItr b, ConItr e);
0063     bool testWrite(ConItr b, ConItr e);
0064 };
0065 
0066 typedef bt::PtrMap<net::Poll *, PollPipePair>::iterator PollPipePairItr;
0067 typedef QMap<quint16, Connection::Ptr>::iterator ConnectionMapItr;
0068 
0069 class UTPServer::Private : public net::ServerSocket::DataHandler
0070 {
0071 public:
0072     Private(UTPServer *p);
0073     ~Private() override;
0074 
0075     bool bind(const net::Address &addr);
0076     void syn(const PacketParser &parser, bt::Buffer::Ptr buffer, const net::Address &addr);
0077     void reset(const Header *hdr);
0078     void wakeUpPollPipes(Connection::Ptr conn, bool readable, bool writeable);
0079     Connection::Ptr find(quint16 conn_id);
0080     void stop();
0081     void dataReceived(bt::Buffer::Ptr buffer, const net::Address &addr) override;
0082     void readyToWrite(net::ServerSocket *sock) override;
0083 
0084 public:
0085     UTPServer *p;
0086     QList<net::ServerSocket::Ptr> sockets;
0087     bool running;
0088     QMap<quint16, Connection::Ptr> connections;
0089     UTPServerThread *utp_thread;
0090     QRecursiveMutex mutex;
0091     bt::PtrMap<net::Poll *, PollPipePair> poll_pipes;
0092     bool create_sockets;
0093     bt::Uint8 tos;
0094     OutputQueue output_queue;
0095     QList<mse::EncryptedPacketSocket::Ptr> pending;
0096     QMutex pending_mutex;
0097     MainThreadCall *mtc;
0098     QList<Connection::WPtr> last_accepted;
0099     QTimer *timer;
0100 };
0101 }
0102 
0103 #endif