socketinputstream.h
来自「天之炼狱1服务器端源文件游戏服务端不完整」· C头文件 代码 · 共 183 行
H
183 行
//////////////////////////////////////////////////////////////////////// // Filename : SocketInputStream.h // Written by : reiot@ewestsoft.com// Description :// ////////////////////////////////////////////////////////////////////////// *Reiot's Notes*//// 矫胶袍俊辑 啊厘 后锅窍霸 荤侩登绰 努贰胶吝狼 窍唱捞促.// 加档俊 公瘤阜瘤茄 康氢阑 固摹骨肺, 父老 粱歹 加档甫 焊碍窍绊// 酵促搁, exception阑 哗绊 re-write 窍扼. //// 泅犁 nonblocking 捞 辈厘洒-撅荐肺-凉扼 腹捞 惯积茄促绊 沁阑锭,// 捞巴捞 NonBlockingIOException栏肺 wrapping瞪锭 overhead啊 惯积且// 犬伏捞 臭促绊 眠螟等促.////////////////////////////////////////////////////////////////////////#ifndef __SOCKET_INPUT_STREAM_H__#define __SOCKET_INPUT_STREAM_H__// include files#include "Types.h"#include "Exception.h"#include "Socket.h"// constant definitionsconst uint DefaultSocketInputBufferSize = 81920;// forward declarationclass Packet;////////////////////////////////////////////////////////////////////////// class SocketInputStream////////////////////////////////////////////////////////////////////////class SocketInputStream {//////////////////////////////////////////////////// constructor/destructor//////////////////////////////////////////////////public : // constructor SocketInputStream (Socket* sock, uint BufferSize = DefaultSocketInputBufferSize) throw (Error); // destructor virtual ~SocketInputStream () throw (Error); //////////////////////////////////////////////////// methods//////////////////////////////////////////////////public : // read data from stream (input buffer) uint read (char* buf, uint len) throw (ProtocolException, Error); uint read (string & str, uint len) throw (ProtocolException, Error); void readPacket (Packet* p) throw (ProtocolException, Error); template<typename T> uint read( T& buf ) throw (ProtocolException, Error);/* uint read (bool & buf) throw (ProtocolException, Error) { return read((char*)&buf, szbool ); } uint read (char & buf) throw (ProtocolException, Error) { return read((char*)&buf, szchar ); } uint read (uchar & buf) throw (ProtocolException, Error) { return read((char*)&buf, szuchar ); } uint read (short & buf) throw (ProtocolException, Error) { return read((char*)&buf, szshort ); } uint read (ushort & buf) throw (ProtocolException, Error) { return read((char*)&buf, szushort); } uint read (int & buf) throw (ProtocolException, Error) { return read((char*)&buf, szint ); } uint read (uint & buf) throw (ProtocolException, Error) { return read((char*)&buf, szuint ); } uint read (long & buf) throw (ProtocolException, Error) { return read((char*)&buf, szlong ); } uint read (ulong & buf) throw (ProtocolException, Error) { return read((char*)&buf, szulong ); }*/ // peek data from stream (input buffer) bool peek (char* buf, uint len) throw (ProtocolException, Error); // skip data from stream (input buffer) void skip (uint len) throw (ProtocolException, Error); // fill stream (input buffer) from socket uint fill () throw (IOException, Error); uint fill_RAW () throw (IOException, Error); // resize buffer void resize (int size) throw (IOException, Error); // get buffer length uint capacity () const throw () { return m_BufferLen; } // get data length in buffer uint length () const throw (); uint size () const throw () { return length(); } // check if buffer is empty bool isEmpty () const throw () { return m_Head == m_Tail; } // get debug string string toString () const throw ();//////////////////////////////////////////////////// attributes//////////////////////////////////////////////////private : // socket Socket* m_pSocket; // buffer char* m_Buffer; // buffer length uint m_BufferLen; // buffer head/tail uint m_Head; uint m_Tail;};////////////////////////////////////////////////////////////////////////// read data from input buffer////////////////////////////////////////////////////////////////////////template<typename T>uint SocketInputStream::read ( T& buf ) throw ( ProtocolException , Error ){ uint len = (uint)sizeof(T); // 夸没茄 父怒狼 单捞鸥啊 滚欺郴俊 粮犁窍瘤 臼阑 版快 抗寇甫 带柳促. // 父距 葛电 read 啊 peek() 肺 眉农茄 饶 龋免等促搁, 酒贰 if-throw 绰 // 吝汗等 皑捞 乐促. 蝶扼辑, 内膏飘肺 贸府秦档 公规窍促. // 窜 酒贰 内靛甫 内膏飘贸府窍搁, 官肺 酒贰狼 if-else 甫 'if'-'else if'-'else' // 肺 荐沥秦拎具 茄促. if ( len > length() ) throw InsufficientDataException( len - length() ); if ( m_Head < m_Tail ) // normal order { // // H T // 0123456789 // ...abcd... // buf = *(T*)(m_Buffer+m_Head); } else // reversed order ( m_Head > m_Tail ) { // // T H // 0123456789 // abcd...efg // uint rightLen = m_BufferLen - m_Head; if ( len <= rightLen ) { buf = *(T*)(m_Buffer+m_Head); } else { memcpy( (char*)&buf , &m_Buffer[m_Head] , rightLen ); memcpy( ((char*)(&buf)+rightLen), m_Buffer, len - rightLen ); } } m_Head = ( m_Head + len ) % m_BufferLen; return len; // __END_CATCH}#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?