You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FdInStream.cxx 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <sys/time.h>
  25. #ifdef _WIN32
  26. #include <winsock2.h>
  27. #define close closesocket
  28. #undef errno
  29. #define errno WSAGetLastError()
  30. #include <os/winerrno.h>
  31. #else
  32. #include <sys/types.h>
  33. #include <sys/socket.h>
  34. #include <unistd.h>
  35. #endif
  36. /* Old systems have select() in sys/time.h */
  37. #ifdef HAVE_SYS_SELECT_H
  38. #include <sys/select.h>
  39. #endif
  40. #include <rdr/FdInStream.h>
  41. #include <rdr/Exception.h>
  42. using namespace rdr;
  43. FdInStream::FdInStream(int fd_, bool closeWhenDone_)
  44. : fd(fd_), closeWhenDone(closeWhenDone_)
  45. {
  46. }
  47. FdInStream::~FdInStream()
  48. {
  49. if (closeWhenDone) close(fd);
  50. }
  51. bool FdInStream::fillBuffer(size_t maxSize)
  52. {
  53. size_t n = readFd((U8*)end, maxSize);
  54. if (n == 0)
  55. return false;
  56. end += n;
  57. return true;
  58. }
  59. //
  60. // readFd() reads up to the given length in bytes from the
  61. // file descriptor into a buffer. Zero is
  62. // returned if no bytes can be read. Otherwise it returns the number of bytes read. It
  63. // never attempts to recv() unless select() indicates that the fd is readable -
  64. // this means it can be used on an fd which has been set non-blocking. It also
  65. // has to cope with the annoying possibility of both select() and recv()
  66. // returning EINTR.
  67. //
  68. size_t FdInStream::readFd(void* buf, size_t len)
  69. {
  70. int n;
  71. do {
  72. fd_set fds;
  73. struct timeval tv;
  74. tv.tv_sec = tv.tv_usec = 0;
  75. FD_ZERO(&fds);
  76. FD_SET(fd, &fds);
  77. n = select(fd+1, &fds, 0, 0, &tv);
  78. } while (n < 0 && errno == EINTR);
  79. if (n < 0)
  80. throw SystemException("select",errno);
  81. if (n == 0)
  82. return 0;
  83. do {
  84. n = ::recv(fd, (char*)buf, len, 0);
  85. } while (n < 0 && errno == EINTR);
  86. if (n < 0)
  87. throw SystemException("read",errno);
  88. if (n == 0)
  89. throw EndOfStream();
  90. return n;
  91. }