instance.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * The names of its contributors may not be used to endorse or
  21. * promote products derived from this software without specific prior
  22. * written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. #include "config.h"
  38. #include "util/instance.h"
  39. #include <cstdio>
  40. #include <netdb.h>
  41. #include <poll.h>
  42. #include <sys/socket.h>
  43. #include <sys/types.h>
  44. #include <netinet/in.h>
  45. namespace gearman_util {
  46. bool Instance::run()
  47. {
  48. while (not _operations.empty())
  49. {
  50. Operation::vector::value_type operation= _operations.back();
  51. switch (state)
  52. {
  53. case NOT_WRITING:
  54. {
  55. free_addrinfo();
  56. struct addrinfo ai;
  57. memset(&ai, 0, sizeof(struct addrinfo));
  58. ai.ai_socktype= SOCK_STREAM;
  59. ai.ai_protocol= IPPROTO_TCP;
  60. int ret= getaddrinfo(_host.c_str(), _port.c_str(), &ai, &_addrinfo);
  61. if (ret)
  62. {
  63. std::cerr << "Failed to connect on " << _host.c_str() << ":" << _port.c_str() << " with " << gai_strerror(ret) << std::endl;
  64. return false;
  65. }
  66. }
  67. _addrinfo_next= _addrinfo;
  68. state= CONNECT;
  69. break;
  70. case NEXT_CONNECT_ADDRINFO:
  71. if (_addrinfo_next->ai_next == NULL)
  72. {
  73. std::cerr << "Error connecting to " << _host.c_str() << "." << std::endl;
  74. return false;
  75. }
  76. _addrinfo_next= _addrinfo_next->ai_next;
  77. case CONNECT:
  78. close_socket();
  79. _sockfd= socket(_addrinfo_next->ai_family,
  80. _addrinfo_next->ai_socktype,
  81. _addrinfo_next->ai_protocol);
  82. if (_sockfd == INVALID_SOCKET)
  83. {
  84. perror("socket");
  85. continue;
  86. }
  87. if (connect(_sockfd, _addrinfo_next->ai_addr, _addrinfo_next->ai_addrlen) < 0)
  88. {
  89. switch(errno)
  90. {
  91. case EAGAIN:
  92. case EINTR:
  93. state= CONNECT;
  94. break;
  95. case EINPROGRESS:
  96. state= CONNECTING;
  97. break;
  98. case ECONNREFUSED:
  99. case ENETUNREACH:
  100. case ETIMEDOUT:
  101. default:
  102. state= NEXT_CONNECT_ADDRINFO;
  103. break;
  104. }
  105. }
  106. else
  107. {
  108. state= CONNECTING;
  109. }
  110. break;
  111. case CONNECTING:
  112. // Add logic for poll() for nonblocking.
  113. state= CONNECTED;
  114. break;
  115. case CONNECTED:
  116. case WRITING:
  117. {
  118. size_t packet_length= operation->size();
  119. const char *packet= operation->ptr();
  120. while(packet_length)
  121. {
  122. ssize_t write_size= send(_sockfd, packet, packet_length, 0);
  123. if (write_size < 0)
  124. {
  125. switch(errno)
  126. {
  127. default:
  128. std::cerr << "Failed during send(" << strerror(errno) << ")" << std::endl;
  129. break;
  130. }
  131. }
  132. packet_length-= static_cast<size_t>(write_size);
  133. packet+= static_cast<size_t>(write_size);
  134. }
  135. }
  136. state= READING;
  137. break;
  138. case READING:
  139. if (operation->has_response())
  140. {
  141. size_t total_read;
  142. ssize_t read_length;
  143. do
  144. {
  145. char buffer[1024];
  146. read_length= recv(_sockfd, buffer, sizeof(buffer), 0);
  147. if (read_length < 0)
  148. {
  149. switch(errno)
  150. {
  151. default:
  152. std::cerr << "Error occured while reading data from " << _host.c_str() << std::endl;
  153. return false;
  154. }
  155. }
  156. operation->push(buffer, static_cast<size_t>(read_length));
  157. total_read+= static_cast<size_t>(read_length);
  158. } while (more_to_read());
  159. } // end has_response
  160. state= FINISHED;
  161. break;
  162. case FINISHED:
  163. operation->print();
  164. if (operation->reconnect())
  165. {
  166. }
  167. _operations.pop_back();
  168. delete operation;
  169. state= CONNECTED;
  170. break;
  171. } // end switch
  172. }
  173. return true;
  174. } // end run()
  175. bool Instance::more_to_read() const
  176. {
  177. struct pollfd fds;
  178. fds.fd= _sockfd;
  179. fds.events = POLLIN;
  180. if (poll(&fds, 1, 5) < 1) // Default timeout is 5
  181. {
  182. return false;
  183. }
  184. return true;
  185. }
  186. void Instance::close_socket()
  187. {
  188. if (_sockfd == INVALID_SOCKET)
  189. return;
  190. /* in case of death shutdown to avoid blocking at close() */
  191. if (shutdown(_sockfd, SHUT_RDWR) == SOCKET_ERROR && get_socket_errno() != ENOTCONN)
  192. {
  193. perror("shutdown");
  194. }
  195. else if (closesocket(_sockfd) == SOCKET_ERROR)
  196. {
  197. perror("close");
  198. }
  199. _sockfd= INVALID_SOCKET;
  200. }
  201. void Instance::free_addrinfo()
  202. {
  203. if (not _addrinfo)
  204. return;
  205. freeaddrinfo(_addrinfo);
  206. _addrinfo= NULL;
  207. _addrinfo_next= NULL;
  208. }
  209. } // namespace gearman_util