• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • network
ksocketfactory.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the KDE libraries
3 * Copyright (C) 2007 Thiago Macieira <thiago@kde.org>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include "ksocketfactory.h"
22
23#include <QSslSocket>
24#include <QTcpSocket>
25#include <QTcpServer>
26#include <QUdpSocket>
27#include <QUrl>
28
29#include "klocalizedstring.h"
30
31#include <config-network.h>
32
33using namespace KSocketFactory;
34
35class _k_internal_QTcpSocketSetError: public QAbstractSocket
36{
37public:
38 _k_internal_QTcpSocketSetError(); // not defined anywhere!
39 using QAbstractSocket::setSocketError;
40 using QAbstractSocket::setSocketState;
41 using QAbstractSocket::setErrorString;
42};
43
44static inline void setError(QAbstractSocket *socket, QAbstractSocket::SocketError error,
45 const QString &errorString)
46{
47 _k_internal_QTcpSocketSetError *hackSocket = static_cast<_k_internal_QTcpSocketSetError *>(socket);
48 hackSocket->setSocketError(error);
49 hackSocket->setErrorString(errorString);
50}
51
52void KSocketFactory::connectToHost(QTcpSocket *socket, const QString &protocol, const QString &host,
53 quint16 port)
54{
55 if (!socket)
56 return;
57
58#ifndef QT_NO_NETWORKPROXY
59 socket->setProxy(proxyForConnection(protocol, host));
60#endif
61 socket->connectToHost(host, port);
62}
63
64void KSocketFactory::connectToHost(QTcpSocket *socket, const QUrl &url)
65{
66 connectToHost(socket, url.scheme(), url.host(), url.port());
67}
68
69QTcpSocket *KSocketFactory::connectToHost(const QString &protocol, const QString &host, quint16 port,
70 QObject *parent)
71{
72 // ### TO-DO: find a way to determine if we should use QSslSocket or plain QTcpSocket
73 QTcpSocket *socket = new QSslSocket(parent);
74 connectToHost(socket, protocol, host, port);
75 return socket;
76}
77
78QTcpSocket *KSocketFactory::connectToHost(const QUrl &url, QObject *parent)
79{
80 return connectToHost(url.scheme(), url.host(), url.port(), parent);
81}
82
83void KSocketFactory::synchronousConnectToHost(QTcpSocket *socket, const QString &protocol,
84 const QString &host, quint16 port, int msecs)
85{
86 if (!socket)
87 return;
88
89 connectToHost(socket, protocol, host, port);
90 if (!socket->waitForConnected(msecs))
91 setError(socket, QAbstractSocket::SocketTimeoutError,
92 i18n("Timed out trying to connect to remote host"));
93}
94
95void KSocketFactory::synchronousConnectToHost(QTcpSocket *socket, const QUrl &url, int msecs)
96{
97 synchronousConnectToHost(socket, url.scheme(), url.host(), url.port(), msecs);
98}
99
100QTcpSocket *KSocketFactory::synchronousConnectToHost(const QString &protocol, const QString &host,
101 quint16 port, int msecs, QObject *parent)
102{
103 QTcpSocket *socket = connectToHost(protocol, host, port, parent);
104 if (!socket->waitForConnected(msecs))
105 setError(socket, QAbstractSocket::SocketTimeoutError,
106 i18n("Timed out trying to connect to remote host"));
107 return socket;
108}
109
110QTcpSocket *KSocketFactory::synchronousConnectToHost(const QUrl &url, int msecs, QObject *parent)
111{
112 return synchronousConnectToHost(url.scheme(), url.host(), url.port(), msecs, parent);
113}
114
115QTcpServer *KSocketFactory::listen(const QString &protocol, const QHostAddress &address, quint16 port,
116 QObject *parent)
117{
118 QTcpServer *server = new QTcpServer(parent);
119#ifndef QT_NO_NETWORKPROXY
120 server->setProxy(proxyForListening(protocol));
121#endif
122 server->listen(address, port);
123 return server;
124}
125
126QUdpSocket *KSocketFactory::datagramSocket(const QString &protocol, const QString &host, QObject *parent)
127{
128 QUdpSocket *socket = new QUdpSocket(parent);
129#ifndef QT_NO_NETWORKPROXY
130 // ### do something else?
131 socket->setProxy(proxyForDatagram(protocol, host));
132#endif
133 return socket;
134}
135
136#ifndef QT_NO_NETWORKPROXY
137QNetworkProxy KSocketFactory::proxyForConnection(const QString &, const QString &)
138{
139 return QNetworkProxy::DefaultProxy;
140}
141
142QNetworkProxy KSocketFactory::proxyForListening(const QString &)
143{
144 return QNetworkProxy::DefaultProxy;
145}
146
147QNetworkProxy KSocketFactory::proxyForDatagram(const QString &, const QString &)
148{
149 return QNetworkProxy::DefaultProxy;
150}
151#endif
QObject
QString
QTcpSocket
QUrl
klocalizedstring.h
i18n
QString i18n(const char *text)
Returns a localized version of a string.
Definition: klocalizedstring.h:630
setError
static void setError(QAbstractSocket *socket, QAbstractSocket::SocketError error, const QString &errorString)
Definition: ksocketfactory.cpp:44
ksocketfactory.h
KSocketFactory
KSocketFactory provides functions for opening sockets to remote hosts.
KSocketFactory::proxyForConnection
QNetworkProxy proxyForConnection(const QString &protocol, const QString &host)
Definition: ksocketfactory.cpp:137
KSocketFactory::datagramSocket
QUdpSocket * datagramSocket(const QString &protocol, const QString &host, QObject *parent=0)
Definition: ksocketfactory.cpp:126
KSocketFactory::connectToHost
QTcpSocket * connectToHost(const QString &protocol, const QString &host, quint16 port, QObject *parent=0)
Initiates a TCP/IP socket connection to remote node (host) host, using the protocol.
Definition: ksocketfactory.cpp:69
KSocketFactory::proxyForDatagram
QNetworkProxy proxyForDatagram(const QString &protocol, const QString &host)
Definition: ksocketfactory.cpp:147
KSocketFactory::proxyForListening
QNetworkProxy proxyForListening(const QString &protocol)
Definition: ksocketfactory.cpp:142
KSocketFactory::synchronousConnectToHost
QTcpSocket * synchronousConnectToHost(const QString &protocol, const QString &host, quint16 port, int msecs=30000, QObject *parent=0)
This function behaves exactly like connectToHost() above, except that the socket it returns is either...
Definition: ksocketfactory.cpp:100
KSocketFactory::listen
QTcpServer * listen(const QString &protocol, const QHostAddress &address=QHostAddress::Any, quint16 port=0, QObject *parent=0)
Opens a TCP/IP socket for listening protocol protocol, binding only at address address.
Definition: ksocketfactory.cpp:115
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal