Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "abstractsocket.h"
- AbstractSocket::AbstractSocket()
- {
- timeoutvalsec = 2;
- if(INVALID_SOCKET == (s = socket(AF_INET, SOCK_STREAM, 0)))
- {
- throw "with poop"; // Hihi
- }
- }
- VOID AbstractSocket::Disconnect()
- {
- closesocket(s);
- s = INVALID_SOCKET;
- }
- __forceinline bool HasConnectedSuccessful(SOCKET s)
- {
- SOCKADDR out = {0};
- int outlen = sizeof(SOCKADDR);
- const unsigned char nulledIP[14]= {0};
- getpeername(s, &out, &outlen);
- return 0 != memcmp(out.sa_data, nulledIP, sizeof(nulledIP));
- }
- BOOL AbstractSocket::ConnectWithTimeout()
- {
- fd_set write, err;
- struct timeval timeout;
- int status;
- unsigned long opt = 1;
- ioctlsocket(s, FIONBIO, &opt);
- if (SOCKET_ERROR == (status = connect(s, (struct sockaddr*) &addr, sizeof(addr))))
- {
- if(WSAEWOULDBLOCK != WSAGetLastError())
- {
- return FALSE;
- }
- }
- // Already finished
- if(0 == status)
- return TRUE;
- FD_ZERO(&write);
- FD_ZERO(&err);
- FD_SET(s, &write);
- FD_SET(s, &err);
- timeout.tv_sec = timeoutvalsec;
- timeout.tv_usec = 0;
- if (SOCKET_ERROR == select(0, 0, &write, &err, &timeout) || FD_ISSET(s, &err))
- {
- return FALSE;
- }
- opt = 0;
- ioctlsocket(s, FIONBIO, &opt);
- if (0 == FD_ISSET(s, &write))
- {
- return FALSE;
- }
- return HasConnectedSuccessful(s);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement