[英]What is happened with QSocketNotifier when file descriptor becomes bad?
Assuming I have QSocketNotifier
that is constructed against valid file descriptor (it's actually joystick device - /dev/input/js0
). 假设我有一个针对有效文件描述符构造的QSocketNotifier
(实际上是操纵杆设备- /dev/input/js0
)。 I connect QSocketNotifier::activated
with corresponding handler. 我将QSocketNotifier::activated
与相应的处理程序连接。 When file disappears (hardware is disconnected) - what should I expect? 当文件消失(硬件断开连接)时,我应该期待什么?
As I can see, ::activated
emits repeatedely with the same socket parameter, but there is errno == ENODEV
after read()
call inside handler. 如我所见, ::activated
重复使用相同的套接字参数发出,但是在处理程序内部的read()
调用之后有errno == ENODEV
。 May I expect that ::activated
would be always emitted after descriptor goes bad (and therefore catch disconnection event) or actual behavior of QSocketNotifier
is not defined? 我是否可以期望::activated
总是在描述符变坏(并因此导致断开连接事件)或未定义QSocketNotifier
实际行为后发出?
I can't speak to the behavior of /dev/input/js0
in particular since I've never used that device, but the general behavior for a socket/file-descriptor whose remote-connection has closed would be to return as ready-for-read, and then when the application tries to call recv()
(or read()
) on the file descriptor, either of these calls would return 0 (aka EOF) to indicate that the remote device has closed the connection. 由于我从未使用过/dev/input/js0
的行为,所以我不能特别讲它的行为,但是对于远程连接已关闭的套接字/文件描述符,其一般行为是返回ready- for-read,然后当应用程序尝试在文件描述符上调用recv()
(或read()
)时,这些调用中的任何一个都将返回0(又称EOF),以指示远程设备已关闭连接。
Presumably this (or something similar) is happens when using QSocketNotifier
as well, ie Qt's internal code will notify the QSocketNotifier
that the file descriptor is now ready-for-read, and the QSocketNotifier
will respond by emitting the activated(int)
Qt-signal. 大概在使用QSocketNotifier
时也会发生这种情况(或类似的情况),即Qt的内部代码将通知QSocketNotifier
文件描述符现在可以读取了,并且QSocketNotifier
会通过发出已activated(int)
Qt信号进行响应。 。 So I think your use-pattern is good. 因此,我认为您的使用模式很好。
(What would be bad/undefined behavior is if you (or somebody else) called close()
on the file descriptor without first disabling or destroying any QSocketNotifier
objects that were watching that file-descriptor. That would be bad because the QSocketNotifier
object(s) would have no way to know that the file descriptor was closed, and so they'd keep trying to use it -- probably they would get EBADFS
-type errors when they did so, but there's also a chance that some other thread in your process would create a new file descriptor shortly afterwards for some unrelated purpose, and by chance it would end up with the same int-value as the one that was previously close()
'd, and then you'd have a really fun time debugging the odd behavior that resulted :)) (什么是坏/不确定的行为,如果你(或其他人)被称为close()
在没有首先禁用或破坏任何文件描述符QSocketNotifier
是在看该文件描述符的对象。这将是不好的,因为QSocketNotifier
对象(S )无法知道文件描述符已关闭,因此他们会继续尝试使用它-这样做时可能会遇到EBADFS
类型的错误,但是您的其他线程也有可能进程将在不久后出于一些不相关的目的而创建一个新的文件描述符,并且偶然地,它最终将具有与先前close()
'd相同的int值,然后您将有一个非常有趣的调试时间导致的奇怪行为:))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.