From a98327775b3ec8805308f6655992be62c83685ee Mon Sep 17 00:00:00 2001 From: manojampalam Date: Sun, 28 Feb 2016 14:41:48 -0800 Subject: [PATCH] 2-28-C3 --- .../win32posix/Tests/socketiotests.cpp | 136 ++++-------------- .../win32posix/win32posix/socketio.c | 5 + .../win32posix/win32posix/w32fd.c | 2 +- 3 files changed, 36 insertions(+), 107 deletions(-) diff --git a/contrib/win32/w32-posix-prototype/win32posix/Tests/socketiotests.cpp b/contrib/win32/w32-posix-prototype/win32posix/Tests/socketiotests.cpp index dedb26d..1f55863 100644 --- a/contrib/win32/w32-posix-prototype/win32posix/Tests/socketiotests.cpp +++ b/contrib/win32/w32-posix-prototype/win32posix/Tests/socketiotests.cpp @@ -5,19 +5,8 @@ extern "C" { using namespace Microsoft::VisualStudio::CppUnitTestFramework; -#define PORT "34912" // the port users will be connecting to - -#define BACKLOG 10 // how many pending connections queue will hold - -// get sockaddr, IPv4 or IPv6: -void *get_in_addr(struct sockaddr *sa) -{ - if (sa->sa_family == AF_INET) { - return &(((struct sockaddr_in*)sa)->sin_addr); - } - - return &(((struct sockaddr_in6*)sa)->sin6_addr); -} +#define PORT "34912" +#define BACKLOG 2 int unset_nonblock(int fd) @@ -61,15 +50,30 @@ set_nonblock(int fd) int listen_fd = -1; int accept_fd = -1; int connect_fd = -1; +addrinfo *servinfo; -DWORD WINAPI MyThreadFunction(LPVOID lpParam) +int socket_prepare(char* ip) { - accept_fd = accept(listen_fd, NULL, NULL); + addrinfo hints; + memset(&hints, 0, sizeof(hints)); + hints.ai_socktype = SOCK_STREAM; + if (getaddrinfo(ip, PORT, &hints, &servinfo) == -1) + return -1; + + listen_fd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol); + connect_fd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol); + if ((listen_fd == -1) || (connect_fd == -1)) + return -1; + + if (-1 == bind(listen_fd, servinfo->ai_addr, servinfo->ai_addrlen)) + return -1; + + if (-1 == listen(listen_fd, BACKLOG)) + return -1; + return 0; } - - namespace UnitTests { TEST_CLASS(SocketIOTests) @@ -77,104 +81,29 @@ namespace UnitTests public: - struct addrinfo *servinfo = NULL, *p; - struct addrinfo hints; - - TEST_METHOD_INITIALIZE(TestMethodInitialize) { - w32posix_initialize(); listen_fd = -1; accept_fd = -1; connect_fd = -1; - struct sockaddr_storage their_addr; // connector's address information - socklen_t sin_size; - int yes = 1; - char s[INET6_ADDRSTRLEN]; - int rv; - - memset(&hints, 0, sizeof hints); - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - hints.ai_flags = AI_PASSIVE; // use my IP - - if ((rv = getaddrinfo("::1", PORT, &hints, &servinfo)) != 0) { - fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); - return; - } - - // loop through all the results and bind to the first we can - for (p = servinfo; p != NULL; p = p->ai_next) { - if ((listen_fd = socket(p->ai_family, p->ai_socktype, - p->ai_protocol)) == -1) { - perror("server: socket"); - continue; - } - - if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, - sizeof(int)) == -1) { - perror("setsockopt"); - exit(1); - } - - if (bind(listen_fd, p->ai_addr, p->ai_addrlen) == -1) { - int i = errno; - close(listen_fd); - perror("server: bind"); - continue; - } - - break; - } - - freeaddrinfo(servinfo); // all done with this structure servinfo = NULL; - - if (p == NULL) { - fprintf(stderr, "server: failed to bind\n"); - exit(1); - } - - if (listen(listen_fd, BACKLOG) == -1) { - perror("listen"); - exit(1); - } - } TEST_METHOD_CLEANUP(TestMethodCleanup) { - if (servinfo) - freeaddrinfo(servinfo); - if (listen_fd != -1) - close(listen_fd); - if (connect_fd != -1) - close(connect_fd); - if (accept_fd != -1) - close(accept_fd); + if (servinfo) freeaddrinfo(servinfo); + if (listen_fd != -1) close(listen_fd); + if (connect_fd != -1) close(connect_fd); + if (accept_fd != -1) close(accept_fd); w32posix_done(); - } TEST_METHOD(TestMethod1) { - int rv; - struct sockaddr_storage their_addr; - socklen_t sin_size; int ret; - servinfo = NULL; - - memset(&hints, 0, sizeof hints); - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - - rv = getaddrinfo("::1", PORT, &hints, &servinfo); - Assert::AreEqual(rv, 0, L"getaddreinfo failed", LINE_INFO()); - - p = servinfo; - connect_fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol); - Assert::AreNotEqual(connect_fd, -1, L"connect_fd", LINE_INFO()); + ret = socket_prepare("::1"); + Assert::AreEqual(ret, 0, L"failed to prepare sockets", LINE_INFO()); //set_nonblock(listen_fd); //set_nonblock(connect_fd); @@ -186,10 +115,7 @@ namespace UnitTests //FD_SET(listen_fd, &read_set); //FD_SET(connect_fd, &write_set); - //HANDLE thread = CreateThread(NULL, 0, MyThreadFunction, &connect_fd, 0, NULL); - - - //sin_size = sizeof(their_addr); + //sin_size = sizeof(their_addr); //accept_fd = accept(listen_fd, (struct sockaddr *)&their_addr, &sin_size); //Assert::AreEqual(accept_fd, -1, L"", LINE_INFO()); //Assert::AreEqual(errno, EAGAIN, L"", LINE_INFO()); @@ -197,13 +123,11 @@ namespace UnitTests ret = connect(connect_fd, servinfo->ai_addr, servinfo->ai_addrlen); Assert::AreEqual(ret, 0, L"", LINE_INFO()); - MyThreadFunction(NULL); + accept_fd = accept(listen_fd, NULL, NULL); + Assert::AreNotEqual(accept_fd, -1, L"", LINE_INFO()); - //WaitForSingleObject(thread, INFINITE); - //CloseHandle(thread); - int i = 9; /* accept_fd = accept(listen_fd, (struct sockaddr *)&their_addr, &sin_size); Assert::AreNotEqual(accept_fd, -1, L"", LINE_INFO()); */ diff --git a/contrib/win32/w32-posix-prototype/win32posix/win32posix/socketio.c b/contrib/win32/w32-posix-prototype/win32posix/win32posix/socketio.c index faafa06..5afd10a 100644 --- a/contrib/win32/w32-posix-prototype/win32posix/win32posix/socketio.c +++ b/contrib/win32/w32-posix-prototype/win32posix/win32posix/socketio.c @@ -508,6 +508,11 @@ int socketio_close(struct w32_io* pio) { CloseHandle(pio->read_overlapped.hEvent); if (pio->context) free(pio->context); + //TODO: cleanup other details in pio->context + } + else if (pio->type == CONNECT_FD) { + if (pio->write_overlapped.hEvent) + CloseHandle(pio->write_overlapped.hEvent); } else { if (pio->read_details.buf) diff --git a/contrib/win32/w32-posix-prototype/win32posix/win32posix/w32fd.c b/contrib/win32/w32-posix-prototype/win32posix/win32posix/w32fd.c index 0005fd6..6b3a281 100644 --- a/contrib/win32/w32-posix-prototype/win32posix/win32posix/w32fd.c +++ b/contrib/win32/w32-posix-prototype/win32posix/win32posix/w32fd.c @@ -284,7 +284,7 @@ int w32_close(int fd) { debug("io:%p, type:%d, fd:%d, table_index:%d", pio, pio->type, fd, pio->table_index); fd_table_clear(pio->table_index); - if ((pio->type == LISTEN_FD) || (pio->type == SOCK_FD)) { + if ((pio->type == LISTEN_FD) || (pio->type == CONNECT_FD) || (pio->type == SOCK_FD)) { return socketio_close(pio); } else