diff --git a/src/core/thread.cpp b/src/core/thread.cpp index 63c7e4b6..d3631b21 100644 --- a/src/core/thread.cpp +++ b/src/core/thread.cpp @@ -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); diff --git a/src/core/thread.h b/src/core/thread.h index b7e70838..bb18690c 100644 --- a/src/core/thread.h +++ b/src/core/thread.h @@ -18,9 +18,16 @@ ***************************************************************************/ /* Base class for threads (used by network client/server). */ +#ifndef _THREAD_H_ +#define _THREAD_H_ + #include #include +#ifndef NANOSECONDS_PER_SECOND +#define NANOSECONDS_PER_SECOND 1000000000 +#endif + class Thread { public: @@ -71,3 +78,4 @@ private: friend class ThreadStarter; }; +#endif diff --git a/src/net/clientdata.h b/src/net/clientdata.h new file mode 100644 index 00000000..8e4afb6c --- /dev/null +++ b/src/net/clientdata.h @@ -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 +#include + + +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 diff --git a/src/net/clientstate.h b/src/net/clientstate.h new file mode 100644 index 00000000..c0c1f386 --- /dev/null +++ b/src/net/clientstate.h @@ -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 +#include + +#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 diff --git a/src/net/clientthread.h b/src/net/clientthread.h index f3e51331..6a6d8645 100644 --- a/src/net/clientthread.h +++ b/src/net/clientthread.h @@ -18,11 +18,15 @@ ***************************************************************************/ /* Network client thread. */ +#ifndef _CLIENTTHREAD_H_ +#define _CLIENTTHREAD_H_ + #include #include #include 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 m_data; + + ClientState *m_curState; + +friend class ClientStateInit; +friend class ClientStateResolve; +friend class ClientStateConnect; }; +#endif diff --git a/src/net/common/clientstate.cpp b/src/net/common/clientstate.cpp new file mode 100644 index 00000000..0d4ad0a4 --- /dev/null +++ b/src/net/common/clientstate.cpp @@ -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 +#include +#include +#include +#include + +#include +#include + +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); +} diff --git a/src/net/common/clientthread.cpp b/src/net/common/clientthread.cpp index 9c3d8bc9..ebe9ec12 100644 --- a/src/net/common/clientthread.cpp +++ b/src/net/common/clientthread.cpp @@ -18,30 +18,19 @@ ***************************************************************************/ #include -#include +#include +#include + +#include 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; +} diff --git a/src/net/common/socket_helper_cmn.cpp b/src/net/common/socket_helper_cmn.cpp index b582128c..155b9d85 100644 --- a/src/net/common/socket_helper_cmn.cpp +++ b/src/net/common/socket_helper_cmn.cpp @@ -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) diff --git a/src/net/genericsocket.h b/src/net/genericsocket.h index 873842ac..157a894a 100644 --- a/src/net/genericsocket.h +++ b/src/net/genericsocket.h @@ -28,6 +28,8 @@ #include #include #include +#include +#include #endif #endif diff --git a/src/net/socket_helper.h b/src/net/socket_helper.h index 8b871770..16c07240 100644 --- a/src/net/socket_helper.h +++ b/src/net/socket_helper.h @@ -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). */ diff --git a/src/net/socket_msg.h b/src/net/socket_msg.h new file mode 100644 index 00000000..e05ab552 --- /dev/null +++ b/src/net/socket_msg.h @@ -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 +