Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qlocalsocket.cpp
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
// Qt-Security score:significant reason:default
4
5
#
include
<
QtNetwork
/
private
/
qtnetworkglobal_p
.
h
>
6
7
#
include
"qlocalsocket.h"
8
#
include
"qlocalsocket_p.h"
9
10
#
include
<
QtCore
/
qdebug
.
h
>
11
12
QT_BEGIN_NAMESPACE
13
14
/*!
15
\class QLocalSocket
16
\since 4.4
17
\inmodule QtNetwork
18
19
\brief The QLocalSocket class provides a local socket.
20
21
On Windows this is a named pipe and on Unix this is a local domain socket.
22
23
If an error occurs, error() returns the type of error, and
24
errorString() can be called to get a human readable description
25
of what happened.
26
27
Although QLocalSocket is designed for use with an event loop, it's possible
28
to use it without one. In that case, you must use waitForConnected(),
29
waitForReadyRead(), waitForBytesWritten(), and waitForDisconnected()
30
which blocks until the operation is complete or the timeout expires.
31
32
\sa QLocalServer
33
*/
34
35
/*!
36
\enum QLocalSocket::SocketOption
37
\since 6.2
38
This enum describes the possible options that can be used to connect to
39
a server. Currently, on Linux and Android it is used for specifying
40
connection to a server listening to a socket bound to an abstract address.
41
42
\value NoOptions No options have been set.
43
\value AbstractNamespaceOption
44
The socket will try to connect to an abstract address. This flag is specific
45
to Linux and Android. On other platforms is ignored.
46
47
\sa socketOptions
48
*/
49
50
/*!
51
\fn void QLocalSocket::connectToServer(OpenMode openMode)
52
\since 5.1
53
54
Attempts to make a connection to serverName().
55
setServerName() must be called before you open the connection.
56
Alternatively you can use connectToServer(const QString &name, OpenMode openMode);
57
58
The socket is opened in the given \a openMode and first enters ConnectingState.
59
If a connection is established, QLocalSocket enters ConnectedState and emits connected().
60
61
After calling this function, the socket can emit errorOccurred() to signal that an error occurred.
62
63
\sa state(), serverName(), waitForConnected()
64
*/
65
66
/*!
67
\fn void QLocalSocket::open(OpenMode openMode)
68
69
Equivalent to connectToServer(OpenMode mode).
70
The socket is opened in the given \a openMode to the server defined by setServerName().
71
72
Note that unlike in most other QIODevice subclasses, open() may not open the device directly.
73
The function return false if the socket was already connected or if the server to connect
74
to was not defined and true in any other case. The connected() or errorOccurred() signals will be
75
emitted once the device is actually open (or the connection failed).
76
77
See connectToServer() for more details.
78
*/
79
80
/*!
81
\fn void QLocalSocket::connected()
82
83
This signal is emitted after connectToServer() has been called and
84
a connection has been successfully established.
85
86
\sa connectToServer(), disconnected()
87
*/
88
89
/*!
90
\fn bool QLocalSocket::setSocketDescriptor(qintptr socketDescriptor,
91
LocalSocketState socketState, OpenMode openMode)
92
93
Initializes QLocalSocket with the native socket descriptor
94
\a socketDescriptor. Returns \c true if socketDescriptor is accepted
95
as a valid socket descriptor; otherwise returns \c false. The socket is
96
opened in the mode specified by \a openMode, and enters the socket state
97
specified by \a socketState.
98
99
\note It is not possible to initialize two local sockets with the same
100
native socket descriptor.
101
102
\sa socketDescriptor(), state(), openMode()
103
*/
104
105
/*!
106
\fn qintptr QLocalSocket::socketDescriptor() const
107
108
Returns the native socket descriptor of the QLocalSocket object if
109
this is available; otherwise returns -1.
110
111
The socket descriptor is not available when QLocalSocket
112
is in UnconnectedState.
113
The type of the descriptor depends on the platform:
114
115
\list
116
\li On Windows, the returned value is a
117
\l{Winsock 2 Socket Handle}.
118
119
\li On INTEGRITY, the returned value is the
120
QTcpSocket socket descriptor and the type is defined by
121
\l{QTcpSocket::socketDescriptor}{socketDescriptor}.
122
123
\li On all other UNIX-like operating systems, the type is
124
a file descriptor representing a socket.
125
\endlist
126
127
\sa setSocketDescriptor()
128
*/
129
130
/*!
131
\fn qint64 QLocalSocket::readData(char *data, qint64 c)
132
\reimp
133
*/
134
135
/*!
136
\fn qint64 QLocalSocket::readLineData(char *data, qint64 maxSize)
137
\reimp
138
*/
139
140
/*!
141
\fn qint64 QLocalSocket::skipData(qint64 maxSize)
142
\reimp
143
*/
144
145
/*!
146
\fn qint64 QLocalSocket::writeData(const char *data, qint64 c)
147
\reimp
148
*/
149
150
/*!
151
\fn void QLocalSocket::abort()
152
153
Aborts the current connection and resets the socket.
154
Unlike disconnectFromServer(), this function immediately closes the socket,
155
clearing any pending data in the write buffer.
156
157
\sa disconnectFromServer(), close()
158
*/
159
160
/*!
161
\fn qint64 QLocalSocket::bytesAvailable() const
162
\reimp
163
*/
164
165
/*!
166
\fn qint64 QLocalSocket::bytesToWrite() const
167
\reimp
168
*/
169
170
/*!
171
\fn bool QLocalSocket::canReadLine() const
172
\reimp
173
*/
174
175
/*!
176
\fn void QLocalSocket::close()
177
178
Closes the I/O device for the socket and calls disconnectFromServer()
179
to close the socket's connection.
180
181
See QIODevice::close() for a description of the actions that occur when an I/O
182
device is closed.
183
184
\sa abort()
185
*/
186
187
/*!
188
\fn bool QLocalSocket::waitForBytesWritten(int msecs)
189
\reimp
190
*/
191
192
/*!
193
\fn bool QLocalSocket::waitForBytesWritten(QDeadlineTimer deadline)
194
\since 6.13
195
\overload
196
*/
197
198
/*!
199
\fn bool QLocalSocket::flush()
200
201
This function writes as much as possible from the internal write buffer
202
to the socket, without blocking. If any data was written, this function
203
returns \c true; otherwise false is returned.
204
205
Call this function if you need QLocalSocket to start sending buffered data
206
immediately. The number of bytes successfully written depends on the
207
operating system. In most cases, you do not need to call this function,
208
because QLocalSocket will start sending data automatically once control
209
goes back to the event loop. In the absence of an event loop, call
210
waitForBytesWritten() instead.
211
212
\sa write(), waitForBytesWritten()
213
*/
214
215
/*!
216
\fn void QLocalSocket::disconnectFromServer()
217
218
Attempts to close the socket. If there is pending data waiting to be
219
written, QLocalSocket will enter ClosingState and wait until all data
220
has been written. Eventually, it will enter UnconnectedState and emit
221
the disconnected() signal.
222
223
\sa connectToServer()
224
*/
225
226
/*!
227
\fn QLocalSocket::LocalSocketError QLocalSocket::error() const
228
229
Returns the type of error that last occurred.
230
231
\sa state(), errorString()
232
*/
233
234
/*!
235
\fn bool QLocalSocket::isValid() const
236
237
Returns \c true if the socket is valid and ready for use; otherwise
238
returns \c false.
239
240
\note The socket's state must be ConnectedState before reading
241
and writing can occur.
242
243
\sa state(), connectToServer()
244
*/
245
246
/*!
247
\fn qint64 QLocalSocket::readBufferSize() const
248
249
Returns the size of the internal read buffer. This limits the amount of
250
data that the client can receive before you call read() or readAll().
251
A read buffer size of 0 (the default) means that the buffer has no size
252
limit, ensuring that no data is lost.
253
254
\sa setReadBufferSize(), read()
255
*/
256
257
/*!
258
\fn void QLocalSocket::setReadBufferSize(qint64 size)
259
260
Sets the size of QLocalSocket's internal read buffer to be \a size bytes.
261
262
If the buffer size is limited to a certain size, QLocalSocket won't
263
buffer more than this size of data. Exceptionally, a buffer size of 0
264
means that the read buffer is unlimited and all incoming data is buffered.
265
This is the default.
266
267
This option is useful if you only read the data at certain points in
268
time (e.g., in a real-time streaming application) or if you want to
269
protect your socket against receiving too much data, which may eventually
270
cause your application to run out of memory.
271
272
\sa readBufferSize(), read()
273
*/
274
275
/*!
276
\fn bool QLocalSocket::waitForConnected(int msecs)
277
278
Waits until the socket is connected, up to \a msecs milliseconds. If the
279
connection has been established, this function returns \c true; otherwise
280
it returns \c false. In the case where it returns \c false, you can call
281
error() to determine the cause of the error.
282
283
The following example waits up to one second for a connection
284
to be established:
285
286
\snippet code/src_network_socket_qlocalsocket_unix.cpp 0
287
288
If \a msecs is -1, this function will not time out.
289
290
\sa connectToServer(), connected()
291
*/
292
293
/*!
294
\fn bool QLocalSocket::waitForConnected(QDeadlineTimer deadline)
295
\since 6.13
296
\overload
297
298
Waits until the socket is connected, or until \a deadline expires. If the
299
connection has been established, this function returns \c true; otherwise
300
it returns \c false. In the case where it returns \c false, you can call
301
error() to determine the cause of the error.
302
303
\sa connectToServer(), connected()
304
*/
305
306
/*!
307
\fn bool QLocalSocket::waitForDisconnected(int msecs)
308
309
Waits until the socket has disconnected, up to \a msecs milliseconds. If the
310
connection was successfully disconnected, this function returns \c true;
311
otherwise it returns \c false (if the operation timed out, if an error
312
occurred, or if this QLocalSocket is already disconnected). In the case
313
where it returns \c false, you can call error() to determine the cause of
314
the error.
315
316
The following example waits up to one second for a connection
317
to be closed:
318
319
\snippet code/src_network_socket_qlocalsocket_unix.cpp 1
320
321
If \a msecs is -1, this function will not time out.
322
323
\sa disconnectFromServer(), close()
324
*/
325
326
/*!
327
\fn bool QLocalSocket::waitForDisconnected(QDeadlineTimer deadline)
328
\since 6.13
329
\overload
330
331
Waits until the socket has disconnected, or until \a deadline expires. If the
332
connection was successfully disconnected, this function returns \c true;
333
otherwise it returns \c false (if the operation timed out, if an error
334
occurred, or if this QLocalSocket is already disconnected). In the case
335
where it returns \c false, you can call error() to determine the cause of
336
the error.
337
338
\sa disconnectFromServer(), close()
339
*/
340
341
/*!
342
\fn bool QLocalSocket::waitForReadyRead(int msecs)
343
344
This function blocks until data is available for reading and the
345
\l{QIODevice::}{readyRead()} signal has been emitted. The function
346
will timeout after \a msecs milliseconds; the default timeout is
347
30000 milliseconds.
348
349
The function returns \c true if data is available for reading;
350
otherwise it returns \c false (if an error occurred or the
351
operation timed out).
352
353
\sa waitForBytesWritten()
354
*/
355
356
/*!
357
\fn bool QLocalSocket::waitForReadyRead(QDeadlineTimer deadline)
358
\since 6.13
359
\overload
360
361
This function blocks until data is available for reading and the
362
\l{QIODevice::}{readyRead()} signal has been emitted. The function
363
will timeout when \a deadline expires; the default deadline is
364
30000 milliseconds from the moment the function is called.
365
366
The function returns \c true if data is available for reading;
367
otherwise it returns \c false (if an error occurred or the
368
operation timed out).
369
370
\sa waitForBytesWritten()
371
*/
372
373
/*!
374
\fn void QLocalSocket::disconnected()
375
376
This signal is emitted when the socket has been disconnected.
377
378
\sa connectToServer(), disconnectFromServer(), abort(), connected()
379
*/
380
381
/*!
382
\fn void QLocalSocket::errorOccurred(QLocalSocket::LocalSocketError socketError)
383
\since 5.15
384
385
This signal is emitted after an error occurred. The \a socketError
386
parameter describes the type of error that occurred.
387
388
\sa error(), errorString()
389
*/
390
391
/*!
392
\fn void QLocalSocket::stateChanged(QLocalSocket::LocalSocketState socketState)
393
394
This signal is emitted whenever QLocalSocket's state changes.
395
The \a socketState parameter is the new state.
396
397
\sa state()
398
*/
399
400
/*!
401
Creates a new local socket. The \a parent argument is passed to
402
QObject's constructor.
403
*/
404
QLocalSocket::QLocalSocket(QObject * parent)
405
: QIODevice(*
new
QLocalSocketPrivate, parent)
406
{
407
Q_D(QLocalSocket);
408
409
d->readBufferChunkSize = 0;
// force QIODevice unbuffered mode
410
d->init();
411
}
412
413
/*!
414
Destroys the socket, closing the connection if necessary.
415
*/
416
QLocalSocket::~QLocalSocket()
417
{
418
abort();
419
#
if
!
defined
(
Q_OS_WIN
)
&&
!
defined
(
QT_LOCALSOCKET_TCP
)
420
Q_D(QLocalSocket);
421
d->unixSocket.setParent(
nullptr
);
422
#
endif
423
}
424
425
bool
QLocalSocket::open(OpenMode openMode)
426
{
427
connectToServer(openMode);
428
return
isOpen();
429
}
430
431
/*! \overload
432
433
Set the server \a name and attempts to make a connection to it.
434
435
The socket is opened in the given \a openMode and first enters ConnectingState.
436
If a connection is established, QLocalSocket enters ConnectedState and emits connected().
437
438
After calling this function, the socket can emit errorOccurred() to signal that an error occurred.
439
440
\sa state(), serverName(), waitForConnected()
441
*/
442
void
QLocalSocket::connectToServer(
const
QString &name, OpenMode openMode)
443
{
444
setServerName(name);
445
connectToServer(openMode);
446
}
447
448
/*!
449
\since 5.1
450
451
Set the \a name of the peer to connect to.
452
On Windows name is the name of a named pipe; on Unix name is the name of a local domain socket.
453
454
This function must be called when the socket is not connected.
455
*/
456
void
QLocalSocket::setServerName(
const
QString & name)
457
{
458
Q_D(QLocalSocket);
459
if
(d->state != UnconnectedState) {
460
qWarning(
"QLocalSocket::setServerName() called while not in unconnected state"
);
461
return
;
462
}
463
d->serverName = name;
464
}
465
466
/*!
467
Returns the name of the peer as specified by setServerName(), or an
468
empty QString if setServerName() has not been called or connectToServer() failed.
469
470
\sa connectToServer(), fullServerName()
471
472
*/
473
QString QLocalSocket::serverName()
const
474
{
475
Q_D(
const
QLocalSocket);
476
return
d->serverName;
477
}
478
479
/*!
480
\property QLocalSocket::socketOptions
481
\since 6.2
482
\brief the socket options.
483
484
Options must be set while the socket is in \l{UnconnectedState} state.
485
486
\sa connectToServer()
487
*/
488
QLocalSocket::SocketOptions QLocalSocket::socketOptions()
const
489
{
490
Q_D(
const
QLocalSocket);
491
return
d->socketOptions;
492
}
493
494
void
QLocalSocket::setSocketOptions(QLocalSocket::SocketOptions option)
495
{
496
Q_D(QLocalSocket);
497
if
(d->state != UnconnectedState) {
498
qWarning(
"QLocalSocket::setSocketOptions() called while not in unconnected state"
);
499
return
;
500
}
501
d->socketOptions = option;
502
}
503
504
QBindable<QLocalSocket::SocketOptions> QLocalSocket::bindableSocketOptions()
505
{
506
Q_D(QLocalSocket);
507
return
&d->socketOptions;
508
}
509
510
/*!
511
Returns the server path that the socket is connected to.
512
513
\note The return value of this function is platform specific.
514
515
\sa connectToServer(), serverName()
516
*/
517
QString QLocalSocket::fullServerName()
const
518
{
519
Q_D(
const
QLocalSocket);
520
return
d->fullServerName;
521
}
522
523
/*!
524
Returns the state of the socket.
525
526
\sa error()
527
*/
528
QLocalSocket::LocalSocketState QLocalSocket::state()
const
529
{
530
Q_D(
const
QLocalSocket);
531
return
d->state;
532
}
533
534
/*! \reimp
535
*/
536
bool
QLocalSocket::isSequential()
const
537
{
538
return
true
;
539
}
540
541
/*!
542
\enum QLocalSocket::LocalSocketError
543
544
The LocalServerError enumeration represents the errors that can occur.
545
The most recent error can be retrieved through a call to
546
\l QLocalSocket::error().
547
548
\value ConnectionRefusedError The connection was refused by
549
the peer (or timed out).
550
\value PeerClosedError The remote socket closed the connection.
551
Note that the client socket (i.e., this socket) will be closed
552
after the remote close notification has been sent.
553
\value ServerNotFoundError The local socket name was not found.
554
\value SocketAccessError The socket operation failed because the
555
application lacked the required privileges.
556
\value SocketResourceError The local system ran out of resources
557
(e.g., too many sockets).
558
\value SocketTimeoutError The socket operation timed out.
559
\value DatagramTooLargeError The datagram was larger than the operating
560
system's limit (which can be as low as 8192 bytes).
561
\value ConnectionError An error occurred with the connection.
562
\value UnsupportedSocketOperationError The requested socket operation
563
is not supported by the local operating system.
564
\value OperationError An operation was attempted while the socket was in a state that
565
did not permit it.
566
\value UnknownSocketError An unidentified error occurred.
567
*/
568
569
/*!
570
\enum QLocalSocket::LocalSocketState
571
572
This enum describes the different states in which a socket can be.
573
574
\sa QLocalSocket::state()
575
576
\value UnconnectedState The socket is not connected.
577
\value ConnectingState The socket has started establishing a connection.
578
\value ConnectedState A connection is established.
579
\value ClosingState The socket is about to close
580
(data may still be waiting to be written).
581
*/
582
583
QT_END_NAMESPACE
584
585
#
include
"moc_qlocalsocket.cpp"
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qtbase
src
network
socket
qlocalsocket.cpp
Generated on
for Qt by
1.18.0