Warning: Enables debug mode.

More steps towards downloading avatars from external server. This is currently BROKEN.
This commit is contained in:
lotodore
2009-03-21 01:39:00 +00:00
parent b1bad8d144
commit ebce3ee214
9 changed files with 253 additions and 15 deletions
+3
View File
@@ -32,6 +32,7 @@ class ClientContext;
class ClientState;
class SenderThread;
class ReceiverHelper;
class DownloaderThread;
class ClientSenderCallback;
class Game;
class NetPacket;
@@ -173,6 +174,8 @@ private:
boost::shared_ptr<ReceiverHelper> m_receiver;
boost::shared_ptr<DownloaderThread> m_avatarDownloader;
GameData m_gameData;
StartData m_startData;
PlayerDataList m_playerDataList;
+1 -1
View File
@@ -460,7 +460,7 @@ ClientStateReadingServerList::Process(ClientThread &client)
const TiXmlNode *portNode = firstServer->FirstChild("Port");
// Currently, only IPv4 is supported for avatar servers.
const TiXmlNode *avatarNode = firstServer->FirstChild("AvatarServerIPv4Address");
const TiXmlNode *avatarNode = firstServer->FirstChild("AvatarServerAddress");
if (!addrNode || !addrNode->ToElement() || !portNode || !portNode->ToElement())
throw ClientException(__FILE__, __LINE__, ERR_SOCK_INVALID_SERVERLIST_XML, 0);
+30 -7
View File
@@ -23,6 +23,7 @@
#include <net/clientcontext.h>
#include <net/senderthread.h>
#include <net/receiverhelper.h>
#include <net/downloaderthread.h>
#include <net/clientexception.h>
#include <net/socket_msg.h>
#include <core/avatarmanager.h>
@@ -36,6 +37,8 @@
#include <memory>
#include <cassert>
#define TEMP_AVATAR_FILENAME "avatar.tmp"
using namespace std;
@@ -331,6 +334,11 @@ ClientThread::GetAvatarManager()
void
ClientThread::Main()
{
if (!GetContext().GetAvatarServerAddr().empty())
{
m_avatarDownloader.reset(new DownloaderThread);
m_avatarDownloader->Run();
}
SetState(CLIENT_INITIAL_STATE::Instance());
try
@@ -368,6 +376,12 @@ ClientThread::Main()
{
GetCallback().SignalNetClientError(e.GetErrorId(), e.GetOsErrorCode());
}
if (m_avatarDownloader)
{
m_avatarDownloader->SignalTermination();
m_avatarDownloader->Join(DOWNLOADER_THREAD_TERMINATE_TIMEOUT);
m_avatarDownloader.reset();
}
}
void
@@ -520,13 +534,22 @@ ClientThread::RetrieveAvatarIfNeeded(unsigned id, const PlayerInfo &info)
{
m_avatarHasRequestedList.push_back(id); // Never remove from this list. Only request once.
// TODO: download from avatar server if applicable.
boost::shared_ptr<NetPacket> retrieveAvatar(new NetPacketRetrieveAvatar);
NetPacketRetrieveAvatar::Data retrieveAvatarData;
retrieveAvatarData.requestId = id;
retrieveAvatarData.avatar = info.avatar;
static_cast<NetPacketRetrieveAvatar *>(retrieveAvatar.get())->SetData(retrieveAvatarData);
GetContext().GetSessionData()->GetSender().Send(GetContext().GetSessionData(), retrieveAvatar);
// Download from avatar server if applicable.
string avatarServerAddress(GetContext().GetAvatarServerAddr());
if (!avatarServerAddress.empty() && m_avatarDownloader)
{
string filename(TEMP_AVATAR_FILENAME);
m_avatarDownloader->QueueDownload(id, avatarServerAddress + filename, GetContext().GetCacheDir() + filename);
}
else
{
boost::shared_ptr<NetPacket> retrieveAvatar(new NetPacketRetrieveAvatar);
NetPacketRetrieveAvatar::Data retrieveAvatarData;
retrieveAvatarData.requestId = id;
retrieveAvatarData.avatar = info.avatar;
static_cast<NetPacketRetrieveAvatar *>(retrieveAvatar.get())->SetData(retrieveAvatarData);
GetContext().GetSessionData()->GetSender().Send(GetContext().GetSessionData(), retrieveAvatar);
}
}
}
}
+124
View File
@@ -0,0 +1,124 @@
/***************************************************************************
* Copyright (C) 2009 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/downloaderthread.h>
#include <net/downloadhelper.h>
#include <net/netexception.h>
#include <boost/filesystem.hpp>
#include <core/loghelper.h>
#include <algorithm>
#define DOWNLOAD_DELAY_MSEC 10
using namespace std;
using namespace boost::filesystem;
DownloaderThread::DownloaderThread()
: m_downloadInProgress(false)
{
m_downloadHelper.reset(new DownloadHelper());
}
DownloaderThread::~DownloaderThread()
{
}
void
DownloaderThread::QueueDownload(unsigned downloadId, const std::string &url, const std::string &filename)
{
boost::mutex::scoped_lock lock(m_downloadQueueMutex);
m_downloadQueue.push(DownloadData(downloadId, url, filename));
}
bool
DownloaderThread::PollDownloadResult(unsigned &downloadId, std::vector<unsigned char> &filedata)
{
bool result = false;
boost::mutex::scoped_lock lock(m_downloadDoneQueueMutex);
if (!m_downloadDoneQueue.empty())
{
const ResultData &d = m_downloadDoneQueue.front();
downloadId = d.id;
filedata = d.data;
m_downloadDoneQueue.pop();
result = true;
}
return result;
}
void
DownloaderThread::Main()
{
while (!ShouldTerminate())
{
try
{
if (m_downloadInProgress)
{
m_downloadInProgress = !m_downloadHelper->Process();
}
if (!m_downloadInProgress)
{
// Previous download was finished.
if (m_curDownloadData)
{
path filepath(m_curDownloadData->filename);
ifstream instream(filepath.file_string().c_str(), ios_base::in | ios_base::binary);
vector<unsigned char> fileData;
copy(istream_iterator<unsigned char>(instream), istream_iterator<unsigned char>(), back_inserter(fileData));
instream.close();
remove(filepath);
{
boost::mutex::scoped_lock lock(m_downloadDoneQueueMutex);
m_downloadDoneQueue.push(ResultData(m_curDownloadData->id, fileData));
}
m_curDownloadData.reset();
}
// Take a break.
Msleep(DOWNLOAD_DELAY_MSEC);
// Start next download.
{
boost::mutex::scoped_lock lock(m_downloadQueueMutex);
if (!m_downloadQueue.empty())
{
m_curDownloadData.reset(new DownloadData(m_downloadQueue.front()));
m_downloadQueue.pop();
}
}
if (m_curDownloadData && !m_curDownloadData->filename.empty())
{
path filepath(m_curDownloadData->filename);
m_downloadHelper->Init(m_curDownloadData->address, filepath.file_string().c_str());
m_downloadInProgress = true;
}
}
}
catch (const NetException &e)
{
LOG_ERROR(e.what());
}
}
}
+85
View File
@@ -0,0 +1,85 @@
/***************************************************************************
* Copyright (C) 2009 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. *
***************************************************************************/
/* Network file download thread. */
#ifndef _DOWNLOADERTHREAD_H_
#define _DOWNLOADERTHREAD_H_
#include <core/thread.h>
#include <queue>
#include <vector>
#include <boost/shared_ptr.hpp>
#define DOWNLOADER_THREAD_TERMINATE_TIMEOUT THREAD_WAIT_INFINITE
class DownloadHelper;
class DownloaderThread : public Thread
{
public:
DownloaderThread();
virtual ~DownloaderThread();
void QueueDownload(unsigned downloadId, const std::string &url, const std::string &filename);
bool PollDownloadResult(unsigned &downloadId, std::vector<unsigned char> &filedata);
protected:
struct DownloadData
{
DownloadData() : id(0) {}
DownloadData(unsigned i, const std::string &a, const std::string &f)
: id(i), address(a), filename(f) {}
unsigned id;
std::string address;
std::string filename;
};
struct ResultData
{
ResultData() : id(0) {}
ResultData(unsigned i, const std::vector<unsigned char> &d)
: id(i), data(d) {}
unsigned id;
std::vector<unsigned char> data;
};
typedef std::queue<DownloadData> DownloadDataQueue;
typedef std::queue<ResultData> DownloadDoneQueue;
// Main function of the thread.
virtual void Main();
private:
DownloadDataQueue m_downloadQueue;
mutable boost::mutex m_downloadQueueMutex;
DownloadDoneQueue m_downloadDoneQueue;
mutable boost::mutex m_downloadDoneQueueMutex;
boost::shared_ptr<DownloadHelper> m_downloadHelper;
bool m_downloadInProgress;
boost::shared_ptr<DownloadData> m_curDownloadData;
};
#endif
+1 -1
View File
@@ -32,7 +32,7 @@
#include <sstream>
#define NET_CLIENT_TERMINATE_TIMEOUT_MSEC 1000
#define NET_CLIENT_TERMINATE_TIMEOUT_MSEC 2000
#define NET_IRC_TERMINATE_TIMEOUT_MSEC 2000
#define NET_DEFAULT_GAME "default"