diff --git a/depends_linux/include/proto4z/proto4z.h b/depends_linux/include/proto4z/proto4z.h index b1fd3e5e..b0fa69c8 100644 --- a/depends_linux/include/proto4z/proto4z.h +++ b/depends_linux/include/proto4z/proto4z.h @@ -9,7 +9,7 @@ * * =============================================================================== * - * Copyright (C) 2013-2014 YaweiZhang . + * Copyright (C) 2013-2015 YaweiZhang . * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -132,35 +132,18 @@ enum ZSummer_EndianType //|----- header -----|------- body --------| //|--packlen-|-protoID--|------- body --------| -//|---------------------------------------------| //PackLenIsContainHead -// |-----------------------| //not PackLenIsContainHead +typedef unsigned int Integer; +typedef unsigned short ProtoInteger; -struct DefaultStreamHeadTraits -{ - typedef unsigned short Integer; //User-Defined Integer Type must in [unsigned char, unsigned short, unsigned int, unsigned long long]. - typedef unsigned short ProtoInteger; - const static Integer MaxPackLen = (Integer)-1; //User-Defined. example: Integer = unsigned short(-1) ==>(65535) - const static bool PackLenIsContainHead = true; //User-Defined - const static ZSummer_EndianType EndianType = LittleEndian;//User-Defined -}; - -//protocol Traits example, User-Defined -struct TestBigStreamHeadTraits -{ - typedef unsigned int Integer; - typedef unsigned short ProtoInteger; - const static Integer MaxPackLen = -1; - const static bool PackLenIsContainHead = false; - const static ZSummer_EndianType EndianType = BigEndian; -}; +const static Integer MaxPackLen = 1024*1024; //stream translate to Integer with endian type. -template +template Integer streamToInteger(const char stream[sizeof(Integer)]); //integer translate to stream with endian type. -template +template void integerToStream(Integer integer, char *stream); //!get runtime local endian type. @@ -183,10 +166,8 @@ enum INTEGRITY_RET_TYPE //! first: IRT_SUCCESS data integrity. second: current integrity data lenght. //! first: IRT_SHORTAGE data not integrity. second: shortage lenght. //! first: IRT_CORRUPTION data corruption. second: data lenght -template -inline std::pair -checkBuffIntegrity(const char * buff, typename StreamHeadTrait::Integer curBuffLen, -typename StreamHeadTrait::Integer maxBuffLen /*= StreamHeadTrait::MaxPackLen*/); +inline std::pair +checkBuffIntegrity(const char * buff, Integer curBuffLen, Integer maxBuffLen /*= MaxPackLen*/); @@ -195,17 +176,13 @@ typename StreamHeadTrait::Integer maxBuffLen /*= StreamHeadTrait::MaxPackLen*/); ////////////////////////////////////////////////////////////////////////// //StreamHeadTrait: User-Defined like DefaultStreamHeadTrait -template > + class WriteStream { public: - typedef typename StreamHeadTrait::Integer Integer; - typedef typename StreamHeadTrait::ProtoInteger ProtoInteger; - - //! bNoWrite : if true then WriteStream will not do any write operation. //! attachData : Attach to the existing memory. - WriteStream(ProtoInteger pID, char * attachData = NULL, Integer maxStreamLen = 0, bool bNoWrite = false); + inline WriteStream(ProtoInteger pID, char * attachData = NULL, Integer maxStreamLen = 0, bool bNoWrite = false); ~WriteStream(){} public: //get total stream buff, the pointer must be used immediately. @@ -219,22 +196,22 @@ class WriteStream inline Integer getStreamBodyLen(){ return _cursor - _headLen; } //write original data. - inline WriteStream & appendOriginalData(const void * data, Integer unit); - - - inline WriteStream & operator << (bool data) { return writeSimpleData(data); } - inline WriteStream & operator << (char data) { return writeSimpleData(data); } - inline WriteStream & operator << (unsigned char data) { return writeSimpleData(data); } - inline WriteStream & operator << (short data) { return writeIntegerData(data); } - inline WriteStream & operator << (unsigned short data) { return writeIntegerData(data); } - inline WriteStream & operator << (int data) { return writeIntegerData(data); } - inline WriteStream & operator << (unsigned int data) { return writeIntegerData(data); } - inline WriteStream & operator << (long data) { return writeIntegerData((long long)data); } - inline WriteStream & operator << (unsigned long data) { return writeIntegerData((unsigned long long)data); } - inline WriteStream & operator << (long long data) { return writeIntegerData(data); } - inline WriteStream & operator << (unsigned long long data) { return writeIntegerData(data); } - inline WriteStream & operator << (float data) { return writeIntegerData(data); } - inline WriteStream & operator << (double data) { return writeIntegerData(data); } + inline WriteStream & appendOriginalData(const void * data, Integer unit); + inline WriteStream & fixOriginalData(Integer offset, Integer unit); + + inline WriteStream & operator << (bool data) { return writeSimpleData(data); } + inline WriteStream & operator << (char data) { return writeSimpleData(data); } + inline WriteStream & operator << (unsigned char data) { return writeSimpleData(data); } + inline WriteStream & operator << (short data) { return writeIntegerData(data); } + inline WriteStream & operator << (unsigned short data) { return writeIntegerData(data); } + inline WriteStream & operator << (int data) { return writeIntegerData(data); } + inline WriteStream & operator << (unsigned int data) { return writeIntegerData(data); } + inline WriteStream & operator << (long data) { return writeIntegerData((long long)data); } + inline WriteStream & operator << (unsigned long data) { return writeIntegerData((unsigned long long)data); } + inline WriteStream & operator << (long long data) { return writeIntegerData(data); } + inline WriteStream & operator << (unsigned long long data) { return writeIntegerData(data); } + inline WriteStream & operator << (float data) { return writeIntegerData(data); } + inline WriteStream & operator << (double data) { return writeIntegerData(data); } protected: //! check move cursor is valid. if invalid then throw exception. @@ -245,14 +222,14 @@ class WriteStream //! write integer data with endian type. template - inline WriteStream & writeIntegerData(T t); + inline WriteStream & writeIntegerData(T t); //! write some types of data with out endian type. It's relative to writeIntegerData method. template - inline WriteStream & writeSimpleData(T t); + inline WriteStream & writeSimpleData(T t); private: - std::basic_string, _Alloc > _data; //! If not attach any memory, class WriteStream will used this to managing memory. + std::string _data; //! If not attach any memory, class WriteStream will used this to managing memory. char * _attachData;//! attach memory pointer Integer _maxStreamLen; //! can write max size Integer _cursor; //! current move cursor. @@ -265,14 +242,11 @@ class WriteStream //class ReadStream: De-serialization the specified data from byte stream. ////////////////////////////////////////////////////////////////////////// -//StreamHeadTrait: User-Defined like DefaultStreamHeadTrait -template + class ReadStream { public: - typedef typename StreamHeadTrait::Integer Integer; - typedef typename StreamHeadTrait::ProtoInteger ProtoInteger; - ReadStream(const char *attachData, Integer attachDataLen, bool isHaveHeader = true); + inline ReadStream(const char *attachData, Integer attachDataLen, bool isHaveHeader = true); ~ReadStream(){} public: //reset cursor @@ -299,25 +273,25 @@ class ReadStream inline void skipOriginalData(Integer unit); - inline ReadStream & operator >> (bool & data) { return readSimpleData(data); } - inline ReadStream & operator >> (char & data) { return readSimpleData(data); } - inline ReadStream & operator >> (unsigned char & data) { return readSimpleData(data); } - inline ReadStream & operator >> (short & data) { return readIntegerData(data); } - inline ReadStream & operator >> (unsigned short & data) { return readIntegerData(data); } - inline ReadStream & operator >> (int & data) { return readIntegerData(data); } - inline ReadStream & operator >> (unsigned int & data) { return readIntegerData(data); } - inline ReadStream & operator >> (long & data){ long long tmp = 0;ReadStream & ret = readIntegerData(tmp);data =(long) tmp;return ret;} - inline ReadStream & operator >> (unsigned long & data){ unsigned long long tmp = 0;ReadStream & ret = readIntegerData(tmp);data = (unsigned long)tmp;return ret;} - inline ReadStream & operator >> (long long & data) { return readIntegerData(data); } - inline ReadStream & operator >> (unsigned long long & data) { return readIntegerData(data); } - inline ReadStream & operator >> (float & data) { return readIntegerData(data); } - inline ReadStream & operator >> (double & data) { return readIntegerData(data); } + inline ReadStream & operator >> (bool & data) { return readSimpleData(data); } + inline ReadStream & operator >> (char & data) { return readSimpleData(data); } + inline ReadStream & operator >> (unsigned char & data) { return readSimpleData(data); } + inline ReadStream & operator >> (short & data) { return readIntegerData(data); } + inline ReadStream & operator >> (unsigned short & data) { return readIntegerData(data); } + inline ReadStream & operator >> (int & data) { return readIntegerData(data); } + inline ReadStream & operator >> (unsigned int & data) { return readIntegerData(data); } + inline ReadStream & operator >> (long & data){ long long tmp = 0;ReadStream & ret = readIntegerData(tmp);data =(long) tmp;return ret;} + inline ReadStream & operator >> (unsigned long & data){ unsigned long long tmp = 0;ReadStream & ret = readIntegerData(tmp);data = (unsigned long)tmp;return ret;} + inline ReadStream & operator >> (long long & data) { return readIntegerData(data); } + inline ReadStream & operator >> (unsigned long long & data) { return readIntegerData(data); } + inline ReadStream & operator >> (float & data) { return readIntegerData(data); } + inline ReadStream & operator >> (double & data) { return readIntegerData(data); } protected: inline void checkMoveCursor(Integer unit); template - inline ReadStream & readIntegerData(T & t); + inline ReadStream & readIntegerData(T & t); template - inline ReadStream & readSimpleData(T & t); + inline ReadStream & readSimpleData(T & t); private: const char * _attachData; @@ -333,29 +307,29 @@ class ReadStream ////////////////////////////////////////////////////////////////////////// //write c-style string -template -inline WriteStream & operator << (WriteStream & ws, const char *const data) + +inline WriteStream & operator << (WriteStream & ws, const char *const data) { - typename StreamHeadTrait::Integer len = (typename StreamHeadTrait::Integer)strlen(data); + Integer len = (Integer)strlen(data); ws << len; ws.appendOriginalData(data, len); return ws; } //write std::string -template -inline WriteStream & operator << (WriteStream & ws, const std::basic_string & data) +template +inline WriteStream & operator << (WriteStream & ws, const std::basic_string & data) { - typename StreamHeadTrait::Integer len = (typename StreamHeadTrait::Integer)data.length(); + Integer len = (Integer)data.length(); ws << len; ws.appendOriginalData(data.c_str(), len); return ws; } //read std::string -template -inline ReadStream & operator >> (ReadStream & rs, std::basic_string & data) +template +inline ReadStream & operator >> (ReadStream & rs, std::basic_string & data) { - typename StreamHeadTrait::Integer len = 0; + Integer len = 0; rs >> len; data.assign(rs.peekOriginalData(len), len); rs.skipOriginalData(len); @@ -364,10 +338,10 @@ inline ReadStream & operator >> (ReadStream & //std::vector -template -inline WriteStream & operator << (WriteStream & ws, const std::vector & vct) +template +inline WriteStream & operator << (WriteStream & ws, const std::vector & vct) { - ws << (typename StreamHeadTrait::Integer)vct.size(); + ws << (Integer)vct.size(); for (typename std::vector::const_iterator iter = vct.begin(); iter != vct.end(); ++iter) { ws << *iter; @@ -375,12 +349,12 @@ inline WriteStream & operator << (WriteStream return ws; } -template -inline ReadStream & operator >> (ReadStream & rs, std::vector & vct) +template +inline ReadStream & operator >> (ReadStream & rs, std::vector & vct) { - typename StreamHeadTrait::Integer totalCount = 0; + Integer totalCount = 0; rs >> totalCount; - for (typename StreamHeadTrait::Integer i = 0; i < totalCount; ++i) + for (Integer i = 0; i < totalCount; ++i) { T t; rs >> t; @@ -390,10 +364,10 @@ inline ReadStream & operator >> (ReadStream & } //std::set -template -inline WriteStream & operator << (WriteStream & ws, const std::set & k) +template +inline WriteStream & operator << (WriteStream & ws, const std::set & k) { - ws << (typename StreamHeadTrait::Integer)k.size(); + ws << (Integer)k.size(); for (typename std::set::const_iterator iter = k.begin(); iter != k.end(); ++iter) { ws << *iter; @@ -401,12 +375,12 @@ inline WriteStream & operator << (WriteStream return ws; } -template -inline ReadStream & operator >> (ReadStream & rs, std::set & k) +template +inline ReadStream & operator >> (ReadStream & rs, std::set & k) { - typename StreamHeadTrait::Integer totalCount = 0; + Integer totalCount = 0; rs >> totalCount; - for (typename StreamHeadTrait::Integer i = 0; i < totalCount; ++i) + for (Integer i = 0; i < totalCount; ++i) { Key t; rs >> t; @@ -416,10 +390,10 @@ inline ReadStream & operator >> (ReadStream & } //std::multiset -template -inline WriteStream & operator << (WriteStream & ws, const std::multiset & k) +template +inline WriteStream & operator << (WriteStream & ws, const std::multiset & k) { - ws << (typename StreamHeadTrait::Integer)k.size(); + ws << (Integer)k.size(); for (typename std::multiset::const_iterator iter = k.begin(); iter != k.end(); ++iter) { ws << *iter; @@ -427,12 +401,12 @@ inline WriteStream & operator << (WriteStream return ws; } -template -inline ReadStream & operator >> (ReadStream & rs, std::multiset & k) +template +inline ReadStream & operator >> (ReadStream & rs, std::multiset & k) { - typename StreamHeadTrait::Integer totalCount = 0; + Integer totalCount = 0; rs >> totalCount; - for (typename StreamHeadTrait::Integer i = 0; i < totalCount; ++i) + for (Integer i = 0; i < totalCount; ++i) { Key t; rs >> t; @@ -442,10 +416,10 @@ inline ReadStream & operator >> (ReadStream & } //std::map -template -inline WriteStream & operator << (WriteStream & ws, const std::map & kv) +template +inline WriteStream & operator << (WriteStream & ws, const std::map & kv) { - ws << (typename StreamHeadTrait::Integer)kv.size(); + ws << (Integer)kv.size(); for (typename std::map::const_iterator iter = kv.begin(); iter != kv.end(); ++iter) { ws << iter->first; @@ -454,12 +428,12 @@ inline WriteStream & operator << (WriteStream return ws; } -template -inline ReadStream & operator >> (ReadStream & rs, std::map & kv) +template +inline ReadStream & operator >> (ReadStream & rs, std::map & kv) { - typename StreamHeadTrait::Integer totalCount = 0; + Integer totalCount = 0; rs >> totalCount; - for (typename StreamHeadTrait::Integer i = 0; i < totalCount; ++i) + for (Integer i = 0; i < totalCount; ++i) { std::pair pr; rs >> pr.first; @@ -470,10 +444,10 @@ inline ReadStream & operator >> (ReadStream & } //std::multimap -template -inline WriteStream & operator << (WriteStream & ws, const std::multimap & kv) +template +inline WriteStream & operator << (WriteStream & ws, const std::multimap & kv) { - ws << (typename StreamHeadTrait::Integer)kv.size(); + ws << (Integer)kv.size(); for (typename std::multimap::const_iterator iter = kv.begin(); iter != kv.end(); ++iter) { ws << iter->first; @@ -482,12 +456,12 @@ inline WriteStream & operator << (WriteStream return ws; } -template -inline ReadStream & operator >> (ReadStream & rs, std::multimap & kv) +template +inline ReadStream & operator >> (ReadStream & rs, std::multimap & kv) { - typename StreamHeadTrait::Integer totalCount = 0; + Integer totalCount = 0; rs >> totalCount; - for (typename StreamHeadTrait::Integer i = 0; i < totalCount; ++i) + for (Integer i = 0; i < totalCount; ++i) { std::pair pr; rs >> pr.first; @@ -499,10 +473,10 @@ inline ReadStream & operator >> (ReadStream & //std::list -template -inline WriteStream & operator << (WriteStream & ws, const std::list & l) +template +inline WriteStream & operator << (WriteStream & ws, const std::list & l) { - ws << (typename StreamHeadTrait::Integer)l.size(); + ws << (Integer)l.size(); for (typename std::list::const_iterator iter = l.begin(); iter != l.end(); ++iter) { ws << *iter; @@ -510,12 +484,12 @@ inline WriteStream & operator << (WriteStream return ws; } -template -inline ReadStream & operator >> (ReadStream & rs, std::list & l) +template +inline ReadStream & operator >> (ReadStream & rs, std::list & l) { - typename StreamHeadTrait::Integer totalCount = 0; + Integer totalCount = 0; rs >> totalCount; - for (typename StreamHeadTrait::Integer i = 0; i < totalCount; ++i) + for (Integer i = 0; i < totalCount; ++i) { Value t; rs >> t; @@ -524,10 +498,10 @@ inline ReadStream & operator >> (ReadStream & return rs; } //std::deque -template -inline WriteStream & operator << (WriteStream & ws, const std::deque & l) +template +inline WriteStream & operator << (WriteStream & ws, const std::deque & l) { - ws << (typename StreamHeadTrait::Integer)l.size(); + ws << (Integer)l.size(); for (typename std::deque::const_iterator iter = l.begin(); iter != l.end(); ++iter) { ws << *iter; @@ -535,12 +509,12 @@ inline WriteStream & operator << (WriteStream return ws; } -template -inline ReadStream & operator >> (ReadStream & rs, std::deque & l) +template +inline ReadStream & operator >> (ReadStream & rs, std::deque & l) { - typename StreamHeadTrait::Integer totalCount = 0; + Integer totalCount = 0; rs >> totalCount; - for (typename StreamHeadTrait::Integer i = 0; i < totalCount; ++i) + for (Integer i = 0; i < totalCount; ++i) { Value t; rs >> t; @@ -564,7 +538,7 @@ inline ZSummer_EndianType __localEndianType() return BigEndian; } -template +template Integer streamToInteger(const char stream[sizeof(Integer)]) { unsigned short integerLen = sizeof(Integer); @@ -575,7 +549,7 @@ Integer streamToInteger(const char stream[sizeof(Integer)]) } else { - if (StreamHeadTrait::EndianType != __localEndianType()) + if (LittleEndian != __localEndianType()) { unsigned char *dst = (unsigned char*)&integer; unsigned char *src = (unsigned char*)stream + integerLen; @@ -593,7 +567,7 @@ Integer streamToInteger(const char stream[sizeof(Integer)]) return integer; } -template +template void integerToStream(Integer integer, char *stream) { unsigned short integerLen = sizeof(Integer); @@ -603,7 +577,7 @@ void integerToStream(Integer integer, char *stream) } else { - if (StreamHeadTrait::EndianType != __localEndianType()) + if (LittleEndian != __localEndianType()) { unsigned char *src = (unsigned char*)&integer + integerLen; unsigned char *dst = (unsigned char*)stream; @@ -625,27 +599,18 @@ void integerToStream(Integer integer, char *stream) ////////////////////////////////////////////////////////////////////////// -template -inline std::pair checkBuffIntegrity(const char * buff, typename StreamHeadTrait::Integer curBuffLen, typename StreamHeadTrait::Integer maxBuffLen) + +inline std::pair checkBuffIntegrity(const char * buff, Integer curBuffLen, Integer maxBuffLen) { //! 检查包头是否完整 - unsigned short headLen = (unsigned short)sizeof(typename StreamHeadTrait::Integer) + (unsigned short)sizeof(typename StreamHeadTrait::ProtoInteger); + unsigned short headLen = (unsigned short)sizeof(Integer) + (unsigned short)sizeof(ProtoInteger); if (curBuffLen < headLen) { return std::make_pair(IRT_SHORTAGE, headLen - curBuffLen); } //! 获取包长度 - typename StreamHeadTrait::Integer packLen = streamToInteger(buff); - if (!StreamHeadTrait::PackLenIsContainHead) - { - typename StreamHeadTrait::Integer oldInteger = packLen; - packLen += headLen; - if (packLen < oldInteger) //over range - { - return std::make_pair(IRT_CORRUPTION, curBuffLen); - } - } + Integer packLen = streamToInteger(buff); //! check if (packLen > maxBuffLen) @@ -669,8 +634,8 @@ inline std::pair checkBuf //! implement ////////////////////////////////////////////////////////////////////////// -template -WriteStream::WriteStream(ProtoInteger pID, char * attachData, Integer maxStreamLen, bool bNoWrite) + +inline WriteStream::WriteStream(ProtoInteger pID, char * attachData, Integer maxStreamLen, bool bNoWrite) { _pID = pID; _attachData = attachData; @@ -684,32 +649,28 @@ WriteStream::WriteStream(ProtoInteger pID, char * at _data.reserve(1200); _data.resize(_cursor, '\0'); } - if (_maxStreamLen < _headLen || _maxStreamLen > StreamHeadTrait::MaxPackLen) + if (_maxStreamLen < _headLen || _maxStreamLen > MaxPackLen) { - _maxStreamLen = StreamHeadTrait::MaxPackLen; + _maxStreamLen = MaxPackLen; } //write header - Integer packLen = 0; - if (StreamHeadTrait::PackLenIsContainHead) - { - packLen = _headLen; - } + Integer packLen = _headLen; if (_attachData) { - integerToStream(packLen, &_attachData[0]); - integerToStream(pID, &_attachData[sizeof(Integer)]); + integerToStream(packLen, &_attachData[0]); + integerToStream(pID, &_attachData[sizeof(Integer)]); } else { - integerToStream(packLen, &_data[0]); - integerToStream(pID, &_data[sizeof(Integer)]); + integerToStream(packLen, &_data[0]); + integerToStream(pID, &_data[sizeof(Integer)]); } } -template -inline void WriteStream::checkMoveCursor(Integer unit) + +inline void WriteStream::checkMoveCursor(Integer unit) { if (_maxStreamLen < _headLen) { @@ -729,32 +690,28 @@ inline void WriteStream::checkMoveCursor(Integer uni } } -template -inline void WriteStream::fixPackLen() + +inline void WriteStream::fixPackLen() { if (_isNoWrite) { return; } Integer packLen = _cursor; - if (!StreamHeadTrait::PackLenIsContainHead) - { - packLen -= _headLen; - } if (_attachData) { - integerToStream(packLen, &_attachData[0]); + integerToStream(packLen, &_attachData[0]); } else { - integerToStream(packLen, &_data[0]); + integerToStream(packLen, &_data[0]); } } -template -inline char* WriteStream::getStream() + +inline char* WriteStream::getStream() { if (_isNoWrite) { @@ -771,8 +728,8 @@ inline char* WriteStream::getStream() return NULL; } -template -inline char* WriteStream::getStreamBody() + +inline char* WriteStream::getStreamBody() { if (_isNoWrite) { @@ -789,8 +746,8 @@ inline char* WriteStream::getStreamBody() return NULL; } -template -inline WriteStream & WriteStream::appendOriginalData(const void * data, Integer unit) + +inline WriteStream & WriteStream::appendOriginalData(const void * data, Integer unit) { checkMoveCursor(unit); if (!_isNoWrite) @@ -808,9 +765,24 @@ inline WriteStream & WriteStream::a fixPackLen(); return *this; } - -template template -inline WriteStream & WriteStream::writeIntegerData(T t) +inline WriteStream & WriteStream::fixOriginalData(Integer offset, Integer unit) +{ + if (offset + sizeof(Integer) > _cursor) + { + throw std::runtime_error("fixOriginalData over stream."); + } + if (_attachData) + { + integerToStream(unit, &_attachData[offset]); + } + else + { + integerToStream(unit, &_data[offset]); + } + return *this; +} + template +inline WriteStream & WriteStream::writeIntegerData(T t) { Integer unit = sizeof(T); checkMoveCursor(unit); @@ -818,14 +790,14 @@ inline WriteStream & WriteStream::w { if (_attachData) { - integerToStream(t, &_attachData[_cursor]); + integerToStream(t, &_attachData[_cursor]); } else { _data.append((const char*)&t, unit); - if (StreamHeadTrait::EndianType != __localEndianType()) + if (LittleEndian != __localEndianType()) { - integerToStream(t, &_data[_cursor]); + integerToStream(t, &_data[_cursor]); } } } @@ -835,8 +807,8 @@ inline WriteStream & WriteStream::w } -template template -inline WriteStream & WriteStream::writeSimpleData(T t) + template +inline WriteStream & WriteStream::writeSimpleData(T t) { Integer unit = sizeof(T); checkMoveCursor(unit); @@ -865,15 +837,15 @@ inline WriteStream & WriteStream::w ////////////////////////////////////////////////////////////////////////// //! implement ////////////////////////////////////////////////////////////////////////// -template -ReadStream::ReadStream(const char *attachData, Integer attachDataLen, bool isHaveHeader) + +inline ReadStream::ReadStream(const char *attachData, Integer attachDataLen, bool isHaveHeader) { _attachData = attachData; _maxDataLen = attachDataLen; _isHaveHeader = isHaveHeader; - if (_maxDataLen > StreamHeadTrait::MaxPackLen) + if (_maxDataLen > MaxPackLen) { - _maxDataLen = StreamHeadTrait::MaxPackLen; + _maxDataLen = MaxPackLen; } if (_maxDataLen < sizeof(Integer) + sizeof(ProtoInteger)) { @@ -883,12 +855,8 @@ ReadStream::ReadStream(const char *attachData, Integer attachDa if (_isHaveHeader) { _cursor = sizeof(Integer) + sizeof(ProtoInteger); - _pID = streamToInteger(&_attachData[sizeof(Integer)]); - Integer len = streamToInteger(&_attachData[0]); - if (!StreamHeadTrait::PackLenIsContainHead) - { - len += _cursor; - } + _pID = streamToInteger(&_attachData[sizeof(Integer)]); + Integer len = streamToInteger(&_attachData[0]); if (len < _maxDataLen) // if stream invalid, ReadStream try read data as much as possible. { _maxDataLen = len; @@ -904,8 +872,7 @@ ReadStream::ReadStream(const char *attachData, Integer attachDa } -template -inline void ReadStream::resetMoveCursor() +inline void ReadStream::resetMoveCursor() { if (_isHaveHeader) { @@ -917,8 +884,8 @@ inline void ReadStream::resetMoveCursor() } } -template -inline void ReadStream::checkMoveCursor(Integer unit) + +inline void ReadStream::checkMoveCursor(Integer unit) { if (_cursor > _maxDataLen) { @@ -935,20 +902,20 @@ inline void ReadStream::checkMoveCursor(Integer unit) } -template -inline const char* ReadStream::getStream() + +inline const char* ReadStream::getStream() { return _attachData; } -template -inline typename ReadStream::Integer ReadStream::getStreamLen() + +inline Integer ReadStream::getStreamLen() { return _maxDataLen; } -template -inline const char* ReadStream::getStreamBody() + +inline const char* ReadStream::getStreamBody() { if (_isHaveHeader) { @@ -957,8 +924,8 @@ inline const char* ReadStream::getStreamBody() return _attachData; } -template -inline typename ReadStream::Integer ReadStream::getStreamBodyLen() + +inline Integer ReadStream::getStreamBodyLen() { if (_isHaveHeader) { @@ -967,29 +934,29 @@ inline typename ReadStream::Integer ReadStream return getStreamLen(); } -template -inline const char* ReadStream::getStreamUnread() + +inline const char* ReadStream::getStreamUnread() { return &_attachData[_cursor]; } -template -typename ReadStream::Integer ReadStream::getStreamUnreadLen() + +inline Integer ReadStream::getStreamUnreadLen() { return getStreamLen() - _cursor; } -template template -inline ReadStream & ReadStream::readIntegerData(T & t) + template +inline ReadStream & ReadStream::readIntegerData(T & t) { Integer unit = sizeof(T); checkMoveCursor(unit); - t = streamToInteger(&_attachData[_cursor]); + t = streamToInteger(&_attachData[_cursor]); _cursor += unit; return * this; } -template template -inline ReadStream & ReadStream::readSimpleData(T & t) + template +inline ReadStream & ReadStream::readSimpleData(T & t) { Integer unit = sizeof(T); checkMoveCursor(unit); @@ -997,14 +964,14 @@ inline ReadStream & ReadStream::readSimpleData _cursor += unit; return * this; } -template -inline const char * ReadStream::peekOriginalData(Integer unit) + +inline const char * ReadStream::peekOriginalData(Integer unit) { checkMoveCursor(unit); return &_attachData[_cursor]; } -template -inline void ReadStream::skipOriginalData(Integer unit) + +inline void ReadStream::skipOriginalData(Integer unit) { checkMoveCursor(unit); _cursor += unit; diff --git a/depends_linux/include/zsummerX/frame/FrameHeader.h b/depends_linux/include/zsummerX/frame/FrameHeader.h index a77792fc..1699fe12 100644 --- a/depends_linux/include/zsummerX/frame/FrameHeader.h +++ b/depends_linux/include/zsummerX/frame/FrameHeader.h @@ -148,8 +148,8 @@ struct FrameStreamTraits }; -typedef zsummer::proto4z::ReadStream ReadStreamPack; -typedef zsummer::proto4z::WriteStream WriteStreamPack; +typedef zsummer::proto4z::ReadStream ReadStreamPack; +typedef zsummer::proto4z::WriteStream WriteStreamPack; //!register message with original net pack, if return false other register will not receive this message. diff --git a/depends_linux/lib/liblog4z.a b/depends_linux/lib/liblog4z.a index f458569c..3e51ab88 100644 Binary files a/depends_linux/lib/liblog4z.a and b/depends_linux/lib/liblog4z.a differ diff --git a/depends_linux/lib/liblog4z_d.a b/depends_linux/lib/liblog4z_d.a index fdadb77b..4be13ea3 100644 Binary files a/depends_linux/lib/liblog4z_d.a and b/depends_linux/lib/liblog4z_d.a differ diff --git a/depends_linux/lib/libzsummerX.a b/depends_linux/lib/libzsummerX.a index b0883766..b935cfaf 100644 Binary files a/depends_linux/lib/libzsummerX.a and b/depends_linux/lib/libzsummerX.a differ diff --git a/depends_linux/lib/libzsummerX_d.a b/depends_linux/lib/libzsummerX_d.a index d7923e54..8d62a7ee 100644 Binary files a/depends_linux/lib/libzsummerX_d.a and b/depends_linux/lib/libzsummerX_d.a differ diff --git a/depends_win/include/proto4z/proto4z.h b/depends_win/include/proto4z/proto4z.h index b1fd3e5e..b0fa69c8 100644 --- a/depends_win/include/proto4z/proto4z.h +++ b/depends_win/include/proto4z/proto4z.h @@ -9,7 +9,7 @@ * * =============================================================================== * - * Copyright (C) 2013-2014 YaweiZhang . + * Copyright (C) 2013-2015 YaweiZhang . * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -132,35 +132,18 @@ enum ZSummer_EndianType //|----- header -----|------- body --------| //|--packlen-|-protoID--|------- body --------| -//|---------------------------------------------| //PackLenIsContainHead -// |-----------------------| //not PackLenIsContainHead +typedef unsigned int Integer; +typedef unsigned short ProtoInteger; -struct DefaultStreamHeadTraits -{ - typedef unsigned short Integer; //User-Defined Integer Type must in [unsigned char, unsigned short, unsigned int, unsigned long long]. - typedef unsigned short ProtoInteger; - const static Integer MaxPackLen = (Integer)-1; //User-Defined. example: Integer = unsigned short(-1) ==>(65535) - const static bool PackLenIsContainHead = true; //User-Defined - const static ZSummer_EndianType EndianType = LittleEndian;//User-Defined -}; - -//protocol Traits example, User-Defined -struct TestBigStreamHeadTraits -{ - typedef unsigned int Integer; - typedef unsigned short ProtoInteger; - const static Integer MaxPackLen = -1; - const static bool PackLenIsContainHead = false; - const static ZSummer_EndianType EndianType = BigEndian; -}; +const static Integer MaxPackLen = 1024*1024; //stream translate to Integer with endian type. -template +template Integer streamToInteger(const char stream[sizeof(Integer)]); //integer translate to stream with endian type. -template +template void integerToStream(Integer integer, char *stream); //!get runtime local endian type. @@ -183,10 +166,8 @@ enum INTEGRITY_RET_TYPE //! first: IRT_SUCCESS data integrity. second: current integrity data lenght. //! first: IRT_SHORTAGE data not integrity. second: shortage lenght. //! first: IRT_CORRUPTION data corruption. second: data lenght -template -inline std::pair -checkBuffIntegrity(const char * buff, typename StreamHeadTrait::Integer curBuffLen, -typename StreamHeadTrait::Integer maxBuffLen /*= StreamHeadTrait::MaxPackLen*/); +inline std::pair +checkBuffIntegrity(const char * buff, Integer curBuffLen, Integer maxBuffLen /*= MaxPackLen*/); @@ -195,17 +176,13 @@ typename StreamHeadTrait::Integer maxBuffLen /*= StreamHeadTrait::MaxPackLen*/); ////////////////////////////////////////////////////////////////////////// //StreamHeadTrait: User-Defined like DefaultStreamHeadTrait -template > + class WriteStream { public: - typedef typename StreamHeadTrait::Integer Integer; - typedef typename StreamHeadTrait::ProtoInteger ProtoInteger; - - //! bNoWrite : if true then WriteStream will not do any write operation. //! attachData : Attach to the existing memory. - WriteStream(ProtoInteger pID, char * attachData = NULL, Integer maxStreamLen = 0, bool bNoWrite = false); + inline WriteStream(ProtoInteger pID, char * attachData = NULL, Integer maxStreamLen = 0, bool bNoWrite = false); ~WriteStream(){} public: //get total stream buff, the pointer must be used immediately. @@ -219,22 +196,22 @@ class WriteStream inline Integer getStreamBodyLen(){ return _cursor - _headLen; } //write original data. - inline WriteStream & appendOriginalData(const void * data, Integer unit); - - - inline WriteStream & operator << (bool data) { return writeSimpleData(data); } - inline WriteStream & operator << (char data) { return writeSimpleData(data); } - inline WriteStream & operator << (unsigned char data) { return writeSimpleData(data); } - inline WriteStream & operator << (short data) { return writeIntegerData(data); } - inline WriteStream & operator << (unsigned short data) { return writeIntegerData(data); } - inline WriteStream & operator << (int data) { return writeIntegerData(data); } - inline WriteStream & operator << (unsigned int data) { return writeIntegerData(data); } - inline WriteStream & operator << (long data) { return writeIntegerData((long long)data); } - inline WriteStream & operator << (unsigned long data) { return writeIntegerData((unsigned long long)data); } - inline WriteStream & operator << (long long data) { return writeIntegerData(data); } - inline WriteStream & operator << (unsigned long long data) { return writeIntegerData(data); } - inline WriteStream & operator << (float data) { return writeIntegerData(data); } - inline WriteStream & operator << (double data) { return writeIntegerData(data); } + inline WriteStream & appendOriginalData(const void * data, Integer unit); + inline WriteStream & fixOriginalData(Integer offset, Integer unit); + + inline WriteStream & operator << (bool data) { return writeSimpleData(data); } + inline WriteStream & operator << (char data) { return writeSimpleData(data); } + inline WriteStream & operator << (unsigned char data) { return writeSimpleData(data); } + inline WriteStream & operator << (short data) { return writeIntegerData(data); } + inline WriteStream & operator << (unsigned short data) { return writeIntegerData(data); } + inline WriteStream & operator << (int data) { return writeIntegerData(data); } + inline WriteStream & operator << (unsigned int data) { return writeIntegerData(data); } + inline WriteStream & operator << (long data) { return writeIntegerData((long long)data); } + inline WriteStream & operator << (unsigned long data) { return writeIntegerData((unsigned long long)data); } + inline WriteStream & operator << (long long data) { return writeIntegerData(data); } + inline WriteStream & operator << (unsigned long long data) { return writeIntegerData(data); } + inline WriteStream & operator << (float data) { return writeIntegerData(data); } + inline WriteStream & operator << (double data) { return writeIntegerData(data); } protected: //! check move cursor is valid. if invalid then throw exception. @@ -245,14 +222,14 @@ class WriteStream //! write integer data with endian type. template - inline WriteStream & writeIntegerData(T t); + inline WriteStream & writeIntegerData(T t); //! write some types of data with out endian type. It's relative to writeIntegerData method. template - inline WriteStream & writeSimpleData(T t); + inline WriteStream & writeSimpleData(T t); private: - std::basic_string, _Alloc > _data; //! If not attach any memory, class WriteStream will used this to managing memory. + std::string _data; //! If not attach any memory, class WriteStream will used this to managing memory. char * _attachData;//! attach memory pointer Integer _maxStreamLen; //! can write max size Integer _cursor; //! current move cursor. @@ -265,14 +242,11 @@ class WriteStream //class ReadStream: De-serialization the specified data from byte stream. ////////////////////////////////////////////////////////////////////////// -//StreamHeadTrait: User-Defined like DefaultStreamHeadTrait -template + class ReadStream { public: - typedef typename StreamHeadTrait::Integer Integer; - typedef typename StreamHeadTrait::ProtoInteger ProtoInteger; - ReadStream(const char *attachData, Integer attachDataLen, bool isHaveHeader = true); + inline ReadStream(const char *attachData, Integer attachDataLen, bool isHaveHeader = true); ~ReadStream(){} public: //reset cursor @@ -299,25 +273,25 @@ class ReadStream inline void skipOriginalData(Integer unit); - inline ReadStream & operator >> (bool & data) { return readSimpleData(data); } - inline ReadStream & operator >> (char & data) { return readSimpleData(data); } - inline ReadStream & operator >> (unsigned char & data) { return readSimpleData(data); } - inline ReadStream & operator >> (short & data) { return readIntegerData(data); } - inline ReadStream & operator >> (unsigned short & data) { return readIntegerData(data); } - inline ReadStream & operator >> (int & data) { return readIntegerData(data); } - inline ReadStream & operator >> (unsigned int & data) { return readIntegerData(data); } - inline ReadStream & operator >> (long & data){ long long tmp = 0;ReadStream & ret = readIntegerData(tmp);data =(long) tmp;return ret;} - inline ReadStream & operator >> (unsigned long & data){ unsigned long long tmp = 0;ReadStream & ret = readIntegerData(tmp);data = (unsigned long)tmp;return ret;} - inline ReadStream & operator >> (long long & data) { return readIntegerData(data); } - inline ReadStream & operator >> (unsigned long long & data) { return readIntegerData(data); } - inline ReadStream & operator >> (float & data) { return readIntegerData(data); } - inline ReadStream & operator >> (double & data) { return readIntegerData(data); } + inline ReadStream & operator >> (bool & data) { return readSimpleData(data); } + inline ReadStream & operator >> (char & data) { return readSimpleData(data); } + inline ReadStream & operator >> (unsigned char & data) { return readSimpleData(data); } + inline ReadStream & operator >> (short & data) { return readIntegerData(data); } + inline ReadStream & operator >> (unsigned short & data) { return readIntegerData(data); } + inline ReadStream & operator >> (int & data) { return readIntegerData(data); } + inline ReadStream & operator >> (unsigned int & data) { return readIntegerData(data); } + inline ReadStream & operator >> (long & data){ long long tmp = 0;ReadStream & ret = readIntegerData(tmp);data =(long) tmp;return ret;} + inline ReadStream & operator >> (unsigned long & data){ unsigned long long tmp = 0;ReadStream & ret = readIntegerData(tmp);data = (unsigned long)tmp;return ret;} + inline ReadStream & operator >> (long long & data) { return readIntegerData(data); } + inline ReadStream & operator >> (unsigned long long & data) { return readIntegerData(data); } + inline ReadStream & operator >> (float & data) { return readIntegerData(data); } + inline ReadStream & operator >> (double & data) { return readIntegerData(data); } protected: inline void checkMoveCursor(Integer unit); template - inline ReadStream & readIntegerData(T & t); + inline ReadStream & readIntegerData(T & t); template - inline ReadStream & readSimpleData(T & t); + inline ReadStream & readSimpleData(T & t); private: const char * _attachData; @@ -333,29 +307,29 @@ class ReadStream ////////////////////////////////////////////////////////////////////////// //write c-style string -template -inline WriteStream & operator << (WriteStream & ws, const char *const data) + +inline WriteStream & operator << (WriteStream & ws, const char *const data) { - typename StreamHeadTrait::Integer len = (typename StreamHeadTrait::Integer)strlen(data); + Integer len = (Integer)strlen(data); ws << len; ws.appendOriginalData(data, len); return ws; } //write std::string -template -inline WriteStream & operator << (WriteStream & ws, const std::basic_string & data) +template +inline WriteStream & operator << (WriteStream & ws, const std::basic_string & data) { - typename StreamHeadTrait::Integer len = (typename StreamHeadTrait::Integer)data.length(); + Integer len = (Integer)data.length(); ws << len; ws.appendOriginalData(data.c_str(), len); return ws; } //read std::string -template -inline ReadStream & operator >> (ReadStream & rs, std::basic_string & data) +template +inline ReadStream & operator >> (ReadStream & rs, std::basic_string & data) { - typename StreamHeadTrait::Integer len = 0; + Integer len = 0; rs >> len; data.assign(rs.peekOriginalData(len), len); rs.skipOriginalData(len); @@ -364,10 +338,10 @@ inline ReadStream & operator >> (ReadStream & //std::vector -template -inline WriteStream & operator << (WriteStream & ws, const std::vector & vct) +template +inline WriteStream & operator << (WriteStream & ws, const std::vector & vct) { - ws << (typename StreamHeadTrait::Integer)vct.size(); + ws << (Integer)vct.size(); for (typename std::vector::const_iterator iter = vct.begin(); iter != vct.end(); ++iter) { ws << *iter; @@ -375,12 +349,12 @@ inline WriteStream & operator << (WriteStream return ws; } -template -inline ReadStream & operator >> (ReadStream & rs, std::vector & vct) +template +inline ReadStream & operator >> (ReadStream & rs, std::vector & vct) { - typename StreamHeadTrait::Integer totalCount = 0; + Integer totalCount = 0; rs >> totalCount; - for (typename StreamHeadTrait::Integer i = 0; i < totalCount; ++i) + for (Integer i = 0; i < totalCount; ++i) { T t; rs >> t; @@ -390,10 +364,10 @@ inline ReadStream & operator >> (ReadStream & } //std::set -template -inline WriteStream & operator << (WriteStream & ws, const std::set & k) +template +inline WriteStream & operator << (WriteStream & ws, const std::set & k) { - ws << (typename StreamHeadTrait::Integer)k.size(); + ws << (Integer)k.size(); for (typename std::set::const_iterator iter = k.begin(); iter != k.end(); ++iter) { ws << *iter; @@ -401,12 +375,12 @@ inline WriteStream & operator << (WriteStream return ws; } -template -inline ReadStream & operator >> (ReadStream & rs, std::set & k) +template +inline ReadStream & operator >> (ReadStream & rs, std::set & k) { - typename StreamHeadTrait::Integer totalCount = 0; + Integer totalCount = 0; rs >> totalCount; - for (typename StreamHeadTrait::Integer i = 0; i < totalCount; ++i) + for (Integer i = 0; i < totalCount; ++i) { Key t; rs >> t; @@ -416,10 +390,10 @@ inline ReadStream & operator >> (ReadStream & } //std::multiset -template -inline WriteStream & operator << (WriteStream & ws, const std::multiset & k) +template +inline WriteStream & operator << (WriteStream & ws, const std::multiset & k) { - ws << (typename StreamHeadTrait::Integer)k.size(); + ws << (Integer)k.size(); for (typename std::multiset::const_iterator iter = k.begin(); iter != k.end(); ++iter) { ws << *iter; @@ -427,12 +401,12 @@ inline WriteStream & operator << (WriteStream return ws; } -template -inline ReadStream & operator >> (ReadStream & rs, std::multiset & k) +template +inline ReadStream & operator >> (ReadStream & rs, std::multiset & k) { - typename StreamHeadTrait::Integer totalCount = 0; + Integer totalCount = 0; rs >> totalCount; - for (typename StreamHeadTrait::Integer i = 0; i < totalCount; ++i) + for (Integer i = 0; i < totalCount; ++i) { Key t; rs >> t; @@ -442,10 +416,10 @@ inline ReadStream & operator >> (ReadStream & } //std::map -template -inline WriteStream & operator << (WriteStream & ws, const std::map & kv) +template +inline WriteStream & operator << (WriteStream & ws, const std::map & kv) { - ws << (typename StreamHeadTrait::Integer)kv.size(); + ws << (Integer)kv.size(); for (typename std::map::const_iterator iter = kv.begin(); iter != kv.end(); ++iter) { ws << iter->first; @@ -454,12 +428,12 @@ inline WriteStream & operator << (WriteStream return ws; } -template -inline ReadStream & operator >> (ReadStream & rs, std::map & kv) +template +inline ReadStream & operator >> (ReadStream & rs, std::map & kv) { - typename StreamHeadTrait::Integer totalCount = 0; + Integer totalCount = 0; rs >> totalCount; - for (typename StreamHeadTrait::Integer i = 0; i < totalCount; ++i) + for (Integer i = 0; i < totalCount; ++i) { std::pair pr; rs >> pr.first; @@ -470,10 +444,10 @@ inline ReadStream & operator >> (ReadStream & } //std::multimap -template -inline WriteStream & operator << (WriteStream & ws, const std::multimap & kv) +template +inline WriteStream & operator << (WriteStream & ws, const std::multimap & kv) { - ws << (typename StreamHeadTrait::Integer)kv.size(); + ws << (Integer)kv.size(); for (typename std::multimap::const_iterator iter = kv.begin(); iter != kv.end(); ++iter) { ws << iter->first; @@ -482,12 +456,12 @@ inline WriteStream & operator << (WriteStream return ws; } -template -inline ReadStream & operator >> (ReadStream & rs, std::multimap & kv) +template +inline ReadStream & operator >> (ReadStream & rs, std::multimap & kv) { - typename StreamHeadTrait::Integer totalCount = 0; + Integer totalCount = 0; rs >> totalCount; - for (typename StreamHeadTrait::Integer i = 0; i < totalCount; ++i) + for (Integer i = 0; i < totalCount; ++i) { std::pair pr; rs >> pr.first; @@ -499,10 +473,10 @@ inline ReadStream & operator >> (ReadStream & //std::list -template -inline WriteStream & operator << (WriteStream & ws, const std::list & l) +template +inline WriteStream & operator << (WriteStream & ws, const std::list & l) { - ws << (typename StreamHeadTrait::Integer)l.size(); + ws << (Integer)l.size(); for (typename std::list::const_iterator iter = l.begin(); iter != l.end(); ++iter) { ws << *iter; @@ -510,12 +484,12 @@ inline WriteStream & operator << (WriteStream return ws; } -template -inline ReadStream & operator >> (ReadStream & rs, std::list & l) +template +inline ReadStream & operator >> (ReadStream & rs, std::list & l) { - typename StreamHeadTrait::Integer totalCount = 0; + Integer totalCount = 0; rs >> totalCount; - for (typename StreamHeadTrait::Integer i = 0; i < totalCount; ++i) + for (Integer i = 0; i < totalCount; ++i) { Value t; rs >> t; @@ -524,10 +498,10 @@ inline ReadStream & operator >> (ReadStream & return rs; } //std::deque -template -inline WriteStream & operator << (WriteStream & ws, const std::deque & l) +template +inline WriteStream & operator << (WriteStream & ws, const std::deque & l) { - ws << (typename StreamHeadTrait::Integer)l.size(); + ws << (Integer)l.size(); for (typename std::deque::const_iterator iter = l.begin(); iter != l.end(); ++iter) { ws << *iter; @@ -535,12 +509,12 @@ inline WriteStream & operator << (WriteStream return ws; } -template -inline ReadStream & operator >> (ReadStream & rs, std::deque & l) +template +inline ReadStream & operator >> (ReadStream & rs, std::deque & l) { - typename StreamHeadTrait::Integer totalCount = 0; + Integer totalCount = 0; rs >> totalCount; - for (typename StreamHeadTrait::Integer i = 0; i < totalCount; ++i) + for (Integer i = 0; i < totalCount; ++i) { Value t; rs >> t; @@ -564,7 +538,7 @@ inline ZSummer_EndianType __localEndianType() return BigEndian; } -template +template Integer streamToInteger(const char stream[sizeof(Integer)]) { unsigned short integerLen = sizeof(Integer); @@ -575,7 +549,7 @@ Integer streamToInteger(const char stream[sizeof(Integer)]) } else { - if (StreamHeadTrait::EndianType != __localEndianType()) + if (LittleEndian != __localEndianType()) { unsigned char *dst = (unsigned char*)&integer; unsigned char *src = (unsigned char*)stream + integerLen; @@ -593,7 +567,7 @@ Integer streamToInteger(const char stream[sizeof(Integer)]) return integer; } -template +template void integerToStream(Integer integer, char *stream) { unsigned short integerLen = sizeof(Integer); @@ -603,7 +577,7 @@ void integerToStream(Integer integer, char *stream) } else { - if (StreamHeadTrait::EndianType != __localEndianType()) + if (LittleEndian != __localEndianType()) { unsigned char *src = (unsigned char*)&integer + integerLen; unsigned char *dst = (unsigned char*)stream; @@ -625,27 +599,18 @@ void integerToStream(Integer integer, char *stream) ////////////////////////////////////////////////////////////////////////// -template -inline std::pair checkBuffIntegrity(const char * buff, typename StreamHeadTrait::Integer curBuffLen, typename StreamHeadTrait::Integer maxBuffLen) + +inline std::pair checkBuffIntegrity(const char * buff, Integer curBuffLen, Integer maxBuffLen) { //! 检查包头是否完整 - unsigned short headLen = (unsigned short)sizeof(typename StreamHeadTrait::Integer) + (unsigned short)sizeof(typename StreamHeadTrait::ProtoInteger); + unsigned short headLen = (unsigned short)sizeof(Integer) + (unsigned short)sizeof(ProtoInteger); if (curBuffLen < headLen) { return std::make_pair(IRT_SHORTAGE, headLen - curBuffLen); } //! 获取包长度 - typename StreamHeadTrait::Integer packLen = streamToInteger(buff); - if (!StreamHeadTrait::PackLenIsContainHead) - { - typename StreamHeadTrait::Integer oldInteger = packLen; - packLen += headLen; - if (packLen < oldInteger) //over range - { - return std::make_pair(IRT_CORRUPTION, curBuffLen); - } - } + Integer packLen = streamToInteger(buff); //! check if (packLen > maxBuffLen) @@ -669,8 +634,8 @@ inline std::pair checkBuf //! implement ////////////////////////////////////////////////////////////////////////// -template -WriteStream::WriteStream(ProtoInteger pID, char * attachData, Integer maxStreamLen, bool bNoWrite) + +inline WriteStream::WriteStream(ProtoInteger pID, char * attachData, Integer maxStreamLen, bool bNoWrite) { _pID = pID; _attachData = attachData; @@ -684,32 +649,28 @@ WriteStream::WriteStream(ProtoInteger pID, char * at _data.reserve(1200); _data.resize(_cursor, '\0'); } - if (_maxStreamLen < _headLen || _maxStreamLen > StreamHeadTrait::MaxPackLen) + if (_maxStreamLen < _headLen || _maxStreamLen > MaxPackLen) { - _maxStreamLen = StreamHeadTrait::MaxPackLen; + _maxStreamLen = MaxPackLen; } //write header - Integer packLen = 0; - if (StreamHeadTrait::PackLenIsContainHead) - { - packLen = _headLen; - } + Integer packLen = _headLen; if (_attachData) { - integerToStream(packLen, &_attachData[0]); - integerToStream(pID, &_attachData[sizeof(Integer)]); + integerToStream(packLen, &_attachData[0]); + integerToStream(pID, &_attachData[sizeof(Integer)]); } else { - integerToStream(packLen, &_data[0]); - integerToStream(pID, &_data[sizeof(Integer)]); + integerToStream(packLen, &_data[0]); + integerToStream(pID, &_data[sizeof(Integer)]); } } -template -inline void WriteStream::checkMoveCursor(Integer unit) + +inline void WriteStream::checkMoveCursor(Integer unit) { if (_maxStreamLen < _headLen) { @@ -729,32 +690,28 @@ inline void WriteStream::checkMoveCursor(Integer uni } } -template -inline void WriteStream::fixPackLen() + +inline void WriteStream::fixPackLen() { if (_isNoWrite) { return; } Integer packLen = _cursor; - if (!StreamHeadTrait::PackLenIsContainHead) - { - packLen -= _headLen; - } if (_attachData) { - integerToStream(packLen, &_attachData[0]); + integerToStream(packLen, &_attachData[0]); } else { - integerToStream(packLen, &_data[0]); + integerToStream(packLen, &_data[0]); } } -template -inline char* WriteStream::getStream() + +inline char* WriteStream::getStream() { if (_isNoWrite) { @@ -771,8 +728,8 @@ inline char* WriteStream::getStream() return NULL; } -template -inline char* WriteStream::getStreamBody() + +inline char* WriteStream::getStreamBody() { if (_isNoWrite) { @@ -789,8 +746,8 @@ inline char* WriteStream::getStreamBody() return NULL; } -template -inline WriteStream & WriteStream::appendOriginalData(const void * data, Integer unit) + +inline WriteStream & WriteStream::appendOriginalData(const void * data, Integer unit) { checkMoveCursor(unit); if (!_isNoWrite) @@ -808,9 +765,24 @@ inline WriteStream & WriteStream::a fixPackLen(); return *this; } - -template template -inline WriteStream & WriteStream::writeIntegerData(T t) +inline WriteStream & WriteStream::fixOriginalData(Integer offset, Integer unit) +{ + if (offset + sizeof(Integer) > _cursor) + { + throw std::runtime_error("fixOriginalData over stream."); + } + if (_attachData) + { + integerToStream(unit, &_attachData[offset]); + } + else + { + integerToStream(unit, &_data[offset]); + } + return *this; +} + template +inline WriteStream & WriteStream::writeIntegerData(T t) { Integer unit = sizeof(T); checkMoveCursor(unit); @@ -818,14 +790,14 @@ inline WriteStream & WriteStream::w { if (_attachData) { - integerToStream(t, &_attachData[_cursor]); + integerToStream(t, &_attachData[_cursor]); } else { _data.append((const char*)&t, unit); - if (StreamHeadTrait::EndianType != __localEndianType()) + if (LittleEndian != __localEndianType()) { - integerToStream(t, &_data[_cursor]); + integerToStream(t, &_data[_cursor]); } } } @@ -835,8 +807,8 @@ inline WriteStream & WriteStream::w } -template template -inline WriteStream & WriteStream::writeSimpleData(T t) + template +inline WriteStream & WriteStream::writeSimpleData(T t) { Integer unit = sizeof(T); checkMoveCursor(unit); @@ -865,15 +837,15 @@ inline WriteStream & WriteStream::w ////////////////////////////////////////////////////////////////////////// //! implement ////////////////////////////////////////////////////////////////////////// -template -ReadStream::ReadStream(const char *attachData, Integer attachDataLen, bool isHaveHeader) + +inline ReadStream::ReadStream(const char *attachData, Integer attachDataLen, bool isHaveHeader) { _attachData = attachData; _maxDataLen = attachDataLen; _isHaveHeader = isHaveHeader; - if (_maxDataLen > StreamHeadTrait::MaxPackLen) + if (_maxDataLen > MaxPackLen) { - _maxDataLen = StreamHeadTrait::MaxPackLen; + _maxDataLen = MaxPackLen; } if (_maxDataLen < sizeof(Integer) + sizeof(ProtoInteger)) { @@ -883,12 +855,8 @@ ReadStream::ReadStream(const char *attachData, Integer attachDa if (_isHaveHeader) { _cursor = sizeof(Integer) + sizeof(ProtoInteger); - _pID = streamToInteger(&_attachData[sizeof(Integer)]); - Integer len = streamToInteger(&_attachData[0]); - if (!StreamHeadTrait::PackLenIsContainHead) - { - len += _cursor; - } + _pID = streamToInteger(&_attachData[sizeof(Integer)]); + Integer len = streamToInteger(&_attachData[0]); if (len < _maxDataLen) // if stream invalid, ReadStream try read data as much as possible. { _maxDataLen = len; @@ -904,8 +872,7 @@ ReadStream::ReadStream(const char *attachData, Integer attachDa } -template -inline void ReadStream::resetMoveCursor() +inline void ReadStream::resetMoveCursor() { if (_isHaveHeader) { @@ -917,8 +884,8 @@ inline void ReadStream::resetMoveCursor() } } -template -inline void ReadStream::checkMoveCursor(Integer unit) + +inline void ReadStream::checkMoveCursor(Integer unit) { if (_cursor > _maxDataLen) { @@ -935,20 +902,20 @@ inline void ReadStream::checkMoveCursor(Integer unit) } -template -inline const char* ReadStream::getStream() + +inline const char* ReadStream::getStream() { return _attachData; } -template -inline typename ReadStream::Integer ReadStream::getStreamLen() + +inline Integer ReadStream::getStreamLen() { return _maxDataLen; } -template -inline const char* ReadStream::getStreamBody() + +inline const char* ReadStream::getStreamBody() { if (_isHaveHeader) { @@ -957,8 +924,8 @@ inline const char* ReadStream::getStreamBody() return _attachData; } -template -inline typename ReadStream::Integer ReadStream::getStreamBodyLen() + +inline Integer ReadStream::getStreamBodyLen() { if (_isHaveHeader) { @@ -967,29 +934,29 @@ inline typename ReadStream::Integer ReadStream return getStreamLen(); } -template -inline const char* ReadStream::getStreamUnread() + +inline const char* ReadStream::getStreamUnread() { return &_attachData[_cursor]; } -template -typename ReadStream::Integer ReadStream::getStreamUnreadLen() + +inline Integer ReadStream::getStreamUnreadLen() { return getStreamLen() - _cursor; } -template template -inline ReadStream & ReadStream::readIntegerData(T & t) + template +inline ReadStream & ReadStream::readIntegerData(T & t) { Integer unit = sizeof(T); checkMoveCursor(unit); - t = streamToInteger(&_attachData[_cursor]); + t = streamToInteger(&_attachData[_cursor]); _cursor += unit; return * this; } -template template -inline ReadStream & ReadStream::readSimpleData(T & t) + template +inline ReadStream & ReadStream::readSimpleData(T & t) { Integer unit = sizeof(T); checkMoveCursor(unit); @@ -997,14 +964,14 @@ inline ReadStream & ReadStream::readSimpleData _cursor += unit; return * this; } -template -inline const char * ReadStream::peekOriginalData(Integer unit) + +inline const char * ReadStream::peekOriginalData(Integer unit) { checkMoveCursor(unit); return &_attachData[_cursor]; } -template -inline void ReadStream::skipOriginalData(Integer unit) + +inline void ReadStream::skipOriginalData(Integer unit) { checkMoveCursor(unit); _cursor += unit; diff --git a/depends_win/include/zsummerX/frame/FrameHeader.h b/depends_win/include/zsummerX/frame/FrameHeader.h index a77792fc..1699fe12 100644 --- a/depends_win/include/zsummerX/frame/FrameHeader.h +++ b/depends_win/include/zsummerX/frame/FrameHeader.h @@ -148,8 +148,8 @@ struct FrameStreamTraits }; -typedef zsummer::proto4z::ReadStream ReadStreamPack; -typedef zsummer::proto4z::WriteStream WriteStreamPack; +typedef zsummer::proto4z::ReadStream ReadStreamPack; +typedef zsummer::proto4z::WriteStream WriteStreamPack; //!register message with original net pack, if return false other register will not receive this message. diff --git a/depends_win/lib/log4z_win64.lib b/depends_win/lib/log4z_win64.lib index e27909a0..0f23dde5 100644 Binary files a/depends_win/lib/log4z_win64.lib and b/depends_win/lib/log4z_win64.lib differ diff --git a/depends_win/lib/log4z_win64.pdb b/depends_win/lib/log4z_win64.pdb index 872b3843..ccaae525 100644 Binary files a/depends_win/lib/log4z_win64.pdb and b/depends_win/lib/log4z_win64.pdb differ diff --git a/depends_win/lib/log4z_win64d.idb b/depends_win/lib/log4z_win64d.idb index b7de662c..762328da 100644 Binary files a/depends_win/lib/log4z_win64d.idb and b/depends_win/lib/log4z_win64d.idb differ diff --git a/depends_win/lib/log4z_win64d.lib b/depends_win/lib/log4z_win64d.lib index 9b2f3d01..e7a31fd0 100644 Binary files a/depends_win/lib/log4z_win64d.lib and b/depends_win/lib/log4z_win64d.lib differ diff --git a/depends_win/lib/log4z_win64d.pdb b/depends_win/lib/log4z_win64d.pdb index 3fc633fb..f9be6e07 100644 Binary files a/depends_win/lib/log4z_win64d.pdb and b/depends_win/lib/log4z_win64d.pdb differ diff --git a/depends_win/lib/zsummerX_win64.lib b/depends_win/lib/zsummerX_win64.lib index dfc80e64..6d9f05cd 100644 Binary files a/depends_win/lib/zsummerX_win64.lib and b/depends_win/lib/zsummerX_win64.lib differ diff --git a/depends_win/lib/zsummerX_win64d.idb b/depends_win/lib/zsummerX_win64d.idb index e140e771..3e69b53b 100644 Binary files a/depends_win/lib/zsummerX_win64d.idb and b/depends_win/lib/zsummerX_win64d.idb differ diff --git a/depends_win/lib/zsummerX_win64d.lib b/depends_win/lib/zsummerX_win64d.lib index 8f776be8..0d77524b 100644 Binary files a/depends_win/lib/zsummerX_win64d.lib and b/depends_win/lib/zsummerX_win64d.lib differ diff --git a/depends_win/lib/zsummerx_win64.pdb b/depends_win/lib/zsummerx_win64.pdb index f8fa9484..fd14f9ec 100644 Binary files a/depends_win/lib/zsummerx_win64.pdb and b/depends_win/lib/zsummerx_win64.pdb differ diff --git a/depends_win/lib/zsummerx_win64d.pdb b/depends_win/lib/zsummerx_win64d.pdb index dbecb707..bfe14b7b 100644 Binary files a/depends_win/lib/zsummerx_win64d.pdb and b/depends_win/lib/zsummerx_win64d.pdb differ diff --git a/protocol/C++/ProtoCommon.h b/protocol/C++/ProtoCommon.h index aaf03400..72936ac3 100644 --- a/protocol/C++/ProtoCommon.h +++ b/protocol/C++/ProtoCommon.h @@ -1,70 +1,103 @@ - #ifndef _PROTOCOMMON_H_ - #define _PROTOCOMMON_H_ - - const unsigned short BEC_SUCCESS = 0; //成功 - const unsigned short BEC_UNKOWN_ERROR = 1; //未知错误 - const unsigned short BEC_INNER_ERROR = 2; //内部错误 - const unsigned short BEC_DB_ERROR = 3; //数据库错误 - const unsigned short BEC_PARAM_DENIED = 4; //非法参数 - const unsigned short BEC_PERMISSION_DENIED = 5; //权限非法 - const unsigned short BEC_SYSTEM_ERROR = 6; //系统错误 - const unsigned short BEC_INVALIDE_USERID = 50; //用户ID无效 - const unsigned short BEC_AUTH_USER_NOT_EXIST = 60; //认证错误:用户不存在 - const unsigned short BEC_AUTH_PASSWD_INCORRECT = 61; //认证错误:密码错误 - const unsigned short BEC_AUTH_ING = 62; //认证错误:正在认证中... - const unsigned short BEC_AUTH_AREADY_AUTH = 63; //认证错误:已认证 - const unsigned short BEC_AUTH_LIMITE_COUNT = 64; //认证错误:认证次数超过限制 - - struct UserInfo //用户信息 - { - unsigned long long uid; - std::string nickName; //用户昵称 - unsigned int iconID; //头像 - unsigned int level; //等级 - unsigned int diamond; //当前剩余的充值钻石 - unsigned int hisotryDiamond; //历史充值钻石总额 - unsigned int giftDiamond; //当前剩余的赠送钻石 - unsigned int joinTime; //加入时间 - UserInfo() - { - uid = 0; - iconID = 0; - level = 0; - diamond = 0; - hisotryDiamond = 0; - giftDiamond = 0; - joinTime = 0; - } - }; - template - T & operator << (T & t, const UserInfo & data) - { - t << data.uid; - t << data.nickName; - t << data.iconID; - t << data.level; - t << data.diamond; - t << data.hisotryDiamond; - t << data.giftDiamond; - t << data.joinTime; - return t; - } - template - T & operator >> (T & t, UserInfo & data) - { - t >> data.uid; - t >> data.nickName; - t >> data.iconID; - t >> data.level; - t >> data.diamond; - t >> data.hisotryDiamond; - t >> data.giftDiamond; - t >> data.joinTime; - return t; - } - - typedef std::vector UserInfoVct; - - #endif - \ No newline at end of file +#ifndef _PROTOCOMMON_H_ +#define _PROTOCOMMON_H_ + +const unsigned short BEC_SUCCESS = 0; //成功 +const unsigned short BEC_UNKOWN_ERROR = 1; //未知错误 +const unsigned short BEC_INNER_ERROR = 2; //内部错误 +const unsigned short BEC_DB_ERROR = 3; //数据库错误 +const unsigned short BEC_PARAM_DENIED = 4; //非法参数 +const unsigned short BEC_PERMISSION_DENIED = 5; //权限非法 +const unsigned short BEC_SYSTEM_ERROR = 6; //系统错误 +const unsigned short BEC_INVALIDE_USERID = 50; //用户ID无效 +const unsigned short BEC_AUTH_USER_NOT_EXIST = 60; //认证错误:用户不存在 +const unsigned short BEC_AUTH_PASSWD_INCORRECT = 61; //认证错误:密码错误 +const unsigned short BEC_AUTH_ING = 62; //认证错误:正在认证中... +const unsigned short BEC_AUTH_AREADY_AUTH = 63; //认证错误:已认证 +const unsigned short BEC_AUTH_LIMITE_COUNT = 64; //认证错误:认证次数超过限制 + +struct UserInfo //用户信息 +{ + unsigned long long uid; + std::string nickName; //用户昵称 + unsigned int iconID; //头像 + unsigned int level; //等级 + unsigned int diamond; //当前剩余的充值钻石 + unsigned int hisotryDiamond; //历史充值钻石总额 + unsigned int giftDiamond; //当前剩余的赠送钻石 + unsigned int joinTime; //加入时间 + UserInfo() + { + uid = 0; + iconID = 0; + level = 0; + diamond = 0; + hisotryDiamond = 0; + giftDiamond = 0; + joinTime = 0; + } +}; +inline zsummer::proto4z::WriteStream & operator << (zsummer::proto4z::WriteStream & ws, const UserInfo & data) +{ + unsigned long long tag = 255ULL; + ws << (zsummer::proto4z::Integer)0; + zsummer::proto4z::Integer offset = ws.getStreamLen(); + ws << tag; + ws << data.uid; + ws << data.nickName; + ws << data.iconID; + ws << data.level; + ws << data.diamond; + ws << data.hisotryDiamond; + ws << data.giftDiamond; + ws << data.joinTime; + ws.fixOriginalData(offset - 4, ws.getStreamLen() - offset); + return ws; +} +inline zsummer::proto4z::ReadStream & operator >> (zsummer::proto4z::ReadStream & rs, UserInfo & data) +{ + zsummer::proto4z::Integer sttLen = 0; + rs >> sttLen; + zsummer::proto4z::Integer cursor = rs.getStreamUnreadLen(); + unsigned long long tag = 0; + rs >> tag; + if ( (1ULL << 0) & tag) + { + rs >> data.uid; + } + if ( (1ULL << 1) & tag) + { + rs >> data.nickName; + } + if ( (1ULL << 2) & tag) + { + rs >> data.iconID; + } + if ( (1ULL << 3) & tag) + { + rs >> data.level; + } + if ( (1ULL << 4) & tag) + { + rs >> data.diamond; + } + if ( (1ULL << 5) & tag) + { + rs >> data.hisotryDiamond; + } + if ( (1ULL << 6) & tag) + { + rs >> data.giftDiamond; + } + if ( (1ULL << 7) & tag) + { + rs >> data.joinTime; + } + cursor = cursor - rs.getStreamUnreadLen(); + rs.skipOriginalData(sttLen - cursor); + return rs; +} + +typedef std::vector UserInfoVct; + +#endif diff --git a/protocol/C++/ProtoLogin.h b/protocol/C++/ProtoLogin.h index a69388e9..ace370bf 100644 --- a/protocol/C++/ProtoLogin.h +++ b/protocol/C++/ProtoLogin.h @@ -1,164 +1,259 @@ - #ifndef _PROTOLOGIN_H_ - #define _PROTOLOGIN_H_ - - - const unsigned short ID_C2LS_LoginReq = 102; //登录请求 - struct C2LS_LoginReq //登录请求 - { - std::string user; - std::string passwd; - inline unsigned short GetProtoID() { return 102;} - inline std::string GetProtoName() { return "ID_C2LS_LoginReq";} - }; - template - T & operator << (T & t, const C2LS_LoginReq & data) - { - t << data.user; - t << data.passwd; - return t; - } - template - T & operator >> (T & t, C2LS_LoginReq & data) - { - t >> data.user; - t >> data.passwd; - return t; - } - - const unsigned short ID_LS2C_LoginAck = 103; //登录结果 - struct LS2C_LoginAck //登录结果 - { - unsigned int retCode; - unsigned int needCreateUser; - UserInfo info; //认证成功但昵称为空 则说明需要创建用户信息 - LS2C_LoginAck() - { - retCode = 0; - needCreateUser = 0; - } - inline unsigned short GetProtoID() { return 103;} - inline std::string GetProtoName() { return "ID_LS2C_LoginAck";} - }; - template - T & operator << (T & t, const LS2C_LoginAck & data) - { - t << data.retCode; - t << data.needCreateUser; - t << data.info; - return t; - } - template - T & operator >> (T & t, LS2C_LoginAck & data) - { - t >> data.retCode; - t >> data.needCreateUser; - t >> data.info; - return t; - } - - const unsigned short ID_C2LS_CreateUserReq = 108; //填写用户信息 - struct C2LS_CreateUserReq //填写用户信息 - { - std::string nickName; //昵称 - unsigned int iconID; //头像 - C2LS_CreateUserReq() - { - iconID = 0; - } - inline unsigned short GetProtoID() { return 108;} - inline std::string GetProtoName() { return "ID_C2LS_CreateUserReq";} - }; - template - T & operator << (T & t, const C2LS_CreateUserReq & data) - { - t << data.nickName; - t << data.iconID; - return t; - } - template - T & operator >> (T & t, C2LS_CreateUserReq & data) - { - t >> data.nickName; - t >> data.iconID; - return t; - } - - const unsigned short ID_LS2C_CreateUserAck = 109; //返回 - struct LS2C_CreateUserAck //返回 - { - unsigned short retCode; - unsigned int needCreateUser; //nickname冲突需要重新创建 - UserInfo info; - LS2C_CreateUserAck() - { - retCode = 0; - needCreateUser = 0; - } - inline unsigned short GetProtoID() { return 109;} - inline std::string GetProtoName() { return "ID_LS2C_CreateUserAck";} - }; - template - T & operator << (T & t, const LS2C_CreateUserAck & data) - { - t << data.retCode; - t << data.needCreateUser; - t << data.info; - return t; - } - template - T & operator >> (T & t, LS2C_CreateUserAck & data) - { - t >> data.retCode; - t >> data.needCreateUser; - t >> data.info; - return t; - } - - const unsigned short ID_C2AS_ClientPulse = 106; //客户端存活脉冲 - struct C2AS_ClientPulse //客户端存活脉冲 - { - inline unsigned short GetProtoID() { return 106;} - inline std::string GetProtoName() { return "ID_C2AS_ClientPulse";} - }; - template - T & operator << (T & t, const C2AS_ClientPulse & data) - { - return t; - } - template - T & operator >> (T & t, C2AS_ClientPulse & data) - { - return t; - } - - const unsigned short ID_AS2C_ServerPulse = 107; //服务端存活脉冲 - struct AS2C_ServerPulse //服务端存活脉冲 - { - unsigned int timeStamp; //服务器当前UTC时间戳 - unsigned int timeTick; //服务器当前tick时间戳 毫秒, 服务启动时刻为0 - AS2C_ServerPulse() - { - timeStamp = 0; - timeTick = 0; - } - inline unsigned short GetProtoID() { return 107;} - inline std::string GetProtoName() { return "ID_AS2C_ServerPulse";} - }; - template - T & operator << (T & t, const AS2C_ServerPulse & data) - { - t << data.timeStamp; - t << data.timeTick; - return t; - } - template - T & operator >> (T & t, AS2C_ServerPulse & data) - { - t >> data.timeStamp; - t >> data.timeTick; - return t; - } - - #endif - \ No newline at end of file +#ifndef _PROTOLOGIN_H_ +#define _PROTOLOGIN_H_ + + +const unsigned short ID_C2LS_LoginReq = 102; //登录请求 +struct C2LS_LoginReq //登录请求 +{ + std::string user; + std::string passwd; + inline unsigned short GetProtoID() { return 102;} + inline std::string GetProtoName() { return "ID_C2LS_LoginReq";} +}; +inline zsummer::proto4z::WriteStream & operator << (zsummer::proto4z::WriteStream & ws, const C2LS_LoginReq & data) +{ + unsigned long long tag = 3ULL; + ws << (zsummer::proto4z::Integer)0; + zsummer::proto4z::Integer offset = ws.getStreamLen(); + ws << tag; + ws << data.user; + ws << data.passwd; + ws.fixOriginalData(offset - 4, ws.getStreamLen() - offset); + return ws; +} +inline zsummer::proto4z::ReadStream & operator >> (zsummer::proto4z::ReadStream & rs, C2LS_LoginReq & data) +{ + zsummer::proto4z::Integer sttLen = 0; + rs >> sttLen; + zsummer::proto4z::Integer cursor = rs.getStreamUnreadLen(); + unsigned long long tag = 0; + rs >> tag; + if ( (1ULL << 0) & tag) + { + rs >> data.user; + } + if ( (1ULL << 1) & tag) + { + rs >> data.passwd; + } + cursor = cursor - rs.getStreamUnreadLen(); + rs.skipOriginalData(sttLen - cursor); + return rs; +} + +const unsigned short ID_LS2C_LoginAck = 103; //登录结果 +struct LS2C_LoginAck //登录结果 +{ + unsigned int retCode; + unsigned int needCreateUser; + UserInfo info; //认证成功但昵称为空 则说明需要创建用户信息 + LS2C_LoginAck() + { + retCode = 0; + needCreateUser = 0; + } + inline unsigned short GetProtoID() { return 103;} + inline std::string GetProtoName() { return "ID_LS2C_LoginAck";} +}; +inline zsummer::proto4z::WriteStream & operator << (zsummer::proto4z::WriteStream & ws, const LS2C_LoginAck & data) +{ + unsigned long long tag = 7ULL; + ws << (zsummer::proto4z::Integer)0; + zsummer::proto4z::Integer offset = ws.getStreamLen(); + ws << tag; + ws << data.retCode; + ws << data.needCreateUser; + ws << data.info; + ws.fixOriginalData(offset - 4, ws.getStreamLen() - offset); + return ws; +} +inline zsummer::proto4z::ReadStream & operator >> (zsummer::proto4z::ReadStream & rs, LS2C_LoginAck & data) +{ + zsummer::proto4z::Integer sttLen = 0; + rs >> sttLen; + zsummer::proto4z::Integer cursor = rs.getStreamUnreadLen(); + unsigned long long tag = 0; + rs >> tag; + if ( (1ULL << 0) & tag) + { + rs >> data.retCode; + } + if ( (1ULL << 1) & tag) + { + rs >> data.needCreateUser; + } + if ( (1ULL << 2) & tag) + { + rs >> data.info; + } + cursor = cursor - rs.getStreamUnreadLen(); + rs.skipOriginalData(sttLen - cursor); + return rs; +} + +const unsigned short ID_C2LS_CreateUserReq = 108; //填写用户信息 +struct C2LS_CreateUserReq //填写用户信息 +{ + std::string nickName; //昵称 + unsigned int iconID; //头像 + C2LS_CreateUserReq() + { + iconID = 0; + } + inline unsigned short GetProtoID() { return 108;} + inline std::string GetProtoName() { return "ID_C2LS_CreateUserReq";} +}; +inline zsummer::proto4z::WriteStream & operator << (zsummer::proto4z::WriteStream & ws, const C2LS_CreateUserReq & data) +{ + unsigned long long tag = 3ULL; + ws << (zsummer::proto4z::Integer)0; + zsummer::proto4z::Integer offset = ws.getStreamLen(); + ws << tag; + ws << data.nickName; + ws << data.iconID; + ws.fixOriginalData(offset - 4, ws.getStreamLen() - offset); + return ws; +} +inline zsummer::proto4z::ReadStream & operator >> (zsummer::proto4z::ReadStream & rs, C2LS_CreateUserReq & data) +{ + zsummer::proto4z::Integer sttLen = 0; + rs >> sttLen; + zsummer::proto4z::Integer cursor = rs.getStreamUnreadLen(); + unsigned long long tag = 0; + rs >> tag; + if ( (1ULL << 0) & tag) + { + rs >> data.nickName; + } + if ( (1ULL << 1) & tag) + { + rs >> data.iconID; + } + cursor = cursor - rs.getStreamUnreadLen(); + rs.skipOriginalData(sttLen - cursor); + return rs; +} + +const unsigned short ID_LS2C_CreateUserAck = 109; //返回 +struct LS2C_CreateUserAck //返回 +{ + unsigned short retCode; + unsigned int needCreateUser; //nickname冲突需要重新创建 + UserInfo info; + LS2C_CreateUserAck() + { + retCode = 0; + needCreateUser = 0; + } + inline unsigned short GetProtoID() { return 109;} + inline std::string GetProtoName() { return "ID_LS2C_CreateUserAck";} +}; +inline zsummer::proto4z::WriteStream & operator << (zsummer::proto4z::WriteStream & ws, const LS2C_CreateUserAck & data) +{ + unsigned long long tag = 7ULL; + ws << (zsummer::proto4z::Integer)0; + zsummer::proto4z::Integer offset = ws.getStreamLen(); + ws << tag; + ws << data.retCode; + ws << data.needCreateUser; + ws << data.info; + ws.fixOriginalData(offset - 4, ws.getStreamLen() - offset); + return ws; +} +inline zsummer::proto4z::ReadStream & operator >> (zsummer::proto4z::ReadStream & rs, LS2C_CreateUserAck & data) +{ + zsummer::proto4z::Integer sttLen = 0; + rs >> sttLen; + zsummer::proto4z::Integer cursor = rs.getStreamUnreadLen(); + unsigned long long tag = 0; + rs >> tag; + if ( (1ULL << 0) & tag) + { + rs >> data.retCode; + } + if ( (1ULL << 1) & tag) + { + rs >> data.needCreateUser; + } + if ( (1ULL << 2) & tag) + { + rs >> data.info; + } + cursor = cursor - rs.getStreamUnreadLen(); + rs.skipOriginalData(sttLen - cursor); + return rs; +} + +const unsigned short ID_C2AS_ClientPulse = 106; //客户端存活脉冲 +struct C2AS_ClientPulse //客户端存活脉冲 +{ + inline unsigned short GetProtoID() { return 106;} + inline std::string GetProtoName() { return "ID_C2AS_ClientPulse";} +}; +inline zsummer::proto4z::WriteStream & operator << (zsummer::proto4z::WriteStream & ws, const C2AS_ClientPulse & data) +{ + unsigned long long tag = 0ULL; + ws << (zsummer::proto4z::Integer)0; + zsummer::proto4z::Integer offset = ws.getStreamLen(); + ws << tag; + ws.fixOriginalData(offset - 4, ws.getStreamLen() - offset); + return ws; +} +inline zsummer::proto4z::ReadStream & operator >> (zsummer::proto4z::ReadStream & rs, C2AS_ClientPulse & data) +{ + zsummer::proto4z::Integer sttLen = 0; + rs >> sttLen; + zsummer::proto4z::Integer cursor = rs.getStreamUnreadLen(); + unsigned long long tag = 0; + rs >> tag; + cursor = cursor - rs.getStreamUnreadLen(); + rs.skipOriginalData(sttLen - cursor); + return rs; +} + +const unsigned short ID_AS2C_ServerPulse = 107; //服务端存活脉冲 +struct AS2C_ServerPulse //服务端存活脉冲 +{ + unsigned int timeStamp; //服务器当前UTC时间戳 + unsigned int timeTick; //服务器当前tick时间戳 毫秒, 服务启动时刻为0 + AS2C_ServerPulse() + { + timeStamp = 0; + timeTick = 0; + } + inline unsigned short GetProtoID() { return 107;} + inline std::string GetProtoName() { return "ID_AS2C_ServerPulse";} +}; +inline zsummer::proto4z::WriteStream & operator << (zsummer::proto4z::WriteStream & ws, const AS2C_ServerPulse & data) +{ + unsigned long long tag = 3ULL; + ws << (zsummer::proto4z::Integer)0; + zsummer::proto4z::Integer offset = ws.getStreamLen(); + ws << tag; + ws << data.timeStamp; + ws << data.timeTick; + ws.fixOriginalData(offset - 4, ws.getStreamLen() - offset); + return ws; +} +inline zsummer::proto4z::ReadStream & operator >> (zsummer::proto4z::ReadStream & rs, AS2C_ServerPulse & data) +{ + zsummer::proto4z::Integer sttLen = 0; + rs >> sttLen; + zsummer::proto4z::Integer cursor = rs.getStreamUnreadLen(); + unsigned long long tag = 0; + rs >> tag; + if ( (1ULL << 0) & tag) + { + rs >> data.timeStamp; + } + if ( (1ULL << 1) & tag) + { + rs >> data.timeTick; + } + cursor = cursor - rs.getStreamUnreadLen(); + rs.skipOriginalData(sttLen - cursor); + return rs; +} + +#endif diff --git a/protocol/CSharp/ProtoCommon.cs b/protocol/CSharp/ProtoCommon.cs index 50cf4f8f..0c04923c 100644 --- a/protocol/CSharp/ProtoCommon.cs +++ b/protocol/CSharp/ProtoCommon.cs @@ -1,136 +1,172 @@ - namespace Proto4z - { - class STATIC_BEC_SUCCESS //成功 - { - public static Proto4z.ui16 value = 0; - } - class STATIC_BEC_UNKOWN_ERROR //未知错误 - { - public static Proto4z.ui16 value = 1; - } - class STATIC_BEC_INNER_ERROR //内部错误 - { - public static Proto4z.ui16 value = 2; - } - class STATIC_BEC_DB_ERROR //数据库错误 - { - public static Proto4z.ui16 value = 3; - } - class STATIC_BEC_PARAM_DENIED //非法参数 - { - public static Proto4z.ui16 value = 4; - } - class STATIC_BEC_PERMISSION_DENIED //权限非法 - { - public static Proto4z.ui16 value = 5; - } - class STATIC_BEC_SYSTEM_ERROR //系统错误 - { - public static Proto4z.ui16 value = 6; - } - class STATIC_BEC_INVALIDE_USERID //用户ID无效 - { - public static Proto4z.ui16 value = 50; - } - class STATIC_BEC_AUTH_USER_NOT_EXIST //认证错误:用户不存在 - { - public static Proto4z.ui16 value = 60; - } - class STATIC_BEC_AUTH_PASSWD_INCORRECT //认证错误:密码错误 - { - public static Proto4z.ui16 value = 61; - } - class STATIC_BEC_AUTH_ING //认证错误:正在认证中... - { - public static Proto4z.ui16 value = 62; - } - class STATIC_BEC_AUTH_AREADY_AUTH //认证错误:已认证 - { - public static Proto4z.ui16 value = 63; - } - class STATIC_BEC_AUTH_LIMITE_COUNT //认证错误:认证次数超过限制 - { - public static Proto4z.ui16 value = 64; - } - - class UserInfo: Proto4z.IProtoObject //用户信息 - { - public Proto4z.ui64 uid; - public Proto4z.String nickName; //用户昵称 - public Proto4z.i32 iconID; //头像 - public Proto4z.i32 level; //等级 - public Proto4z.i32 diamond; //当前剩余的充值钻石 - public Proto4z.i32 hisotryDiamond; //历史充值钻石总额 - public Proto4z.i32 giftDiamond; //当前剩余的赠送钻石 - public Proto4z.ui32 joinTime; //加入时间 - public System.Collections.Generic.List __encode() - { - var ret = new System.Collections.Generic.List(); - ret.AddRange(uid.__encode()); - ret.AddRange(nickName.__encode()); - ret.AddRange(iconID.__encode()); - ret.AddRange(level.__encode()); - ret.AddRange(diamond.__encode()); - ret.AddRange(hisotryDiamond.__encode()); - ret.AddRange(giftDiamond.__encode()); - ret.AddRange(joinTime.__encode()); - return ret; - } - public int __decode(byte[] binData, ref int pos) - { - uid = new Proto4z.ui64(); - uid.__decode(binData, ref pos); - nickName = new Proto4z.String(); - nickName.__decode(binData, ref pos); - iconID = new Proto4z.i32(); - iconID.__decode(binData, ref pos); - level = new Proto4z.i32(); - level.__decode(binData, ref pos); - diamond = new Proto4z.i32(); - diamond.__decode(binData, ref pos); - hisotryDiamond = new Proto4z.i32(); - hisotryDiamond.__decode(binData, ref pos); - giftDiamond = new Proto4z.i32(); - giftDiamond.__decode(binData, ref pos); - joinTime = new Proto4z.ui32(); - joinTime.__decode(binData, ref pos); - return pos; - } - } - - class UserInfoVct : System.Collections.Generic.List, Proto4z.IProtoObject - { - public System.Collections.Generic.List __encode() - { - var ret = new System.Collections.Generic.List(); - var len = new Proto4z.ui16((System.UInt16)this.Count); - ret.AddRange(len.__encode()); - for (int i = 0; i < this.Count; i++ ) - { - ret.AddRange(this[i].__encode()); - } - return ret; - } - - public int __decode(byte[] binData, ref int pos) - { - var len = new Proto4z.ui16(0); - len.__decode(binData, ref pos); - if(len.val > 0) - { - for (int i=0; i __encode() + { + Proto4z.ui32 sttLen = 0; + Proto4z.ui64 tag = 255; + + var data = new System.Collections.Generic.List(); + data.AddRange(uid.__encode()); + data.AddRange(nickName.__encode()); + data.AddRange(iconID.__encode()); + data.AddRange(level.__encode()); + data.AddRange(diamond.__encode()); + data.AddRange(hisotryDiamond.__encode()); + data.AddRange(giftDiamond.__encode()); + data.AddRange(joinTime.__encode()); + sttLen = (System.UInt32)data.Count + 8; + var ret = new System.Collections.Generic.List(); + ret.AddRange(sttLen.__encode()); + ret.AddRange(tag.__encode()); + ret.AddRange(data); + return ret; + } + public int __decode(byte[] binData, ref int pos) + { + Proto4z.ui32 offset = 0; + Proto4z.ui64 tag = 0; + offset.__decode(binData, ref pos); + offset.val += (System.UInt32)pos; + tag.__decode(binData, ref pos); + uid = new Proto4z.ui64(); + if ((tag.val & ((System.UInt64)1 << 0)) != 0) + { + uid.__decode(binData, ref pos); + } + nickName = new Proto4z.String(); + if ((tag.val & ((System.UInt64)1 << 1)) != 0) + { + nickName.__decode(binData, ref pos); + } + iconID = new Proto4z.i32(); + if ((tag.val & ((System.UInt64)1 << 2)) != 0) + { + iconID.__decode(binData, ref pos); + } + level = new Proto4z.i32(); + if ((tag.val & ((System.UInt64)1 << 3)) != 0) + { + level.__decode(binData, ref pos); + } + diamond = new Proto4z.i32(); + if ((tag.val & ((System.UInt64)1 << 4)) != 0) + { + diamond.__decode(binData, ref pos); + } + hisotryDiamond = new Proto4z.i32(); + if ((tag.val & ((System.UInt64)1 << 5)) != 0) + { + hisotryDiamond.__decode(binData, ref pos); + } + giftDiamond = new Proto4z.i32(); + if ((tag.val & ((System.UInt64)1 << 6)) != 0) + { + giftDiamond.__decode(binData, ref pos); + } + joinTime = new Proto4z.ui32(); + if ((tag.val & ((System.UInt64)1 << 7)) != 0) + { + joinTime.__decode(binData, ref pos); + } + return (int)offset.val; + } + } + + class UserInfoVct : System.Collections.Generic.List, Proto4z.IProtoObject + { + public System.Collections.Generic.List __encode() + { + var ret = new System.Collections.Generic.List(); + var len = new Proto4z.ui32((System.UInt32)this.Count); + ret.AddRange(len.__encode()); + for (int i = 0; i < this.Count; i++ ) + { + ret.AddRange(this[i].__encode()); + } + return ret; + } + + public int __decode(byte[] binData, ref int pos) + { + var len = new Proto4z.ui32(0); + len.__decode(binData, ref pos); + if(len.val > 0) + { + for (int i=0; i __encode() - { - var ret = new System.Collections.Generic.List(); - ret.AddRange(user.__encode()); - ret.AddRange(passwd.__encode()); - return ret; - } - public int __decode(byte[] binData, ref int pos) - { - user = new Proto4z.String(); - user.__decode(binData, ref pos); - passwd = new Proto4z.String(); - passwd.__decode(binData, ref pos); - return pos; - } - } - - class LS2C_LoginAck: Proto4z.IProtoObject //登录结果 - { - public Proto4z.ui16 getProtoID() { return new Proto4z.ui16(103); } - public string getProtoName() { return "LS2C_LoginAck"; } - public Proto4z.i32 retCode; - public Proto4z.i32 needCreateUser; - public Proto4z.UserInfo info; //认证成功但昵称为空 则说明需要创建用户信息 - public System.Collections.Generic.List __encode() - { - var ret = new System.Collections.Generic.List(); - ret.AddRange(retCode.__encode()); - ret.AddRange(needCreateUser.__encode()); - ret.AddRange(info.__encode()); - return ret; - } - public int __decode(byte[] binData, ref int pos) - { - retCode = new Proto4z.i32(); - retCode.__decode(binData, ref pos); - needCreateUser = new Proto4z.i32(); - needCreateUser.__decode(binData, ref pos); - info = new Proto4z.UserInfo(); - info.__decode(binData, ref pos); - return pos; - } - } - - class C2LS_CreateUserReq: Proto4z.IProtoObject //填写用户信息 - { - public Proto4z.ui16 getProtoID() { return new Proto4z.ui16(108); } - public string getProtoName() { return "C2LS_CreateUserReq"; } - public Proto4z.String nickName; //昵称 - public Proto4z.i32 iconID; //头像 - public System.Collections.Generic.List __encode() - { - var ret = new System.Collections.Generic.List(); - ret.AddRange(nickName.__encode()); - ret.AddRange(iconID.__encode()); - return ret; - } - public int __decode(byte[] binData, ref int pos) - { - nickName = new Proto4z.String(); - nickName.__decode(binData, ref pos); - iconID = new Proto4z.i32(); - iconID.__decode(binData, ref pos); - return pos; - } - } - - class LS2C_CreateUserAck: Proto4z.IProtoObject //返回 - { - public Proto4z.ui16 getProtoID() { return new Proto4z.ui16(109); } - public string getProtoName() { return "LS2C_CreateUserAck"; } - public Proto4z.ui16 retCode; - public Proto4z.i32 needCreateUser; //nickname冲突需要重新创建 - public Proto4z.UserInfo info; - public System.Collections.Generic.List __encode() - { - var ret = new System.Collections.Generic.List(); - ret.AddRange(retCode.__encode()); - ret.AddRange(needCreateUser.__encode()); - ret.AddRange(info.__encode()); - return ret; - } - public int __decode(byte[] binData, ref int pos) - { - retCode = new Proto4z.ui16(); - retCode.__decode(binData, ref pos); - needCreateUser = new Proto4z.i32(); - needCreateUser.__decode(binData, ref pos); - info = new Proto4z.UserInfo(); - info.__decode(binData, ref pos); - return pos; - } - } - - class C2AS_ClientPulse: Proto4z.IProtoObject //客户端存活脉冲 - { - public Proto4z.ui16 getProtoID() { return new Proto4z.ui16(106); } - public string getProtoName() { return "C2AS_ClientPulse"; } - public System.Collections.Generic.List __encode() - { - var ret = new System.Collections.Generic.List(); - return ret; - } - public int __decode(byte[] binData, ref int pos) - { - return pos; - } - } - - class AS2C_ServerPulse: Proto4z.IProtoObject //服务端存活脉冲 - { - public Proto4z.ui16 getProtoID() { return new Proto4z.ui16(107); } - public string getProtoName() { return "AS2C_ServerPulse"; } - public Proto4z.ui32 timeStamp; //服务器当前UTC时间戳 - public Proto4z.ui32 timeTick; //服务器当前tick时间戳 毫秒, 服务启动时刻为0 - public System.Collections.Generic.List __encode() - { - var ret = new System.Collections.Generic.List(); - ret.AddRange(timeStamp.__encode()); - ret.AddRange(timeTick.__encode()); - return ret; - } - public int __decode(byte[] binData, ref int pos) - { - timeStamp = new Proto4z.ui32(); - timeStamp.__decode(binData, ref pos); - timeTick = new Proto4z.ui32(); - timeTick.__decode(binData, ref pos); - return pos; - } - } - - } - - - \ No newline at end of file +namespace Proto4z +{ + + class C2LS_LoginReq: Proto4z.IProtoObject //登录请求 + { + static public Proto4z.ui16 getProtoID() { return new Proto4z.ui16(102); } + static public string getProtoName() { return "C2LS_LoginReq"; } + public Proto4z.String user; + public Proto4z.String passwd; + public System.Collections.Generic.List __encode() + { + Proto4z.ui32 sttLen = 0; + Proto4z.ui64 tag = 3; + + var data = new System.Collections.Generic.List(); + data.AddRange(user.__encode()); + data.AddRange(passwd.__encode()); + sttLen = (System.UInt32)data.Count + 8; + var ret = new System.Collections.Generic.List(); + ret.AddRange(sttLen.__encode()); + ret.AddRange(tag.__encode()); + ret.AddRange(data); + return ret; + } + public int __decode(byte[] binData, ref int pos) + { + Proto4z.ui32 offset = 0; + Proto4z.ui64 tag = 0; + offset.__decode(binData, ref pos); + offset.val += (System.UInt32)pos; + tag.__decode(binData, ref pos); + user = new Proto4z.String(); + if ((tag.val & ((System.UInt64)1 << 0)) != 0) + { + user.__decode(binData, ref pos); + } + passwd = new Proto4z.String(); + if ((tag.val & ((System.UInt64)1 << 1)) != 0) + { + passwd.__decode(binData, ref pos); + } + return (int)offset.val; + } + } + + class LS2C_LoginAck: Proto4z.IProtoObject //登录结果 + { + static public Proto4z.ui16 getProtoID() { return new Proto4z.ui16(103); } + static public string getProtoName() { return "LS2C_LoginAck"; } + public Proto4z.i32 retCode; + public Proto4z.i32 needCreateUser; + public Proto4z.UserInfo info; //认证成功但昵称为空 则说明需要创建用户信息 + public System.Collections.Generic.List __encode() + { + Proto4z.ui32 sttLen = 0; + Proto4z.ui64 tag = 7; + + var data = new System.Collections.Generic.List(); + data.AddRange(retCode.__encode()); + data.AddRange(needCreateUser.__encode()); + data.AddRange(info.__encode()); + sttLen = (System.UInt32)data.Count + 8; + var ret = new System.Collections.Generic.List(); + ret.AddRange(sttLen.__encode()); + ret.AddRange(tag.__encode()); + ret.AddRange(data); + return ret; + } + public int __decode(byte[] binData, ref int pos) + { + Proto4z.ui32 offset = 0; + Proto4z.ui64 tag = 0; + offset.__decode(binData, ref pos); + offset.val += (System.UInt32)pos; + tag.__decode(binData, ref pos); + retCode = new Proto4z.i32(); + if ((tag.val & ((System.UInt64)1 << 0)) != 0) + { + retCode.__decode(binData, ref pos); + } + needCreateUser = new Proto4z.i32(); + if ((tag.val & ((System.UInt64)1 << 1)) != 0) + { + needCreateUser.__decode(binData, ref pos); + } + info = new Proto4z.UserInfo(); + if ((tag.val & ((System.UInt64)1 << 2)) != 0) + { + info.__decode(binData, ref pos); + } + return (int)offset.val; + } + } + + class C2LS_CreateUserReq: Proto4z.IProtoObject //填写用户信息 + { + static public Proto4z.ui16 getProtoID() { return new Proto4z.ui16(108); } + static public string getProtoName() { return "C2LS_CreateUserReq"; } + public Proto4z.String nickName; //昵称 + public Proto4z.i32 iconID; //头像 + public System.Collections.Generic.List __encode() + { + Proto4z.ui32 sttLen = 0; + Proto4z.ui64 tag = 3; + + var data = new System.Collections.Generic.List(); + data.AddRange(nickName.__encode()); + data.AddRange(iconID.__encode()); + sttLen = (System.UInt32)data.Count + 8; + var ret = new System.Collections.Generic.List(); + ret.AddRange(sttLen.__encode()); + ret.AddRange(tag.__encode()); + ret.AddRange(data); + return ret; + } + public int __decode(byte[] binData, ref int pos) + { + Proto4z.ui32 offset = 0; + Proto4z.ui64 tag = 0; + offset.__decode(binData, ref pos); + offset.val += (System.UInt32)pos; + tag.__decode(binData, ref pos); + nickName = new Proto4z.String(); + if ((tag.val & ((System.UInt64)1 << 0)) != 0) + { + nickName.__decode(binData, ref pos); + } + iconID = new Proto4z.i32(); + if ((tag.val & ((System.UInt64)1 << 1)) != 0) + { + iconID.__decode(binData, ref pos); + } + return (int)offset.val; + } + } + + class LS2C_CreateUserAck: Proto4z.IProtoObject //返回 + { + static public Proto4z.ui16 getProtoID() { return new Proto4z.ui16(109); } + static public string getProtoName() { return "LS2C_CreateUserAck"; } + public Proto4z.ui16 retCode; + public Proto4z.i32 needCreateUser; //nickname冲突需要重新创建 + public Proto4z.UserInfo info; + public System.Collections.Generic.List __encode() + { + Proto4z.ui32 sttLen = 0; + Proto4z.ui64 tag = 7; + + var data = new System.Collections.Generic.List(); + data.AddRange(retCode.__encode()); + data.AddRange(needCreateUser.__encode()); + data.AddRange(info.__encode()); + sttLen = (System.UInt32)data.Count + 8; + var ret = new System.Collections.Generic.List(); + ret.AddRange(sttLen.__encode()); + ret.AddRange(tag.__encode()); + ret.AddRange(data); + return ret; + } + public int __decode(byte[] binData, ref int pos) + { + Proto4z.ui32 offset = 0; + Proto4z.ui64 tag = 0; + offset.__decode(binData, ref pos); + offset.val += (System.UInt32)pos; + tag.__decode(binData, ref pos); + retCode = new Proto4z.ui16(); + if ((tag.val & ((System.UInt64)1 << 0)) != 0) + { + retCode.__decode(binData, ref pos); + } + needCreateUser = new Proto4z.i32(); + if ((tag.val & ((System.UInt64)1 << 1)) != 0) + { + needCreateUser.__decode(binData, ref pos); + } + info = new Proto4z.UserInfo(); + if ((tag.val & ((System.UInt64)1 << 2)) != 0) + { + info.__decode(binData, ref pos); + } + return (int)offset.val; + } + } + + class C2AS_ClientPulse: Proto4z.IProtoObject //客户端存活脉冲 + { + static public Proto4z.ui16 getProtoID() { return new Proto4z.ui16(106); } + static public string getProtoName() { return "C2AS_ClientPulse"; } + public System.Collections.Generic.List __encode() + { + Proto4z.ui32 sttLen = 0; + Proto4z.ui64 tag = 0; + + var data = new System.Collections.Generic.List(); + sttLen = (System.UInt32)data.Count + 8; + var ret = new System.Collections.Generic.List(); + ret.AddRange(sttLen.__encode()); + ret.AddRange(tag.__encode()); + ret.AddRange(data); + return ret; + } + public int __decode(byte[] binData, ref int pos) + { + Proto4z.ui32 offset = 0; + Proto4z.ui64 tag = 0; + offset.__decode(binData, ref pos); + offset.val += (System.UInt32)pos; + tag.__decode(binData, ref pos); + return (int)offset.val; + } + } + + class AS2C_ServerPulse: Proto4z.IProtoObject //服务端存活脉冲 + { + static public Proto4z.ui16 getProtoID() { return new Proto4z.ui16(107); } + static public string getProtoName() { return "AS2C_ServerPulse"; } + public Proto4z.ui32 timeStamp; //服务器当前UTC时间戳 + public Proto4z.ui32 timeTick; //服务器当前tick时间戳 毫秒, 服务启动时刻为0 + public System.Collections.Generic.List __encode() + { + Proto4z.ui32 sttLen = 0; + Proto4z.ui64 tag = 3; + + var data = new System.Collections.Generic.List(); + data.AddRange(timeStamp.__encode()); + data.AddRange(timeTick.__encode()); + sttLen = (System.UInt32)data.Count + 8; + var ret = new System.Collections.Generic.List(); + ret.AddRange(sttLen.__encode()); + ret.AddRange(tag.__encode()); + ret.AddRange(data); + return ret; + } + public int __decode(byte[] binData, ref int pos) + { + Proto4z.ui32 offset = 0; + Proto4z.ui64 tag = 0; + offset.__decode(binData, ref pos); + offset.val += (System.UInt32)pos; + tag.__decode(binData, ref pos); + timeStamp = new Proto4z.ui32(); + if ((tag.val & ((System.UInt64)1 << 0)) != 0) + { + timeStamp.__decode(binData, ref pos); + } + timeTick = new Proto4z.ui32(); + if ((tag.val & ((System.UInt64)1 << 1)) != 0) + { + timeTick.__decode(binData, ref pos); + } + return (int)offset.val; + } + } + +} + + diff --git a/protocol/genProto.exe b/protocol/genProto.exe index 68fba9c0..4df707cf 100644 Binary files a/protocol/genProto.exe and b/protocol/genProto.exe differ diff --git a/protocol/lua/ProtoCommon.lua b/protocol/lua/ProtoCommon.lua index 01690a2e..2519e45a 100644 --- a/protocol/lua/ProtoCommon.lua +++ b/protocol/lua/ProtoCommon.lua @@ -1,30 +1,29 @@ Protoz.BEC_SUCCESS = 0--成功 - Protoz.BEC_UNKOWN_ERROR = 1--未知错误 - Protoz.BEC_INNER_ERROR = 2--内部错误 - Protoz.BEC_DB_ERROR = 3--数据库错误 - Protoz.BEC_PARAM_DENIED = 4--非法参数 - Protoz.BEC_PERMISSION_DENIED = 5--权限非法 - Protoz.BEC_SYSTEM_ERROR = 6--系统错误 - Protoz.BEC_INVALIDE_USERID = 50--用户ID无效 - Protoz.BEC_AUTH_USER_NOT_EXIST = 60--认证错误:用户不存在 - Protoz.BEC_AUTH_PASSWD_INCORRECT = 61--认证错误:密码错误 - Protoz.BEC_AUTH_ING = 62--认证错误:正在认证中... - Protoz.BEC_AUTH_AREADY_AUTH = 63--认证错误:已认证 - Protoz.BEC_AUTH_LIMITE_COUNT = 64--认证错误:认证次数超过限制 - - Protoz.UserInfo = {} --用户信息 - Protoz.UserInfo.__getName = "UserInfo" - Protoz.UserInfo[1] = {name="uid", type="ui64"} - Protoz.UserInfo[2] = {name="nickName", type="string"} --用户昵称 - Protoz.UserInfo[3] = {name="iconID", type="i32"} --头像 - Protoz.UserInfo[4] = {name="level", type="i32"} --等级 - Protoz.UserInfo[5] = {name="diamond", type="i32"} --当前剩余的充值钻石 - Protoz.UserInfo[6] = {name="hisotryDiamond", type="i32"} --历史充值钻石总额 - Protoz.UserInfo[7] = {name="giftDiamond", type="i32"} --当前剩余的赠送钻石 - Protoz.UserInfo[8] = {name="joinTime", type="ui32"} --加入时间 - - Protoz.UserInfoVct = {} - Protoz.UserInfoVct.__getName = "UserInfoVct" - Protoz.UserInfoVct.__getDesc = "array" - Protoz.UserInfoVct.__getTypeV = "UserInfo" - \ No newline at end of file +Protoz.BEC_UNKOWN_ERROR = 1--未知错误 +Protoz.BEC_INNER_ERROR = 2--内部错误 +Protoz.BEC_DB_ERROR = 3--数据库错误 +Protoz.BEC_PARAM_DENIED = 4--非法参数 +Protoz.BEC_PERMISSION_DENIED = 5--权限非法 +Protoz.BEC_SYSTEM_ERROR = 6--系统错误 +Protoz.BEC_INVALIDE_USERID = 50--用户ID无效 +Protoz.BEC_AUTH_USER_NOT_EXIST = 60--认证错误:用户不存在 +Protoz.BEC_AUTH_PASSWD_INCORRECT = 61--认证错误:密码错误 +Protoz.BEC_AUTH_ING = 62--认证错误:正在认证中... +Protoz.BEC_AUTH_AREADY_AUTH = 63--认证错误:已认证 +Protoz.BEC_AUTH_LIMITE_COUNT = 64--认证错误:认证次数超过限制 + +Protoz.UserInfo = {} --用户信息 +Protoz.UserInfo.__getName = "UserInfo" +Protoz.UserInfo[1] = {name="uid", type="ui64" } +Protoz.UserInfo[2] = {name="nickName", type="string" } --用户昵称 +Protoz.UserInfo[3] = {name="iconID", type="i32" } --头像 +Protoz.UserInfo[4] = {name="level", type="i32" } --等级 +Protoz.UserInfo[5] = {name="diamond", type="i32" } --当前剩余的充值钻石 +Protoz.UserInfo[6] = {name="hisotryDiamond", type="i32" } --历史充值钻石总额 +Protoz.UserInfo[7] = {name="giftDiamond", type="i32" } --当前剩余的赠送钻石 +Protoz.UserInfo[8] = {name="joinTime", type="ui32" } --加入时间 + +Protoz.UserInfoVct = {} +Protoz.UserInfoVct.__getName = "UserInfoVct" +Protoz.UserInfoVct.__getDesc = "array" +Protoz.UserInfoVct.__getTypeV = "UserInfo" diff --git a/protocol/lua/ProtoLogin.lua b/protocol/lua/ProtoLogin.lua index 66975ba2..93cbe1ea 100644 --- a/protocol/lua/ProtoLogin.lua +++ b/protocol/lua/ProtoLogin.lua @@ -1,43 +1,42 @@ - Protoz.register(102,"C2LS_LoginReq") - Protoz.C2LS_LoginReq = {} --登录请求 - Protoz.C2LS_LoginReq.__getID = 102 - Protoz.C2LS_LoginReq.__getName = "C2LS_LoginReq" - Protoz.C2LS_LoginReq[1] = {name="user", type="string"} - Protoz.C2LS_LoginReq[2] = {name="passwd", type="string"} - - Protoz.register(103,"LS2C_LoginAck") - Protoz.LS2C_LoginAck = {} --登录结果 - Protoz.LS2C_LoginAck.__getID = 103 - Protoz.LS2C_LoginAck.__getName = "LS2C_LoginAck" - Protoz.LS2C_LoginAck[1] = {name="retCode", type="i32"} - Protoz.LS2C_LoginAck[2] = {name="needCreateUser", type="i32"} - Protoz.LS2C_LoginAck[3] = {name="info", type="UserInfo"} --认证成功但昵称为空 则说明需要创建用户信息 - - Protoz.register(108,"C2LS_CreateUserReq") - Protoz.C2LS_CreateUserReq = {} --填写用户信息 - Protoz.C2LS_CreateUserReq.__getID = 108 - Protoz.C2LS_CreateUserReq.__getName = "C2LS_CreateUserReq" - Protoz.C2LS_CreateUserReq[1] = {name="nickName", type="string"} --昵称 - Protoz.C2LS_CreateUserReq[2] = {name="iconID", type="i32"} --头像 - - Protoz.register(109,"LS2C_CreateUserAck") - Protoz.LS2C_CreateUserAck = {} --返回 - Protoz.LS2C_CreateUserAck.__getID = 109 - Protoz.LS2C_CreateUserAck.__getName = "LS2C_CreateUserAck" - Protoz.LS2C_CreateUserAck[1] = {name="retCode", type="ui16"} - Protoz.LS2C_CreateUserAck[2] = {name="needCreateUser", type="i32"} --nickname冲突需要重新创建 - Protoz.LS2C_CreateUserAck[3] = {name="info", type="UserInfo"} - - Protoz.register(106,"C2AS_ClientPulse") - Protoz.C2AS_ClientPulse = {} --客户端存活脉冲 - Protoz.C2AS_ClientPulse.__getID = 106 - Protoz.C2AS_ClientPulse.__getName = "C2AS_ClientPulse" - - Protoz.register(107,"AS2C_ServerPulse") - Protoz.AS2C_ServerPulse = {} --服务端存活脉冲 - Protoz.AS2C_ServerPulse.__getID = 107 - Protoz.AS2C_ServerPulse.__getName = "AS2C_ServerPulse" - Protoz.AS2C_ServerPulse[1] = {name="timeStamp", type="ui32"} --服务器当前UTC时间戳 - Protoz.AS2C_ServerPulse[2] = {name="timeTick", type="ui32"} --服务器当前tick时间戳 毫秒, 服务启动时刻为0 - \ No newline at end of file +Protoz.register(102,"C2LS_LoginReq") +Protoz.C2LS_LoginReq = {} --登录请求 +Protoz.C2LS_LoginReq.__getID = 102 +Protoz.C2LS_LoginReq.__getName = "C2LS_LoginReq" +Protoz.C2LS_LoginReq[1] = {name="user", type="string" } +Protoz.C2LS_LoginReq[2] = {name="passwd", type="string" } + +Protoz.register(103,"LS2C_LoginAck") +Protoz.LS2C_LoginAck = {} --登录结果 +Protoz.LS2C_LoginAck.__getID = 103 +Protoz.LS2C_LoginAck.__getName = "LS2C_LoginAck" +Protoz.LS2C_LoginAck[1] = {name="retCode", type="i32" } +Protoz.LS2C_LoginAck[2] = {name="needCreateUser", type="i32" } +Protoz.LS2C_LoginAck[3] = {name="info", type="UserInfo" } --认证成功但昵称为空 则说明需要创建用户信息 + +Protoz.register(108,"C2LS_CreateUserReq") +Protoz.C2LS_CreateUserReq = {} --填写用户信息 +Protoz.C2LS_CreateUserReq.__getID = 108 +Protoz.C2LS_CreateUserReq.__getName = "C2LS_CreateUserReq" +Protoz.C2LS_CreateUserReq[1] = {name="nickName", type="string" } --昵称 +Protoz.C2LS_CreateUserReq[2] = {name="iconID", type="i32" } --头像 + +Protoz.register(109,"LS2C_CreateUserAck") +Protoz.LS2C_CreateUserAck = {} --返回 +Protoz.LS2C_CreateUserAck.__getID = 109 +Protoz.LS2C_CreateUserAck.__getName = "LS2C_CreateUserAck" +Protoz.LS2C_CreateUserAck[1] = {name="retCode", type="ui16" } +Protoz.LS2C_CreateUserAck[2] = {name="needCreateUser", type="i32" } --nickname冲突需要重新创建 +Protoz.LS2C_CreateUserAck[3] = {name="info", type="UserInfo" } + +Protoz.register(106,"C2AS_ClientPulse") +Protoz.C2AS_ClientPulse = {} --客户端存活脉冲 +Protoz.C2AS_ClientPulse.__getID = 106 +Protoz.C2AS_ClientPulse.__getName = "C2AS_ClientPulse" + +Protoz.register(107,"AS2C_ServerPulse") +Protoz.AS2C_ServerPulse = {} --服务端存活脉冲 +Protoz.AS2C_ServerPulse.__getID = 107 +Protoz.AS2C_ServerPulse.__getName = "AS2C_ServerPulse" +Protoz.AS2C_ServerPulse[1] = {name="timeStamp", type="ui32" } --服务器当前UTC时间戳 +Protoz.AS2C_ServerPulse[2] = {name="timeTick", type="ui32" } --服务器当前tick时间戳 毫秒, 服务启动时刻为0 diff --git a/protocol/lua/proto4z.lua b/protocol/lua/proto4z.lua index bfd53276..5d91f5d2 100644 --- a/protocol/lua/proto4z.lua +++ b/protocol/lua/proto4z.lua @@ -1,9 +1,50 @@ + +--[[ +/* + * proto4z License + * ----------- + * + * proto4z is licensed under the terms of the MIT license reproduced below. + * This means that proto4z is free software and can be used for both academic + * and commercial purposes at absolutely no cost. + * + * + * =============================================================================== + * + * Copyright (C) 2013-2015 YaweiZhang . + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * =============================================================================== + * + * (end of COPYRIGHT) + */ + ]]-- + + -- proto4z core file -- Auther: zhangyawei -- mail:yawei_zhang@foxmail.com -- date: 2015-01-12 + Protoz = {} @@ -187,20 +228,14 @@ function Protoz.__pack(val, tp) elseif tp == "ui32" or tp == "unsigned int" then return string.pack(" + diff --git a/source/common/common.vcxproj.filters b/source/common/common.vcxproj.filters index 71616227..6cb1755c 100644 --- a/source/common/common.vcxproj.filters +++ b/source/common/common.vcxproj.filters @@ -201,6 +201,9 @@ md5 + + lua + diff --git a/source/common/lua/protoz_bit.c b/source/common/lua/protoz_bit.c new file mode 100644 index 00000000..345c0aa5 --- /dev/null +++ b/source/common/lua/protoz_bit.c @@ -0,0 +1,152 @@ +/* + * proto4z License + * ----------- + * + * proto4z is licensed under the terms of the MIT license reproduced below. + * This means that proto4z is free software and can be used for both academic + * and commercial purposes at absolutely no cost. + * + * + * =============================================================================== + * + * Copyright (C) 2013-2015 YaweiZhang . + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * =============================================================================== + * + * (end of COPYRIGHT) + */ + + + + +#include "lua.h" +#include "lauxlib.h" +#include +#include + + +int luaopen_protoz_bit(lua_State *L); + + + +static const unsigned short __gc_localEndianType = 1; +static bool islocalLittleEndian() +{ + if (*(const unsigned char *)&__gc_localEndianType == 1) + { + return true; + } + return false; +} + + + +static unsigned long long streamToInteger(const char stream[]) +{ + unsigned long long integer = 0; + size_t len = sizeof(unsigned long long); + if (!islocalLittleEndian()) + { + unsigned char *dst = (unsigned char*)&integer; + unsigned char *src = (unsigned char*)stream + sizeof(unsigned long long); + while (len > 0) + { + *dst++ = *--src; + len--; + } + } + else + { + memcpy(&integer, stream, len); + } + return integer; +} + + +static int checkBitTrue(lua_State *L) +{ + size_t len = 0; + const char * log = luaL_checklstring(L, 1, &len); + size_t pos = luaL_checkinteger(L, 2); + if (len != 8) + { + return 0; + } + lua_settop(L, 0); + unsigned long long val = streamToInteger(log); + if (val & (1ULL << pos)) + { + lua_pushboolean(L, 1); + } + else + { + lua_pushnil(L); + } + return 1; +} + +static int checkStringToBit(lua_State *L) +{ + size_t i, len = 0; + const char * log = luaL_checklstring(L, 1, &len); + unsigned long long pos = 0; + if (len > 64) + { + return 0; + } + + for (i = 0; i < len; i++) + { + if (log[i] == '1') + { + pos |= (1ULL << i); + } + else + { + pos &= ~(unsigned long long)(1ULL << i); + } + } + + lua_pushlstring(L, (const char*)&pos, 8); + return 1; +} + + +luaL_Reg protoz_bit[] = { + { "checkBitTrue", checkBitTrue }, + { "checkStringToBit", checkStringToBit }, + + { NULL, NULL } +}; + + +int luaopen_protoz_bit(lua_State *L) +{ + lua_newtable(L); + luaL_Reg *l; + for (l = protoz_bit; l->name != NULL; l++) { + lua_pushcclosure(L, l->func, 0); /* closure with those upvalues */ + lua_setfield(L, -2, l->name); + } + lua_setglobal(L, "Protoz_bit"); + return 0; +} + diff --git a/source/stressTest/stressMain.cpp b/source/stressTest/stressMain.cpp index e565787e..7880ec8f 100644 --- a/source/stressTest/stressMain.cpp +++ b/source/stressTest/stressMain.cpp @@ -26,6 +26,7 @@ extern "C" #include "lua/lua.h" #include "lua/lualib.h" #include "lua/lpack.h" +int luaopen_protoz_bit(lua_State *L); } @@ -102,6 +103,7 @@ int main(int argc, char* argv[]) luaL_openlibs(L); /* open libraries */ luaopen_summer(L); luaopen_pack(L); + luaopen_protoz_bit(L); lua_gc(L, LUA_GCRESTART, 0); status = luaL_dofile(L, "./main.lua");