Name resolution can be aborted. Fixed bug in socket_set_port.
This commit is contained in:
@@ -20,11 +20,11 @@
|
||||
#include <net/clientstate.h>
|
||||
#include <net/clientthread.h>
|
||||
#include <net/clientdata.h>
|
||||
#include <net/resolverthread.h>
|
||||
#include <net/clientexception.h>
|
||||
#include <net/socket_msg.h>
|
||||
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -65,37 +65,37 @@ ClientStateInit::Process(ClientThread &client)
|
||||
if (!IS_VALID_SOCKET(data.sockfd))
|
||||
throw ClientException(ERR_SOCK_CREATION_FAILED, SOCKET_ERRNO());
|
||||
|
||||
#if 0
|
||||
unsigned long mode = 1;
|
||||
if (IOCTLSOCKET(data.sockfd, FIONBIO, &mode) == SOCKET_ERROR)
|
||||
throw ClientException(ERR_SOCK_CREATION_FAILED, SOCKET_ERRNO());
|
||||
#endif
|
||||
// unsigned long mode = 1;
|
||||
// if (IOCTLSOCKET(data.sockfd, FIONBIO, &mode) == SOCKET_ERROR)
|
||||
// throw ClientException(ERR_SOCK_CREATION_FAILED, SOCKET_ERRNO());
|
||||
|
||||
client.SetState(ClientStateResolve::Instance());
|
||||
client.SetState(ClientStateStartResolve::Instance());
|
||||
|
||||
return MSG_SOCK_INIT_DONE;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
ClientStateResolve &
|
||||
ClientStateResolve::Instance()
|
||||
ClientStateStartResolve &
|
||||
ClientStateStartResolve::Instance()
|
||||
{
|
||||
static ClientStateResolve state;
|
||||
static ClientStateStartResolve state;
|
||||
return state;
|
||||
}
|
||||
|
||||
ClientStateResolve::ClientStateResolve()
|
||||
ClientStateStartResolve::ClientStateStartResolve()
|
||||
{
|
||||
}
|
||||
|
||||
ClientStateResolve::~ClientStateResolve()
|
||||
ClientStateStartResolve::~ClientStateStartResolve()
|
||||
{
|
||||
}
|
||||
|
||||
int
|
||||
ClientStateResolve::Process(ClientThread &client)
|
||||
ClientStateStartResolve::Process(ClientThread &client)
|
||||
{
|
||||
int retVal;
|
||||
|
||||
ClientData &data = client.GetData();
|
||||
|
||||
data.clientAddr.ss_family = data.addrFamily;
|
||||
@@ -110,28 +110,93 @@ ClientStateResolve::Process(ClientThread &client)
|
||||
// Success - but we still need to set the port.
|
||||
if (!socket_set_port(data.serverPort, data.addrFamily, (struct sockaddr *)&data.clientAddr, data.GetServerAddrSize()))
|
||||
throw ClientException(ERR_SOCK_SET_PORT_FAILED, 0);
|
||||
|
||||
// No need to resolve - start connecting.
|
||||
client.SetState(ClientStateConnect::Instance());
|
||||
retVal = MSG_SOCK_RESOLVE_DONE;
|
||||
}
|
||||
else
|
||||
{
|
||||
// This did not work out - try name resolution.
|
||||
ostringstream tmpStr;
|
||||
tmpStr << data.serverPort;
|
||||
// TODO: start in a separate thread, since it is blocking.
|
||||
if (!socket_resolve(
|
||||
data.serverAddr.c_str(),
|
||||
tmpStr.str().c_str(),
|
||||
data.addrFamily,
|
||||
SOCK_STREAM,
|
||||
0,
|
||||
(struct sockaddr *)&data.clientAddr,
|
||||
data.GetServerAddrSize()))
|
||||
{
|
||||
throw ClientException(ERR_SOCK_RESOLVE_FAILED, 0);
|
||||
}
|
||||
}
|
||||
client.SetState(ClientStateConnect::Instance());
|
||||
// Start name resolution in a separate thread, since it is blocking
|
||||
// for up to about 30 seconds.
|
||||
std::auto_ptr<ResolverThread> resolver(new ResolverThread);
|
||||
resolver->Init(data);
|
||||
resolver->Run();
|
||||
|
||||
return MSG_SOCK_RESOLVE_DONE;
|
||||
ClientStateResolving::Instance().SetResolver(resolver.release());
|
||||
client.SetState(ClientStateResolving::Instance());
|
||||
|
||||
retVal = MSG_SOCK_INTERNAL_PENDING;
|
||||
}
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
ClientStateResolving &
|
||||
ClientStateResolving::Instance()
|
||||
{
|
||||
static ClientStateResolving state;
|
||||
return state;
|
||||
}
|
||||
|
||||
ClientStateResolving::ClientStateResolving()
|
||||
: m_resolver(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
ClientStateResolving::~ClientStateResolving()
|
||||
{
|
||||
Cleanup();
|
||||
}
|
||||
|
||||
void
|
||||
ClientStateResolving::SetResolver(ResolverThread *resolver)
|
||||
{
|
||||
Cleanup();
|
||||
|
||||
m_resolver = resolver;
|
||||
}
|
||||
|
||||
int
|
||||
ClientStateResolving::Process(ClientThread &client)
|
||||
{
|
||||
int retVal;
|
||||
|
||||
if (!m_resolver)
|
||||
throw ClientException(ERR_SOCK_RESOLVE_FAILED, 0);
|
||||
|
||||
if (m_resolver->Join(100))
|
||||
{
|
||||
ClientData &data = client.GetData();
|
||||
bool success = m_resolver->GetResult(data);
|
||||
Cleanup(); // Not required, but better keep things clean.
|
||||
|
||||
if (!success)
|
||||
throw ClientException(ERR_SOCK_RESOLVE_FAILED, 0);
|
||||
|
||||
client.SetState(ClientStateConnect::Instance());
|
||||
retVal = MSG_SOCK_RESOLVE_DONE;
|
||||
}
|
||||
else
|
||||
retVal = MSG_SOCK_INTERNAL_PENDING;
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ClientStateResolving::Cleanup()
|
||||
{
|
||||
if (m_resolver)
|
||||
{
|
||||
if (m_resolver->Join(500))
|
||||
delete m_resolver;
|
||||
// If the resolver does not terminate fast enough, leave it
|
||||
// as memory leak.
|
||||
m_resolver = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user