POCO连接卡住了

时间:2015-01-14 18:38:54

标签: c++ tcp connection poco-libraries reactor

我正在使用POCO reactor模式来处理传入的tcp连接。连接可能需要几秒到几分钟,具体取决于请求类型,如下所示:

  try{
    ServerSocket serverSocket(port);
    reactor = new SocketReactor();
    ParallelSocketAcceptor<BFSTcpServiceHandler,SocketReactor> acceptor(serverSocket, *reactor);
    //Start Reactor
    reactor->run();
  }catch(Exception&e){
    LOG(ERROR)<<"ERROR in initializing TCPServer:"<<e.message();
    return;
  }

以下是处理程序:

BFSTcpServiceHandler::BFSTcpServiceHandler(StreamSocket& _socket,
    SocketReactor& _reactor): socket(_socket),reactor(_reactor) {
  //Set Keeep Alive for socket
  socket.setKeepAlive(false);

  //Register Callbacks
  reactor.addEventHandler(socket, NObserver<BFSTcpServiceHandler,
    ReadableNotification>(*this, &BFSTcpServiceHandler::onReadable));
  /*reactor.addEventHandler(socket, NObserver<BFSTcpServiceHandler,
    WritableNotification>(*this, &BFSTcpServiceHandler::onWriteable));*/
  reactor.addEventHandler(socket, NObserver<BFSTcpServiceHandler,
    ShutdownNotification>(*this, &BFSTcpServiceHandler::onShutdown));
  reactor.addEventHandler(socket, NObserver<BFSTcpServiceHandler,
    ErrorNotification>(*this, &BFSTcpServiceHandler::onError));
  reactor.addEventHandler(socket, NObserver<BFSTcpServiceHandler,
    TimeoutNotification>(*this, &BFSTcpServiceHandler::onTimeout));
  /*reactor.addEventHandler(socket, NObserver<BFSTcpServiceHandler,
    IdleNotification>(*this, &BFSTcpServiceHandler::onIdle));*/
}

BFSTcpServiceHandler::~BFSTcpServiceHandler() {
  //Unregister Callbacks
  reactor.removeEventHandler(socket, NObserver<BFSTcpServiceHandler,
    ReadableNotification>(*this, &BFSTcpServiceHandler::onReadable));
    ...
  //Close socket
  try {
    socket.close();
  }catch(...){}
}

void BFSTcpServiceHandler::onReadable(
    const Poco::AutoPtr<Poco::Net::ReadableNotification>& pNf) {
  //LOG(ERROR)<<"onReadable:"<<socket.peerAddress().toString();
  try{
   //Read and process request
  } catch(Exception &e){
    LOG(ERROR)<<"Error in reading request:"<<e.message();
    delete this;
  }
  //So after a connection is served just close it!
  delete this;
}

void BFSTcpServiceHandler::onShutdown(
    const Poco::AutoPtr<Poco::Net::ShutdownNotification>& pNf) {
  LOG(ERROR)<<"onShutdown:"<<socket.peerAddress().toString();

  //Call destructor of this class
  delete this;
}

void BFSTcpServiceHandler::onWriteable(
    const Poco::AutoPtr<Poco::Net::WritableNotification>& pNf) {
  static bool once = true;
  if(once) {
    LOG(ERROR)<<"onWritable:"<<socket.peerAddress().toString()<<" keepAlive?"<<socket.getKeepAlive()<<" isBlocking?"<<socket.getBlocking()<<" noDeley?"<<socket.getNoDelay();
    once = false;
  }
}

void BFSTcpServiceHandler::onTimeout(
    const Poco::AutoPtr<Poco::Net::TimeoutNotification>& pNf) {
  LOG(ERROR)<<"\nTIMEOUT! onTimeout:"<<socket.peerAddress().toString();
}

void BFSTcpServiceHandler::onError(
    const Poco::AutoPtr<Poco::Net::ErrorNotification>& pNf) {
  LOG(ERROR)<<"\nERROR! onError:"<<socket.peerAddress().toString();
}

void BFSTcpServiceHandler::onIdle(
    const Poco::AutoPtr<Poco::Net::IdleNotification>& pNf) {
  LOG(ERROR)<<"\nIDLE! onIdle:"<<socket.peerAddress().toString();
}

代码工作正常;然而,过了一段时间它会卡住意味着服务器确实接受了连接但是onReadable根本不再被调用。例如,在它卡住后我可以telnet到服务器但是当我发送数据onReadable时不会被触发。使用netstat我意识到一些数据被保存在RCV_QEUEUE中,并且reactor不会触发onReadable事件。

我认为这是由于达到了系统的连接/文件限制,但实际上并没有在系统卡住时打开很多连接。

感谢任何评论或帮助。

谢谢,

1 个答案:

答案 0 :(得分:0)

问题是使用了有故障的NIC /驱动程序。我将代码更改为常规POSIX套接字并遇到了同样的问题,并且切换NIC解决了这个问题。我不确定这是驱动程序还是硬件问题。