示例#1
0
void VTcpServer::run()
{
  while (runThread().active())
  {
    VTcpSession* newTCPSession = accept();
    if (newTCPSession == NULL)
    {
      SET_DEBUG_ERROR(VNetError, "error in accept", WSAGetLastError());
      break;
    }
    if (!newTCPSession->open())
    {
      error = newTCPSession->error;
      continue;
    }
    VTcpSessionThread* thread = new VTcpSessionThread(this, newTCPSession);
    //thread->moveToThread(runThread().m_qthread); // gilgil temp 2012.08.16
    thread->name = className();
    threadList.lock();
    threadList.append(thread);
    threadList.unlock();
    if (!thread->open())
    {
      error = thread->error;
      break;
    }
  }
}
示例#2
0
 virtual void run()
 {
   VHttpProxy* proxy = (VHttpProxy*)owner;
   LOG_DEBUG("stt"); // gilgil temp 2013.10.19
   while (true)
   {
     QByteArray msg;
     int readLen = outClient->tcpSession->read(msg);
     if (readLen == VERR_FAIL) break;
     emit proxy->beforeResponse(msg, outClient, inSession);
     int writeLen = inSession->write(msg);
     if (writeLen == VERR_FAIL) break;
    }
   inSession->close();
   outClient->close();
   LOG_DEBUG("end"); // gilgil temp 2013.10.19
 }
示例#3
0
void VHttpProxy::run(VTcpSession* tcpSession)
{
  LOG_DEBUG("stt"); // gilgil temp 2013.10.19
  VTcpSession*          inSession = tcpSession;
  VHttpRequest           request;
  VTcpClient            outClient;
  VHttpProxyOutInThread* thread = NULL;

  QByteArray totalPacket;
  while (true)
  {
    QByteArray packet;
    int readLen = inSession->read(packet);
    if (readLen == VERR_FAIL) break;
    // LOG_DEBUG("%s", packet.data()); // gilgil temp

    totalPacket += packet;
    if (!request.parse(totalPacket))
    {
      if (outClient.active())
      {
        outClient.tcpSession->write(totalPacket);
        totalPacket = "";
      }
      continue;
    }

    QString host;
    int port;
    QUrl url = request.requestLine.path;
    if (!url.isRelative())
    {
      host = url.host();
      port = url.port();
      if (port == -1) port = 80;

      QByteArray newPath = url.path().toUtf8();
      if (url.hasQuery())
        newPath += "?" + url.query(QUrl::FullyEncoded).toLatin1();
      request.requestLine.path = newPath;
    } else
    if (!request.findHost(host, port))
    {
      LOG_ERROR("can not find host:%s", totalPacket.data());
      break;
    }

    if (outClient.host != host || outClient.port != port)
    {
      outClient.close();
      if (thread != NULL) delete thread;

      outClient.host = host;
      outClient.port = port;
      if (!outClient.open())
      {
        LOG_ERROR("%s", outClient.error.msg);
        break;
      }
      thread = new VHttpProxyOutInThread(&outClient, inSession, this);
      thread->open();
    }

    emit beforeRequest(request, inSession, &outClient);
    outClient.tcpSession->write(request.toByteArray());
    totalPacket = "";
  }
  LOG_DEBUG("end"); // gilgil temp 2013.10.19
  outClient.close();
  if (thread != NULL) delete thread;
}
示例#4
0
bool VTcpServer::doClose()
{
  threadTag = 1011; // gilgil temp 2014.04.04
  VLock lock(stateCloseCs); // gilgil temp 2014.03.14
  threadTag = 1012; // gilgil temp 2014.04.04

  // ------------------------------------------------------------------------
  // Close acceptSession and thread
  // ------------------------------------------------------------------------
  acceptSession->close();
  runThread().close();


  threadTag = 1016; // gilgil temp 2014.04.04
  threadList.lock();
  // ------------------------------------------------------------------------
  // Close all connected socket
  // ------------------------------------------------------------------------
  for (VTcpSessionThreadList::iterator it = threadList.begin(); it != threadList.end(); it++)
  {
    VTcpSessionThread* thread = *it;
    // ----- by gilgil 2012.12.07 -----
    // When TcpServer is active, session thread is deleted automatically,
    // while, when TcpServer is closing state, session thread is deleted manually.
    // thread->freeOnTerminate = false; // gilgil temp 2012.12.07 // gilgil temp 2014.04.04
    // --------------------------------
    VTcpSession* tcpSession = thread->tcpSession;
    tcpSession->close();
  }
  threadList.unlock();
  threadTag = 1018; // gilgil temp 2014.04.04

  // ------------------------------------------------------------------------
  // Wait until all connection is disconnected
  // ------------------------------------------------------------------------
  VTick beg = tick();
  while (true)
  {
    msleep(1);
    threadList.lock();
    int count = threadList.count();
    threadList.unlock();
    if (count == 0) break;
    VTick now = tick();
    if (now - beg > vd::DEFAULT_TIMEOUT * 2) // gilgil temp 2014.04.04
    {
      LOG_FATAL("timeout session count=%d", threadList.count());
      // break; // gilgil temp 2012.11.27
    }
  }
  /*
  threadList.lock();
  int count = threadList.count();
  threaddList.un
  for (VTcpSessionThreadList::iterator it = threadList.begin(); it != threadList.end(); it++)
  {
    threadTag = 1020; // gilgil temp 2014.04.04
    VTcpSessionThread* thread = *it;
    threadTag = 1022; // gilgil temp 2014.04.04
    thread->wait(vd::DEFAULT_TIMEOUT + 2000); // gilgil temp 2014.04.04
    threadTag = 1023; // gilgil temp 2014.04.04
    delete thread;
    threadTag = 1024; // gilgil temp 2014.04.04
    VTick now = tick();
    if (now - beg > vd::DEFAULT_TIMEOUT * 2) // gilgil temp 2014.04.04
    {
      LOG_FATAL("timeout session count=%d", threadList.count());
      // break; // gilgil temp 2012.11.27
    }
    threadTag = 1025; // gilgil temp 2014.04.04
  }
  threadList.clear();
  threadList.unlock();
  threadTag = 1026; // gilgil temp 2014.04.04
  */

  return true;
}