NetPlay: Mesen no longer hangs for 10+ seconds when trying to connect to a ip/port that doesn't respond

This commit is contained in:
Souryo 2016-02-10 20:19:06 -05:00
parent 45eeb0426b
commit b4abaf36dc
2 changed files with 21 additions and 10 deletions

View file

@ -620,7 +620,9 @@ namespace Mesen.GUI.Forms
} else { } else {
frmClientConfig frm = new frmClientConfig(); frmClientConfig frm = new frmClientConfig();
if(frm.ShowDialog(sender) == System.Windows.Forms.DialogResult.OK) { if(frm.ShowDialog(sender) == System.Windows.Forms.DialogResult.OK) {
InteropEmu.Connect(ConfigManager.Config.ClientConnectionInfo.Host, ConfigManager.Config.ClientConnectionInfo.Port, ConfigManager.Config.Profile.PlayerName, ConfigManager.Config.Profile.PlayerAvatar, (UInt16)ConfigManager.Config.Profile.PlayerAvatar.Length, ConfigManager.Config.ClientConnectionInfo.Spectator); Task.Run(() => {
InteropEmu.Connect(ConfigManager.Config.ClientConnectionInfo.Host, ConfigManager.Config.ClientConnectionInfo.Port, ConfigManager.Config.Profile.PlayerName, ConfigManager.Config.Profile.PlayerAvatar, (UInt16)ConfigManager.Config.Profile.PlayerAvatar.Length, ConfigManager.Config.ClientConnectionInfo.Spectator);
});
} }
} }
} }

View file

@ -127,19 +127,28 @@ bool Socket::Connect(const char* hostname, uint16_t port)
std::cout << "Failed to resolve hostname." << std::endl; std::cout << "Failed to resolve hostname." << std::endl;
SetConnectionErrorFlag(); SetConnectionErrorFlag();
} else { } else {
// Setup our socket address structure //Set socket in non-blocking mode
u_long iMode = 0; u_long iMode = 1;
ioctlsocket(_socket, FIONBIO, &iMode); ioctlsocket(_socket, FIONBIO, &iMode);
// Attempt to connect to server // Attempt to connect to server
if(connect(_socket, addrInfo->ai_addr, (int)addrInfo->ai_addrlen) == SOCKET_ERROR) { connect(_socket, addrInfo->ai_addr, (int)addrInfo->ai_addrlen);
std::cout << "Failed to establish connection with server." << std::endl;
SetConnectionErrorFlag();
} else {
iMode = 1;
ioctlsocket(_socket, FIONBIO, &iMode);
fd_set writeSockets;
writeSockets.fd_count = 1;
writeSockets.fd_array[0] = _socket;
//Timeout after 3 seconds
TIMEVAL timeout;
timeout.tv_sec = 3;
timeout.tv_usec = 0;
// check if the socket is ready
if(select(0, nullptr, &writeSockets, nullptr, &timeout)) {
result = true; result = true;
} else {
//Could not connect
SetConnectionErrorFlag();
} }
freeaddrinfo(addrInfo); freeaddrinfo(addrInfo);