Connection.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /***************************************************************************
  2. *
  3. * Project _____ __ ____ _ _
  4. * ( _ ) /__\ (_ _)_| |_ _| |_
  5. * )(_)( /(__)\ )( (_ _)(_ _)
  6. * (_____)(__)(__)(__) |_| |_|
  7. *
  8. *
  9. * Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. ***************************************************************************/
  24. #include "./Connection.hpp"
  25. #if defined(WIN32) || defined(_WIN32)
  26. #include <io.h>
  27. #include <winsock2.h>
  28. #else
  29. #include <unistd.h>
  30. #include <sys/socket.h>
  31. #endif
  32. #include <thread>
  33. #include <chrono>
  34. #include <fcntl.h>
  35. namespace oatpp { namespace network { namespace tcp {
  36. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  37. // Connection
  38. oatpp::data::stream::DefaultInitializedContext Connection::DEFAULT_CONTEXT(data::stream::StreamType::STREAM_INFINITE);
  39. Connection::Connection(v_io_handle handle)
  40. : m_handle(handle)
  41. {
  42. #if defined(WIN32) || defined(_WIN32)
  43. // in Windows, there is no reliable method to get if a socket is blocking or not.
  44. // Eevery socket is created blocking in Windows so we assume this state and pray.
  45. setStreamIOMode(data::stream::BLOCKING);
  46. #else
  47. auto flags = fcntl(m_handle, F_GETFL);
  48. if (flags < 0) {
  49. throw std::runtime_error("[oatpp::network::tcp::Connection::Connection()]: Error. Can't get socket flags.");
  50. }
  51. if((flags & O_NONBLOCK) > 0) {
  52. m_mode = data::stream::IOMode::ASYNCHRONOUS;
  53. } else {
  54. m_mode = data::stream::IOMode::BLOCKING;
  55. }
  56. #endif
  57. }
  58. Connection::~Connection(){
  59. close();
  60. }
  61. v_io_size Connection::write(const void *buff, v_buff_size count, async::Action& action){
  62. #if defined(WIN32) || defined(_WIN32)
  63. auto result = ::send(m_handle, (const char*) buff, (int)count, 0);
  64. if(result == SOCKET_ERROR) {
  65. auto e = WSAGetLastError();
  66. if(e == WSAEWOULDBLOCK){
  67. if(m_mode == data::stream::ASYNCHRONOUS) {
  68. action = oatpp::async::Action::createIOWaitAction(m_handle, oatpp::async::Action::IOEventType::IO_EVENT_WRITE);
  69. }
  70. return IOError::RETRY_WRITE; // For async io. In case socket is non-blocking
  71. } else if(e == WSAEINTR) {
  72. return IOError::RETRY_WRITE;
  73. } else if(e == WSAECONNRESET) {
  74. return IOError::BROKEN_PIPE;
  75. } else {
  76. //OATPP_LOGD("Connection", "write errno=%d", e);
  77. return IOError::BROKEN_PIPE; // Consider all other errors as a broken pipe.
  78. }
  79. }
  80. return result;
  81. #else
  82. errno = 0;
  83. v_int32 flags = 0;
  84. #ifdef MSG_NOSIGNAL
  85. flags |= MSG_NOSIGNAL;
  86. #endif
  87. auto result = ::send(m_handle, buff, (size_t)count, flags);
  88. if(result < 0) {
  89. auto e = errno;
  90. if(e == EAGAIN || e == EWOULDBLOCK){
  91. if(m_mode == data::stream::ASYNCHRONOUS) {
  92. action = oatpp::async::Action::createIOWaitAction(m_handle, oatpp::async::Action::IOEventType::IO_EVENT_WRITE);
  93. }
  94. return IOError::RETRY_WRITE; // For async io. In case socket is non-blocking
  95. } else if(e == EINTR) {
  96. return IOError::RETRY_WRITE;
  97. } else if(e == EPIPE) {
  98. return IOError::BROKEN_PIPE;
  99. } else {
  100. //OATPP_LOGD("Connection", "write errno=%d", e);
  101. return IOError::BROKEN_PIPE; // Consider all other errors as a broken pipe.
  102. }
  103. }
  104. return result;
  105. #endif
  106. }
  107. v_io_size Connection::read(void *buff, v_buff_size count, async::Action& action){
  108. #if defined(WIN32) || defined(_WIN32)
  109. auto result = ::recv(m_handle, (char*)buff, (int)count, 0);
  110. if(result == SOCKET_ERROR) {
  111. auto e = WSAGetLastError();
  112. if(e == WSAEWOULDBLOCK){
  113. if(m_mode == data::stream::ASYNCHRONOUS) {
  114. action = oatpp::async::Action::createIOWaitAction(m_handle, oatpp::async::Action::IOEventType::IO_EVENT_READ);
  115. }
  116. return IOError::RETRY_READ; // For async io. In case socket is non-blocking
  117. } else if(e == WSAEINTR) {
  118. return IOError::RETRY_READ;
  119. } else if(e == WSAECONNRESET) {
  120. return IOError::BROKEN_PIPE;
  121. } else {
  122. //OATPP_LOGD("Connection", "write errno=%d", e);
  123. return IOError::BROKEN_PIPE; // Consider all other errors as a broken pipe.
  124. }
  125. }
  126. return result;
  127. #else
  128. errno = 0;
  129. auto result = ::read(m_handle, buff, (size_t)count);
  130. if(result < 0) {
  131. auto e = errno;
  132. if(e == EAGAIN || e == EWOULDBLOCK){
  133. if(m_mode == data::stream::ASYNCHRONOUS) {
  134. action = oatpp::async::Action::createIOWaitAction(m_handle, oatpp::async::Action::IOEventType::IO_EVENT_READ);
  135. }
  136. return IOError::RETRY_READ; // For async io. In case socket is non-blocking
  137. } else if(e == EINTR) {
  138. return IOError::RETRY_READ;
  139. } else if(e == ECONNRESET) {
  140. return IOError::BROKEN_PIPE;
  141. } else {
  142. //OATPP_LOGD("Connection", "write errno=%d", e);
  143. return IOError::BROKEN_PIPE; // Consider all other errors as a broken pipe.
  144. }
  145. }
  146. return result;
  147. #endif
  148. }
  149. #if defined(WIN32) || defined(_WIN32)
  150. void Connection::setStreamIOMode(oatpp::data::stream::IOMode ioMode) {
  151. u_long flags;
  152. switch(ioMode) {
  153. case data::stream::BLOCKING:
  154. flags = 0;
  155. if(NO_ERROR != ioctlsocket(m_handle, FIONBIO, &flags)) {
  156. throw std::runtime_error("[oatpp::network::tcp::Connection::setStreamIOMode()]: Error. Can't set stream I/O mode to IOMode::BLOCKING.");
  157. }
  158. m_mode = data::stream::BLOCKING;
  159. break;
  160. case data::stream::ASYNCHRONOUS:
  161. flags = 1;
  162. if(NO_ERROR != ioctlsocket(m_handle, FIONBIO, &flags)) {
  163. throw std::runtime_error("[oatpp::network::tcp::Connection::setStreamIOMode()]: Error. Can't set stream I/O mode to IOMode::ASYNCHRONOUS.");
  164. }
  165. m_mode = data::stream::ASYNCHRONOUS;
  166. break;
  167. }
  168. }
  169. #else
  170. void Connection::setStreamIOMode(oatpp::data::stream::IOMode ioMode) {
  171. auto flags = fcntl(m_handle, F_GETFL);
  172. if (flags < 0) {
  173. throw std::runtime_error("[oatpp::network::tcp::Connection::setStreamIOMode()]: Error. Can't get socket flags.");
  174. }
  175. switch(ioMode) {
  176. case oatpp::data::stream::IOMode::BLOCKING:
  177. flags = flags & (~O_NONBLOCK);
  178. if (fcntl(m_handle, F_SETFL, flags) < 0) {
  179. throw std::runtime_error("[oatpp::network::tcp::Connection::setStreamIOMode()]: Error. Can't set stream I/O mode to IOMode::BLOCKING.");
  180. }
  181. m_mode = data::stream::BLOCKING;
  182. break;
  183. case oatpp::data::stream::IOMode::ASYNCHRONOUS:
  184. flags = (flags | O_NONBLOCK);
  185. if (fcntl(m_handle, F_SETFL, flags) < 0) {
  186. throw std::runtime_error("[oatpp::network::tcp::Connection::setStreamIOMode()]: Error. Can't set stream I/O mode to IOMode::ASYNCHRONOUS.");
  187. }
  188. m_mode = data::stream::ASYNCHRONOUS;
  189. break;
  190. }
  191. }
  192. #endif
  193. void Connection::setOutputStreamIOMode(oatpp::data::stream::IOMode ioMode) {
  194. setStreamIOMode(ioMode);
  195. }
  196. oatpp::data::stream::IOMode Connection::getOutputStreamIOMode() {
  197. return m_mode;
  198. }
  199. oatpp::data::stream::Context& Connection::getOutputStreamContext() {
  200. return DEFAULT_CONTEXT;
  201. }
  202. void Connection::setInputStreamIOMode(oatpp::data::stream::IOMode ioMode) {
  203. setStreamIOMode(ioMode);
  204. }
  205. oatpp::data::stream::IOMode Connection::getInputStreamIOMode() {
  206. return m_mode;
  207. }
  208. oatpp::data::stream::Context& Connection::getInputStreamContext() {
  209. return DEFAULT_CONTEXT;
  210. }
  211. void Connection::close(){
  212. #if defined(WIN32) || defined(_WIN32)
  213. ::closesocket(m_handle);
  214. #else
  215. ::close(m_handle);
  216. #endif
  217. }
  218. }}}