/* * 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; version 2 of the License. * * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * Authors: Manuel Mausz (manuel@mausz.at) * Christian Raschko (c.raschko@netcore.at) */ //Header guard #ifndef TSCLIENT_H #define TSCLIENT_H //Libraries #include #include #include "tsheaders.h" #include "tsserver.h" //! Thread response timeout #define THREAD_TIMEOUT 4000 //! Maximum time to leave #define MAX_TTL 5 // better than void * union TSPtr { TSPlayer *pPlayer; TSChannel *pChannel; TSServer *pServer; TSClient *pClient; wxString *pString; }; //! Only for internal use struct TSCmd : wxObject { DECLARE_CLASS(TSCmd) //CTor TSCmd() { id = 0; executed = false; error = false; ttl = MAX_TTL; pktid = 0; oldid = 0; client = NULL; param1.pPlayer = NULL; param1.pPlayer = NULL; } TSCmd(wxUint32 c) { TSCmd(); id = c; } TSCmd(wxUint32 c, TSCmd *old) { TSCmd(); id = c; pktid = old->pktid; oldid = old->id; client = old->client; } TSCmd(const TSCmd &cmd) : wxObject() { id = cmd.id; oldid = cmd.oldid; executed = cmd.executed; error = cmd.error; ttl = cmd.ttl; pktid = cmd.pktid; client = cmd.client; param1 = cmd.param1; param2 = cmd.param2; lasterror = cmd.lasterror; } wxUint32 id; wxUint32 oldid; bool executed; bool error; wxUint32 ttl; wxUint32 pktid; TSClient *client; TSPtr param1; TSPtr param2; wxString lasterror; }; //! TeamSpeak client, main class. /*! TSClient is the main class to connect to a * TeamSpeak server. These stepps are necessary * to create and use a TSClient object. * - Create a TSClient object. * - Change the attributes. * - Create a TSPlayer object. * - Change the attributes. * - Link it to the TSClient object. * - Create a TSServer object. * - Change the attributes. * - Link it to the TSClient object. * - Call Connect(). * * Example: * \code * * TSClient *pTSC = new TSClient; * pTSC->SetPlatform(_T("Windows XP")); * pTSC->SetVersionNumber(_T("2.0.32.60")); * * TSPlayer *pTSP = new TSPlayer; * pTSP->SetLoginName(_T("admin")); * pTSP->SetLoginPassword(_T("testpassword")); * pTSP->SetNickname(_T("penner")); * //now the TSClient takes care of the pointer. * pTSC->SetPlayer(pTSP); * * TSServer *pTSS = new TSServer; * pTSS->SetServerAddress(_T("game3.clan-server.at")); * pTSS->SetPort(8767); * //now the TSClient takes care of the pointer. * pTSC->SetServer(pTSS); * * if(!pTSC->Connect()) * { * wxLogError(_T("%s, terminating.\n"), pTSC->GetLastError().c_str()); * return EXIT_FAILURE; * } * * pTSC->Disconnect(); * delete pTSC; * \endcode */ class TSClient : public wxObject { DECLARE_CLASS(TSClient) public: /*! Default CTor, Initializes the object. * Creates all necessary member objects. */ TSClient(); /*! Default DTor. */ ~TSClient(); /*! Connects to a Teasmpeak server. * \return Returns false if fails, check GetLastError for details. * \sa GetLastError() */ bool Connect(); /*! Disconnects from a Teasmpeak server. * \return Returns false if fails, check GetLastError for details. * \sa GetLastError() */ bool Disconnect(bool reconnect = true); /*! Sets the platform string (e.g. Windows XP, Linux). * \param str Platform string. * \return Returns false if fails, check GetLastError for details. * \sa GetLastError() */ bool SetPlatform(wxString const &str); /*! Sets the TeamSpeak player. * The class takes care of the pointer, no delete call. * \param pPlayer TeamSpeak Player. * \return Returns false if fails, check GetLastError for details. * \sa TSPlayer GetLastError() */ bool SetPlayer(TSPlayer *pPlayer); /*! Sets the TeamSpeak player channel. * The class takes care of the pointer, no delete call. * \param pChannel player channel. * \return Returns false if fails, check GetLastError for details. * \sa TSChannel GetLastError() */ bool SetChannel(TSChannel *pChannel); /*! Sets the TeamSpeak server. * The class takes care of the pointer, no delete call. * \param pServer Server to use for connection. * \return Returns false if fails, check GetLastError for details. * \sa TSServer GetLastError() */ bool SetServer(TSServer *pServer); /*! Sets the TeamSpeak connection. * The class takes care of the pointer, no delete call. * \param pConnection Connection object. * \return Returns false if fails, check GetLastError for details. * \sa TSConnection GetLastError() */ bool SetConnection(TSConnectionThread *pConnection); /*! Sets the TeamSpeak client version number (e.g. "2.0.32.60"). * \param str Client version number. * \return Returns false if fails, check GetLastError for details. * \sa TSConnection GetLastError() */ bool SetVersionNumber(wxString const &str); /*! Sets the session id. * \param id Session id. * \return Returns false if fails, check GetLastError for details. * \sa GetLastError() */ bool SetSessionId(wxUint32 const id); /*! Check if connected. * \return Returns true if connected. * \sa GetLastError() */ bool IsConnected(); /*! Returns the state of the reconnectflag * \return Returns false if fails, check GetLastError for details. * \sa GetLastError() */ bool IsReconnectActive() { return m_pReconnectAct; } /*! Sets Sync onject, for thread sync. * \param sync Sync onject. * \return Returns false if fails, check GetLastError for details. * \sa GetLastError() */ bool SetSync(TSCmd *sync); /*! Gets the TeamSpeak client Platform string. * \return Client Platform string. */ wxString const &GetPlatform() const { return m_Platform; } /*! Gets the LastError message, call this method * to get more information for an error. * \return LastError message. */ wxString const &GetLastError() const { return m_LastError; } /*! Gets the version number * \return Version number. */ wxString const &GetVersionNumber() const { return m_VersionNumber; } /*! Gets the TeamSpeak player. * \return TeamSpeak player. * \sa TSPlayer */ TSPlayer *GetPlayer() const { return m_pPlayer; } /*! Gets all TeamSpeak players on the server. * \return TeamSpeak players. * \sa TSPlayer */ TSpPlayerArray *GetPlayers() const { return m_ppPlayers; } /*! Gets the TeamSpeak channel. * \return TeamSpeak channel. * \sa TSChannel */ TSChannel *GetChannel() const { return m_pChannel; } /*! Gets all TeamSpeak channels on the server. * \return TeamSpeak channels. * \sa TSChannel */ TSpChannelArray *GetChannels() const { return m_ppChannels; } /*! Gets the TeamSpeak server. * \return TeamSpeak server. * \sa TSServer */ TSServer *GetServer() const { return m_pServer; } /*! Gets the TeamSpeak connection. * \return TeamSpeak connection. * \sa TSConnection */ TSConnectionThread *GetConnection() const { return m_pConnection; } /*! Gets the session id. * \return Session id. */ wxUint32 GetSessionId() const { return m_SessionId; } /*! Gets Sync object, for thread sync * \return Sync object. */ TSCmd *GetSync() const { return m_pSync; } /*! Send a command to a Teasmpeak server. * \param cmd Command to send. * \return Returns false if fails, check GetLastError for details. * \sa GetLastError() TSCommand for commands */ bool SendCommand(wxUint32 cmd); /*! Send a command to a Teasmpeak server. * \param cmd Command to send. * \return Returns false if fails, check GetLastError for details. * \sa GetLastError() TSCommand for commands */ bool SendCommand(TSCmd *cmd); /*! Finds a player * \param id Player id. * \return Returns NULL if fails, check GetLastError for details. * \sa GetLastError() */ TSPlayer *FindPlayer(wxUint32 id); /*! Finds a player * \param str Player nick. * \return Returns NULL if fails, check GetLastError for details. * \sa GetLastError() */ TSPlayer *FindPlayer(wxString const &str); /*! Finds a channel * \param id Channel id. * \return Returns NULL if fails, check GetLastError for details. * \sa GetLastError() */ TSChannel *FindChannel(wxUint32 id); /*! Finds a channel * \param id Channel id. * \return Returns NULL if fails, check GetLastError for details. * \sa GetLastError() */ TSChannel *FindChannel(wxString const &str); /*! Finds default channel * \return Returns NULL if fails, check GetLastError for details. * \sa GetLastError() */ TSChannel *FindDefaultChannel(); /*! Finds all channel with this parent id. * \param id Channel id. * \param channels Channel array * \sa TSpChannelArray */ bool FindChannelsByParent(wxUint32 id, TSpChannelArray *channels); /*! Finds all channels per name, regex. * \param str Channel name. * \param channels Channel array * \sa TSpChannelArray */ bool FindChannelsByName(wxString const &str, TSpChannelArray *channels); /*! Finds all players per name, regex. * \param str Player name. * \param players Player array * \sa TSpPlayerArray */ bool FindPlayersByName(wxString const &str, TSpPlayerArray *players); /*! Finds all players in channel. * \param channel Channel. * \param players Player array * \sa TSpPlayerArray */ bool FindPlayersByChannel(TSChannel *channel, TSpPlayerArray *players); /*! Creates a channel. * \param channel Channel object. * \return Returns false if fails, check GetLastError for details. * \sa GetLastError() TSChannel */ bool CreateChannel(TSChannel *channel); /*! Deletes a channel. * \param channel Channel object. * \return Returns false if fails, check GetLastError for details. * \sa GetLastError() TSChannel */ bool DeleteChannel(TSChannel *channel); /*! Modify a channel. * \param channel Channel object. * \return Returns false if fails, check GetLastError for details. * \sa GetLastError() TSChannel */ bool ModifyChannel(TSChannel *channel); /*! Moves a Player. * \param player Player object. * \return Returns false if fails, check GetLastError for details. * \sa GetLastError() TSChannel */ bool MovePlayer(TSPlayer *player); /*! Kick a Player. * \param player Player object. * \param msg Kick message. * \return Returns false if fails, check GetLastError for details. * \sa GetLastError() TSChannel */ bool KickPlayer(TSPlayer *player, wxString msg = _T("")); /*! Dumps object. * \param ostrm Stream to write. */ void Dump(wxOutputStream &ostrm) const; private: //Sets the LastError message. void SetLastError(wxString const &str) { m_LastError = str; } //Members TSPlayer *m_pPlayer; TSpPlayerArray *m_ppPlayers; TSChannel *m_pChannel; TSpChannelArray *m_ppChannels; TSServer *m_pServer; TSConnectionThread *m_pConnection; wxString m_LastError; wxString m_VersionNumber; wxString m_Platform; wxUint32 m_SessionId; wxMutex *m_pMutex; TSCmd *m_pSync; bool m_pReconnectAct; public: bool m_Lock; }; #endif