X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fbin%2Fmemcapable.cc;h=122c294e79adc6e4c9b1008e6eeb8c1d9255ea96;hb=92d18858b417309f6bdee6bce464a4f3d6a375fd;hp=017a42b4bef2c1f77c5cf7dde865267f3bc3c299;hpb=77fc9c3ba95eb502d1a66146251acfcfd5606df8;p=awesomized%2Flibmemcached diff --git a/src/bin/memcapable.cc b/src/bin/memcapable.cc index 017a42b4..122c294e 100644 --- a/src/bin/memcapable.cc +++ b/src/bin/memcapable.cc @@ -1,6 +1,6 @@ /* +--------------------------------------------------------------------+ - | libmemcached - C/C++ Client Library for memcached | + | libmemcached-awesome - C/C++ Client Library for memcached | +--------------------------------------------------------------------+ | Redistribution and use in source and binary forms, with or without | | modification, are permitted under the terms of the BSD license. | @@ -9,7 +9,7 @@ | the terms online at: https://opensource.org/licenses/BSD-3-Clause | +--------------------------------------------------------------------+ | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | - | Copyright (c) 2020 Michael Wallner | + | Copyright (c) 2020-2021 Michael Wallner https://awesome.co/ | +--------------------------------------------------------------------+ */ @@ -17,28 +17,26 @@ #include "mem_config.h" -#ifdef HAVE_POLL_H -# include -#else -# include "poll/poll.h" -#endif - #include #include #include #include #include -#include +#include #include -#include -#include -#include +#include +#include +#include #include -#include +#if HAVE_UNISTD_H +# include +#endif -#include "libmemcached-1.0/memcached.h" +#include "p9y/getopt.hpp" +#include "p9y/socket.hpp" +#include "p9y/poll.hpp" -#include "libmemcached/socket.hpp" +#include "libmemcached-1.0/memcached.h" #include "libmemcachedprotocol-0.0/binary.h" #include "libmemcached/byteorder.h" @@ -132,14 +130,14 @@ static memcached_socket_t set_noblock(void) { int flags = fcntl(sock, F_GETFL, 0); if (flags == -1) { perror("Failed to get socket flags"); - memcached_close_socket(sock); + closesocket(sock); return INVALID_SOCKET; } if ((flags & O_NONBLOCK) != O_NONBLOCK) { if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1) { perror("Failed to set socket to nonblocking mode"); - memcached_close_socket(sock); + closesocket(sock); return INVALID_SOCKET; } } @@ -175,16 +173,16 @@ static memcached_socket_t connect_server(const char *hostname, const char *port) return sock; } -static ssize_t timeout_io_op(memcached_socket_t fd, short direction, void *buf, size_t len) { +static ssize_t timeout_io_op(memcached_socket_t fd, short direction, const char *buf, size_t len) { ssize_t ret; if (direction == POLLOUT) { ret = send(fd, buf, len, 0); } else { - ret = recv(fd, buf, len, 0); + ret = recv(fd, const_cast(buf), len, 0); } - - if (ret == SOCKET_ERROR && get_socket_errno() == EWOULDBLOCK) { + int local_errno = get_socket_errno(); + if (ret == SOCKET_ERROR && local_errno == EWOULDBLOCK || (EAGAIN != EWOULDBLOCK && local_errno == EAGAIN)) { struct pollfd fds; memset(&fds, 0, sizeof(struct pollfd)); fds.events = direction; @@ -195,7 +193,7 @@ static ssize_t timeout_io_op(memcached_socket_t fd, short direction, void *buf, if (direction == POLLOUT) { ret = send(fd, buf, len, 0); } else { - ret = recv(fd, buf, len, 0); + ret = recv(fd, const_cast(buf), len, 0); } } else if (err == 0) { errno = ETIMEDOUT; @@ -250,7 +248,7 @@ static enum test_return retry_write(const void *buf, size_t len) { do { size_t num_bytes = len - offset; - ssize_t nw = timeout_io_op(sock, POLLOUT, (void *) (ptr + offset), num_bytes); + ssize_t nw = timeout_io_op(sock, POLLOUT, (ptr + offset), num_bytes); if (nw == -1) { verify(get_socket_errno() == EINTR || get_socket_errno() == EAGAIN); } else { @@ -1870,7 +1868,8 @@ int main(int argc, char **argv) { "\t\t\tEverything else will start the test\n" "\t-T n\tJust run the test named n\n" "\t-a\tOnly test the ascii protocol\n" - "\t-b\tOnly test the binary protocol\n", + "\t-b\tOnly test the binary protocol\n\n" + "WARNING: This will FLUSH your server!\n\n", argv[0]); return EXIT_SUCCESS; } @@ -1881,10 +1880,17 @@ int main(int argc, char **argv) { return EXIT_FAILURE; } - //initialize_sockets(); +#ifdef _WIN32 + WSADATA wsaData; + if (WSAStartup(MAKEWORD(2, 2), &wsaData)) { + fprintf(stderr, "Socket Initialization Error.\n"); + return EXIT_FAILURE; + } +#endif // _WIN32 + sock = connect_server(hostname, port); if (sock == INVALID_SOCKET) { - fprintf(stderr, "Failed to connect to <%s:%s>: %s\n", hostname ?: "(null)", port ?: "(null)", + fprintf(stderr, "Failed to connect to <%s:%s>: %s\n", hostname, port, strerror(get_socket_errno())); return EXIT_FAILURE; } @@ -1942,8 +1948,8 @@ int main(int argc, char **argv) { if (reconnect) { closesocket(sock); if ((sock = connect_server(hostname, port)) == INVALID_SOCKET) { - fprintf(stderr, "Failed to connect to <%s:%s>: %s\n", hostname ?: "(null)", - port ?: "(null)", strerror(get_socket_errno())); + fprintf(stderr, "Failed to connect to <%s:%s>: %s\n", hostname, + port, strerror(get_socket_errno())); fprintf(stderr, "%d of %d tests failed\n", failed, total); return EXIT_FAILURE; } @@ -1957,5 +1963,9 @@ int main(int argc, char **argv) { fprintf(stderr, "%d of %d tests failed\n", failed, total); } +#ifdef _WIN32 + WSACleanup(); +#endif + return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; }