source: trunk/src/network/socket/qlocalserver.cpp@ 182

Last change on this file since 182 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 11.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtNetwork module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qlocalserver.h"
43#include "qlocalserver_p.h"
44#include "qlocalsocket.h"
45
46QT_BEGIN_NAMESPACE
47
48#ifndef QT_NO_LOCALSERVER
49
50/*!
51 \class QLocalServer
52 \since 4.4
53
54 \brief The QLocalServer class provides a local socket based server.
55
56 This class makes it possible to accept incoming local socket
57 connections.
58
59 Call listen() to have the server start listening
60 for incoming connections on a specified key. The
61 newConnection() signal is then emitted each time a client
62 connects to the server.
63
64 Call nextPendingConnection() to accept the pending connection
65 as a connected QLocalSocket. The function returns a pointer to a
66 QLocalSocket that can be used for communicating with the client.
67
68 If an error occurs, serverError() returns the type of error, and
69 errorString() can be called to get a human readable description
70 of what happened.
71
72 When listening for connections, the name which the server is
73 listening on is available through serverName().
74
75 Calling close() makes QLocalServer stop listening for incoming connections.
76
77 Although QLocalServer is designed for use with an event loop, it's possible
78 to use it without one. In that case, you must use waitForNewConnection(),
79 which blocks until either a connection is available or a timeout expires.
80
81 Note that this feature is not supported on Windows 9x.
82
83 \sa QLocalSocket, QTcpServer
84*/
85
86/*!
87 Create a new local socket server with the given \a parent.
88
89 \sa listen()
90 */
91QLocalServer::QLocalServer(QObject *parent)
92 : QObject(*new QLocalServerPrivate, parent)
93{
94 Q_D(QLocalServer);
95 d->init();
96}
97
98/*!
99 Destroys the QLocalServer object. If the server is listening for
100 connections, it is automatically closed.
101
102 Any client QLocalSockets that are still connected must either
103 disconnect or be reparented before the server is deleted.
104
105 \sa close()
106 */
107QLocalServer::~QLocalServer()
108{
109 if (isListening())
110 close();
111}
112
113/*!
114 Stop listening for incoming connections. Existing connections are not
115 effected, but any new connections will be refused.
116
117 \sa isListening(), listen()
118 */
119void QLocalServer::close()
120{
121 Q_D(QLocalServer);
122 if (!isListening())
123 return;
124 qDeleteAll(d->pendingConnections);
125 d->pendingConnections.clear();
126 d->closeServer();
127 d->serverName = QString();
128 d->fullServerName = QString();
129 d->errorString = QString();
130 d->error = QAbstractSocket::UnknownSocketError;
131}
132
133/*!
134 Returns the human-readable message appropriate to the current error
135 reported by serverError(). If no suitable string is available, an empty
136 string is returned.
137
138 \sa serverError()
139 */
140QString QLocalServer::errorString() const
141{
142 Q_D(const QLocalServer);
143 return d->errorString;
144}
145
146/*!
147 Returns true if the server has a pending connection; otherwise
148 returns false.
149
150 \sa nextPendingConnection(), setMaxPendingConnections()
151 */
152bool QLocalServer::hasPendingConnections() const
153{
154 Q_D(const QLocalServer);
155 return !(d->pendingConnections.isEmpty());
156}
157
158/*!
159 This virtual function is called by QLocalServer when a new connection
160 is available. \a socketDescriptor is the native socket descriptor for
161 the accepted connection.
162
163 The base implementation creates a QLocalSocket, sets the socket descriptor
164 and then stores the QLocalSocket in an internal list of pending
165 connections. Finally newConnection() is emitted.
166
167 Reimplement this function to alter the server's behavior
168 when a connection is available.
169