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

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

Sends a menssage from client to server or vice-versa.

Writes the message in the shared file filedes.

Parameters:
[in] buffer Message to be sent.
[in] buffer_size Size of the message to send
[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 139 of file winscard_msg.c.

Referenced by ContextThread(), MSGFunctionDemarshall(), SCardEstablishContextTH(), and WrapSHMWrite().

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

      /*
       * repeat until all data is written 
       */
      while (remaining > 0)
      {
            fd_set write_fd;
            struct timeval timeout;
            int selret;

            FD_ZERO(&write_fd);
            FD_SET(filedes, &write_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, NULL, &write_fd, NULL, &timeout);

            /*
             * try to write only when the file descriptor is writable 
             */
            if (selret > 0)
            {
                  int written;

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

                  if (written > 0)
                  {
                        /*
                         * we wrote something 
                         */
                        buffer += written;
                        remaining -= written;
                  } else if (written == 0)
                  {
                        /*
                         * peer closed the socket 
                         */
                        retval = -1;
                        break;
                  } else
                  {
                        /*
                         * we ignore the signals and socket full situations, all
                         * other errors are fatal 
                         */
                        if (errno != EINTR && errno != EAGAIN)
                        {
                              retval = -1;
                              break;
                        }
                  }
            } else if (selret == 0)
            {
                  /*
                   * timeout 
                   */
                  retval = -1;
                  break;
            } else
            {
                  /*
                   * ignore signals 
                   */
                  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