source: trunk/src/network/socket/qlocalsocket.cpp@ 67

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

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

File size: 15.1 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 "qlocalsocket.h"
43#include "qlocalsocket_p.h"
44
45#ifndef QT_NO_LOCALSOCKET
46
47QT_BEGIN_NAMESPACE
48
49/*!
50 \class QLocalSocket
51 \since 4.4
52
53 \brief The QLocalSocket class provides a local socket.
54
55 On Windows this is a named pipe and on Unix this is a local domain socket.
56
57 If an error occurs, socketError() returns the type of error, and
58 errorString() can be called to get a human readable description
59 of what happened.
60
61 Although QLocalSocket is designed for use with an event loop, it's possible
62 to use it without one. In that case, you must use waitForConnected(),
63 waitForReadyRead(), waitForBytesWritten(), and waitForDisconnected()
64 which blocks until the operation is complete or the timeout expires.
65
66 Note that this feature is not supported on Window 9x.
67
68 \sa QLocalServer
69*/
70
71/*!
72 \fn void QLocalSocket::connectToServer(const QString &name, OpenMode openMode)
73
74 Attempts to make a connection to \a name.
75
76 The socket is opened in the given \a openMode and first enters ConnectingState.
77 It then attempts to connect to the address or addresses returned by the lookup.
78 Finally, if a connection is established, QLocalSocket enters ConnectedState
79 and emits connected().
80
81 At any point, the socket can emit error() to signal that an error occurred.
82
83 See also state(), serverName(), and waitForConnected().
84*/
85
86/*!
87 \fn void QLocalSocket::connected()
88
89 This signal is emitted after connectToServer() has been called and
90 a connection has been successfully established.
91
92 \sa connectToServer(), disconnected()
93*/
94
95/*!
96 \fn bool QLocalSocket::setSocketDescriptor(quintptr socketDescriptor,
97 LocalSocketState socketState, OpenMode openMode)
98
99 Initializes QLocalSocket with the native socket descriptor
100 \a socketDescriptor. Returns true if socketDescriptor is accepted
101 as a valid socket descriptor; otherwise returns false. The socket is
102 opened in the mode specified by \a openMode, and enters the socket state
103 specified by \a socketState.
104
105 Note: It is not possible to initialize two local sockets with the same
106 native socket descriptor.
107
108 \sa socketDescriptor(), state(), openMode()
109*/
110
111/*!
112 \fn quintptr QLocalSocket::socketDescriptor() const
113
114 Returns the native socket descriptor of the QLocalSocket object if
115 this is available; otherwise returns -1.
116
117 The socket descriptor is not available when QLocalSocket
118 is in UnconnectedState.
119
120 \sa setSocketDescriptor()
121*/
122
123/*!
124 \fn qint64 QLocalSocket::readData(char *data, qint64 c)
125 \reimp
126*/
127
128/*!
129 \fn qint64 QLocalSocket::writeData(const char *data, qint64 c)
130 \reimp
131*/
132
133/*!
134 \fn void QLocalSocket::abort()
135
136 Aborts the current connection and resets the socket.
137 Unlike disconnectFromServer(), this function immediately closes the socket,
138 clearing any pending data in the write buffer.
139
140 \sa disconnectFromServer(), close()
141*/
142
143/*!
144 \fn qint64 QLocalSocket::bytesAvailable() const
145 \reimp
146*/
147
148/*!
149 \fn qint64 QLocalSocket::bytesToWrite() const
150 \reimp
151*/
152
153/*!
154 \fn bool QLocalSocket::canReadLine() const
155 \reimp
156*/
157
158/*!
159 \fn void QLocalSocket::close()
160 \reimp
161*/
162
163/*!
164 \fn bool QLocalSocket::waitForBytesWritten(int msecs)
165 \reimp
166*/
167
168/*!
169 \fn bool QLocalSocket::flush()
170
171 This function writes as much as possible from the internal write buffer
172 to the socket, without blocking. If any data was written, this function
173 returns true; otherwise false is returned.
174
175 Call this function if you need QLocalSocket to start sending buffered data
176 immediately. The number of bytes successfully written depends on the
177 operating system. In most cases, you do not need to call this function,
178 because QLocalSocket will start sending data automatically once control
179 goes back to the event loop. In the absence of an event loop, call
180 waitForBytesWritten() instead.
181
182 \sa write(), waitForBytesWritten()
183*/
184
185/*!
186 \fn void QLocalSocket::disconnectFromServer()
187
188 Attempts to close the socket. If there is pending data waiting to be
189 written, QLocalSocket will enter ClosingState and wait until all data
190 has been written. Eventually, it will enter UnconnectedState and emit
191 the disconnectedFromServer() signal.
192
193 \sa connectToServer()
194*/
195
196/*!
197 \fn QLocalSocket::LocalSocketError QLocalSocket::error() const
198
199 Returns the type of error that last occurred.
200
201 \sa state(), errorString()
202*/
203
204/*!
205 \fn bool QLocalSocket::isValid() const
206
207 Returns true if the socket is valid and ready for use; otherwise
208 returns false.
209
210 Note: The socket's state must be ConnectedState before reading
211 and writing can occur.
212
213 \sa state()
214*/
215
216/*!
217 \fn qint64 QLocalSocket::readBufferSize() const
218
219 Returns the size of the internal read buffer. This limits the amount of
220 data that the client can receive before you call read() or readAll().
221 A read buffer size of 0 (the default) means that the buffer has no size
222 limit, ensuring that no data is lost.
223
224 \sa setReadBufferSize(), read()
225*/
226
227/*!
228 \fn void QLocalSocket::setReadBufferSize(qint64 size)
229
230 Sets the size of QLocalSocket's internal read buffer to be \a size bytes.
231
232 If the buffer size is limited to a certain size, QLocalSocket won't
233 buffer more than this size of data. Exceptionally, a buffer size of 0
234 means that the read buffer is unlimited and all incoming data is buffered.
235 This is the default.
236
237 This option is useful if you only read the data at certain points in
238 time (e.g., in a real-time streaming application) or if you want to
239 protect your socket against receiving too much data, which may eventually
240 cause your application to run out of memory.
241
242 \sa readBufferSize(), read()
243*/
244
245/*!
246 \fn bool QLocalSocket::waitForConnected(int msec)
247
248 Waits until the socket is connected, up to \a msec milliseconds. If the
249 connection has been established, this function returns true; otherwise
250 it returns false. In the case where it returns false, you can call
251 error() to determine the cause of the error.
252
253 The following example waits up to one second for a connection
254 to be established:
255
256 \snippet doc/src/snippets/code/src_network_socket_qlocalsocket_unix.cpp 0
257
258 If msecs is -1, this function will not time out.
259
260 \sa connectToServer(), connected()
261*/
262
263/*!
264 \fn bool QLocalSocket::waitForDisconnected(int msecs)
265
266 Waits until the socket has disconnected, up to \a msecs
267 milliseconds. If the connection has been disconnected, this
268 function returns true; otherwise it returns false. In the case
269 where it returns false, you can call error() to determine
270 the cause of the error.
271
272 The following example waits up to one second for a connection
273 to be closed:
274
275 \snippet doc/src/snippets/code/src_network_socket_qlocalsocket_unix.cpp 1
276
277 If msecs is -1, this function will not time out.
278
279 \sa disconnectFromServer(), close()
280*/
281
282/*!
283 \fn bool QLocalSocket::waitForReadyRead(int msecs)
284
285 This function blocks until data is available for reading and the
286 \l{QIODevice::}{readyRead()} signal has been emitted. The function
287 will timeout after \a msecs milliseconds; the default timeout is
288 30000 milliseconds.
289
290 The function returns true if data is available for reading;
291 otherwise it returns false (if an error occurred or the
292 operation timed out).
293
294 \sa waitForBytesWritten()
295*/
296
297/*!
298 \fn void QLocalSocket::disconnected()
299
300 This signal is emitted when the socket has been disconnected.
301
302 \sa connectToServer(), disconnectFromServer(), abort(), connected()
303*/
304
305/*!
306 \fn void QLocalSocket::error(QLocalSocket::LocalSocketError socketError)
307
308 This signal is emitted after an error occurred. The \a socketError
309 parameter describes the type of error that occurred.
310
311 QLocalSocket::LocalSocketError is not a registered metatype, so for queued
312 connections, you will have to register it with Q_DECLARE_METATYPE.
313
314 \sa error(), errorString()
315*/
316
317/*!
318 \fn void QLocalSocket::stateChanged(QLocalSocket::LocalSocketState socketState)
319
320 This signal is emitted whenever QLocalSocket's state changes.
321 The \a socketState parameter is the new state.
322
323 QLocalSocket::SocketState is not a registered metatype, so for queued
324 connections, you will have to register it with Q_DECLARE_METATYPE.
325
326 \sa state()
327*/
328
329/*!
330 Creates a new local socket. The \a parent argument is passed to
331 QObject's constructor.
332 */
333QLocalSocket::QLocalSocket(QObject * parent)
334 : QIODevice(*new QLocalSocketPrivate, parent)
335{
336 Q_D(QLocalSocket);
337 d->init();
338}
339
340/*!
341 Destroys the socket, closing the connection if necessary.
342 */
343QLocalSocket::~QLocalSocket()
344{
345 close();
346#ifndef Q_OS_WIN
347 Q_D(QLocalSocket);
348 d->unixSocket.setParent(0);
349#endif
350}
351
352/*!
353 Returns the name of the peer as specified by connectToServer(), or an
354 empty QString if connectToServer() has not been called or it failed.
355
356 \sa connectToServer(), fullServerName()
357
358 */
359QString QLocalSocket::serverName() const
360{
361 Q_D(const QLocalSocket);
362 return d->serverName;
363}
364
365/*!
366 Returns the server path that the socket is connected to.
367
368 Note: This is platform specific
369
370 \sa connectToServer(), serverName()
371 */
372QString QLocalSocket::fullServerName() const
373{
374 Q_D(const QLocalSocket);
375 return d->fullServerName;
376}
377
378/*!
379 Returns the state of the socket.
380
381 \sa error()
382 */
383QLocalSocket::LocalSocketState QLocalSocket::state() const
384{
385 Q_D(const QLocalSocket);
386 return d->state;
387}
388
389/*! \reimp
390*/
391bool QLocalSocket::isSequential() const
392{
393 return true;
394}
395
396/*!
397 \enum QLocalSocket::LocalSocketError
398
399 The LocalServerError enumeration represents the errors that can occur.
400 The most recent error can be retrieved through a call to
401 \l QLocalSocket::error().
402
403 \value ConnectionRefusedError The connection was refused by
404 the peer (or timed out).
405 \value PeerClosedError The remote socket closed the connection.
406 Note that the client socket (i.e., this socket) will be closed
407 after the remote close notification has been sent.
408 \value ServerNotFoundError The local socket name was not found.
409 \value SocketAccessError The socket operation failed because the
410 application lacked the required privileges.
411 \value SocketResourceError The local system ran out of resources
412 (e.g., too many sockets).
413 \value SocketTimeoutError The socket operation timed out.
414 \value DatagramTooLargeError The datagram was larger than the operating
415 system's limit (which can be as low as 8192 bytes).
416 \value ConnectionError An error occurred with the connection.
417 \value UnsupportedSocketOperationError The requested socket operation
418 is not supported by the local operating system.
419 \value UnknownSocketError An unidentified error occurred.
420 */
421
422/*!
423 \enum QLocalSocket::LocalSocketState
424
425 This enum describes the different states in which a socket can be.
426
427 \sa QLocalSocket::state()
428
429 \value UnconnectedState The socket is not connected.
430 \value ConnectingState The socket has started establishing a connection.
431 \value ConnectedState A connection is established.
432 \value ClosingState The socket is about to close
433 (data may still be waiting to be written).
434 */
435
436#ifndef QT_NO_DEBUG_STREAM
437QDebug operator<<(QDebug debug, QLocalSocket::LocalSocketError error)
438{
439 switch (error) {
440 case QLocalSocket::ConnectionRefusedError:
441 debug << "QLocalSocket::ConnectionRefusedError";
442 break;
443 case QLocalSocket::PeerClosedError:
444 debug << "QLocalSocket::PeerClosedError";
445 break;
446 case QLocalSocket::ServerNotFoundError:
447 debug << "QLocalSocket::ServerNotFoundError";
448 break;
449 case QLocalSocket::SocketAccessError:
450 debug << "QLocalSocket::SocketAccessError";
451 break;
452 case QLocalSocket::SocketResourceError:
453 debug << "QLocalSocket::SocketResourceError";
454 break;
455 case QLocalSocket::SocketTimeoutError:
456 debug << "QLocalSocket::SocketTimeoutError";
457 break;
458 case QLocalSocket::DatagramTooLargeError:
459 debug << "QLocalSocket::DatagramTooLargeError";
460 break;
461 case QLocalSocket::ConnectionError:
462 debug << "QLocalSocket::ConnectionError";
463 break;
464 case QLocalSocket::UnsupportedSocketOperationError:
465 debug << "QLocalSocket::UnsupportedSocketOperationError";
466 break;
467 case QLocalSocket::UnknownSocketError:
468 debug << "QLocalSocket::UnknownSocketError";
469 break;
470 default:
471 debug << "QLocalSocket::SocketError(" << int(error) << ")";
472 break;
473 }
474 return debug;
475}
476
477QDebug operator<<(QDebug debug, QLocalSocket::LocalSocketState state)
478{
479 switch (state) {
480 case QLocalSocket::UnconnectedState:
481 debug << "QLocalSocket::UnconnectedState";
482 break;
483 case QLocalSocket::ConnectingState:
484 debug << "QLocalSocket::ConnectingState";
485 break;
486 case QLocalSocket::ConnectedState:
487 debug << "QLocalSocket::ConnectedState";
488 break;
489 case QLocalSocket::ClosingState:
490 debug << "QLocalSocket::ClosingState";
491 break;
492 default:
493 debug << "QLocalSocket::SocketState(" << int(state) << ")";
494 break;
495 }
496 return debug;
497}
498#endif
499
500QT_END_NAMESPACE
501
502#endif
503
504#include "moc_qlocalsocket.cpp"
Note: See TracBrowser for help on using the repository browser.