Properly initialize Winsock on startup

Also fix typo in _get_last_error which caused Winsock connect to fail.
This commit is contained in:
Fabio Alessandrelli 2018-09-13 15:47:00 +02:00
parent 9b31d2da1c
commit 01c3c1a07b
5 changed files with 24 additions and 1 deletions

View File

@ -150,9 +150,24 @@ NetSocket *NetSocketPosix::_create_func() {
} }
void NetSocketPosix::make_default() { void NetSocketPosix::make_default() {
#if defined(WINDOWS_ENABLED)
if (_create == NULL) {
WSADATA data;
WSAStartup(MAKEWORD(2, 2), &data);
}
#endif
_create = _create_func; _create = _create_func;
} }
void NetSocketPosix::cleanup() {
#if defined(WINDOWS_ENABLED)
if (_create != NULL) {
WSACleanup();
}
_create = NULL;
#endif
}
NetSocketPosix::NetSocketPosix() { NetSocketPosix::NetSocketPosix() {
_sock = SOCK_EMPTY; _sock = SOCK_EMPTY;
_ip_type = IP::TYPE_NONE; _ip_type = IP::TYPE_NONE;
@ -169,10 +184,11 @@ NetSocketPosix::NetError NetSocketPosix::_get_socket_error() {
if (err == WSAEISCONN) if (err == WSAEISCONN)
return ERR_NET_IS_CONNECTED; return ERR_NET_IS_CONNECTED;
if (err == WSAEINPROGRESS || errno == WSAEALREADY) if (err == WSAEINPROGRESS || err == WSAEALREADY)
return ERR_NET_IN_PROGRESS; return ERR_NET_IN_PROGRESS;
if (err == WSAEWOULDBLOCK) if (err == WSAEWOULDBLOCK)
return ERR_NET_WOULD_BLOCK; return ERR_NET_WOULD_BLOCK;
ERR_PRINTS("Socket error: " + itos(err));
return ERR_NET_OTHER; return ERR_NET_OTHER;
#else #else
if (errno == EISCONN) if (errno == EISCONN)
@ -181,6 +197,7 @@ NetSocketPosix::NetError NetSocketPosix::_get_socket_error() {
return ERR_NET_IN_PROGRESS; return ERR_NET_IN_PROGRESS;
if (errno == EAGAIN || errno == EWOULDBLOCK) if (errno == EAGAIN || errno == EWOULDBLOCK)
return ERR_NET_WOULD_BLOCK; return ERR_NET_WOULD_BLOCK;
ERR_PRINTS("Socket error: " + itos(errno));
return ERR_NET_OTHER; return ERR_NET_OTHER;
#endif #endif
} }

View File

@ -67,6 +67,7 @@ protected:
public: public:
static void make_default(); static void make_default();
static void cleanup();
virtual Error open(Type p_sock_type, IP::Type &ip_type); virtual Error open(Type p_sock_type, IP::Type &ip_type);
virtual void close(); virtual void close();

View File

@ -139,6 +139,8 @@ void OS_Unix::initialize_core() {
} }
void OS_Unix::finalize_core() { void OS_Unix::finalize_core() {
NetSocketPosix::cleanup();
} }
void OS_Unix::alert(const String &p_alert, const String &p_title) { void OS_Unix::alert(const String &p_alert, const String &p_title) {

View File

@ -406,6 +406,8 @@ void OSUWP::finalize() {
} }
void OSUWP::finalize_core() { void OSUWP::finalize_core() {
NetSocketPosix::cleanup();
} }
void OSUWP::alert(const String &p_alert, const String &p_title) { void OSUWP::alert(const String &p_alert, const String &p_title) {

View File

@ -1512,6 +1512,7 @@ void OS_Windows::finalize_core() {
timeEndPeriod(1); timeEndPeriod(1);
memdelete(process_map); memdelete(process_map);
NetSocketPosix::cleanup();
} }
void OS_Windows::alert(const String &p_alert, const String &p_title) { void OS_Windows::alert(const String &p_alert, const String &p_title) {