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.

FdOutStream.cxx 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2011 Pierre Ossman for Cendio AB
  3. * Copyright 2017 Peter Astrand <astrand@cendio.se> for Cendio AB
  4. *
  5. * This is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This software is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this software; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  18. * USA.
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. #include <config.h>
  22. #endif
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <errno.h>
  26. #ifdef _WIN32
  27. #include <winsock2.h>
  28. #undef errno
  29. #define errno WSAGetLastError()
  30. #include <os/winerrno.h>
  31. #else
  32. #include <sys/types.h>
  33. #include <unistd.h>
  34. #include <sys/time.h>
  35. #include <sys/socket.h>
  36. #endif
  37. /* Old systems have select() in sys/time.h */
  38. #ifdef HAVE_SYS_SELECT_H
  39. #include <sys/select.h>
  40. #endif
  41. #include <rdr/FdOutStream.h>
  42. #include <rdr/Exception.h>
  43. #include <rfb/util.h>
  44. using namespace rdr;
  45. FdOutStream::FdOutStream(int fd_)
  46. : fd(fd_)
  47. {
  48. gettimeofday(&lastWrite, NULL);
  49. }
  50. FdOutStream::~FdOutStream()
  51. {
  52. }
  53. unsigned FdOutStream::getIdleTime()
  54. {
  55. return rfb::msSince(&lastWrite);
  56. }
  57. void FdOutStream::cork(bool enable)
  58. {
  59. BufferedOutStream::cork(enable);
  60. #ifdef TCP_CORK
  61. int one = enable ? 1 : 0;
  62. setsockopt(fd, IPPROTO_TCP, TCP_CORK, (char *)&one, sizeof(one));
  63. #endif
  64. }
  65. bool FdOutStream::flushBuffer()
  66. {
  67. size_t n = writeFd((const void*) sentUpTo, ptr - sentUpTo);
  68. if (n == 0)
  69. return false;
  70. sentUpTo += n;
  71. return true;
  72. }
  73. //
  74. // writeFd() writes up to the given length in bytes from the given
  75. // buffer to the file descriptor. It returns the number of bytes written. It
  76. // never attempts to send() unless select() indicates that the fd is writable
  77. // - this means it can be used on an fd which has been set non-blocking. It
  78. // also has to cope with the annoying possibility of both select() and send()
  79. // returning EINTR.
  80. //
  81. size_t FdOutStream::writeFd(const void* data, size_t length)
  82. {
  83. int n;
  84. do {
  85. fd_set fds;
  86. struct timeval tv;
  87. tv.tv_sec = tv.tv_usec = 0;
  88. FD_ZERO(&fds);
  89. FD_SET(fd, &fds);
  90. n = select(fd+1, 0, &fds, 0, &tv);
  91. } while (n < 0 && errno == EINTR);
  92. if (n < 0)
  93. throw SystemException("select", errno);
  94. if (n == 0)
  95. return 0;
  96. do {
  97. // select only guarantees that you can write SO_SNDLOWAT without
  98. // blocking, which is normally 1. Use MSG_DONTWAIT to avoid
  99. // blocking, when possible.
  100. #ifndef MSG_DONTWAIT
  101. n = ::send(fd, (const char*)data, length, 0);
  102. #else
  103. n = ::send(fd, (const char*)data, length, MSG_DONTWAIT);
  104. #endif
  105. } while (n < 0 && (errno == EINTR));
  106. if (n < 0)
  107. throw SystemException("write", errno);
  108. gettimeofday(&lastWrite, NULL);
  109. return n;
  110. }