X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=util%2Finstance.cc;h=1b99739762d40e158b63a41a0c70688ca58503f9;hb=16381b55407865d6d45e73518865e18bf72c3b13;hp=19d01fcf913c4d5d805d9e7e6eded819521ffd9e;hpb=920b0f21e75bb5b145a7de7383f5ae8c4a2c358a;p=m6w6%2Flibmemcached diff --git a/util/instance.cc b/util/instance.cc index 19d01fcf..1b997397 100644 --- a/util/instance.cc +++ b/util/instance.cc @@ -36,18 +36,38 @@ */ -#include +#include "mem_config.h" #include "util/instance.hpp" #include -#include #include #include +#include #include +#include #include #include -#include + +#ifdef HAVE_UNISTD_H +# include +#endif + +#ifndef INVALID_SOCKET +# define INVALID_SOCKET -1 +#endif + +#ifndef SOCKET_ERROR +# define SOCKET_ERROR -1 +#endif + +#ifndef get_socket_errno +# define get_socket_errno() errno +#endif + +#ifndef closesocket +# define closesocket(a) close(a) +#endif namespace datadifferential { @@ -207,26 +227,35 @@ bool Instance::run() case READING: if (operation->has_response()) { - size_t total_read; ssize_t read_length; do { char buffer[BUFSIZ]; - read_length= recv(_sockfd, buffer, sizeof(buffer), 0); + read_length= ::recv(_sockfd, buffer, sizeof(buffer), 0); if (read_length < 0) { switch(errno) { default: - std::cerr << "Error occured while reading data from " << _host.c_str() << std::endl; + _last_error.clear(); + _last_error+= "Error occured while reading data from "; + _last_error+= _host; return false; } } + else if (read_length == 0) + { + _last_error.clear(); + _last_error+= "Socket was shutdown while reading from "; + _last_error+= _host; + + return false; + } operation->push(buffer, static_cast(read_length)); - total_read+= static_cast(read_length); + } while (more_to_read()); } // end has_response @@ -276,7 +305,9 @@ bool Instance::more_to_read() const void Instance::close_socket() { if (_sockfd == INVALID_SOCKET) + { return; + } /* in case of death shutdown to avoid blocking at close() */ if (shutdown(_sockfd, SHUT_RDWR) == SOCKET_ERROR && get_socket_errno() != ENOTCONN) @@ -293,8 +324,10 @@ void Instance::close_socket() void Instance::free_addrinfo() { - if (not _addrinfo) + if (_addrinfo == NULL) + { return; + } freeaddrinfo(_addrinfo); _addrinfo= NULL;