cyphesis/common/AtlasStreamClient.h

205 lines
6.4 KiB
C
Raw Permalink Normal View History

// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2009 Alistair Riddoch
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#ifndef COMMON_ATLAS_STREAM_CLIENT_H
#define COMMON_ATLAS_STREAM_CLIENT_H
#include "common/OperationRouter.h"
#include <Atlas/Objects/Decoder.h>
#include <Atlas/Objects/ObjectsFwd.h>
#include <Atlas/Objects/Root.h>
#include <Atlas/Objects/SmartPtr.h>
#include <Atlas/Objects/Operation.h>
2014-02-02 01:01:53 +01:00
#include <boost/asio.hpp>
#include <chrono>
#include <functional>
#include <list>
2014-02-02 01:01:53 +01:00
namespace Atlas {
class Codec;
} // namespace Atlas
class ClientTask;
2014-02-02 01:01:53 +01:00
class StreamClientSocketBase
{
public:
StreamClientSocketBase(boost::asio::io_service& io_service, std::function<void()>& dispatcher);
2014-02-02 01:01:53 +01:00
virtual ~StreamClientSocketBase();
int negotiate(Atlas::Objects::ObjectsDecoder& decoder);
std::iostream& getIos();
Atlas::Codec& getCodec();
Atlas::Objects::ObjectsEncoder& getEncoder();
2017-05-27 20:27:48 +02:00
virtual size_t write() = 0;
int poll(const boost::posix_time::time_duration& duration);
int poll(const boost::posix_time::time_duration& duration, const std::function<bool()>& exitCheckerFn);
2014-02-02 01:01:53 +01:00
protected:
enum
{
read_buffer_size = 16384
};
boost::asio::io_service& m_io_service;
std::function<void()> mDispatcher;
2014-02-02 01:01:53 +01:00
boost::asio::streambuf mBuffer;
boost::asio::streambuf mReadBuffer;
std::iostream m_ios;
Atlas::Codec* m_codec;
Atlas::Objects::ObjectsEncoder * m_encoder;
bool m_is_connected;
2017-05-27 20:27:48 +02:00
virtual size_t read_blocking() = 0;
2014-02-02 01:01:53 +01:00
virtual void do_read() = 0;
};
class TcpStreamClientSocket : public StreamClientSocketBase
{
public:
TcpStreamClientSocket(boost::asio::io_service& io_service, std::function<void()>& dispatcher, boost::asio::ip::tcp::endpoint endpoint);
2017-05-27 20:27:48 +02:00
size_t write() override;
2014-02-02 01:01:53 +01:00
protected:
boost::asio::ip::tcp::socket m_socket;
2017-05-27 20:27:48 +02:00
size_t read_blocking() override;
void do_read() override;
2014-02-02 01:01:53 +01:00
};
class LocalStreamClientSocket : public StreamClientSocketBase
{
public:
LocalStreamClientSocket(boost::asio::io_service& io_service, std::function<void()>& dispatcher, boost::asio::local::stream_protocol::endpoint endpoint);
2017-05-27 20:27:48 +02:00
size_t write() override;
2014-02-02 01:01:53 +01:00
protected:
boost::asio::local::stream_protocol::socket m_socket;
2017-05-27 20:27:48 +02:00
size_t read_blocking() override;
void do_read() override;
2014-02-02 01:01:53 +01:00
};
class AtlasStreamClient : public Atlas::Objects::ObjectsDecoder
{
protected:
2014-02-02 01:01:53 +01:00
boost::asio::io_service m_io_service;
/// \brief Use a "work" instance to make sure the io_service never runs out of work and is stopped.
boost::asio::io_service::work m_io_work;
2014-02-02 01:01:53 +01:00
/// \brief Flag to indicate that a reply has been received from the server
bool reply_flag;
/// \brief Flag to indicate that an error has been received from the server
bool error_flag;
/// \brief Counter used to track serial numbers sent to the server
int serialNo;
2014-02-02 01:01:53 +01:00
StreamClientSocketBase* m_socket;
ClientTask * m_currentTask;
std::string m_username;
2017-05-27 20:27:48 +02:00
size_t m_spacing;
/// \brief Store for reply data from the server
Atlas::Objects::Root m_infoReply;
/// \brief Account identifier returned after successful login
2012-05-31 15:25:54 +01:00
std::string m_accountId;
2009-11-28 16:07:07 +00:00
/// \brief Account type returned after login
2012-05-31 15:25:54 +01:00
std::string m_accountType;
/// \brief Stored error message from the last received Error operation
std::string m_errorMessage;
std::list<Atlas::Objects::Operation::RootOperation> mOps;
2009-11-24 18:48:49 +00:00
// void objectArrived(const Atlas::Objects::Root &);
int waitForLoginResponse();
void dispatch();
void objectArrived(const Atlas::Objects::Root &) override;
virtual void operation(const Atlas::Objects::Operation::RootOperation &);
virtual void infoArrived(const Atlas::Objects::Operation::RootOperation &);
virtual void errorArrived(const Atlas::Objects::Operation::RootOperation &);
virtual void appearanceArrived(const Operation &);
virtual void disappearanceArrived(const Operation &);
virtual void sightArrived(const Operation &);
virtual void soundArrived(const Operation &);
virtual void loginSuccess(const Atlas::Objects::Root & arg);
public:
AtlasStreamClient();
~AtlasStreamClient() override;
int newSerialNo() {
return ++serialNo;
}
const Atlas::Objects::Root & getInfoReply() const {
return m_infoReply;
}
Atlas::Objects::Root & getInfoReply() {
return m_infoReply;
}
2009-12-12 17:09:44 +00:00
const std::string & errorMessage() const {
return m_errorMessage;
}
2017-05-27 20:27:48 +02:00
size_t spacing() const {
2012-01-11 19:03:56 +00:00
return m_spacing;
}
2017-07-01 19:36:11 +02:00
virtual void send(const Atlas::Objects::Operation::RootOperation & op);
int connect(const std::string & host, unsigned short port = 6767);
int connectLocal(const std::string & host);
2012-07-27 09:29:51 +01:00
int cleanDisconnect();
int login(const std::string & username, const std::string & password);
int create(const std::string & type,
const std::string & username,
const std::string & password);
2015-05-05 22:01:11 +02:00
int pollOne(const boost::posix_time::time_duration& duration);
int poll(const boost::posix_time::time_duration& duration);
int poll(int seconds = 0, int microseconds = 0);
2017-07-01 19:36:11 +02:00
void output(const Atlas::Message::Element & item, size_t depth = 0) const;
void output(const Atlas::Objects::Root & item) const;
int runTask(ClientTask * task, const std::string & arg);
int endTask();
/**
* Checks if there's an active task.
* @return True if there's a task set.
*/
bool hasTask() const;
/**
* Poll the server until the current task has completed.
* @return 0 if successful
*/
int pollUntilTaskComplete();
};
#endif // COMMON_ATLAS_STREAM_CLIENT_H