PageRenderTime 35ms CodeModel.GetById 24ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/TGame/TCommon/Message/MsgBuffer.h

http://awoe.googlecode.com/
C++ Header | 103 lines | 78 code | 23 blank | 2 comment | 3 complexity | 7dadc65c003285f8376d75e782977a1e MD5 | raw file
  1#ifndef	___MSG_BUFFER___
  2#define ___MSG_BUFFER___
  3
  4#include "Entry/LibExportCom.h"
  5
  6#pragma warning(push)
  7#pragma warning(disable:4275)
  8#pragma warning(disable:4251)
  9
 10
 11
 12#define MSG_BUFF_SIZE 1024
 13
 14
 15class TCOM_API MsgBuffer	:	public std::streambuf
 16{
 17public:
 18	typedef boost::asio::const_buffers_1 const_buffers_type;
 19	typedef boost::asio::mutable_buffers_1 mutable_buffers_type;
 20
 21	MsgBuffer()
 22	{
 23		setg(buffer_, buffer_, buffer_ + MSG_BUFF_SIZE);
 24		setp(buffer_, buffer_ + MSG_BUFF_SIZE);
 25	}
 26
 27	std::size_t size() const
 28	{
 29		return pptr() - gptr();
 30	}
 31
 32	std::size_t max_size() const
 33	{
 34		return MSG_BUFF_SIZE;
 35	}
 36
 37	mutable_buffers_type prepare(std::size_t n = MSG_BUFF_SIZE)
 38	{
 39		return boost::asio::buffer(boost::asio::mutable_buffer(pptr(), MSG_BUFF_SIZE));
 40	}
 41
 42	const_buffers_type data() const
 43	{
 44		return boost::asio::buffer(boost::asio::const_buffer(gptr(), (pptr() - gptr()) ));
 45	}
 46
 47	void commit(std::size_t n)
 48	{
 49		if (pptr() + n > epptr())
 50			n = epptr() - pptr();
 51		pbump(static_cast<int>(n));
 52		setg(eback(), gptr(), pptr());
 53	}
 54
 55	void consume(std::size_t n)
 56	{
 57		if (gptr() + n > pptr())
 58			n = pptr() - gptr();
 59		gbump(static_cast<int>(n));
 60	}
 61
 62	void update_buffer_length4() 	
 63	{ 		
 64		short val = size(); 		
 65		memcpy(gptr()+4, (char*)&val, 2);  	
 66	}
 67protected:
 68	enum { buffer_delta = 128 };
 69
 70	int_type underflow()
 71	{
 72		if (gptr() < pptr())
 73		{
 74			setg(&buffer_[0], gptr(), pptr());
 75			return traits_type::to_int_type(*gptr());
 76		}
 77		else
 78		{
 79			return traits_type::eof();
 80		}
 81	}
 82
 83
 84	int_type overflow(int_type c)
 85	{
 86		// detail in boost buffer
 87		return traits_type::not_eof(c);
 88	}
 89
 90	void reserve(std::size_t n)
 91	{
 92		//	detail in boost buffer
 93	}
 94private:
 95	char buffer_[MSG_BUFF_SIZE];
 96};
 97
 98
 99typedef boost::object_pool<MsgBuffer>	MsgBufferFactory;
100
101#pragma warning(pop)
102
103#endif