| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 | 
							- #pragma once
 
- #include "../global.h"
 
- #include <string>
 
- #include <vector>
 
- #include <set>
 
- #include <boost/type_traits/is_fundamental.hpp>
 
- #include <boost/type_traits/is_enum.hpp>
 
- #include <boost/type_traits/is_pointer.hpp> 
 
- #include <boost/type_traits/is_class.hpp> 
 
- #include  <boost/type_traits/remove_pointer.hpp>
 
- #include <boost/mpl/eval_if.hpp>
 
- #include <boost/mpl/equal_to.hpp>
 
- #include <boost/mpl/int.hpp>
 
- #include <boost/mpl/identity.hpp>
 
- const int version = 63;
 
- class CConnection;
 
- namespace mpl = boost::mpl;
 
- namespace boost
 
- {
 
- 	namespace asio
 
- 	{
 
- 		namespace ip
 
- 		{
 
- 			class tcp;
 
- 		}
 
- 		class io_service;
 
- 		template <typename Protocol> class stream_socket_service;
 
- 		template <typename Protocol,typename StreamSocketService>
 
- 		class basic_stream_socket;
 
- 		template <typename Protocol> class socket_acceptor_service;
 
- 		template <typename Protocol,typename SocketAcceptorService>
 
- 		class basic_socket_acceptor;
 
- 	}
 
- 	class mutex;
 
- };
 
- enum SerializationLvl
 
- {
 
- 	Wrong=0,
 
- 	Primitive,
 
- 	Pointer,
 
- 	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>,
 
- 			mpl::int_<Primitive>,
 
- 		//else
 
- 		typename mpl::eval_if<
 
- 			boost::is_pointer<T>,
 
- 			mpl::int_<Pointer>,
 
- 		//else
 
- 		typename mpl::eval_if<
 
- 			boost::is_enum<T>,
 
- 			mpl::int_<Primitive>,
 
- 		//else
 
- 			mpl::int_<Wrong>
 
- 		>
 
- 		>
 
- 		>
 
- 		>
 
- 		>::type type;
 
- 	static const int value = SerializationLevel::type::value;
 
- };
 
- template <typename Serializer> class DLL_EXPORT COSer
 
- {
 
- public:
 
- 	bool saving;
 
- 	COSer(){saving=true;};
 
-     Serializer * This()
 
- 	{
 
- 		return static_cast<Serializer*>(this);
 
- 	}
 
- 	template<class T>
 
- 	Serializer & operator<<(const T &t)
 
- 	{
 
- 		this->This()->save(t);
 
- 		return * this->This();
 
- 	}
 
- 	
 
- 	template<class T>
 
- 	COSer & operator&(T & t){
 
- 		return * this->This() << t;
 
- 	}
 
- };
 
- template <typename Serializer> class DLL_EXPORT CISer
 
- {
 
- public:
 
- 	bool saving;
 
- 	CISer(){saving = false;};
 
-     Serializer * This()
 
- 	{
 
- 		return static_cast<Serializer*>(this);
 
- 	}
 
- 	template<class T>
 
- 	Serializer & operator>>(T &t)
 
- 	{
 
- 		this->This()->load(t);
 
- 		return * this->This();
 
- 	}
 
- 	
 
- 	template<class T>
 
- 	CISer & operator&(T & t){
 
- 		return * this->This() >> t;
 
- 	}
 
- };
 
- 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>
 
- 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>
 
- 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!");
 
- 	}
 
- };
 
- class DLL_EXPORT CConnection
 
- 	:public CISer<CConnection>, public COSer<CConnection>
 
- {
 
- 	std::ostream &out;
 
- 	CConnection(void);
 
- 	void init();
 
- public:
 
- 	boost::mutex *rmx, *wmx; // read/write mutexes
 
- 	template <typename T>
 
- 	void savePrimitive(const T &data)
 
- 	{
 
- 		write(&data,sizeof(data));
 
- 	}
 
- 	template <typename T>
 
- 	void loadPrimitive(T &data)
 
- 	{
 
- 		read(&data,sizeof(data));
 
- 	}
 
- 	
 
- 	
 
- 	template <typename T>
 
- 	void saveSerializable(const T &data)
 
- 	{
 
- 		const_cast<T&>(data).serialize(*static_cast<COSer<CConnection>*>(this),version);
 
- 	}
 
- 	template <typename T>
 
- 	void loadSerializable(T &data)
 
- 	{
 
- 		data.serialize(*static_cast<CISer<CConnection>*>(this),version);
 
- 	}	
 
- 	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;
 
- 		typedef typename boost::remove_pointer<T>::type npT;
 
- 		data = new npT;
 
- 		*this >> *data;
 
- 	}
 
- 	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];
 
- 	}
 
- 	
 
- 	template <typename T>
 
- 	void saveSerializable(const std::set<T> &data)
 
- 	{
 
- 		std::set<T> &d = const_cast<std::set<T> &>(data);
 
- 		boost::uint32_t length = d.size();
 
- 		*this << length;
 
- 		for(typename std::set<T>::iterator i=d.begin();i!=d.end();i++)
 
- 			*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);
 
- 		}
 
- 	}
 
- 	
 
- 	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;
 
- 	}
 
- 	
 
- 	template <typename T1, typename T2>
 
- 	void saveSerializable(const std::map<T1,T2> &data)
 
- 	{
 
- 		*this << ui32(data.size());
 
- 		for(typename std::map<T1,T2>::const_iterator i=data.begin();i!=data.end();i++)
 
- 			*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];
 
- 		}
 
- 	}
 
- 	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
 
- 			typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Pointer> >,
 
- 				mpl::identity<SavePointer<CConnection,T> >,
 
- 			//else if
 
- 			typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Serializable> >,
 
- 				mpl::identity<SaveSerializable<CConnection,T> >,
 
- 			//else
 
- 				mpl::identity<SaveWrong<CConnection,T> >
 
- 			>
 
- 			>
 
- 			>::type typex;
 
- 		typex::invoke(*this, data);
 
- 	}
 
- 	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
 
- 			typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Pointer> >,
 
- 				mpl::identity<LoadPointer<CConnection,T> >,
 
- 			//else if
 
- 			typename mpl::eval_if<mpl::equal_to<SerializationLevel<T>,mpl::int_<Serializable> >,
 
- 				mpl::identity<LoadSerializable<CConnection,T> >,
 
- 			//else
 
- 				mpl::identity<LoadWrong<CConnection,T> >
 
- 			>
 
- 			>
 
- 			>::type typex;
 
- 		typex::invoke(*this, data);
 
- 	}
 
- 	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
 
- 	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
 
- 	int write(const void * data, unsigned size);
 
- 	int read(void * data, unsigned size);
 
- 	int readLine(void * data, unsigned maxSize);
 
- 	void close();
 
- 	~CConnection(void);
 
- };
 
- template<> DLL_EXPORT 
 
- void CConnection::saveSerializable<std::string>(const std::string &data);
 
- template <>DLL_EXPORT 
 
- void CConnection::loadSerializable<std::string>(std::string &data);
 
 
  |