1
0
Fork 0

issue 389.1: ensure GeneralClientConnection socket is valid for MSVC (#5328)

This commit is contained in:
Vasiliy 2018-05-14 14:55:51 +03:00 committed by Andrey Abramov
parent 1c1d8f2528
commit b13a8da1ce
2 changed files with 39 additions and 5 deletions

View File

@ -83,10 +83,40 @@ int EndpointMock::port() const {
return 0;
}
GeneralClientConnectionMock::GeneralClientConnectionMock()
: GeneralClientConnection(&endpoint, 0, 0, 0),
nil(file_open((const file_path_t)nullptr, "rw")) {
#ifndef _WIN32
GeneralClientConnectionMock::GeneralClientConnectionMock()
: GeneralClientConnection(&endpoint, 0, 0, 0),
nil(file_open((const file_path_t)nullptr, "rw")) {
_socket.fileDescriptor = file_no(nil.get()); // must be a readable/writable descriptor
}
#else
GeneralClientConnectionMock::GeneralClientConnectionMock()
: GeneralClientConnection(&endpoint, 0, 0, 0) {
struct sockaddr_in addr;
auto size = (int)sizeof(addr);
auto sock = socket(AF_INET, SOCK_DGRAM, 0); // should not return INVALID_SOCKET
addr.sin_family = AF_INET;
inet_pton(addr.sin_family, "127.0.0.1", &addr.sin_addr);
addr.sin_port = 0;
sock = socket(AF_INET, SOCK_DGRAM, 0); // should not return INVALID_SOCKET
bind(sock, (const struct sockaddr*)&addr, size); // should return 0
memset(&addr, 0, size);
getsockname(sock, (struct sockaddr*)&addr, &size); // should return 0
// make sure something in the socket
sendto(sock, "", 0, 0, (const struct sockaddr*)&addr, size); // should not return SOCKET_ERROR
_socket.fileHandle = sock; // must be a readable/writable descriptor
}
#endif
GeneralClientConnectionMock::~GeneralClientConnectionMock() {
#ifdef _WIN32
if (INVALID_SOCKET != _socket.fileHandle) {
closesocket(_socket.fileHandle);
}
#endif
}
bool GeneralClientConnectionMock::connectSocket() {

View File

@ -77,9 +77,13 @@ class GeneralClientConnectionMock
: public arangodb::httpclient::GeneralClientConnection {
public:
EndpointMock endpoint;
irs::file_utils::handle_t nil;
#ifndef _WIN32
irs::file_utils::handle_t nil;
#endif
GeneralClientConnectionMock();
~GeneralClientConnectionMock();
virtual bool connectSocket() override;
virtual void disconnectSocket() override;
virtual bool readable() override;
@ -119,4 +123,4 @@ class GeneralClientConnectionMapMock: public GeneralClientConnectionMock {
virtual void response(arangodb::basics::StringBuffer& buffer) override;
};
#endif
#endif