Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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::flush()
194
195
This function writes as much as possible from the internal write buffer
196
to the socket, without blocking. If any data was written, this function
197
returns \c true; otherwise false is returned.
198
199
Call this function if you need QLocalSocket to start sending buffered data
200
immediately. The number of bytes successfully written depends on the
201
operating system. In most cases, you do not need to call this function,
202
because QLocalSocket will start sending data automatically once control
203
goes back to the event loop. In the absence of an event loop, call
204
waitForBytesWritten() instead.
205
206
\sa write(), waitForBytesWritten()
207
*/
208
209
/*!
210
\fn void QLocalSocket::disconnectFromServer()
211
212
Attempts to close the socket. If there is pending data waiting to be
213
written, QLocalSocket will enter ClosingState and wait until all data
214
has been written. Eventually, it will enter UnconnectedState and emit
215
the disconnected() signal.
216
217
\sa connectToServer()
218
*/
219
220
/*!
221
\fn QLocalSocket::LocalSocketError QLocalSocket::error() const
222
223
Returns the type of error that last occurred.
224
225
\sa state(), errorString()
226
*/
227
228
/*!
229
\fn bool QLocalSocket::isValid() const
230
231
Returns \c true if the socket is valid and ready for use; otherwise
232
returns \c false.
233
234
\note The socket's state must be ConnectedState before reading
235
and writing can occur.
236
237
\sa state(), connectToServer()
238
*/
239
240
/*!
241
\fn qint64 QLocalSocket::readBufferSize() const
242
243
Returns the size of the internal read buffer. This limits the amount of
244
data that the client can receive before you call read() or readAll().
245
A read buffer size of 0 (the default) means that the buffer has no size
246
limit, ensuring that no data is lost.
247
248
\sa setReadBufferSize(), read()
249
*/
250
251
/*!
252
\fn void QLocalSocket::setReadBufferSize(qint64 size)
253
254
Sets the size of QLocalSocket's internal read buffer to be \a size bytes.
255
256
If the buffer size is limited to a certain size, QLocalSocket won't
257
buffer more than this size of data. Exceptionally, a buffer size of 0
258
means that the read buffer is unlimited and all incoming data is buffered.
259
This is the default.
260
261
This option is useful if you only read the data at certain points in
262
time (e.g., in a real-time streaming application) or if you want to
263
protect your socket against receiving too much data, which may eventually
264
cause your application to run out of memory.
265
266
\sa readBufferSize(), read()
267
*/
268
269
/*!
270
\fn bool QLocalSocket::waitForConnected(int msecs)
271
272
Waits until the socket is connected, up to \a msecs milliseconds. If the
273
connection has been established, this function returns \c true; otherwise
274
it returns \c false. In the case where it returns \c false, you can call
275
error() to determine the cause of the error.
276
277
The following example waits up to one second for a connection
278
to be established:
279
280
\snippet code/src_network_socket_qlocalsocket_unix.cpp 0
281
282
If \a msecs is -1, this function will not time out.
283
284
\sa connectToServer(), connected()
285
*/
286
287
/*!
288
\fn bool QLocalSocket::waitForDisconnected(int msecs)
289
290
Waits until the socket has disconnected, up to \a msecs milliseconds. If the
291
connection was successfully disconnected, this function returns \c true;
292
otherwise it returns \c false (if the operation timed out, if an error
293
occurred, or if this QLocalSocket is already disconnected). In the case
294
where it returns \c false, you can call error() to determine the cause of
295
the error.
296
297
The following example waits up to one second for a connection
298
to be closed:
299
300
\snippet code/src_network_socket_qlocalsocket_unix.cpp 1
301
302
If \a msecs is -1, this function will not time out.
303
304
\sa disconnectFromServer(), close()
305
*/
306
307
/*!
308
\fn bool QLocalSocket::waitForReadyRead(int msecs)
309
310
This function blocks until data is available for reading and the
311
\l{QIODevice::}{readyRead()} signal has been emitted. The function
312
will timeout after \a msecs milliseconds; the default timeout is
313
30000 milliseconds.
314
315
The function returns \c true if data is available for reading;
316
otherwise it returns \c false (if an error occurred or the
317
operation timed out).
318
319
\sa waitForBytesWritten()
320
*/
321
322
/*!
323
\fn void QLocalSocket::disconnected()
324
325
This signal is emitted when the socket has been disconnected.
326
327
\sa connectToServer(), disconnectFromServer(), abort(), connected()
328
*/
329
330
/*!
331
\fn void QLocalSocket::errorOccurred(QLocalSocket::LocalSocketError socketError)
332
\since 5.15
333
334
This signal is emitted after an error occurred. The \a socketError
335
parameter describes the type of error that occurred.
336
337
\sa error(), errorString()
338
*/
339
340
/*!
341
\fn void QLocalSocket::stateChanged(QLocalSocket::LocalSocketState socketState)
342
343
This signal is emitted whenever QLocalSocket's state changes.
344
The \a socketState parameter is the new state.
345
346
\sa state()
347
*/
348
349
/*!
350
Creates a new local socket. The \a parent argument is passed to
351
QObject's constructor.
352
*/
353
QLocalSocket::QLocalSocket(QObject * parent)
354
: QIODevice(*
new
QLocalSocketPrivate, parent)
355
{
356
Q_D(QLocalSocket);
357
358
d->readBufferChunkSize = 0;
// force QIODevice unbuffered mode
359
d->init();
360
}
361
362
/*!
363
Destroys the socket, closing the connection if necessary.
364
*/
365
QLocalSocket::~QLocalSocket()
366
{
367
abort();
368
#
if
!
defined
(
Q_OS_WIN
)
&&
!
defined
(
QT_LOCALSOCKET_TCP
)
369
Q_D(QLocalSocket);
370
d->unixSocket.setParent(
nullptr
);
371
#
endif
372
}
373
374
bool
QLocalSocket::open(OpenMode openMode)
375
{
376
connectToServer(openMode);
377
return
isOpen();
378
}
379
380
/*! \overload
381
382
Set the server \a name and attempts to make a connection to it.
383
384
The socket is opened in the given \a openMode and first enters ConnectingState.
385
If a connection is established, QLocalSocket enters ConnectedState and emits connected().
386
387
After calling this function, the socket can emit errorOccurred() to signal that an error occurred.
388
389
\sa state(), serverName(), waitForConnected()
390
*/
391
void
QLocalSocket::connectToServer(
const
QString &name, OpenMode openMode)
392
{
393
setServerName(name);
394
connectToServer(openMode);
395
}
396
397
/*!
398
\since 5.1
399
400
Set the \a name of the peer to connect to.
401
On Windows name is the name of a named pipe; on Unix name is the name of a local domain socket.
402
403
This function must be called when the socket is not connected.
404
*/
405
void
QLocalSocket::setServerName(
const
QString & name)
406
{
407
Q_D(QLocalSocket);
408
if
(d->state != UnconnectedState) {
409
qWarning(
"QLocalSocket::setServerName() called while not in unconnected state"
);
410
return
;
411
}
412
d->serverName = name;
413
}
414
415
/*!
416
Returns the name of the peer as specified by setServerName(), or an
417
empty QString if setServerName() has not been called or connectToServer() failed.
418
419
\sa connectToServer(), fullServerName()
420
421
*/
422
QString QLocalSocket::serverName()
const
423
{
424
Q_D(
const
QLocalSocket);
425
return
d->serverName;
426
}
427
428
/*!
429
\property QLocalSocket::socketOptions
430
\since 6.2
431
\brief the socket options.
432
433
Options must be set while the socket is in \l{UnconnectedState} state.
434
435
\sa connectToServer()
436
*/
437
QLocalSocket::SocketOptions QLocalSocket::socketOptions()
const
438
{
439
Q_D(
const
QLocalSocket);
440
return
d->socketOptions;
441
}
442
443
void
QLocalSocket::setSocketOptions(QLocalSocket::SocketOptions option)
444
{
445
Q_D(QLocalSocket);
446
if
(d->state != UnconnectedState) {
447
qWarning(
"QLocalSocket::setSocketOptions() called while not in unconnected state"
);
448
return
;
449
}
450
d->socketOptions = option;
451
}
452
453
QBindable<QLocalSocket::SocketOptions> QLocalSocket::bindableSocketOptions()
454
{
455
Q_D(QLocalSocket);
456
return
&d->socketOptions;
457
}
458
459
/*!
460
Returns the server path that the socket is connected to.
461
462
\note The return value of this function is platform specific.
463
464
\sa connectToServer(), serverName()
465
*/
466
QString QLocalSocket::fullServerName()
const
467
{
468
Q_D(
const
QLocalSocket);
469
return
d->fullServerName;
470
}
471
472
/*!
473
Returns the state of the socket.
474
475
\sa error()
476
*/
477
QLocalSocket::LocalSocketState QLocalSocket::state()
const
478
{
479
Q_D(
const
QLocalSocket);
480
return
d->state;
481
}
482
483
/*! \reimp
484
*/
485
bool
QLocalSocket::isSequential()
const
486
{
487
return
true
;
488
}
489
490
/*!
491
\enum QLocalSocket::LocalSocketError
492
493
The LocalServerError enumeration represents the errors that can occur.
494
The most recent error can be retrieved through a call to
495
\l QLocalSocket::error().
496
497
\value ConnectionRefusedError The connection was refused by
498
the peer (or timed out).
499
\value PeerClosedError The remote socket closed the connection.
500
Note that the client socket (i.e., this socket) will be closed
501
after the remote close notification has been sent.
502
\value ServerNotFoundError The local socket name was not found.
503
\value SocketAccessError The socket operation failed because the
504
application lacked the required privileges.
505
\value SocketResourceError The local system ran out of resources
506
(e.g., too many sockets).
507
\value SocketTimeoutError The socket operation timed out.
508
\value DatagramTooLargeError The datagram was larger than the operating
509
system's limit (which can be as low as 8192 bytes).
510
\value ConnectionError An error occurred with the connection.
511
\value UnsupportedSocketOperationError The requested socket operation
512
is not supported by the local operating system.
513
\value OperationError An operation was attempted while the socket was in a state that
514
did not permit it.
515
\value UnknownSocketError An unidentified error occurred.
516
*/
517
518
/*!
519
\enum QLocalSocket::LocalSocketState
520
521
This enum describes the different states in which a socket can be.
522
523
\sa QLocalSocket::state()
524
525
\value UnconnectedState The socket is not connected.
526
\value ConnectingState The socket has started establishing a connection.
527
\value ConnectedState A connection is established.
528
\value ClosingState The socket is about to close
529
(data may still be waiting to be written).
530
*/
531
532
QT_END_NAMESPACE
533
534
#
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.16.1