Cleaned up the code. Added client exception for error handling.

This commit is contained in:
lotodore
2007-02-18 11:26:57 +00:00
parent e6c6fa0eee
commit 9ec3e020dd
9 changed files with 173 additions and 71 deletions
+21 -7
View File
@@ -20,6 +20,10 @@
#include <net/clientthread.h>
#include <net/clientstate.h>
#include <net/clientdata.h>
#include <net/clientcallback.h>
#include <net/clientexception.h>
#include <net/socket_msg.h>
#include <cassert>
@@ -30,7 +34,6 @@ ClientThread::ClientThread(ClientCallback &cb)
: m_curState(NULL), m_callback(cb)
{
m_data.reset(new ClientData);
m_curState = &CLIENT_INITIAL_STATE::Instance();
}
ClientThread::~ClientThread()
@@ -42,18 +45,29 @@ ClientThread::Init(const string &serverAddress, unsigned serverPort, bool ipv6,
{
if (IsRunning())
return; // TODO: throw exception
m_data->addrFamily = ipv6 ? AF_INET6 : AF_INET;
m_data->serverAddr = serverAddress;
m_data->serverPort = serverPort;
m_data->password = pwd;
ClientData &data = GetData();
data.addrFamily = ipv6 ? AF_INET6 : AF_INET;
data.serverAddr = serverAddress;
data.serverPort = serverPort;
data.password = pwd;
}
void
ClientThread::Main()
{
while (!ShouldTerminate())
SetState(CLIENT_INITIAL_STATE::Instance());
try {
while (!ShouldTerminate())
{
int msg = GetState().Process(*this);
if (msg != MSG_SOCK_INTERNAL_PENDING)
m_callback.SignalNetClientSuccess(msg);
}
} catch (const ClientException &e)
{
GetState().Process(*this, m_callback);
m_callback.SignalNetClientError(e.GetErrorId(), e.GetOsErrorCode());
}
}