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

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