Logo Search packages:      
Sourcecode: pcsc-lite version File versions  Download package

int SHMMessageReceive ( void *  buffer,
size_t  buffer_size,
int  filedes,
int  blockAmount 
)

Called by the Client to get the reponse from the server or vice-versa.

Reads the message from the file filedes.

Parameters:
[out] buffer Message read.
[in] buffer_size Size to read
[in] filedes Socket handle.
[in] blockAmount Timeout in milliseconds.
Return values:
0 Success.
-1 Timeout.
-1 Socket is closed.
-1 A signal was received.

Definition at line 262 of file winscard_msg.c.

Referenced by MSGFunctionDemarshall(), SCardEstablishContextTH(), SCardTransmit(), SHMClientRead(), and SHMProcessEventsContext().

{
      /*
       * default is success 
       */
      int retval = 0;
      /*
       * record the time when we started 
       */
      time_t start = time(0);
      /*
       * how many bytes we must read 
       */
      size_t remaining = buffer_size;

      /*
       * repeate until we get the whole message 
       */
      while (remaining > 0)
      {
            fd_set read_fd;
            struct timeval timeout;
            int selret;

            FD_ZERO(&read_fd);
            FD_SET(filedes, &read_fd);

            timeout.tv_usec = 0;
            if ((timeout.tv_sec = start + blockAmount - time(0)) < 0)
            {
                  /*
                   * we already timed out 
                   */
                  retval = -1;
                  break;
            }

            selret = select(filedes + 1, &read_fd, NULL, NULL, &timeout);

            /*
             * try to read only when socket is readable 
             */
            if (selret > 0)
            {
                  int readed;

                  if (!FD_ISSET(filedes, &read_fd))
                  {
                        /*
                         * very strange situation. it should be an assert really 
                         */
                        retval = -1;
                        break;
                  }
                  readed = read(filedes, buffer, remaining);

                  if (readed > 0)
                  {
                        /*
                         * we got something 
                         */
                        buffer += readed;
                        remaining -= readed;
                  } else if (readed == 0)
                  {
                        /*
                         * peer closed the socket 
                         */
                        retval = -1;
                        break;
                  } else
                  {
                        /*
                         * we ignore the signals and empty socket situations, all
                         * other errors are fatal 
                         */
                        if (errno != EINTR && errno != EAGAIN)
                        {
                              retval = -1;
                              break;
                        }
                  }
            } else if (selret == 0)
            {
                  /*
                   * timeout 
                   */
                  retval = -1;
                  break;
            } else
            {
                  /*
                   * we ignore signals, all other errors are fatal 
                   */
                  if (errno != EINTR)
                  {
                        Log2(PCSC_LOG_ERROR, "select returns with failure: %s",
                              strerror(errno));
                        retval = -1;
                        break;
                  }
            }
      }

      return retval;
}


Generated by  Doxygen 1.6.0   Back to index