socket.cxx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Module: Log4CPLUS
  2. // File: socket-win32.cxx
  3. // Created: 4/2003
  4. // Author: Tad E. Smith
  5. //
  6. //
  7. // Copyright 2003-2013 Tad E. Smith
  8. //
  9. // Licensed under the Apache License, Version 2.0 (the "License");
  10. // you may not use this file except in compliance with the License.
  11. // You may obtain a copy of the License at
  12. //
  13. // http://www.apache.org/licenses/LICENSE-2.0
  14. //
  15. // Unless required by applicable law or agreed to in writing, software
  16. // distributed under the License is distributed on an "AS IS" BASIS,
  17. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. // See the License for the specific language governing permissions and
  19. // limitations under the License.
  20. #include <log4cplus/helpers/loglog.h>
  21. #include <log4cplus/internal/socket.h>
  22. namespace log4cplus { namespace helpers {
  23. extern LOG4CPLUS_EXPORT SOCKET_TYPE const INVALID_SOCKET_VALUE
  24. #if defined(_WIN32)
  25. = static_cast<SOCKET_TYPE>(INVALID_SOCKET);
  26. #else
  27. = static_cast<SOCKET_TYPE>(-1);
  28. #endif
  29. //////////////////////////////////////////////////////////////////////////////
  30. // AbstractSocket ctors and dtor
  31. //////////////////////////////////////////////////////////////////////////////
  32. AbstractSocket::AbstractSocket()
  33. : sock(INVALID_SOCKET_VALUE),
  34. state(not_opened),
  35. err(0)
  36. {
  37. }
  38. AbstractSocket::AbstractSocket(SOCKET_TYPE sock_,
  39. SocketState state_, int err_)
  40. : sock(sock_),
  41. state(state_),
  42. err(err_)
  43. {
  44. }
  45. AbstractSocket::AbstractSocket(const AbstractSocket& rhs)
  46. {
  47. copy(rhs);
  48. }
  49. AbstractSocket::~AbstractSocket()
  50. {
  51. close();
  52. }
  53. //////////////////////////////////////////////////////////////////////////////
  54. // AbstractSocket methods
  55. //////////////////////////////////////////////////////////////////////////////
  56. void
  57. AbstractSocket::close()
  58. {
  59. if(sock != INVALID_SOCKET_VALUE) {
  60. closeSocket(sock);
  61. sock = INVALID_SOCKET_VALUE;
  62. }
  63. }
  64. void
  65. AbstractSocket::shutdown()
  66. {
  67. if(sock != INVALID_SOCKET_VALUE) {
  68. shutdownSocket(sock);
  69. }
  70. }
  71. bool
  72. AbstractSocket::isOpen() const
  73. {
  74. return sock != INVALID_SOCKET_VALUE;
  75. }
  76. AbstractSocket&
  77. AbstractSocket::operator=(const AbstractSocket& rhs)
  78. {
  79. if(&rhs != this) {
  80. close();
  81. copy(rhs);
  82. }
  83. return *this;
  84. }
  85. void
  86. AbstractSocket::copy(const AbstractSocket& r)
  87. {
  88. AbstractSocket& rhs = const_cast<AbstractSocket&>(r);
  89. sock = rhs.sock;
  90. state = rhs.state;
  91. err = rhs.err;
  92. rhs.sock = INVALID_SOCKET_VALUE;
  93. rhs.state = not_opened;
  94. rhs.err = 0;
  95. }
  96. //////////////////////////////////////////////////////////////////////////////
  97. // Socket ctors and dtor
  98. //////////////////////////////////////////////////////////////////////////////
  99. Socket::Socket()
  100. : AbstractSocket()
  101. { }
  102. Socket::Socket(const tstring& address, unsigned short port, bool udp /*= false*/)
  103. : AbstractSocket()
  104. {
  105. sock = connectSocket(address, port, udp, state);
  106. if (sock == INVALID_SOCKET_VALUE)
  107. goto error;
  108. if (! udp && setTCPNoDelay (sock, true) != 0)
  109. goto error;
  110. return;
  111. error:
  112. err = get_last_socket_error ();
  113. }
  114. Socket::Socket(SOCKET_TYPE sock_, SocketState state_, int err_)
  115. : AbstractSocket(sock_, state_, err_)
  116. { }
  117. Socket::~Socket()
  118. { }
  119. //////////////////////////////////////////////////////////////////////////////
  120. // Socket methods
  121. //////////////////////////////////////////////////////////////////////////////
  122. bool
  123. Socket::read(SocketBuffer& buffer)
  124. {
  125. long retval = helpers::read(sock, buffer);
  126. if(retval <= 0) {
  127. close();
  128. }
  129. else {
  130. buffer.setSize(retval);
  131. }
  132. return (retval > 0);
  133. }
  134. bool
  135. Socket::write(const SocketBuffer& buffer)
  136. {
  137. long retval = helpers::write(sock, buffer);
  138. if(retval <= 0) {
  139. close();
  140. }
  141. return (retval > 0);
  142. }
  143. bool
  144. Socket::write(const std::string & buffer)
  145. {
  146. long retval = helpers::write (sock, buffer);
  147. if (retval <= 0)
  148. close();
  149. return retval > 0;
  150. }
  151. } } // namespace log4cplus { namespace helpers {