PageRenderTime 60ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/irc/rsl/net/socket/tcpstream.cpp

https://github.com/popovag/multitheftauto
C++ | 114 lines | 74 code | 14 blank | 26 comment | 13 complexity | 8c237160be2a56ac67c11b42b8163850 MD5 | raw file
  1. /*
  2. * Copyright (c) 2007, Alberto Alonso Pinto
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification, are permitted
  6. * provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice, this list of conditions
  9. * and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
  11. * and the following disclaimer in the documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the Alberto Alonso Pinto nor the names of its contributors may be used to endorse or
  13. * promote products derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
  16. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  17. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  19. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  21. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  22. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include <string>
  25. #include <errno.h>
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include "tcpstream.h"
  29. using Rsl::Net::Socket::SocketStreamTCP;
  30. SocketStreamTCP::SocketStreamTCP(int handle)
  31. : SocketStream(handle)
  32. {
  33. }
  34. void SocketStreamTCP::SetHandle(int handle)
  35. {
  36. SocketStream::SetHandle(handle);
  37. }
  38. ssize_t SocketStreamTCP::QueueLength()
  39. {
  40. char tmp;
  41. ssize_t len = -1;
  42. if (Ok())
  43. {
  44. len = recv(m_handle, (void *)&tmp, sizeof(char), MSG_TRUNC | MSG_PEEK);
  45. if (len < 0)
  46. {
  47. m_errno = errno;
  48. }
  49. }
  50. return len;
  51. }
  52. ssize_t SocketStreamTCP::Read(char* buffer, size_t maxLength)
  53. {
  54. ssize_t len = -1;
  55. if (Ok())
  56. {
  57. len = recv(m_handle, buffer, maxLength, 0);
  58. /* TODO: Make non blocking sockets support, so they can return 0 */
  59. if (len == 0)
  60. {
  61. /* EOF */
  62. m_eof = true;
  63. }
  64. else if (len < 1)
  65. {
  66. m_errno = errno;
  67. }
  68. else
  69. {
  70. m_inbytes += len;
  71. }
  72. }
  73. return len;
  74. }
  75. ssize_t SocketStreamTCP::Write(const char* buffer, size_t length)
  76. {
  77. ssize_t len = -1;
  78. if (Ok())
  79. {
  80. len = send(m_handle, buffer, length, 0);
  81. /* TODO: Make non blocking sockets support here too */
  82. if (len == 0)
  83. {
  84. m_eof = true;
  85. }
  86. else if (len == -1)
  87. {
  88. m_errno = errno;
  89. }
  90. else
  91. {
  92. m_outbytes += len;
  93. }
  94. }
  95. return len;
  96. }
  97. ssize_t SocketStreamTCP::WriteString(const std::string& str)
  98. {
  99. return Write(str.c_str(), str.length());
  100. }