diff options
Diffstat (limited to 'cpp/src/qpid/sys')
| -rw-r--r-- | cpp/src/qpid/sys/windows/AsynchIO.cpp | 2 | ||||
| -rwxr-xr-x | cpp/src/qpid/sys/windows/Socket.cpp | 61 | ||||
| -rw-r--r-- | cpp/src/qpid/sys/windows/SocketAddress.cpp | 70 |
3 files changed, 104 insertions, 29 deletions
diff --git a/cpp/src/qpid/sys/windows/AsynchIO.cpp b/cpp/src/qpid/sys/windows/AsynchIO.cpp index 475b18600d..971f0bb665 100644 --- a/cpp/src/qpid/sys/windows/AsynchIO.cpp +++ b/cpp/src/qpid/sys/windows/AsynchIO.cpp @@ -216,7 +216,7 @@ AsynchConnector::AsynchConnector(const Socket& sock, connCallback(socket); } catch(std::exception& e) { if (failCallback) - failCallback(-1, std::string(e.what())); + failCallback(socket, -1, std::string(e.what())); socket.close(); delete &socket; } diff --git a/cpp/src/qpid/sys/windows/Socket.cpp b/cpp/src/qpid/sys/windows/Socket.cpp index a89b0dff1b..8e6233bbf8 100755 --- a/cpp/src/qpid/sys/windows/Socket.cpp +++ b/cpp/src/qpid/sys/windows/Socket.cpp @@ -20,19 +20,18 @@ */ #include "qpid/sys/Socket.h" +#include "qpid/sys/SocketAddress.h" #include "qpid/sys/windows/IoHandlePrivate.h" #include "qpid/sys/windows/check.h" #include "qpid/sys/Time.h" #include <cstdlib> #include <string.h> -#include <iostream> -#include <memory.h> #include <winsock2.h> -#include <ws2tcpip.h> #include <boost/format.hpp> +#include <boost/lexical_cast.hpp> // Need to initialize WinSock. Ideally, this would be a singleton or embedded // in some one-time initialization function. I tried boost singleton and could @@ -149,6 +148,27 @@ Socket::Socket(IOHandlePrivate* h) : IOHandle(h) {} +void +Socket::createSocket(const SocketAddress& sa) const +{ + SOCKET& socket = impl->fd; + if (socket != INVALID_SOCKET) Socket::close(); + + SOCKET s = ::socket (getAddrInfo(sa).ai_family, + getAddrInfo(sa).ai_socktype, + 0); + if (s == INVALID_SOCKET) throw QPID_WINDOWS_ERROR(WSAGetLastError()); + socket = s; + + try { + if (nonblocking) setNonblocking(); + } catch (std::exception&) { + closesocket(s); + socket = INVALID_SOCKET; + throw; + } +} + void Socket::setTimeout(const Duration& interval) const { const SOCKET& socket = impl->fd; @@ -170,41 +190,26 @@ void Socket::setNonblocking() const { void Socket::connect(const std::string& host, uint16_t port) const { - std::stringstream portstream; - portstream << port << std::ends; - std::string portstr = portstream.str(); - std::stringstream namestream; - namestream << host << ":" << port; - connectname = namestream.str(); + SocketAddress sa(host, boost::lexical_cast<std::string>(port)); + connect(sa); +} +void +Socket::connect(const SocketAddress& addr) const +{ const SOCKET& socket = impl->fd; - // TODO: Be good to make this work for IPv6 as well as IPv4. Would require - // other changes, such as waiting to create the socket until after we - // have the address family. Maybe unbundle the translation of names here; - // use TcpAddress to resolve things and make this class take a TcpAddress - // and grab its address family to create the socket. - struct addrinfo hints; - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_INET; // We always creating AF_INET-only sockets. - hints.ai_socktype = SOCK_STREAM; // We always do TCP - addrinfo *addrs; - int status = getaddrinfo(host.c_str(), portstr.c_str(), &hints, &addrs); - if (status != 0) - throw Exception(QPID_MSG("Cannot resolve " << host << ": " << - gai_strerror(status))); - addrinfo *addr = addrs; + const addrinfo *addrs = &(getAddrInfo(addr)); int error = 0; WSASetLastError(0); - while (addr != 0) { - if ((::connect(socket, addr->ai_addr, addr->ai_addrlen) == 0) || + while (addrs != 0) { + if ((::connect(socket, addrs->ai_addr, addrs->ai_addrlen) == 0) || (WSAGetLastError() == WSAEWOULDBLOCK)) break; // Error... save this error code and see if there are other address // to try before throwing the exception. error = WSAGetLastError(); - addr = addr->ai_next; + addrs = addrs->ai_next; } - freeaddrinfo(addrs); if (error) throw qpid::Exception(QPID_MSG(strError(error) << ": " << connectname)); } diff --git a/cpp/src/qpid/sys/windows/SocketAddress.cpp b/cpp/src/qpid/sys/windows/SocketAddress.cpp new file mode 100644 index 0000000000..a3e03c9be8 --- /dev/null +++ b/cpp/src/qpid/sys/windows/SocketAddress.cpp @@ -0,0 +1,70 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +#include "qpid/sys/SocketAddress.h" + +#include "qpid/sys/windows/check.h" + +#include <ws2tcpip.h> +#include <string.h> + +namespace qpid { +namespace sys { + +SocketAddress::SocketAddress(const std::string& host0, const std::string& port0) : + host(host0), + port(port0), + addrInfo(0) +{ + ::addrinfo hints; + ::memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; // In order to allow AF_INET6 we'd have to change createTcp() as well + hints.ai_socktype = SOCK_STREAM; + + const char* node = 0; + if (host.empty()) { + hints.ai_flags |= AI_PASSIVE; + } else { + node = host.c_str(); + } + const char* service = port.empty() ? "0" : port.c_str(); + + int n = ::getaddrinfo(node, service, &hints, &addrInfo); + if (n != 0) + throw Exception(QPID_MSG("Cannot resolve " << host << ": " << ::gai_strerror(n))); +} + +SocketAddress::~SocketAddress() +{ + ::freeaddrinfo(addrInfo); +} + +std::string SocketAddress::asString() const +{ + return host + ":" + port; +} + +const ::addrinfo& getAddrInfo(const SocketAddress& sa) +{ + return *sa.addrInfo; +} + +}} |
