具有多个客户端的 QLocalServer——正确处理

发布于 2024-09-11 08:53:50 字数 1907 浏览 9 评论 0原文

我试图让 IPC 在一台服务器之间工作,以接受来自 Qt 4.6 的多个客户端的请求。一切正常,只是底层命名管道没有关闭,使得 Qt 发出最大句柄达到消息 (62)。

由于我想接受多个客户端连接,因此我按如下方式处理 QLocalServer 类的 newConnections:

void ServerImpl::onNewConnection()
{
    QLocalSocket * plsocket = _server->nextPendingConnection();
    SocketHandler* skh = new SocketHandler(plsocket, this, this);
    connect(plsocket, SIGNAL(readyRead()), skh, SLOT(socketReadyRead()));
    connect(plsocket, SIGNAL(error(QLocalSocket::LocalSocketError)), skh,  
            SLOT(onError(QLocalSocket::LocalSocketError)));
    connect(plsocket, SIGNAL(disconnected()), skh, SLOT(disconnected()));
}

由于每次获得新连接时,从 QLocalServer::nextPendingConnection() 接收到的 QLocalSocket 指针都会被覆盖,因此我通过 SocketHandler 类管理数据请求,它当然封装了一个 QLocalSocket 类:

void SocketHandler::socketReadyRead()
{ 
 QDataStream in(_ps);
 in.setVersion(QDataStream::Qt_4_5);

 forever
 {
  if (_blocksize == 0)
  {
   if (_ps->bytesAvailable() < sizeof(blocksize_t))
    return;

   in >> _blocksize;
  }

  // We need more data?
  if (_ps->bytesAvailable() < _blocksize)
   return;

  _srv->ReadRequest(in);

  // CHECK: Discard remaining blocks
  _blocksize = 0; 
 }
}

这是类声明:

class SocketHandler : public QObject
{
 Q_OBJECT

public:
 SocketHandler(QLocalSocket* ps, ServerImpl* srv, QObject* parent = 0) : 
 QObject(parent), _ps(ps), _blocksize(0), _srv(srv) { };

 virtual ~SocketHandler() {};

public slots:
 void socketReadyRead();
 void onError(QLocalSocket::LocalSocketError);
 void disconnected();

private:

 QLocalSocket*  _ps;
 blocksize_t   _blocksize;
 ServerImpl*  _srv;
};

如上所述,问题是我无法根据客户端请求与服务器断开连接。我尝试了 QLocalSocket::close、QLocalSocket::disconnectFromServer 但没有成功。

我应该采用多线程解决方案吗? (我认为这不能解决问题)。

我正在考虑的另一个解决方案是提供 1:1 QLocalServer<-->QLocalSocket IPC 机制,而不是尝试处理 1:N。

提前致谢。

I'm trying to make IPC work between one server to accept requests from multiple clients with Qt 4.6. All is working, except that the underlying named pipes are not being closed, making Qt to emit the maximum handle reached message (62).

Since I want to accept several client connections, I handle newConnections to the QLocalServer class as follows:

void ServerImpl::onNewConnection()
{
    QLocalSocket * plsocket = _server->nextPendingConnection();
    SocketHandler* skh = new SocketHandler(plsocket, this, this);
    connect(plsocket, SIGNAL(readyRead()), skh, SLOT(socketReadyRead()));
    connect(plsocket, SIGNAL(error(QLocalSocket::LocalSocketError)), skh,  
            SLOT(onError(QLocalSocket::LocalSocketError)));
    connect(plsocket, SIGNAL(disconnected()), skh, SLOT(disconnected()));
}

Since every time I get a new connection the QLocalSocket pointer received from QLocalServer::nextPendingConnection() is overwritten, I manage the data requests through the SocketHandler class, which of course encapsulates a QLocalSocket class:

void SocketHandler::socketReadyRead()
{ 
 QDataStream in(_ps);
 in.setVersion(QDataStream::Qt_4_5);

 forever
 {
  if (_blocksize == 0)
  {
   if (_ps->bytesAvailable() < sizeof(blocksize_t))
    return;

   in >> _blocksize;
  }

  // We need more data?
  if (_ps->bytesAvailable() < _blocksize)
   return;

  _srv->ReadRequest(in);

  // CHECK: Discard remaining blocks
  _blocksize = 0; 
 }
}

Here's the class declaration:

class SocketHandler : public QObject
{
 Q_OBJECT

public:
 SocketHandler(QLocalSocket* ps, ServerImpl* srv, QObject* parent = 0) : 
 QObject(parent), _ps(ps), _blocksize(0), _srv(srv) { };

 virtual ~SocketHandler() {};

public slots:
 void socketReadyRead();
 void onError(QLocalSocket::LocalSocketError);
 void disconnected();

private:

 QLocalSocket*  _ps;
 blocksize_t   _blocksize;
 ServerImpl*  _srv;
};

The problem, as stated above, is that I can't disconnect from server at client request. I tried QLocalSocket::close, QLocalSocket::disconnectFromServer without luck.

Should I go with a multithreaded solution? (I think it won't solve the problem).

Another solution I'm thinking is to offer a 1:1 QLocalServer<-->QLocalSocket IPC mechanism, instead of trying to handle 1:N.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文