Client thread test version. Ready to connect.
This commit is contained in:
@@ -0,0 +1,199 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Lothar May *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#include <net/clientstate.h>
|
||||
#include <net/clientthread.h>
|
||||
#include <net/clientdata.h>
|
||||
#include <net/socket_msg.h>
|
||||
#include <gui/guiinterface.h>
|
||||
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
ClientState::~ClientState()
|
||||
{
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
ClientStateInit &
|
||||
ClientStateInit::Instance()
|
||||
{
|
||||
static ClientStateInit state;
|
||||
return state;
|
||||
}
|
||||
|
||||
ClientStateInit::ClientStateInit()
|
||||
{
|
||||
}
|
||||
|
||||
ClientStateInit::~ClientStateInit()
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
ClientStateInit::Process(ClientThread &client, GuiInterface &gui)
|
||||
{
|
||||
ClientData &data = client.GetData();
|
||||
|
||||
if (data.serverAddr.empty())
|
||||
{
|
||||
gui.showErrorConnectToServerDialog(ERR_SOCK_SERVERADDR_NOT_SET, 0);
|
||||
throw runtime_error("ClientStateInit"); // TODO: own exception
|
||||
}
|
||||
|
||||
if (data.serverPort < 1024)
|
||||
{
|
||||
gui.showErrorConnectToServerDialog(ERR_SOCK_INVALID_PORT, 0);
|
||||
throw runtime_error("ClientStateInit");
|
||||
}
|
||||
|
||||
data.sockfd = socket(data.addrFamily, SOCK_STREAM, 0);
|
||||
if (!IS_VALID_SOCKET(data.sockfd))
|
||||
{
|
||||
gui.showErrorConnectToServerDialog(ERR_SOCK_CREATION_FAILED, SOCKET_ERRNO());
|
||||
throw runtime_error("ClientStateInit");
|
||||
}
|
||||
|
||||
#if 0
|
||||
unsigned long mode = 1;
|
||||
if (IOCTLSOCKET(data.sockfd, FIONBIO, &mode) == SOCKET_ERROR)
|
||||
{
|
||||
gui.showErrorConnectToServerDialog(ERR_SOCK_CREATION_FAILED, SOCKET_ERRNO());
|
||||
throw runtime_error("ClientStateInit");
|
||||
}
|
||||
#endif
|
||||
|
||||
gui.showActionConnectToServerDialog(MSG_SOCK_INIT_DONE);
|
||||
client.SetState(ClientStateResolve::Instance());
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
ClientStateResolve &
|
||||
ClientStateResolve::Instance()
|
||||
{
|
||||
static ClientStateResolve state;
|
||||
return state;
|
||||
}
|
||||
|
||||
ClientStateResolve::ClientStateResolve()
|
||||
{
|
||||
}
|
||||
|
||||
ClientStateResolve::~ClientStateResolve()
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
ClientStateResolve::Process(ClientThread &client, GuiInterface &gui)
|
||||
{
|
||||
ClientData &data = client.GetData();
|
||||
|
||||
data.clientAddr.ss_family = data.addrFamily;
|
||||
|
||||
// Treat the server address as numbers first.
|
||||
if (socket_string_to_addr(data.serverAddr.c_str(), data.addrFamily, (struct sockaddr *)&data.clientAddr, data.GetServerAddrSize()))
|
||||
{
|
||||
// Set the port.
|
||||
if (!socket_set_port(data.serverPort, data.addrFamily, (struct sockaddr *)&data.clientAddr, data.GetServerAddrSize()))
|
||||
{
|
||||
gui.showErrorConnectToServerDialog(ERR_SOCK_SET_PORT_FAILED, 0);
|
||||
throw runtime_error("ClientStateResolve");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// This did not work out - try name resolution.
|
||||
ostringstream tmpStr;
|
||||
tmpStr << data.serverPort;
|
||||
if (!socket_resolve(data.serverAddr.c_str(), tmpStr.str().c_str(), data.addrFamily, SOCK_STREAM, 0, (struct sockaddr *)&data.clientAddr, data.GetServerAddrSize()))
|
||||
{
|
||||
gui.showErrorConnectToServerDialog(ERR_SOCK_RESOLVE_FAILED, 0); // TODO: use errno value
|
||||
throw runtime_error("ClientStateResolve");
|
||||
}
|
||||
}
|
||||
gui.showActionConnectToServerDialog(MSG_SOCK_RESOLVE_DONE);
|
||||
client.SetState(ClientStateConnect::Instance());
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
ClientStateConnect &
|
||||
ClientStateConnect::Instance()
|
||||
{
|
||||
static ClientStateConnect state;
|
||||
return state;
|
||||
}
|
||||
|
||||
ClientStateConnect::ClientStateConnect()
|
||||
{
|
||||
}
|
||||
|
||||
ClientStateConnect::~ClientStateConnect()
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
ClientStateConnect::Process(ClientThread &client, GuiInterface &gui)
|
||||
{
|
||||
ClientData &data = client.GetData();
|
||||
|
||||
if (!IS_VALID_CONNECT(connect(data.sockfd, (struct sockaddr *)&data.clientAddr, data.GetServerAddrSize())))
|
||||
{
|
||||
gui.showErrorConnectToServerDialog(ERR_SOCK_CONNECT_FAILED, SOCKET_ERRNO());
|
||||
throw runtime_error("ClientStateResolve");
|
||||
}
|
||||
gui.showActionConnectToServerDialog(MSG_SOCK_RESOLVE_DONE);
|
||||
client.SetState(ClientStateFinal::Instance());
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
ClientStateFinal &
|
||||
ClientStateFinal::Instance()
|
||||
{
|
||||
static ClientStateFinal state;
|
||||
return state;
|
||||
}
|
||||
|
||||
ClientStateFinal::ClientStateFinal()
|
||||
{
|
||||
}
|
||||
|
||||
ClientStateFinal::~ClientStateFinal()
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
ClientStateFinal::Process(ClientThread &client, GuiInterface &gui)
|
||||
{
|
||||
boost::xtime t;
|
||||
boost::xtime_get(&t, boost::TIME_UTC);
|
||||
t.nsec += 10000;
|
||||
if (t.nsec > NANOSECONDS_PER_SECOND)
|
||||
{
|
||||
t.sec++;
|
||||
t.nsec -= NANOSECONDS_PER_SECOND;
|
||||
}
|
||||
boost::thread::sleep(t);
|
||||
}
|
||||
@@ -18,30 +18,19 @@
|
||||
***************************************************************************/
|
||||
|
||||
#include <net/clientthread.h>
|
||||
#include <net/socket_helper.h>
|
||||
#include <net/clientstate.h>
|
||||
#include <net/clientdata.h>
|
||||
|
||||
#include <cassert>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class ClientData
|
||||
{
|
||||
public:
|
||||
ClientData()
|
||||
: sockfd(INVALID_SOCKET), addrFamily(AF_INET), serverPort(0) {}
|
||||
int GetServerAddrSize() const
|
||||
{
|
||||
return addrFamily == AF_INET6 ? sizeof(sockaddr_in6) : sizeof(sockaddr_in);
|
||||
}
|
||||
|
||||
SOCKET sockfd;
|
||||
int addrFamily;
|
||||
string serverAddr;
|
||||
unsigned serverPort;
|
||||
string password;
|
||||
};
|
||||
|
||||
ClientThread::ClientThread()
|
||||
: m_curState(NULL)
|
||||
{
|
||||
m_data.reset(new ClientData);
|
||||
m_curState = &CLIENT_INITIAL_STATE::Instance();
|
||||
}
|
||||
|
||||
ClientThread::~ClientThread()
|
||||
@@ -62,9 +51,27 @@ ClientThread::Init(const string &serverAddress, unsigned serverPort, bool ipv6,
|
||||
void
|
||||
ClientThread::Main()
|
||||
{
|
||||
while (!this->ShouldTerminate())
|
||||
while (!ShouldTerminate())
|
||||
{
|
||||
// TODO.
|
||||
}
|
||||
}
|
||||
|
||||
const ClientData &
|
||||
ClientThread::GetData() const
|
||||
{
|
||||
assert(m_data.get());
|
||||
return *m_data;
|
||||
}
|
||||
|
||||
ClientData &
|
||||
ClientThread::GetData()
|
||||
{
|
||||
assert(m_data.get());
|
||||
return *m_data;
|
||||
}
|
||||
|
||||
void
|
||||
ClientThread::SetState(ClientState &newState)
|
||||
{
|
||||
m_curState = &newState;
|
||||
}
|
||||
|
||||
@@ -22,6 +22,27 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool
|
||||
socket_set_port(unsigned port, int addrFamily, struct sockaddr *addr, int addrLen)
|
||||
{
|
||||
bool retVal = false;
|
||||
|
||||
if (addr)
|
||||
{
|
||||
if (addrFamily == AF_INET && addrLen >= sizeof(sockaddr_in))
|
||||
{
|
||||
((sockaddr_in *)&addr)->sin_port = htons(port);
|
||||
retVal = true;
|
||||
}
|
||||
else if (addrFamily == AF_INET6 && addrLen >= sizeof(sockaddr_in6))
|
||||
{
|
||||
((sockaddr_in6 *)&addr)->sin6_port = htons(port);
|
||||
retVal = true;
|
||||
}
|
||||
}
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
bool
|
||||
internal_socket_resolve(const char *str, const char *port, int addrFamily, int sockType, int protocol, struct sockaddr *addr, int addrLen)
|
||||
|
||||
Reference in New Issue
Block a user