2008-07-03 18:03:32 +03:00
|
|
|
#pragma once
|
|
|
|
#include "../global.h"
|
|
|
|
#include <string>
|
2008-07-09 20:22:28 +03:00
|
|
|
#include <vector>
|
|
|
|
#include <set>
|
2008-07-25 20:28:28 +03:00
|
|
|
|
|
|
|
#include <boost/type_traits/is_fundamental.hpp>
|
|
|
|
#include <boost/type_traits/is_enum.hpp>
|
2008-08-04 18:56:36 +03:00
|
|
|
#include <boost/type_traits/is_pointer.hpp>
|
|
|
|
#include <boost/type_traits/remove_pointer.hpp>
|
2008-07-25 20:28:28 +03:00
|
|
|
|
|
|
|
#include <boost/mpl/eval_if.hpp>
|
|
|
|
#include <boost/mpl/equal_to.hpp>
|
|
|
|
#include <boost/mpl/int.hpp>
|
|
|
|
#include <boost/mpl/identity.hpp>
|
|
|
|
|
2008-07-09 20:22:28 +03:00
|
|
|
const int version = 63;
|
|
|
|
class CConnection;
|
|
|
|
|
2008-07-25 20:28:28 +03:00
|
|
|
namespace mpl = boost::mpl;
|
|
|
|
|
2008-07-03 18:03:32 +03:00
|
|
|
namespace boost
|
|
|
|
{
|
|
|
|
namespace asio
|
|
|
|
{
|
|
|
|
namespace ip
|
|
|
|
{
|
|
|
|
class tcp;
|
|
|
|
}
|
|
|
|
class io_service;
|
|
|
|
|
|
|
|
template <typename Protocol> class stream_socket_service;
|
2008-07-09 20:22:28 +03:00
|
|
|
template <typename Protocol,typename StreamSocketService>
|
2008-07-03 18:03:32 +03:00
|
|
|
class basic_stream_socket;
|
2008-07-09 20:22:28 +03:00
|
|
|
|
|
|
|
template <typename Protocol> class socket_acceptor_service;
|
|
|
|
template <typename Protocol,typename SocketAcceptorService>
|
|
|
|
class basic_socket_acceptor;
|
2008-07-03 18:03:32 +03:00
|
|
|
}
|
2008-07-25 20:28:28 +03:00
|
|
|
class mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum SerializationLvl
|
|
|
|
{
|
|
|
|
Wrong=0,
|
|
|
|
Primitive,
|
2008-08-04 18:56:36 +03:00
|
|
|
Pointer,
|
2008-07-25 20:28:28 +03:00
|
|
|
Serializable
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct SerializationLevel
|
|
|
|
{
|
|
|
|
typedef mpl::integral_c_tag tag;
|
|
|
|
typedef
|
|
|
|
typename mpl::eval_if<
|
|
|
|
boost::is_fundamental<T>,
|
|
|
|
mpl::int_<Primitive>,
|
|
|
|
//else
|
|
|
|
typename mpl::eval_if<
|
|
|
|
boost::is_class<T>,
|
|
|
|
mpl::int_<Serializable>,
|
|
|
|
//else
|
|
|
|
typename mpl::eval_if<
|
|
|
|
boost::is_array<T>,
|
2008-07-26 16:57:32 +03:00
|
|
|
mpl::int_<Primitive>,
|
2008-07-25 20:28:28 +03:00
|
|
|
//else
|
2008-08-04 18:56:36 +03:00
|
|
|
typename mpl::eval_if<
|
|
|
|
boost::is_pointer<T>,
|
|
|
|
mpl::int_<Pointer>,
|
|
|
|
//else
|
2008-07-25 20:28:28 +03:00
|
|
|
typename mpl::eval_if<
|
|
|
|
boost::is_enum<T>,
|
|
|
|
mpl::int_<Primitive>,
|
|
|
|
//else
|
|
|
|
mpl::int_<Wrong>
|
|
|
|
>
|
|
|
|
>
|
|
|
|
>
|
2008-08-04 18:56:36 +03:00
|
|
|
>
|
2008-07-25 20:28:28 +03:00
|
|
|
>::type type;
|
|
|
|
static const int value = SerializationLevel::type::value;
|
2008-07-03 18:03:32 +03:00
|
|
|
};
|
|
|
|
|
2008-07-25 20:28:28 +03:00
|
|
|
template <typename Serializer> class DLL_EXPORT COSer
|
2008-07-09 20:22:28 +03:00
|
|
|
{
|
|
|
|
public:
|
2008-08-04 18:56:36 +03:00
|
|
|
bool saving;
|
|
|
|
COSer(){saving=true;};
|
2008-07-25 20:28:28 +03:00
|
|
|
Serializer * This()
|
2008-07-09 20:22:28 +03:00
|
|
|
{
|
2008-07-25 20:28:28 +03:00
|
|
|
return static_cast<Serializer*>(this);
|
2008-07-09 20:22:28 +03:00
|
|
|
}
|
2008-07-25 20:28:28 +03:00
|
|
|
|
|
|
|
template<class T>
|
|
|
|
Serializer & operator<<(const T &t)
|
2008-07-09 20:22:28 +03:00
|
|
|
{
|
2008-07-25 20:28:28 +03:00
|
|
|
this->This()->save(t);
|
|
|
|
return * this->This();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2008-07-26 16:57:32 +03:00
|
|
|
COSer & operator&(T & t){
|
2008-07-25 20:28:28 +03:00
|
|
|
return * this->This() << t;
|
2008-07-09 20:22:28 +03:00
|
|
|
}
|
|
|
|
};
|
2008-07-25 20:28:28 +03:00
|
|
|
template <typename Serializer> class DLL_EXPORT CISer
|
2008-07-09 20:22:28 +03:00
|
|
|
{
|
|
|
|
public:
|
2008-08-04 18:56:36 +03:00
|
|
|
bool saving;
|
|
|
|
CISer(){saving = false;};
|
2008-07-25 20:28:28 +03:00
|
|
|
Serializer * This()
|
2008-07-09 20:22:28 +03:00
|
|
|
{
|
2008-07-25 20:28:28 +03:00
|
|
|
return static_cast<Serializer*>(this);
|
2008-07-09 20:22:28 +03:00
|
|
|
}
|
2008-07-25 20:28:28 +03:00
|
|
|
|
|
|
|
template<class T>
|
|
|
|
Serializer & operator>>(T &t)
|
2008-07-09 20:22:28 +03:00
|
|
|
{
|
2008-07-25 20:28:28 +03:00
|
|
|
this->This()->load(t);
|
|
|
|
return * this->This();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2008-07-26 16:57:32 +03:00
|
|
|
CISer & operator&(T & t){
|
2008-07-25 20:28:28 +03:00
|
|
|
return * this->This() >> t;
|
2008-07-09 20:22:28 +03:00
|
|
|
}
|
|
|
|
};
|
2008-07-25 20:28:28 +03:00
|
|
|
|
|
|
|
template<typename Ser,typename T>
|
|
|
|
struct SavePrimitive
|
|
|
|
{
|
|
|
|
static void invoke(Ser &s, const T &data)
|
|
|
|
{
|
|
|
|
s.savePrimitive(data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template<typename Ser,typename T>
|
|
|
|
struct SaveSerializable
|
|
|
|
{
|
|
|
|
static void invoke(Ser &s, const T &data)
|
|
|
|
{
|
|
|
|
s.saveSerializable(data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template<typename Ser,typename T>
|
|
|
|
struct LoadPrimitive
|
|
|
|
{
|
|
|
|
static void invoke(Ser &s, T &data)
|
|
|
|
{
|
|
|
|
s.loadPrimitive(data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template<typename Ser,typename T>
|
2008-08-04 18:56:36 +03:00
|
|
|
struct SavePointer
|
|
|
|
{
|
|
|
|
static void invoke(Ser &s, const T &data)
|
|
|
|
{
|
|
|
|
s.savePointer(data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template<typename Ser,typename T>
|
|
|
|
struct LoadPointer
|
|
|
|
{
|
|
|
|
static void invoke(Ser &s, T &data)
|
|
|
|
{
|
|
|
|
s.loadPointer(data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template<typename Ser,typename T>
|
2008-07-25 20:28:28 +03:00
|
|
|
struct LoadSerializable
|
|
|
|
{
|
|
|
|
static void invoke(Ser &s, T &data)
|
|
|
|
{
|
|
|
|
s.loadSerializable(data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Ser,typename T>
|
|
|
|
struct SaveWrong
|
|
|
|
{
|
|
|
|
static void invoke(Ser &s, const T &data)
|
|
|
|
{
|
|
|
|
throw std::exception("Wrong save serialization call!");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template<typename Ser,typename T>
|
|
|
|
struct LoadWrong
|
|
|
|
{
|
|
|
|
static void invoke(Ser &s, const T &data)
|
|
|
|
{
|
|
|
|
throw std::exception("Wrong load serialization call!");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-07-03 18:03:32 +03:00
|
|
|
class DLL_EXPORT CConnection
|
2008-07-25 20:28:28 +03:00
|
|
|
:public CISer<CConnection>, public COSer<CConnection>
|
2008-07-03 18:03:32 +03:00
|
|
|
{
|
2008-07-25 20:28:28 +03:00
|
|
|
|
|
|
|
|
2008-07-03 18:03:32 +03:00
|
|
|
std::ostream &out;
|
|
|
|
CConnection(void);
|
2008-07-09 20:22:28 +03:00
|
|
|
void init();
|
2008-07-03 18:03:32 +03:00
|
|
|
public:
|
2008-07-27 20:07:37 +03:00
|
|
|
boost::mutex *rmx, *wmx; // read/write mutexes
|
2008-07-25 20:28:28 +03:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void savePrimitive(const T &data)
|
|
|
|
{
|
|
|
|
write(&data,sizeof(data));
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
void loadPrimitive(T &data)
|
|
|
|
{
|
|
|
|
read(&data,sizeof(data));
|
|
|
|
}
|
|
|
|
|
2008-08-04 12:05:52 +03:00
|
|
|
|
|
|
|
|
2008-07-25 20:28:28 +03:00
|
|
|
template <typename T>
|
|
|
|
void saveSerializable(const T &data)
|
|
|
|
{
|
2008-08-04 12:05:52 +03:00
|
|
|
const_cast<T&>(data).serialize(*static_cast<COSer<CConnection>*>(this),version);
|
2008-07-25 20:28:28 +03:00
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
void loadSerializable(T &data)
|
|
|
|
{
|
2008-08-04 12:05:52 +03:00
|
|
|
data.serialize(*static_cast<CISer<CConnection>*>(this),version);
|
2008-08-04 18:56:36 +03:00
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
void savePointer(const T &data)
|
|
|
|
{
|
|
|
|
*this << *data;
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
void loadPointer(T &data)
|
|
|
|
{
|
|
|
|
std::cout<<"Allocating memory for pointer!"<<std::endl;
|
2008-08-04 23:27:47 +03:00
|
|
|
typedef typename boost::remove_pointer<T>::type npT;
|
2008-08-04 18:56:36 +03:00
|
|
|
data = new npT;
|
|
|
|
*this >> *data;
|
2008-07-25 20:28:28 +03:00
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
void saveSerializable(const std::vector<T> &data)
|
|
|
|
{
|
|
|
|
boost::uint32_t length = data.size();
|
|
|
|
*this << length;
|
|
|
|
for(ui32 i=0;i<length;i++)
|
|
|
|
*this << data[i];
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
void loadSerializable(std::vector<T> &data)
|
|
|
|
{
|
|
|
|
boost::uint32_t length;
|
|
|
|
*this >> length;
|
|
|
|
data.resize(length);
|
|
|
|
for(ui32 i=0;i<length;i++)
|
|
|
|
*this >> data[i];
|
|
|
|
}
|
2008-08-04 12:05:52 +03:00
|
|
|
|
2008-07-26 09:39:44 +03:00
|
|
|
template <typename T>
|
|
|
|
void saveSerializable(const std::set<T> &data)
|
|
|
|
{
|
2008-07-26 16:57:32 +03:00
|
|
|
std::set<T> &d = const_cast<std::set<T> &>(data);
|
|
|
|
boost::uint32_t length = d.size();
|
2008-07-26 09:39:44 +03:00
|
|
|
*this << length;
|
2008-08-04 12:05:52 +03:00
|
|
|
for(typename std::set<T>::iterator i=d.begin();i!=d.end();i++)
|
2008-07-26 09:39:44 +03:00
|
|
|
*this << *i;
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
void loadSerializable(std::set<T> &data)
|
|
|
|
{
|
|
|
|
boost::uint32_t length;
|
|
|
|
*this >> length;
|
|
|
|
T ins;
|
|
|
|
for(ui32 i=0;i<length;i++)
|
|
|
|
{
|
|
|
|
*this >> ins;
|
|
|
|
data.insert(ins);
|
|
|
|
}
|
|
|
|
}
|
2008-08-04 12:05:52 +03:00
|
|
|
|
2008-07-30 20:51:19 +03:00
|
|
|
template <typename T1, typename T2>
|
|
|
|
void saveSerializable(const std::pair<T1,T2> &data)
|
|
|
|
{
|
|
|
|
*this << data.first << data.second;
|
|
|
|
}
|
|
|
|
template <typename T1, typename T2>
|
|
|
|
void loadSerializable(std::pair<T1,T2> &data)
|
|
|
|
{
|
|
|
|
*this >> data.first >> data.second;
|
|
|
|
}
|
2008-08-04 12:05:52 +03:00
|
|
|
|
2008-07-31 16:21:42 +03:00
|
|
|
template <typename T1, typename T2>
|
|
|
|
void saveSerializable(const std::map<T1,T2> &data)
|
|
|
|
{
|
|
|
|
*this << ui32(data.size());
|
2008-08-04 12:05:52 +03:00
|
|
|
for(typename std::map<T1,T2>::const_iterator i=data.begin();i!=data.end();i++)
|
2008-07-31 16:21:42 +03:00
|
|
|
*this << i->first << i->second;
|
|
|
|
}
|
|
|
|
template <typename T1, typename T2>
|
|
|
|
void loadSerializable(std::map<T1,T2> &data)
|
|
|
|
{
|
|
|
|
ui32 length;
|
|
|
|
*this >> length;
|
|
|
|
T1 t;
|
|
|
|
for(int i=0;i<length;i++)
|
|
|
|
{
|
|
|
|
*this >> t;
|
|
|
|
*this >> data[t];
|
|
|
|
}
|
|
|
|
}
|
2008-07-25 20:28:28 +03:00
|
|
|
template <typename T>
|
|
|
|
void save(const T &data)
|
|
|
|
{
|
|
|
|
typedef
|
|
|
|
//if
|
|
|
|
typename mpl::eval_if< mpl::equal_to<SerializationLevel<T>,mpl::int_<Primitive> >,
|
|
|
|
mpl::identity<SavePrimitive<CConnection,T> >,
|
|
|
|
//else if
|
2008-08-04 18:56:36 +03:00
|
|
|
typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Pointer> >,
|
|
|
|
mpl::identity<SavePointer<CConnection,T> >,
|
|
|
|
//else if
|
2008-07-25 20:28:28 +03:00
|
|
|
typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Serializable> >,
|
|
|
|
mpl::identity<SaveSerializable<CConnection,T> >,
|
|
|
|
//else
|
|
|
|
mpl::identity<SaveWrong<CConnection,T> >
|
|
|
|
>
|
2008-08-04 18:56:36 +03:00
|
|
|
>
|
2008-07-25 20:28:28 +03:00
|
|
|
>::type typex;
|
|
|
|
typex::invoke(*this, data);
|
|
|
|
}
|
2008-07-26 09:39:44 +03:00
|
|
|
|
2008-07-25 20:28:28 +03:00
|
|
|
template <typename T>
|
|
|
|
void load(T &data)
|
|
|
|
{
|
|
|
|
typedef
|
|
|
|
//if
|
|
|
|
typename mpl::eval_if< mpl::equal_to<SerializationLevel<T>,mpl::int_<Primitive> >,
|
|
|
|
mpl::identity<LoadPrimitive<CConnection,T> >,
|
|
|
|
//else if
|
2008-08-04 18:56:36 +03:00
|
|
|
typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Pointer> >,
|
|
|
|
mpl::identity<LoadPointer<CConnection,T> >,
|
|
|
|
//else if
|
2008-07-25 20:28:28 +03:00
|
|
|
typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Serializable> >,
|
|
|
|
mpl::identity<LoadSerializable<CConnection,T> >,
|
|
|
|
//else
|
|
|
|
mpl::identity<LoadWrong<CConnection,T> >
|
|
|
|
>
|
2008-08-04 18:56:36 +03:00
|
|
|
>
|
2008-07-25 20:28:28 +03:00
|
|
|
>::type typex;
|
|
|
|
typex::invoke(*this, data);
|
|
|
|
}
|
|
|
|
|
2008-07-03 18:03:32 +03:00
|
|
|
boost::asio::basic_stream_socket < boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > * socket;
|
|
|
|
bool logging;
|
|
|
|
bool connected;
|
|
|
|
bool myEndianess, contactEndianess; //true if little endian, if ednianess is different we'll have to revert recieved multi-byte vars
|
|
|
|
boost::asio::io_service *io_service;
|
|
|
|
std::string name; //who uses this connection
|
|
|
|
|
2008-07-09 20:22:28 +03:00
|
|
|
CConnection
|
|
|
|
(std::string host, std::string port, std::string Name, std::ostream & Out);
|
|
|
|
CConnection
|
|
|
|
(boost::asio::basic_socket_acceptor<boost::asio::ip::tcp, boost::asio::socket_acceptor_service<boost::asio::ip::tcp> > * acceptor,
|
|
|
|
boost::asio::io_service *Io_service, std::string Name, std::ostream & Out);
|
|
|
|
CConnection
|
|
|
|
(boost::asio::basic_stream_socket < boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > * Socket,
|
|
|
|
std::string Name, std::ostream & Out); //use immediately after accepting connection into socket
|
2008-07-03 18:03:32 +03:00
|
|
|
int write(const void * data, unsigned size);
|
|
|
|
int read(void * data, unsigned size);
|
|
|
|
int readLine(void * data, unsigned maxSize);
|
|
|
|
~CConnection(void);
|
2008-08-04 12:05:52 +03:00
|
|
|
};
|
|
|
|
|
2008-08-04 18:56:36 +03:00
|
|
|
template<> DLL_EXPORT
|
2008-08-04 12:05:52 +03:00
|
|
|
void CConnection::saveSerializable<std::string>(const std::string &data);
|
2008-08-04 18:56:36 +03:00
|
|
|
template <>DLL_EXPORT
|
2008-08-04 12:05:52 +03:00
|
|
|
void CConnection::loadSerializable<std::string>(std::string &data);
|
|
|
|
|
|
|
|
|