简体   繁体   English

libuv-如何测试侦听套接字与客户端(中继应用程序)的断开连接

[英]libuv - How to test for a disconnection of a listening socket from client (relay application)

My application sporadically modifies and relays messages that it receives to a listener server daemon (all using unix domain sockets, so uv_pipe_t). 我的应用程序偶尔修改接收到的消息并将其中继到侦听器服务器守护程序(全部使用unix域套接字,因此为uv_pipe_t)。

  1. (Workflow that has me stumped) When the first message has to be relayed, it makes a uv_try_write() in the uv_read_done callback function (where it is reading on a listening socket of its own) (让我感到困惑的工作流程)当必须中继第一条消息时,它会在uv_read_done回调函数中创建uv_try_write()(在其自身的侦听套接字上进行读取)

    • If the listening daemon is already up and running, this is the perfect, and the message is relayed 如果侦听守护程序已经启动并正在运行,那么这是完美的选择,并且消息已中继
    • If the listening daemon is not yet up 如果监听守护程序尚未启动
      • uv_try_write fails, I check the status which is -ve (EAGAIN), so I try a connect (uv_pipe_connect). uv_try_write失败,我检查状态为-ve(EAGAIN),所以尝试连接(uv_pipe_connect)。 After this I uv_try_write() again. 之后,我再次uv_try_write()。
      • Since the connect fails (ENOENT, I log an error and give up.) 由于连接失败(ENOENT,我记录了一个错误并放弃。)
    • I now start the listening daemon up 我现在启动监听守护程序
      • The uv_try_write again fails on the first message, despite the connect() (because I presume it makes the connect in the next loop iteration) 尽管使用了connect(),但uv_try_write在第一个消息上再次失败(因为我认为它将在下一个循环迭代中进行连接)
      • The second write onwards works fine and as expected 从第二次起,可以正常工作并按预期进行
    • I kill the listening daemon 我杀死了监听守护进程
      • On the coming write, the app receives a SIGPIPE error ( I have blocked this with sigaction and sigprocmask) 在即将进行的写入操作中,应用程序收到SIGPIPE错误(我已通过sigaction和sigprocmask阻止了此操作)
    • I restart the listening daemon 我重新启动监听守护程序
      • This time the connect() fails with an EISCONN error ( which I presume means that the handle I used in the first connect is still live, and needs to be closed. However, since I cannot detect when the connection was closed from the listener daemon the last time, I cannot know when to close the handle. 这次connect()失败,并显示EISCONN错误(我认为这意味着我在第一个连接中使用的句柄仍然有效,并且需要关闭。)但是,由于我无法从侦听器守护程序检测到何时关闭了连接,最后一次,我不知道何时关闭手柄。
  2. Questions about best practice 有关最佳做法的问题

    • Is the the best way to design the relay app? 是设计中继应用程序的最佳方法吗? Perhaps not, because it is not very resilient dropping messages on reconnections, and I do not want to make hacks around this without ensuring I am following the proper practices using libuv 也许不是,因为在重新连接时丢弃消息不是很灵活,而且我不想在不确保自己遵循使用libuv的正确做法的情况下进行破解。
    • If it is, are any of the following options worthy? 如果是,那么以下任何选项是否值得?
      • some periodic timer setup heartbeating with the peers 一些定期的计时器设置与对等设备一起跳动
      • a uv_check handle that is checking for connection status at every loop iteration somehow. 一个以某种方式在每次循环迭代时检查连接状态的uv_check句柄。 If so, how to check for connection status? 如果是这样,如何检查连接状态? uv_is_writeable always returns 0, even on a connected socket. uv_is_writeable始终返回0,即使在已连接的套接字上也是如此。 Same with uv_is_active 与uv_is_active相同
      • uv_try_write() from the on_connect callback function to send the first message that is getting dropped 来自on_connect回调函数的uv_try_write()发送被删除的第一条消息

Thanks very much in advance for the help! 非常感谢您的帮助!

You can call uv_write after you call uv_pipe_connect , and the write will be queued. 您可以在调用uv_write之后调用uv_pipe_connect ,并且写入将排队。 Writes will happen after the connection succeeds, or fail with UV_ECANCELED if the connection failed. 连接成功后将发生写UV_ECANCELED如果连接失败,将失败并显示UV_ECANCELED

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM