Added connect timeout.

This commit is contained in:
lotodore
2007-05-20 11:11:07 +00:00
parent 4a53678291
commit 5438aa487c
4 changed files with 39 additions and 8 deletions
+5
View File
@@ -2335,6 +2335,11 @@ void mainWindowImpl::networkError(int errorID, int osErrorID) {
tr("Could not connect to the server."), tr("Could not connect to the server."),
QMessageBox::Close); } QMessageBox::Close); }
break; break;
case ERR_SOCK_CONNECT_TIMEOUT:
{ QMessageBox::warning(this, tr("Network Error"),
tr("Connection timed out.\nPlease check the server address.\n\nIf the server is behind a NAT-Router, make sure port forwarding has been set up on server side."),
QMessageBox::Close); }
break;
case ERR_SOCK_SELECT_FAILED: case ERR_SOCK_SELECT_FAILED:
{ QMessageBox::warning(this, tr("Network Error"), { QMessageBox::warning(this, tr("Network Error"),
tr("Internal network error: \"select\" failed."), tr("Internal network error: \"select\" failed."),
+10
View File
@@ -21,8 +21,10 @@
#ifndef _CLIENTSTATE_H_ #ifndef _CLIENTSTATE_H_
#define _CLIENTSTATE_H_ #define _CLIENTSTATE_H_
#include <net/socket_helper.h> // needed for correct order of header files.
#include <string> #include <string>
#include <memory> #include <memory>
#include <core/boost/timer.hpp>
#define CLIENT_INITIAL_STATE ClientStateInit #define CLIENT_INITIAL_STATE ClientStateInit
@@ -110,6 +112,8 @@ public:
virtual ~ClientStateStartConnect(); virtual ~ClientStateStartConnect();
void SetTimer(boost::microsec_timer timer);
// Call connect. // Call connect.
virtual int Process(ClientThread &client); virtual int Process(ClientThread &client);
@@ -128,6 +132,8 @@ public:
virtual ~ClientStateConnecting(); virtual ~ClientStateConnecting();
void SetTimer(const boost::microsec_timer &timer);
// "Poll" for the completion of the TCP/IP connect call. // "Poll" for the completion of the TCP/IP connect call.
virtual int Process(ClientThread &client); virtual int Process(ClientThread &client);
@@ -135,6 +141,10 @@ protected:
// Protected constructor - this is a singleton. // Protected constructor - this is a singleton.
ClientStateConnecting(); ClientStateConnecting();
private:
boost::microsec_timer m_connectTimer;
}; };
// State: Session init. // State: Session init.
+15
View File
@@ -34,6 +34,7 @@
using namespace std; using namespace std;
#define CLIENT_WAIT_TIMEOUT_MSEC 50 #define CLIENT_WAIT_TIMEOUT_MSEC 50
#define CLIENT_CONNECT_TIMEOUT_SEC 10
ClientState::~ClientState() ClientState::~ClientState()
@@ -240,6 +241,9 @@ ClientStateStartConnect::Process(ClientThread &client)
int errCode = SOCKET_ERRNO(); int errCode = SOCKET_ERRNO();
if (errCode == SOCKET_ERR_WOULDBLOCK) if (errCode == SOCKET_ERR_WOULDBLOCK)
{ {
boost::microsec_timer connectTimer;
connectTimer.start();
ClientStateConnecting::Instance().SetTimer(connectTimer);
client.SetState(ClientStateConnecting::Instance()); client.SetState(ClientStateConnecting::Instance());
retVal = MSG_SOCK_INTERNAL_PENDING; retVal = MSG_SOCK_INTERNAL_PENDING;
} }
@@ -267,6 +271,12 @@ ClientStateConnecting::~ClientStateConnecting()
{ {
} }
void
ClientStateConnecting::SetTimer(const boost::microsec_timer &timer)
{
m_connectTimer = timer;
}
int int
ClientStateConnecting::Process(ClientThread &client) ClientStateConnecting::Process(ClientThread &client)
{ {
@@ -295,7 +305,12 @@ ClientStateConnecting::Process(ClientThread &client)
retVal = MSG_SOCK_CONNECT_DONE; retVal = MSG_SOCK_CONNECT_DONE;
} }
else if (selectResult == 0) // timeout else if (selectResult == 0) // timeout
{
if (m_connectTimer.elapsed().seconds() >= CLIENT_CONNECT_TIMEOUT_SEC)
throw ClientException(ERR_SOCK_CONNECT_TIMEOUT, 0);
else
retVal = MSG_SOCK_INTERNAL_PENDING; retVal = MSG_SOCK_INTERNAL_PENDING;
}
else else
throw ClientException(ERR_SOCK_SELECT_FAILED, SOCKET_ERRNO()); throw ClientException(ERR_SOCK_SELECT_FAILED, SOCKET_ERRNO());
+8 -7
View File
@@ -32,13 +32,14 @@
#define ERR_SOCK_LISTEN_FAILED 9 #define ERR_SOCK_LISTEN_FAILED 9
#define ERR_SOCK_ACCEPT_FAILED 10 #define ERR_SOCK_ACCEPT_FAILED 10
#define ERR_SOCK_CONNECT_FAILED 11 #define ERR_SOCK_CONNECT_FAILED 11
#define ERR_SOCK_SELECT_FAILED 12 #define ERR_SOCK_CONNECT_TIMEOUT 12
#define ERR_SOCK_RECV_FAILED 13 #define ERR_SOCK_SELECT_FAILED 13
#define ERR_SOCK_SEND_FAILED 14 #define ERR_SOCK_RECV_FAILED 14
#define ERR_SOCK_CONN_RESET 15 #define ERR_SOCK_SEND_FAILED 15
#define ERR_SOCK_CONN_EXISTS 16 #define ERR_SOCK_CONN_RESET 16
#define ERR_SOCK_INVALID_PACKET 17 #define ERR_SOCK_CONN_EXISTS 17
#define ERR_SOCK_INVALID_STATE 18 #define ERR_SOCK_INVALID_PACKET 18
#define ERR_SOCK_INVALID_STATE 19
// The following errors are game errors. // The following errors are game errors.
#define ERR_NET_VERSION_NOT_SUPPORTED 101 #define ERR_NET_VERSION_NOT_SUPPORTED 101
#define ERR_NET_SERVER_FULL 102 #define ERR_NET_SERVER_FULL 102