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

0001 /*
0002     SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #ifndef NETPORTLIST_H
0007 #define NETPORTLIST_H
0008 
0009 #include <QList>
0010 #include <ktorrent_export.h>
0011 #include <util/constants.h>
0012 
0013 namespace net
0014 {
0015 enum Protocol {
0016     TCP,
0017     UDP,
0018 };
0019 
0020 struct KTORRENT_EXPORT Port {
0021     bt::Uint16 number;
0022     Protocol proto;
0023     bool forward;
0024 
0025     Port();
0026     Port(bt::Uint16 number, Protocol proto, bool forward);
0027     Port(const Port &p);
0028 
0029     bool operator==(const Port &p) const;
0030 };
0031 
0032 /**
0033  * Listener class for the PortList.
0034  */
0035 class KTORRENT_EXPORT PortListener
0036 {
0037 public:
0038     virtual ~PortListener()
0039     {
0040     }
0041 
0042     /**
0043      * A port has been added.
0044      * @param port The port
0045      */
0046     virtual void portAdded(const Port &port) = 0;
0047 
0048     /**
0049      * A port has been removed
0050      * @param port The port
0051      */
0052     virtual void portRemoved(const Port &port) = 0;
0053 };
0054 
0055 /**
0056  * @author Joris Guisson <joris.guisson@gmail.com>
0057  *
0058  * List of ports which are currently being used.
0059  *
0060  */
0061 class KTORRENT_EXPORT PortList : public QList<Port>
0062 {
0063     PortListener *lst;
0064 
0065 public:
0066     PortList();
0067     virtual ~PortList();
0068 
0069     /**
0070      * When a port is in use, this function needs to be called.
0071      * @param number Port number
0072      * @param proto Protocol
0073      * @param forward Whether or not it needs to be forwarded
0074      */
0075     void addNewPort(bt::Uint16 number, Protocol proto, bool forward);
0076 
0077     /**
0078      * Needs to be called when a port is not being using anymore.
0079      * @param number Port number
0080      * @param proto Protocol
0081      */
0082     void removePort(bt::Uint16 number, Protocol proto);
0083 
0084     /**
0085      * Set the port listener.
0086      * @param pl Port listener
0087      */
0088     void setListener(PortListener *pl)
0089     {
0090         lst = pl;
0091     }
0092 };
0093 
0094 }
0095 
0096 #ifdef Q_CC_MSVC
0097 #include <QHash>
0098 inline size_t qHash(const net::Port &port, size_t seed = 0) noexcept
0099 {
0100     return qHash((uint)port.number, seed);
0101 }
0102 #endif
0103 
0104 #endif