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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #define errorNumber WSAGetLastError()
  29. #include <os/winerrno.h>
  30. #else
  31. #include <sys/types.h>
  32. #include <unistd.h>
  33. #include <sys/time.h>
  34. #include <sys/socket.h>
  35. #include <netinet/in.h>
  36. #include <netinet/tcp.h>
  37. #define errorNumber errno
  38. #endif
  39. /* Old systems have select() in sys/time.h */
  40. #ifdef HAVE_SYS_SELECT_H
  41. #include <sys/select.h>
  42. #endif
  43. #include <rdr/FdOutStream.h>
  44. #include <rdr/Exception.h>
  45. #include <rfb/util.h>
  46. using namespace rdr;
  47. FdOutStream::FdOutStream(int fd_)
  48. : BufferedOutStream(false), fd(fd_)
  49. {
  50. gettimeofday(&lastWrite, NULL);
  51. }
  52. FdOutStream::~FdOutStream()
  53. {
  54. }
  55. unsigned FdOutStream::getIdleTime()
  56. {
  57. return rfb::msSince(&lastWrite);
  58. }
  59. void FdOutStream::cork(bool enable)
  60. {
  61. BufferedOutStream::cork(enable);
  62. #ifdef TCP_CORK
  63. int one = enable ? 1 : 0;
  64. setsockopt(fd, IPPROTO_TCP, TCP_CORK, (char *)&one, sizeof(one));
  65. #endif
  66. }
  67. bool FdOutStream::flushBuffer()
  68. {
  69. size_t n = writeFd(sentUpTo, ptr - sentUpTo);
  70. if (n == 0)
  71. return false;
  72. sentUpTo += n;
  73. return true;
  74. }
  75. //
  76. // writeFd() writes up to the given length in bytes from the given
  77. // buffer to the file descriptor. It returns the number of bytes written. It
  78. // never attempts to send() unless select() indicates that the fd is writable
  79. // - this means it can be used on an fd which has been set non-blocking. It
  80. // also has to cope with the annoying possibility of both select() and send()
  81. // returning EINTR.
  82. //
  83. size_t FdOutStream::writeFd(const uint8_t* data, size_t length)
  84. {
  85. int n;
  86. do {
  87. fd_set fds;
  88. struct timeval tv;
  89. tv.tv_sec = tv.tv_usec = 0;
  90. FD_ZERO(&fds);
  91. FD_SET(fd, &fds);
  92. n = select(fd+1, 0, &fds, 0, &tv);
  93. } while (n < 0 && errorNumber == EINTR);
  94. if (n < 0)
  95. throw SystemException("select", errorNumber);
  96. if (n == 0)
  97. return 0;
  98. do {
  99. // select only guarantees that you can write SO_SNDLOWAT without
  100. // blocking, which is normally 1. Use MSG_DONTWAIT to avoid
  101. // blocking, when possible.
  102. #ifndef MSG_DONTWAIT
  103. n = ::send(fd, (const char*)data, length, 0);
  104. #else
  105. n = ::send(fd, (const char*)data, length, MSG_DONTWAIT);
  106. #endif
  107. } while (n < 0 && (errorNumber == EINTR));
  108. if (n < 0)
  109. throw SystemException("write", errorNumber);
  110. gettimeofday(&lastWrite, NULL);
  111. return n;
  112. }