File indexing completed on 2024-04-21 14:55:39

0001 /*
0002  * This file is part of the KDE libraries
0003  * Copyright (C) 2007 Thiago Macieira <thiago@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #ifndef KSOCKETFACTORY_H
0022 #define KSOCKETFACTORY_H
0023 
0024 #include <kdelibs4support_export.h>
0025 #include <QString>
0026 #include <QtNetwork/QNetworkProxy>
0027 #include <QtNetwork/QHostAddress>
0028 
0029 class QTcpSocket;
0030 class QTcpServer;
0031 class QUdpSocket;
0032 class QUrl;
0033 
0034 /**
0035  * @namespace KSocketFactory
0036  * @brief KSocketFactory provides functions for opening sockets to remote hosts
0037  *
0038  * KSocketFactory is a socket-opener group of functions that must be
0039  * used whenever a KDE application wants to communicate with a remote
0040  * host. It will determine the necessary proxy and local KDE settings,
0041  * then open the connection. The typical use-case is:
0042  *
0043  * <code>
0044  *   d->socket = KSocketFactory::connectToHost("www.kde.org", "http");
0045  *   d->socket->setParent(this);
0046  *   QObject::connect(d->socket, SIGNAL(connected()),
0047  *                    this, SLOT(socketConnected()));
0048  * </code>
0049  *
0050  * Synchronous operation is not recommended, since it may lead to UI
0051  * deadlocks. It is preferred to return to the mainloop if the socket
0052  * is being created in the main GUI thread.
0053  *
0054  * However, if the socket is created in an auxiliary thread, it is
0055  * possible to use something equivalent to synchronous mode:
0056  *
0057  * <code>
0058  *   d->socket = KSocketFactory::synchronousConnectToHost("www.kde.org", "http");
0059  *   d->socket->setParent(this);
0060  * </code>
0061  *
0062  * All objects returned from these functions belong to the caller and
0063  * must be disposed of properly. Calling QObject::setParent() or
0064  * passing a parent object is the recommended way.
0065  *
0066  * @author Thiago Macieira <thiago@kde.org>
0067  */
0068 namespace KSocketFactory
0069 {
0070 /**
0071  * Initiates a TCP/IP socket connection to remote node (host) @p
0072  * host, using the @p protocol. Returns a QTcpSocket
0073  * that is connecting (i.e., in a state before
0074  * QAbstractSocket::Connected) in most cases, even if the target
0075  * service doesn't exist or if a connection failure happens.
0076  *
0077  * This function never returns 0. If the returned socket cannot
0078  * connect, it will emit the QAbstractSocket::error()
0079  * signal. Otherwise, the QAbstractSocket::connected() signal will
0080  * be emitted eventually.
0081  *
0082  * The @p protocol parameter <b>must</b> be a string representation
0083  * of the protocol being attempted, like "http", "ftp" or
0084  * "xmpp". It might be used to determine the proxy type -- for
0085  * instance, the proxy for HTTP connections might be different
0086  * from the proxy for other connections. Pass an empty QString if
0087  * the connection is of no established protocol.
0088  *
0089  * The @p port parameter can be used to specify the port
0090  * number lookup if the service is not a well-known service.
0091  *
0092  * @param protocol  the protocol this socket will use
0093  * @param host      the remote node (host) to connect to
0094  * @param port      the port number to connect to
0095  * @param parent    the parent object to be passed to the
0096  *                  QTcpSocket constructor
0097  * @threadsafe
0098  */
0099 KDELIBS4SUPPORT_DEPRECATED_EXPORT QTcpSocket *connectToHost(const QString &protocol, const QString &host,
0100         quint16 port, QObject *parent = nullptr);
0101 
0102 /**
0103  * @overload
0104  */
0105 KDELIBS4SUPPORT_DEPRECATED_EXPORT QTcpSocket *connectToHost(const QUrl &url, QObject *parent = nullptr);
0106 
0107 /**
0108  * @overload
0109  */
0110 KDELIBS4SUPPORT_DEPRECATED_EXPORT void connectToHost(QTcpSocket *socket, const QString &protocol,
0111         const QString &host, quint16 port);
0112 
0113 /**
0114  * @overload
0115  */
0116 KDELIBS4SUPPORT_DEPRECATED_EXPORT void connectToHost(QTcpSocket *socket, const QUrl &url);
0117 
0118 /**
0119  * This function behaves exactly like connectToHost() above, except
0120  * that the socket it returns is either in
0121  * QAbstractSocket::Connected state, or it has failed connecting
0122  * altogether.
0123  *
0124  * This function should be used if a synchronous connection is
0125  * necessary instead of calling
0126  * QAbstractSocket::waitForConnected(), since that may not work on
0127  * all objects returned from connectToHost().
0128  *
0129  * This function does not return 0. If the connection attempt
0130  * failed for some reason, the socket state will be
0131  * QAbstractSocket::UnconnectedState and QAbstractSocket::isValid
0132  * will return false. The socket error state and string will
0133  * contain more information.
0134  * @warning: This function may block.
0135  *
0136  * @param protocol  the protocol this socket will use
0137  * @param host      the remote node (host) to connect to
0138  * @param port      the port number to connect to
0139  * @param msecs     the time (in milliseconds) to wait while
0140  *                  attempting to connect
0141  * @param parent    the parent object to be passed to the
0142  *                  QTcpSocket constructor
0143  * @threadsafe
0144  */
0145 KDELIBS4SUPPORT_DEPRECATED_EXPORT QTcpSocket *synchronousConnectToHost(const QString &protocol,
0146         const QString &host,
0147         quint16 port, int msecs = 30000,
0148         QObject *parent = nullptr);
0149 
0150 /**
0151  * @overload
0152  */
0153 KDELIBS4SUPPORT_DEPRECATED_EXPORT QTcpSocket *synchronousConnectToHost(const QUrl &url, int msecs = 30000,
0154         QObject *parent = nullptr);
0155 
0156 /**
0157  * @overload
0158  */
0159 KDELIBS4SUPPORT_DEPRECATED_EXPORT void synchronousConnectToHost(QTcpSocket *socket, const QString &protocol,
0160         const QString &host, quint16 port,
0161         int msecs = 30000);
0162 
0163 /**
0164  * @overload
0165  */
0166 KDELIBS4SUPPORT_DEPRECATED_EXPORT void synchronousConnectToHost(QTcpSocket *socket, const QUrl &url,
0167         int msecs = 30000);
0168 
0169 /**
0170  * Opens a TCP/IP socket for listening protocol @p protocol, binding
0171  * only at address @p address. The @p port parameter indicates the
0172  * port to be opened.
0173  *
0174  * This function does not return 0. If the opening of the socket
0175  * failed for some reason, it will return a QTcpServer object that
0176  * is not listening (QTcpServer::isListening() returns false),
0177  * with a properly set error string indicating the reason).
0178  *
0179  * Note that passing 0 as the default port number will cause the
0180  * operating system to automatically allocate a free port for the
0181  * socket.
0182  *
0183  * @param protocol  the protocol this socket will accept
0184  * @param address   the address to listen at
0185  * @param port      the default port for the service
0186  * @param parent    the parent object to be passed to the
0187  *                  QTcpServer constructor
0188  */
0189 KDELIBS4SUPPORT_DEPRECATED_EXPORT QTcpServer *listen(const QString &protocol, const QHostAddress &address = QHostAddress::Any,
0190         quint16 port = 0, QObject *parent = nullptr);
0191 
0192 // These functions below aren't done yet
0193 // Undocumented -> don't use!
0194 
0195 KDELIBS4SUPPORT_DEPRECATED_EXPORT QUdpSocket *datagramSocket(const QString &protocol, const QString &host, QObject *parent = nullptr);
0196 
0197 #ifndef QT_NO_NETWORKPROXY
0198 KDELIBS4SUPPORT_DEPRECATED_EXPORT QNetworkProxy proxyForConnection(const QString &protocol, const QString &host);
0199 KDELIBS4SUPPORT_DEPRECATED_EXPORT QNetworkProxy proxyForListening(const QString &protocol);
0200 KDELIBS4SUPPORT_DEPRECATED_EXPORT QNetworkProxy proxyForDatagram(const QString &protocol, const QString &host);
0201 #endif
0202 }
0203 
0204 #endif