// We have no data to write and we want to keep the current connection alive if possible.
// That way we can speed up freeing the current NetworkTransaction.
void Network::CloseTransaction()
{
	// Free the current NetworkTransaction's data (if any)
	NetworkTransaction *r = readyTransactions;
	if (r == NULL)
	{
		return;
	}
	r->FreePbuf();

	// Terminate this connection if this NetworkTransaction indicates a graceful disconnect
	TransactionStatus status = r->status;
	if (!r->LostConnection() && status == disconnected)
	{
//		debugPrintf("Network: CloseRequest is closing connection cs=%08x\n", (unsigned int)locCs);
		ConnectionClosed(r->cs, true);
	}

	// Remove the current item from readyTransactions
	readyTransactions = r->next;

	// Append it to freeTransactions again unless it's already on another list
	if (status != dataSending)
	{
		AppendTransaction(&freeTransactions, r);
	}
}
void CSecEngine::RunL()
	{
	switch ( iRunState )
		{
	case ESocketConnected:
		MakeSecureConnectionL();
		break;

	case ESecureConnected:
		MakePageRequestL();
		break;

	case EGetRequestSent:
		GetServerResponseL();
		break;

	case EDataReceived:
		ReadServerResponseL();
		break;

	case EConnectionClosed:
		ConnectionClosed();
		break;

	default:
		break;
		} // end switch
	}
Exemple #3
0
    size_t
    TCPSocket::doRead(uint8_t* bfr, size_t size)
    {
      ssize_t rv = ::recv(m_handle, (char*)bfr, size, 0);
      if (rv == 0)
      {
        throw ConnectionClosed();
      }
      else if (rv < 0)
      {
        if (errno == ECONNRESET)
          throw ConnectionClosed();
        throw NetworkError(DTR("error receiving data"), getLastErrorMessage());
      }

      return static_cast<size_t>(rv);
    }
Exemple #4
0
int
TCPSocket::read(char* bfr, int size)
{
    int rv = ::recv(m_handle, bfr, size, 0);
    if (rv == 0)
    {
        throw ConnectionClosed();
    }
    else if (rv == -1)
    {
        if (errno == ECONNRESET)
            throw ConnectionClosed();
        throw NetworkError(DTR("error receiving data"), getLastErrorMessage());
    }

    return rv;
}
Exemple #5
0
void CLIENT::Run()
{
	SendMessage("login " + GetPassword());
	if (NeedDebugLog())
	{
		mDebugLog.open("debug.log", std::ofstream::out | std::ofstream::app);
	}

	std::string strLastLineRemaining;
	std::vector<std::string> LastServerResponse;
	for(;;)
	{
		const size_t ReceiveBufferSize = 1<<16;
		char ReceiveBuffer[ ReceiveBufferSize+1 ] = {0};

		int ReceivedBytesCount = recv( mConnectionSocket, ReceiveBuffer, ReceiveBufferSize, 0 );

		if( ReceivedBytesCount == 0 || ReceivedBytesCount == -1)
		{
			// connection is closed or failed
			ConnectionClosed();
			return;
		}
		ReceiveBuffer[ReceivedBytesCount]=0;
		char *line_start = ReceiveBuffer;
		for(;;)
		{
			char *s = strchr(line_start, '\n');
			if (!s)
			{
				strLastLineRemaining = line_start;
				break;
			} else
			{
				std::string alma=strLastLineRemaining;
				*s=0;
				alma+=line_start;
				line_start = s+1;
				strLastLineRemaining = "";
				LastServerResponse.push_back(alma);
				if (alma==".")
				{
					if (NeedDebugLog() && mDebugLog.is_open())
					{
						for(unsigned int i=0;i<LastServerResponse.size();i++)
							mDebugLog<<LastServerResponse[i]<<std::endl;
					}
					std::string strResponse = HandleServerResponse(LastServerResponse);
					if (!strResponse.empty())
					{
						SendMessage(strResponse);
					}
					LastServerResponse.clear();
				}
			}
		}
	}
}
CSecEngine::~CSecEngine()
	{
	// Cancel any outstanding request- this cleans up
	// resources created during a connection
	//Cancel();	
	ConnectionClosed();
	// Clean up engine's permanent resources
	delete (void*)iSndBuffer.Ptr();
	delete (void*)iRcvBuffer.Ptr();
	iTimer.Close();
	iSocketServ.Close();
	}
