KDECore
klocalsocket.cpp
Go to the documentation of this file.
00001 /* 00002 * This file is part of the KDE libraries 00003 * Copyright (C) 2007 Thiago Macieira <thiago@kde.org> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Library General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "klocalsocket.h" 00022 #include "klocalsocket_p.h" 00023 00024 #include <QtCore/QSocketNotifier> 00025 00026 00027 //#define LocalSocket (QAbstractSocket::SocketType(int(QAbstractSocket::UdpSocket) + 1)) 00028 00029 void KLocalSocketPrivate::emitError(QAbstractSocket::SocketError error, const QString &errorString) 00030 { 00031 q->setSocketState(QAbstractSocket::UnconnectedState); 00032 q->setSocketError(error); 00033 q->setErrorString(errorString); 00034 emit q->stateChanged(QAbstractSocket::UnconnectedState); 00035 emit q->error(error); 00036 } 00037 00038 void KLocalSocketServerPrivate::emitError(QAbstractSocket::SocketError an_error, const QString &an_errorString) 00039 { 00040 error = an_error; 00041 errorString = an_errorString; 00042 } 00043 00044 KLocalSocket::KLocalSocket(QObject *parent) 00045 : QTcpSocket(parent), d(new KLocalSocketPrivate(this)) 00046 { 00047 } 00048 00049 KLocalSocket::~KLocalSocket() 00050 { 00051 delete d; 00052 // parent's destructor closes the socket 00053 } 00054 00055 void KLocalSocket::connectToPath(const QString &path, OpenMode mode) 00056 { 00057 // cheat: 00058 connectToHost(path, UnixSocket, mode); 00059 } 00060 00061 void KLocalSocket::connectToPath(const QString &path, LocalSocketType type, OpenMode mode) 00062 { 00063 // cheat: 00064 connectToHost(path, type, mode); 00065 } 00066 00067 void KLocalSocket::connectToHostImplementation(const QString &path, quint16 type, OpenMode mode) 00068 { 00069 if (state() == ConnectedState || state() == ConnectingState) 00070 return; 00071 00072 d->localPath.clear(); 00073 d->peerPath.clear(); 00074 00075 setSocketState(ConnectingState); 00076 emit stateChanged(ConnectingState); 00077 00078 d->connectToPath(path, LocalSocketType(type), mode); 00079 } 00080 00081 void KLocalSocket::disconnectFromHostImplementation() 00082 { 00083 QTcpSocket::disconnectFromHostImplementation(); 00084 00085 d->peerPath.clear(); 00086 d->localPath.clear(); 00087 d->type = UnknownLocalSocketType; 00088 } 00089 00090 void KLocalSocket::disconnectFromPath() 00091 { 00092 // cheat: 00093 disconnectFromHost(); 00094 } 00095 00096 KLocalSocket::LocalSocketType KLocalSocket::localSocketType() const 00097 { 00098 return d->type; 00099 } 00100 00101 QString KLocalSocket::localPath() const 00102 { 00103 return d->localPath; 00104 } 00105 00106 QString KLocalSocket::peerPath() const 00107 { 00108 return d->peerPath; 00109 } 00110 00111 KLocalSocketServerPrivate::KLocalSocketServerPrivate(KLocalSocketServer *qq) 00112 : q(qq), descriptor(-1), maxPendingConnections(30), 00113 state(QAbstractSocket::UnconnectedState), 00114 error(QAbstractSocket::UnknownSocketError), 00115 type(KLocalSocket::UnknownLocalSocketType), 00116 readNotifier(0) 00117 { 00118 } 00119 00120 KLocalSocketServer::KLocalSocketServer(QObject *parent) 00121 : QObject(parent), d(new KLocalSocketServerPrivate(this)) 00122 { 00123 } 00124 00125 KLocalSocketServer::~KLocalSocketServer() 00126 { 00127 close(); 00128 delete d; 00129 } 00130 00131 bool KLocalSocketServer::isListening() const 00132 { 00133 return d->state == QAbstractSocket::ListeningState; 00134 } 00135 00136 bool KLocalSocketServer::listen(const QString &path, KLocalSocket::LocalSocketType type) 00137 { 00138 if (d->state == QAbstractSocket::ListeningState) 00139 return false; // already created 00140 00141 if (!d->listen(path, type)) { 00142 // the private set the error code 00143 return false; 00144 } 00145 00146 d->localPath = path; 00147 return true; 00148 } 00149 00150 void KLocalSocketServer::close() 00151 { 00152 d->close(); 00153 } 00154 00155 void KLocalSocketServer::setMaxPendingConnections(int numConnections) 00156 { 00157 if (numConnections >= 0) { 00158 d->maxPendingConnections = numConnections; 00159 d->readNotifier->setEnabled(d->pendingConnections.size() < d->maxPendingConnections); 00160 } else { 00161 qWarning("KLocalSocketServer::setMaxPendingConnections: cannot set to a negative number"); 00162 } 00163 } 00164 00165 int KLocalSocketServer::maxPendingConnections() const 00166 { 00167 return d->maxPendingConnections; 00168 } 00169 00170 KLocalSocket::LocalSocketType KLocalSocketServer::localSocketType() const 00171 { 00172 return d->type; 00173 } 00174 00175 QString KLocalSocketServer::localPath() const 00176 { 00177 return d->localPath; 00178 } 00179 00180 bool KLocalSocketServer::waitForNewConnection(int msec, bool *timedOut) 00181 { 00182 if (!isListening()) 00183 return false; // can't wait if we're not not listening 00184 00185 return d->waitForNewConnection(msec, timedOut); 00186 } 00187 00188 bool KLocalSocketServer::hasPendingConnections() const 00189 { 00190 return !d->pendingConnections.isEmpty(); 00191 } 00192 00193 KLocalSocket *KLocalSocketServer::nextPendingConnection() 00194 { 00195 if (hasPendingConnections()) { 00196 d->readNotifier->setEnabled((d->pendingConnections.size() - 1) < d->maxPendingConnections); 00197 return d->pendingConnections.dequeue(); 00198 } 00199 return 0; 00200 } 00201 00202 void KLocalSocketServer::incomingConnection(int descriptor) 00203 { 00204 KLocalSocket *socket = new KLocalSocket(this); 00205 KLocalSocketPrivate *socket_d = KLocalSocketPrivate::d(socket); 00206 socket_d->localPath = d->localPath; 00207 socket_d->type = d->type; 00208 00209 socket->setSocketDescriptor(descriptor, QAbstractSocket::ConnectedState, QIODevice::ReadWrite); 00210 d->pendingConnections.enqueue(socket); 00211 00212 emit newConnection(); 00213 } 00214 00215 QAbstractSocket::SocketError KLocalSocketServer::serverError() const 00216 { 00217 return d->error; 00218 } 00219 00220 QString KLocalSocketServer::errorString() const 00221 { 00222 return d->errorString; 00223 } 00224 00225 #include "klocalsocket.moc"
KDE 4.6 API Reference