ACE handle incoming Application Data with FIN piggy-packed failed to detect/handle correctly

47 views Asked by At

windows, ACE library version **7.0.6 **

  1. My app Client socket connected and connection is accepted from server
  2. Application is receiving handle_input() where our app inherits from ACE_Event_Handler in Event_Handler.cpp.
  3. Calling socket recv()

enter image description here

This code did not work for me. I am looping through to read piggybacked FIN with application Data message. Any buddy have idea why does not work?? Any suggestion of how to detect FIN coming from server with application Data message?

//ACE_SSL_SOCK_Stream       socket;
//class ACE_SSL_Export ACE_SSL_SOCK_Stream : public ACE_SSL_SOCK {..}


int SSLStreamConnection::handle_input(ACE_HANDLE hd)
{
   switch (getState())
    {
    case CONNECTED:
        handleIncomingData();
    break;
    //...
}
void SSLStreamConnection::handleIncomingData()
{
    //test socket for error
    const auto size = socket.recv(&test, 1, MSG_PEEK, 0);
    char tmpDbg[256];
    sprintf_s(tmpDbg, sizeof tmpDbg, "HandleIncomingData socketId=%u size=%lld\n", id,size);
    logInfo(tmpDbg);
    if (size == 0)
    {
        sprintf_s(tmpDbg, sizeof tmpDbg, "CLOSED socketId=%u\n", id);
        logWarning(tmpDbg);
        if( appCallback )
                appCallback->receiveData(id, SOCK_CONNECTION_CLOSED, 0, NULL, peerAddr, context);
            //make sure we remove the read mask so we do not keep getting these events
    } 
    else if (size == 1)
    {
        readIncomingData();
    } 
    else
    {
       appCallback->receiveData(id, SOCK_CONNECTION_CLOSED, 0, NULL, peerAddr, context);
    
    }
}

void SSLStreamConnection::readIncomingData()
{
    std::array<char, 128> strDebug = { 0 };

    //there is data to be read - we will ask for an def buffer size for each read
    char* req_data_buf = nullptr;
    int req_buf_size = 0;
    ssize_t req_size_read = 0;

   while (true) 
   {
      appCallback->rxBufferRequest(DEF_TRANSPORT_BUFFER_SIZE, req_data_buf, req_buf_size);

      const size_t tbuf_size = req_buf_size;
      req_size_read = socket.recv(req_data_buf, tbuf_size - 1, 0);  
      //Ask for bufferSize - 1, buffer gets null terminated later.

        if (req_size_read == 0) // Error occurred. Maybe the socket was closed
        {
            sprintf_s(strDebug.data(), strDebug.size(), "ERROR socketId=%u, errno = 0x%x, closing connection", id, errno);
            logError(strDebug.data());

            appCallback->receiveData(id, SOCK_CONNECTION_CLOSED, 0, nullptr, peerAddr, context);
            break;
        }
        else if (req_size_read == -1) // EOF. Nothing more to read, but the socket can be reused
        {
            appCallback->rxBufferFreeCallback(req_data_buf, req_buf_size);
            break;
        }
        else
        {
        // We got data, pass it to the application
            if (appCallback)
                appCallback->receiveData(id, SOCK_RX_DATA, static_cast<int>(req_size_read), req_data_buf, peerAddr, context);
        }
    } 
}



//Expectation is to read the get Zero on FIN and close socket once the Server tell us to close in the logic above:
/*
if (req_size_read == 0) // Error occurred. Maybe the socket was closed
{
  //..
}
*/
0

There are 0 answers