Exemple #7
0
void CLIENT::SendMessage( std::string aMessage )
{
    if (aMessage.length()==0) return;
    if (aMessage[aMessage.length()-1]!='\n') aMessage+="\n";
    if (NeedDebugLog() && mDebugLog.is_open())
    {
        mDebugLog<<"Sent: "<<aMessage;
    }
    int SentBytes = send( mConnectionSocket, aMessage.c_str(), int(aMessage.size()), 0 );
    if (SentBytes!=aMessage.size())
    {
        ConnectionClosed();
    }
}
Exemple #8
0
// 收到关闭完成消息后调用该函数
void CMFPI::WR_CloseComplete(DWORD dwCommand, HRESULT hResult, LPVOID lpBuffer)
{
    if(LFS_SUCCESS == hResult)
	{
		m_bOpened = false;
        emit ConnectionClosed();
		LOGINFO("%s,事件:ConnectionClosed()",__FUNCTION__);
	}
	else
	{
        emit CloseFailed();
		LOGERROR("%s,关闭设备失败,错误码:hResult = %d",__FUNCTION__,hResult);
        Alarm("07000000");
	}
}
void PanoServerTCP::NewConnection() {
  // Open Socket to client.
  QTcpSocket* conn = server_->nextPendingConnection();
  
  if (!conn) {
    log("ERROR: can't establish connection to host.");
    return;
  }
  
  log (QString("Connected to peer :") + conn->peerAddress().toString());
  connections_.push_back(TcpConnection(conn));
  
  latest_conn_ = conn;
  QTimer::singleShot(500, this, SLOT(SendAllImages()));
  
  connect(conn, SIGNAL(readyRead()), this, SLOT(IncomingData()));
  connect(conn, SIGNAL(disconnected()), this, SLOT(ConnectionClosed()));
}
Exemple #10
0
    size_t
    TCPSocket::doWrite(const uint8_t* bfr, size_t size)
    {
      int flags = 0;

#if defined(MSG_NOSIGNAL)
      flags = MSG_NOSIGNAL;
#endif

      ssize_t rv = ::send(m_handle, (char*)bfr, size, flags);

      if (rv < 0)
      {
        if (errno == EPIPE)
          throw ConnectionClosed();
        throw NetworkError(DTR("error sending data"), getLastErrorMessage());
      }

      return static_cast<size_t>(rv);
    }
Exemple #11
0
int
TCPSocket::write(const char* bfr, int size)
{
    int flags = 0;

#if defined(MSG_NOSIGNAL)
    flags = MSG_NOSIGNAL;
#endif

    int rv = ::send(m_handle, bfr, size, flags);

    if (rv == -1)
    {
        if (errno == EPIPE)
            throw ConnectionClosed();
        throw NetworkError(DTR("error sending data"), getLastErrorMessage());
    }

    return rv;
}
Exemple #12
0
void CSecEngine::DoCancel()
	{
	iConsole->Printf(KCancelledMessage);
	ConnectionClosed();
	}
Exemple #13
0
    void
    RequestHandler::handleRequest(TCPSocket* sock)
    {
      char mtd[16];
      char uri[512];
      char bfr[c_max_request_size] = {0};

      // Search for end of request.
      unsigned idx = 0;
      unsigned didx = 0;
      bool eor = false;
      while (!eor && (idx < (c_max_request_size - 1)))
      {
        int rv = sock->read(bfr + idx, 1);

        if (rv <= 0)
          throw ConnectionClosed();

        if (didx == 0 && bfr[idx] == '\r')
          didx = 1;
        else if (didx == 1 && bfr[idx] == '\n')
          didx = 2;
        else if (didx == 2 && bfr[idx] == '\r')
          didx = 3;
        else if (didx == 3 && bfr[idx] == '\n')
        {
          eor = true;
          didx = 0;
        }
        else
          didx = 0;

        ++idx;
      }

      // Get header.
      int size = idx - 4;
      if (size <= 0)
      {
        DUNE_WRN("HTTP", "request too short");
        return;
      }

      char* hdr = new char[size + 1];
      std::memcpy(hdr, bfr, size);
      hdr[size] = 0;

      Utils::TupleList headers(hdr, ":", "\r\n", true);

      // Parse request line.
      if (std::sscanf(hdr, "%s %s %*s", mtd, uri) == 2)
      {
        std::string uri_dec = URL::decode(uri);
        const char* uri_clean = uri_dec.c_str();

        if (std::strcmp(mtd, "GET") == 0)
        {
          handleGET(sock, headers, uri_clean);
        }
        else if (std::strcmp(mtd, "POST") == 0)
        {
          handlePOST(sock, headers, uri_clean);
        }
        else if (std::strcmp(mtd, "PUT") == 0)
        {
          handlePUT(sock, headers, uri_clean);
        }
      }

      delete[] hdr;
    }
 void CommunicationService::OnConnectionClosed(Communication::ConnectionInterface* connection)
 {
     emit( ConnectionClosed(connection) );
 }