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

KDECore

  • sources
  • kde-4.14
  • kdelibs
  • 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 
29 void 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 
38 void KLocalSocketServerPrivate::emitError(QAbstractSocket::SocketError an_error, const QString &an_errorString)
39 {
40  error = an_error;
41  errorString = an_errorString;
42 }
43 
44 KLocalSocket::KLocalSocket(QObject *parent)
45  : QTcpSocket(parent), d(new KLocalSocketPrivate(this))
46 {
47 }
48 
49 KLocalSocket::~KLocalSocket()
50 {
51  delete d;
52  // parent's destructor closes the socket
53 }
54 
55 void KLocalSocket::connectToPath(const QString &path, OpenMode mode)
56 {
57  // cheat:
58  connectToHost(path, UnixSocket, mode);
59 }
60 
61 void KLocalSocket::connectToPath(const QString &path, LocalSocketType type, OpenMode mode)
62 {
63  // cheat:
64  connectToHost(path, type, mode);
65 }
66 
67 void 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 
81 void KLocalSocket::disconnectFromHostImplementation()
82 {
83  QTcpSocket::disconnectFromHostImplementation();
84 
85  d->peerPath.clear();
86  d->localPath.clear();
87  d->type = UnknownLocalSocketType;
88 }
89 
90 void KLocalSocket::disconnectFromPath()
91 {
92  // cheat:
93  disconnectFromHost();
94 }
95 
96 KLocalSocket::LocalSocketType KLocalSocket::localSocketType() const
97 {
98  return d->type;
99 }
100 
101 QString KLocalSocket::localPath() const
102 {
103  return d->localPath;
104 }
105 
106 QString KLocalSocket::peerPath() const
107 {
108  return d->peerPath;
109 }
110 
111 KLocalSocketServerPrivate::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 
120 KLocalSocketServer::KLocalSocketServer(QObject *parent)
121  : QObject(parent), d(new KLocalSocketServerPrivate(this))
122 {
123 }
124 
125 KLocalSocketServer::~KLocalSocketServer()
126 {
127  close();
128  delete d;
129 }
130 
131 bool KLocalSocketServer::isListening() const
132 {
133  return d->state == QAbstractSocket::ListeningState;
134 }
135 
136 bool 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 
150 void KLocalSocketServer::close()
151 {
152  d->close();
153 }
154 
155 void 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 
165 int KLocalSocketServer::maxPendingConnections() const
166 {
167  return d->maxPendingConnections;
168 }
169 
170 KLocalSocket::LocalSocketType KLocalSocketServer::localSocketType() const
171 {
172  return d->type;
173 }
174 
175 QString KLocalSocketServer::localPath() const
176 {
177  return d->localPath;
178 }
179 
180 bool 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 
188 bool KLocalSocketServer::hasPendingConnections() const
189 {
190  return !d->pendingConnections.isEmpty();
191 }
192 
193 KLocalSocket *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 
202 void 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 
215 QAbstractSocket::SocketError KLocalSocketServer::serverError() const
216 {
217  return d->error;
218 }
219 
220 QString KLocalSocketServer::errorString() const
221 {
222  return d->errorString;
223 }
224 
225 #include "klocalsocket.moc"
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::~KLocalSocketServer
virtual ~KLocalSocketServer()
Destroys the KLocalSocketServer object and frees up any resource associated.
Definition: klocalsocket.cpp:125
QAbstractSocket::state
SocketState state() const
KLocalSocketServer::close
void close()
Closes the socket.
Definition: klocalsocket.cpp:150
KLocalSocketServer::hasPendingConnections
virtual bool hasPendingConnections() const
Returns true if a new socket can be received with nextPendingConnection().
Definition: klocalsocket.cpp:188
QAbstractSocket::setSocketState
void setSocketState(SocketState state)
KLocalSocketServer::KLocalSocketServer
KLocalSocketServer(QObject *parent=0)
Creates a KLocalSocketServer object with parent as the parent object.
Definition: klocalsocket.cpp:120
QQueue::enqueue
void enqueue(const T &t)
KLocalSocketServer::maxPendingConnections
int maxPendingConnections() const
Returns the value set with setMaxPendingConnections().
Definition: klocalsocket.cpp:165
KLocalSocketServerPrivate::state
QAbstractSocket::SocketState state
Definition: klocalsocket_p.h:59
KLocalSocketPrivate::q
KLocalSocket *const q
Definition: klocalsocket_p.h:33
KLocalSocket::disconnectFromPath
void disconnectFromPath()
Disconnects the socket from its server.
Definition: klocalsocket.cpp:90
KLocalSocketServer::nextPendingConnection
virtual KLocalSocket * nextPendingConnection()
Returns a new socket if one is available or 0 if none is.
Definition: klocalsocket.cpp:193
KLocalSocket::localSocketType
LocalSocketType localSocketType() const
Returns the socket type for this socket, when connected.
Definition: klocalsocket.cpp:96
KLocalSocketServerPrivate::readNotifier
QSocketNotifier * readNotifier
Definition: klocalsocket_p.h:65
QQueue::dequeue
T dequeue()
KLocalSocketPrivate::localPath
QString localPath
Definition: klocalsocket_p.h:38
QAbstractSocket::error
SocketError error() const
KLocalSocketServer::serverError
QAbstractSocket::SocketError serverError() const
If an error occurred, return the error code.
Definition: klocalsocket.cpp:215
KLocalSocket::KLocalSocket
KLocalSocket(QObject *parent=0)
Creates a KLocalSocket object with parent as the parent object.
Definition: klocalsocket.cpp:44
KLocalSocketServerPrivate::errorString
QString errorString
Definition: klocalsocket_p.h:63
KLocalSocket::UnknownLocalSocketType
Definition: klocalsocket.h:62
QAbstractSocket::setSocketDescriptor
bool setSocketDescriptor(int socketDescriptor, SocketState socketState, QFlags< QIODevice::OpenModeFlag > openMode)
QAbstractSocket
KLocalSocketServerPrivate::localPath
QString localPath
Definition: klocalsocket_p.h:62
KLocalSocketServerPrivate::pendingConnections
QQueue< KLocalSocket * > pendingConnections
Definition: klocalsocket_p.h:66
KLocalSocketServerPrivate::KLocalSocketServerPrivate
KLocalSocketServerPrivate(KLocalSocketServer *qq)
Definition: klocalsocket.cpp:111
KLocalSocketServerPrivate::listen
bool listen(const QString &path, KLocalSocket::LocalSocketType type)
Definition: klocalsocket_unix.cpp:238
QString::clear
void clear()
KLocalSocketServer::errorString
QString errorString() const
If an error occurred, return the error message.
Definition: klocalsocket.cpp:220
QAbstractSocket::connectToHost
void connectToHost(const QString &hostName, quint16 port, QFlags< QIODevice::OpenModeFlag > openMode)
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
KLocalSocket::disconnectFromHostImplementation
void disconnectFromHostImplementation()
Definition: klocalsocket.cpp:81
KLocalSocketServerPrivate
Definition: klocalsocket_p.h:51
QObject
KLocalSocket::localPath
QString localPath() const
Returns the local address of this socket, when connected.
Definition: klocalsocket.cpp:101
KLocalSocketServerPrivate::error
QAbstractSocket::SocketError error
Definition: klocalsocket_p.h:60
KLocalSocketPrivate::connectToPath
void connectToPath(const QString &path, KLocalSocket::LocalSocketType type, QAbstractSocket::OpenMode openMode)
Definition: klocalsocket_unix.cpp:171
klocalsocket_p.h
KLocalSocketServer::localSocketType
KLocalSocket::LocalSocketType localSocketType() const
Returns the socket type that this socket is listening on.
Definition: klocalsocket.cpp:170
KLocalSocketPrivate::emitError
void emitError(QAbstractSocket::SocketError, const QString &errorString)
Definition: klocalsocket.cpp:29
QString
KLocalSocketServer::newConnection
void newConnection()
The newConnection() signal is emitted whenever a new connection is ready and has been accepted...
KLocalSocket
KLocalSocket allows one to create and use local (Unix) sockets.
Definition: klocalsocket.h:51
KLocalSocket::peerPath
QString peerPath() const
Returns the peer address of this socket.
Definition: klocalsocket.cpp:106
KLocalSocketServer
KLocalSocketServer allows one to create a listening local socket and accept incoming connections...
Definition: klocalsocket.h:159
QAbstractSocket::disconnectFromHost
void disconnectFromHost()
QAbstractSocket::stateChanged
void stateChanged(QAbstractSocket::SocketState socketState)
KLocalSocketServerPrivate::maxPendingConnections
int maxPendingConnections
Definition: klocalsocket_p.h:58
KLocalSocketServer::isListening
bool isListening() const
Returns true if the socket is listening, false otherwise.
Definition: klocalsocket.cpp:131
KLocalSocketPrivate::d
static KLocalSocketPrivate * d(KLocalSocket *aq)
Definition: klocalsocket_p.h:47
KLocalSocketServerPrivate::close
void close()
Definition: klocalsocket_unix.cpp:318
QSocketNotifier::setEnabled
void setEnabled(bool enable)
KLocalSocketPrivate::type
KLocalSocket::LocalSocketType type
Definition: klocalsocket_p.h:40
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::connectToHostImplementation
void connectToHostImplementation(const QString &hostName, quint16 port, OpenMode mode)
Definition: klocalsocket.cpp:67
KLocalSocketPrivate
Definition: klocalsocket_p.h:30
KLocalSocket::UnixSocket
Unix sockets.
Definition: klocalsocket.h:60
QAbstractSocket::setSocketError
void setSocketError(SocketError socketError)
KLocalSocketPrivate::peerPath
QString peerPath
Definition: klocalsocket_p.h:39
QIODevice::setErrorString
void setErrorString(const QString &str)
QTcpSocket
KLocalSocketServer::incomingConnection
virtual void incomingConnection(int handle)
Definition: klocalsocket.cpp:202
KLocalSocketServerPrivate::emitError
void emitError(QAbstractSocket::SocketError, const QString &errorString)
Definition: klocalsocket.cpp:38
KLocalSocket::LocalSocketType
LocalSocketType
Defines the local socket type.
Definition: klocalsocket.h:59
QAbstractSocket::disconnectFromHostImplementation
void disconnectFromHostImplementation()
KLocalSocketServerPrivate::type
KLocalSocket::LocalSocketType type
Definition: klocalsocket_p.h:61
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
klocalsocket.h
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
KLocalSocketServerPrivate::waitForNewConnection
bool waitForNewConnection(int msec, bool *timedOut)
Definition: klocalsocket_unix.cpp:337
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:22:11 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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

Search



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