Client thread test version. Ready to connect.

This commit is contained in:
lotodore
2007-02-17 23:44:45 +00:00
parent 6eb192f846
commit 592863ecf5
11 changed files with 478 additions and 23 deletions
+3 -4
View File
@@ -19,10 +19,6 @@
#include "thread.h"
#ifndef NANOSECONDS_PER_SECOND
#define NANOSECONDS_PER_SECOND 1000000000
#endif
// Helper class for thread creation.
class ThreadStarter
{
@@ -66,6 +62,9 @@ Thread::SignalTermination()
bool
Thread::Join(unsigned msecTimeout)
{
if (!IsRunning())
return true;
// Calculate time after timeout
boost::xtime t;
boost::xtime_get(&t, boost::TIME_UTC);
+8
View File
@@ -18,9 +18,16 @@
***************************************************************************/
/* Base class for threads (used by network client/server). */
#ifndef _THREAD_H_
#define _THREAD_H_
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#ifndef NANOSECONDS_PER_SECOND
#define NANOSECONDS_PER_SECOND 1000000000
#endif
class Thread
{
public:
@@ -71,3 +78,4 @@ private:
friend class ThreadStarter;
};
#endif
+46
View File
@@ -0,0 +1,46 @@
/***************************************************************************
* 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. *
***************************************************************************/
/* State of network client. */
#ifndef _CLIENTDATA_H_
#define _CLIENTDATA_H_
#include <string>
#include <net/socket_helper.h>
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;
std::string serverAddr;
unsigned serverPort;
std::string password;
sockaddr_storage clientAddr;
};
#endif
+113
View File
@@ -0,0 +1,113 @@
/***************************************************************************
* 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. *
***************************************************************************/
/* State of network client. */
#ifndef _CLIENTSTATE_H_
#define _CLIENTSTATE_H_
#include <string>
#include <memory>
#define CLIENT_INITIAL_STATE ClientStateInit
class ClientThread;
class GuiInterface;
class ClientState
{
public:
virtual ~ClientState();
// Main processing function of the current state.
virtual void Process(ClientThread &client, GuiInterface &gui) = 0;
};
// State: Initialization.
class ClientStateInit : public ClientState
{
public:
// Access the state singleton.
static ClientStateInit &Instance();
virtual ~ClientStateInit();
// Some basic initialization (socket creation, basic checks).
virtual void Process(ClientThread &client, GuiInterface &gui);
protected:
// Protected constructor - this is a singleton.
ClientStateInit();
};
// State: Resolving name.
class ClientStateResolve : public ClientState
{
public:
// Access the state singleton.
static ClientStateResolve &Instance();
virtual ~ClientStateResolve();
// "Poll" for the completion of the name resolution.
virtual void Process(ClientThread &client, GuiInterface &gui);
protected:
// Protected constructor - this is a singleton.
ClientStateResolve();
};
// State: Connecting to server.
class ClientStateConnect : public ClientState
{
public:
// Access the state singleton.
static ClientStateConnect &Instance();
virtual ~ClientStateConnect();
// "Poll" for the completion of the TCP/IP connect call.
virtual void Process(ClientThread &client, GuiInterface &gui);
protected:
// Protected constructor - this is a singleton.
ClientStateConnect();
};
// State: Final (TODO).
class ClientStateFinal : public ClientState
{
public:
// Access the state singleton.
static ClientStateFinal &Instance();
virtual ~ClientStateFinal();
// sleep.
virtual void Process(ClientThread &client, GuiInterface &gui);
protected:
// Protected constructor - this is a singleton.
ClientStateFinal();
};
#endif
+16
View File
@@ -18,11 +18,15 @@
***************************************************************************/
/* Network client thread. */
#ifndef _CLIENTTHREAD_H_
#define _CLIENTTHREAD_H_
#include <core/thread.h>
#include <string>
#include <memory>
class ClientData;
class ClientState;
class ClientThread : public Thread
{
@@ -40,8 +44,20 @@ protected:
// Main function of the thread.
virtual void Main();
const ClientData &GetData() const;
ClientData &GetData();
void SetState(ClientState &newState);
private:
std::auto_ptr<ClientData> m_data;
ClientState *m_curState;
friend class ClientStateInit;
friend class ClientStateResolve;
friend class ClientStateConnect;
};
#endif
+199
View File
@@ -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);
}
+26 -19
View File
@@ -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;
}
+21
View File
@@ -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)
+2
View File
@@ -28,6 +28,8 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
#endif
+9
View File
@@ -29,11 +29,15 @@
#ifdef _WIN32
#define CLOSESOCKET closesocket
#define IOCTLSOCKET ioctlsocket
#define SOCKET_ERRNO() WSAGetLastError()
#else
#define SOCKET int
#define SOCKET_ERROR -1
#define INVALID_SOCKET -1
#define CLOSESOCKET close
#define SOCKET_ERRNO() errno
#define IOCTLSOCKET ioctl
#endif
#define IS_VALID_SOCKET(_s) ((_s) != INVALID_SOCKET)
@@ -56,6 +60,11 @@ bool socket_string_to_addr(const char *str, int addrFamily, struct sockaddr *add
*/
bool socket_resolve(const char *str, const char *port, int addrFamily, int sockType, int protocol, struct sockaddr *addr, int addrLen);
/**
* Set the port in the sockaddr structure.
*/
bool socket_set_port(unsigned port, int addrFamily, struct sockaddr *addr, int addrLen);
/**
* Internal function (common for all OSs).
*/
+35
View File
@@ -0,0 +1,35 @@
/***************************************************************************
* 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. *
***************************************************************************/
/* Socket message definitions. */
#ifndef _SOCKET_MSG_H_
#define _SOCKET_MSG_H_
#define ERR_SOCK_SERVERADDR_NOT_SET 1
#define ERR_SOCK_INVALID_PORT 2
#define ERR_SOCK_CREATION_FAILED 10
#define ERR_SOCK_SET_PORT_FAILED 11
#define ERR_SOCK_RESOLVE_FAILED 12
#define ERR_SOCK_CONNECT_FAILED 13
#define MSG_SOCK_INIT_DONE 1
#define MSG_SOCK_RESOLVE_DONE 2
#define MSG_SOCK_CONNECT_DONE 3
#endif