#include "mem_config.h"
-#ifdef HAVE_POLL_H
-# include <poll.h>
-#else
-# include "poll/poll.h"
-#endif
-
#include <cassert>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
-#include <ctype.h>
+#include <cctype>
#include <fcntl.h>
-#include <inttypes.h>
-#include <pthread.h>
-#include <signal.h>
+#include <cinttypes>
+#include <ciso646>
+#include <csignal>
#include <sys/types.h>
-#include <unistd.h>
+#if HAVE_UNISTD_H
+# include <unistd.h>
+#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"
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;
}
}
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<char *>(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;
if (direction == POLLOUT) {
ret = send(fd, buf, len, 0);
} else {
- ret = recv(fd, buf, len, 0);
+ ret = recv(fd, const_cast<char *>(buf), len, 0);
}
} else if (err == 0) {
errno = ETIMEDOUT;
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 {
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;
}
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;
}
fprintf(stderr, "%d of %d tests failed\n", failed, total);
}
+#ifdef _WIN32
+ WSACleanup();
+#endif
+
return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}