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

KDECore

  • kdecore
  • network
klocalsocket.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 "klocalsocket.h"
22#include "klocalsocket_p.h"
23
24#include <QtCore/QSocketNotifier>
25
26
27//#define LocalSocket (QAbstractSocket::SocketType(int(QAbstractSocket::UdpSocket) + 1))
28
29void KLocalSocketPrivate::emitError(QAbstractSocket::SocketError error, const QString &errorString)
30{
31 q->setSocketState(QAbstractSocket::UnconnectedState);
32 q->setSocketError(error);
33 q->setErrorString(errorString);
34 emit q->stateChanged(QAbstractSocket::UnconnectedState);
35 emit q->error(error);
36}
37
38void KLocalSocketServerPrivate::emitError(QAbstractSocket::SocketError an_error, const QString &an_errorString)
39{
40 error = an_error;
41 errorString = an_errorString;
42}
43
44KLocalSocket::KLocalSocket(QObject *parent)
45 : QTcpSocket(parent), d(new KLocalSocketPrivate(this))
46{
47}
48
49KLocalSocket::~KLocalSocket()
50{
51 delete d;
52 // parent's destructor closes the socket
53}
54
55void KLocalSocket::connectToPath(const QString &path, OpenMode mode)
56{
57 // cheat:
58 connectToHost(path, UnixSocket, mode);
59}
60
61void KLocalSocket::connectToPath(const QString &path, LocalSocketType type, OpenMode mode)
62{
63 // cheat:
64 connectToHost(path, type, mode);
65}
66
67void KLocalSocket::connectToHostImplementation(const QString &path, quint16 type, OpenMode mode)
68{
69 if (state() == ConnectedState || state() == ConnectingState)
70 return;
71
72 d->localPath.clear();
73 d->peerPath.clear();
74
75 setSocketState(ConnectingState);
76 emit stateChanged(ConnectingState);
77
78 d->connectToPath(path, LocalSocketType(type), mode);
79}
80
81void KLocalSocket::disconnectFromHostImplementation()
82{
83 QTcpSocket::disconnectFromHostImplementation();
84
85 d->peerPath.clear();
86 d->localPath.clear();
87 d->type = UnknownLocalSocketType;
88}
89
90void KLocalSocket::disconnectFromPath()
91{
92 // cheat:
93 disconnectFromHost();
94}
95
96KLocalSocket::LocalSocketType KLocalSocket::localSocketType() const
97{
98 return d->type;
99}
100
101QString KLocalSocket::localPath() const
102{
103 return d->localPath;
104}
105
106QString KLocalSocket::peerPath() const
107{
108 return d->peerPath;
109}
110
111KLocalSocketServerPrivate::KLocalSocketServerPrivate(KLocalSocketServer *qq)
112 : q(qq), descriptor(-1), maxPendingConnections(30),
113 state(QAbstractSocket::UnconnectedState),
114 error(QAbstractSocket::UnknownSocketError),
115 type(KLocalSocket::UnknownLocalSocketType),
116 readNotifier(0)
117{
118}
119
120KLocalSocketServer::KLocalSocketServer(QObject *parent)
121 : QObject(parent), d(new KLocalSocketServerPrivate(this))
122{
123}
124
125KLocalSocketServer::~KLocalSocketServer()
126{
127 close();
128 delete d;
129}
130
131bool KLocalSocketServer::isListening() const
132{
133 return d->state == QAbstractSocket::ListeningState;
134}
135
136bool KLocalSocketServer::listen(const QString &path, KLocalSocket::LocalSocketType type)
137{
138 if (d->state == QAbstractSocket::ListeningState)
139 return false; // already created
140
141 if (!d->listen(path, type)) {
142 // the private set the error code
143 return false;
144 }
145
146 d->localPath = path;
147 return true;
148}
149
150void KLocalSocketServer::close()
151{
152 d->close();
153}
154
155void KLocalSocketServer::setMaxPendingConnections(int numConnections)
156{
157 if (numConnections >= 0) {
158 d->maxPendingConnections = numConnections;
159 d->readNotifier->setEnabled(d->pendingConnections.size() < d->maxPendingConnections);
160 } else {
161 qWarning("KLocalSocketServer::setMaxPendingConnections: cannot set to a negative number");
162 }
163}
164
165int KLocalSocketServer::maxPendingConnections() const
166{
167 return d->maxPendingConnections;
168}
169
170KLocalSocket::LocalSocketType KLocalSocketServer::localSocketType() const
171{
172 return d->type;
173}
174
175QString KLocalSocketServer::localPath() const
176{
177 return d->localPath;
178}
179
180bool KLocalSocketServer::waitForNewConnection(int msec, bool *timedOut)
181{
182 if (!isListening())
183 return false; // can't wait if we're not not listening
184
185 return d->waitForNewConnection(msec, timedOut);
186}
187
188bool KLocalSocketServer::hasPendingConnections() const
189{
190 return !d->pendingConnections.isEmpty();
191}
192
193KLocalSocket *KLocalSocketServer::nextPendingConnection()
194{
195 if (hasPendingConnections()) {
196 d->readNotifier->setEnabled((d->pendingConnections.size() - 1) < d->maxPendingConnections);
197 return d->pendingConnections.dequeue();
198 }
199 return 0;
200}
201
202void KLocalSocketServer::incomingConnection(int descriptor)
203{
204 KLocalSocket *socket = new KLocalSocket(this);
205 KLocalSocketPrivate *socket_d = KLocalSocketPrivate::d(socket);
206 socket_d->localPath = d->localPath;
207 socket_d->type = d->type;
208
209 socket->setSocketDescriptor(descriptor, QAbstractSocket::ConnectedState, QIODevice::ReadWrite);
210 d->pendingConnections.enqueue(socket);
211
212 emit newConnection();
213}
214
215QAbstractSocket::SocketError KLocalSocketServer::serverError() const
216{
217 return d->error;
218}
219
220QString KLocalSocketServer::errorString() const
221{
222 return d->errorString;
223}
224
225#include "klocalsocket.moc"
KLocalSocketPrivate
Definition: klocalsocket_p.h:31
KLocalSocketPrivate::connectToPath
void connectToPath(const QString &path, KLocalSocket::LocalSocketType type, QAbstractSocket::OpenMode openMode)
Definition: klocalsocket_unix.cpp:171
KLocalSocketPrivate::type
KLocalSocket::LocalSocketType type
Definition: klocalsocket_p.h:40
KLocalSocketPrivate::localPath
QString localPath
Definition: klocalsocket_p.h:38
KLocalSocketPrivate::emitError
void emitError(QAbstractSocket::SocketError, const QString &errorString)
Definition: klocalsocket.cpp:29
KLocalSocketPrivate::d
static KLocalSocketPrivate * d(KLocalSocket *aq)
Definition: klocalsocket_p.h:47
KLocalSocketPrivate::q
KLocalSocket *const q
Definition: klocalsocket_p.h:33
KLocalSocketPrivate::peerPath
QString peerPath
Definition: klocalsocket_p.h:39
KLocalSocketServerPrivate
Definition: klocalsocket_p.h:52
KLocalSocketServerPrivate::pendingConnections
QQueue< KLocalSocket * > pendingConnections
Definition: klocalsocket_p.h:66
KLocalSocketServerPrivate::maxPendingConnections
int maxPendingConnections
Definition: klocalsocket_p.h:58
KLocalSocketServerPrivate::errorString
QString errorString
Definition: klocalsocket_p.h:63
KLocalSocketServerPrivate::KLocalSocketServerPrivate
KLocalSocketServerPrivate(KLocalSocketServer *qq)
Definition: klocalsocket.cpp:111
KLocalSocketServerPrivate::emitError
void emitError(QAbstractSocket::SocketError, const QString &errorString)
Definition: klocalsocket.cpp:38
KLocalSocketServerPrivate::close
void close()
Definition: klocalsocket_unix.cpp:318
KLocalSocketServerPrivate::type
KLocalSocket::LocalSocketType type
Definition: klocalsocket_p.h:61
KLocalSocketServerPrivate::localPath
QString localPath
Definition: klocalsocket_p.h:62
KLocalSocketServerPrivate::waitForNewConnection
bool waitForNewConnection(int msec, bool *timedOut)
Definition: klocalsocket_unix.cpp:337
KLocalSocketServerPrivate::error
QAbstractSocket::SocketError error
Definition: klocalsocket_p.h:60
KLocalSocketServerPrivate::state
QAbstractSocket::SocketState state
Definition: klocalsocket_p.h:59
KLocalSocketServerPrivate::readNotifier
QSocketNotifier * readNotifier
Definition: klocalsocket_p.h:65
KLocalSocketServerPrivate::listen
bool listen(const QString &path, KLocalSocket::LocalSocketType type)
Definition: klocalsocket_unix.cpp:238
KLocalSocketServer
KLocalSocketServer allows one to create a listening local socket and accept incoming connections.
Definition: klocalsocket.h:160
KLocalSocketServer::localSocketType
KLocalSocket::LocalSocketType localSocketType() const
Returns the socket type that this socket is listening on.
Definition: klocalsocket.cpp:170
KLocalSocketServer::setMaxPendingConnections
void setMaxPendingConnections(int numConnections)
Sets the maximum number of connections that KLocalSocketServer will accept on your behalf and keep qu...
Definition: klocalsocket.cpp:155
KLocalSocketServer::hasPendingConnections
virtual bool hasPendingConnections() const
Returns true if a new socket can be received with nextPendingConnection().
Definition: klocalsocket.cpp:188
KLocalSocketServer::isListening
bool isListening() const
Returns true if the socket is listening, false otherwise.
Definition: klocalsocket.cpp:131
KLocalSocketServer::errorString
QString errorString() const
If an error occurred, return the error message.
Definition: klocalsocket.cpp:220
KLocalSocketServer::listen
bool listen(const QString &path, KLocalSocket::LocalSocketType type=KLocalSocket::UnixSocket)
Binds this socket to the address path and starts listening there.
Definition: klocalsocket.cpp:136
KLocalSocketServer::maxPendingConnections
int maxPendingConnections() const
Returns the value set with setMaxPendingConnections().
Definition: klocalsocket.cpp:165
KLocalSocketServer::localPath
QString localPath() const
Returns the address of this socket if it is listening on, or QString() if it is not listening.
Definition: klocalsocket.cpp:175
KLocalSocketServer::nextPendingConnection
virtual KLocalSocket * nextPendingConnection()
Returns a new socket if one is available or 0 if none is.
Definition: klocalsocket.cpp:193
KLocalSocketServer::serverError
QAbstractSocket::SocketError serverError() const
If an error occurred, return the error code.
Definition: klocalsocket.cpp:215
KLocalSocketServer::~KLocalSocketServer
virtual ~KLocalSocketServer()
Destroys the KLocalSocketServer object and frees up any resource associated.
Definition: klocalsocket.cpp:125
KLocalSocketServer::incomingConnection
virtual void incomingConnection(int handle)
Definition: klocalsocket.cpp:202
KLocalSocketServer::KLocalSocketServer
KLocalSocketServer(QObject *parent=0)
Creates a KLocalSocketServer object with parent as the parent object.
Definition: klocalsocket.cpp:120
KLocalSocketServer::waitForNewConnection
bool waitForNewConnection(int msec=0, bool *timedOut=0)
Suspends the execution of the calling thread for at most msec milliseconds and wait for a new socket ...
Definition: klocalsocket.cpp:180
KLocalSocketServer::newConnection
void newConnection()
The newConnection() signal is emitted whenever a new connection is ready and has been accepted.
KLocalSocketServer::close
void close()
Closes the socket.
Definition: klocalsocket.cpp:150
KLocalSocket
KLocalSocket allows one to create and use local (Unix) sockets.
Definition: klocalsocket.h:52
KLocalSocket::LocalSocketType
LocalSocketType
Defines the local socket type.
Definition: klocalsocket.h:59
KLocalSocket::UnknownLocalSocketType
@ UnknownLocalSocketType
Definition: klocalsocket.h:62
KLocalSocket::UnixSocket
@ UnixSocket
Unix sockets.
Definition: klocalsocket.h:60
KLocalSocket::~KLocalSocket
virtual ~KLocalSocket()
Destroys the KLocalSocket object and frees up any resources associated.
Definition: klocalsocket.cpp:49
KLocalSocket::connectToPath
void connectToPath(const QString &path, OpenMode mode=ReadWrite)
Opens a connection to a listening Unix socket at path.
Definition: klocalsocket.cpp:55
KLocalSocket::KLocalSocket
KLocalSocket(QObject *parent=0)
Creates a KLocalSocket object with parent as the parent object.
Definition: klocalsocket.cpp:44
KLocalSocket::localPath
QString localPath() const
Returns the local address of this socket, when connected.
Definition: klocalsocket.cpp:101
KLocalSocket::disconnectFromPath
void disconnectFromPath()
Disconnects the socket from its server.
Definition: klocalsocket.cpp:90
KLocalSocket::disconnectFromHostImplementation
void disconnectFromHostImplementation()
Definition: klocalsocket.cpp:81
KLocalSocket::peerPath
QString peerPath() const
Returns the peer address of this socket.
Definition: klocalsocket.cpp:106
KLocalSocket::localSocketType
LocalSocketType localSocketType() const
Returns the socket type for this socket, when connected.
Definition: klocalsocket.cpp:96
KLocalSocket::connectToHostImplementation
void connectToHostImplementation(const QString &hostName, quint16 port, OpenMode mode)
Definition: klocalsocket.cpp:67
QObject
QString
QTcpSocket
klocalsocket.h
klocalsocket_p.h
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