socketoutputstream.h

来自「天之炼狱1服务器端源文件游戏服务端不完整」· C头文件 代码 · 共 197 行

H
197
字号
//////////////////////////////////////////////////////////////////////// // SocketOutputStream.h // // by Reiot// //////////////////////////////////////////////////////////////////////#ifndef __SOCKET_OUTPUT_STREAM_H__#define __SOCKET_OUTPUT_STREAM_H__// include files#include "Types.h"#include "Exception.h"#include "Socket.h"// constant definitionsconst unsigned int DefaultSocketOutputBufferSize = 81920;// forward declarationclass Packet;////////////////////////////////////////////////////////////////////////// class SocketOutputStream////////////////////////////////////////////////////////////////////////class SocketOutputStream {//////////////////////////////////////////////////// constructor/destructor//////////////////////////////////////////////////public :		// constructor	SocketOutputStream (Socket* sock, uint BufferSize = DefaultSocketOutputBufferSize) throw (Error);		// destructor	virtual ~SocketOutputStream () throw (Error);	//////////////////////////////////////////////////// methods//////////////////////////////////////////////////public :		// write data to stream (output buffer)	// *CAUTION*	// string 阑 滚欺俊 writing 且 锭, 磊悼栏肺 size 甫 菊俊 嘿老 荐档 乐促.	// 弊矾唱, string 狼 农扁甫 BYTE/WORD 吝 绢蠢 巴栏肺 且 扒瘤绰 狼巩捞促.	// 菩哦狼 农扁绰 累阑 荐废 亮促绰 沥氓窍俊辑 鞘夸俊 蝶扼辑 string size 蔼阑	// BYTE 肚绰 WORD 甫 荐悼栏肺 荤侩窍档废 茄促.	uint write (const char* buf, uint len) throw (Error);	uint write (const string & buf) throw (Error) { return write(buf.c_str(),buf.size()); }	void writePacket (const Packet* pPacket) throw (ProtocolException, Error);		template<typename T>		uint write( T buf ) throw (Error);/*	uint write (bool   buf) throw (ProtocolException, Error) { return write((const char*)&buf, szbool  ); }	uint write (char   buf) throw (ProtocolException, Error) { return write((const char*)&buf, szchar  ); }	uint write (uchar  buf) throw (ProtocolException, Error) { return write((const char*)&buf, szuchar ); }	uint write (short  buf) throw (ProtocolException, Error) { return write((const char*)&buf, szshort ); }	uint write (ushort buf) throw (ProtocolException, Error) { return write((const char*)&buf, szushort); }	uint write (int    buf) throw (ProtocolException, Error) { return write((const char*)&buf, szint   ); }	uint write (uint   buf) throw (ProtocolException, Error) { return write((const char*)&buf, szuint  ); }	uint write (long   buf) throw (ProtocolException, Error) { return write((const char*)&buf, szlong  ); }	uint write (ulong  buf) throw (ProtocolException, Error) { return write((const char*)&buf, szulong ); }*/	// flush stream (output buffer) to socket	uint flush () throw (IOException, ProtocolException, InvalidProtocolException, Error);	// resize buffer 	void resize (int size) throw (IOException, Error);	// get buffer length	int capacity () const throw () { return m_BufferLen; }     // get data length in buffer    uint length () const throw ();    uint size () const throw () { return length(); }	// get data in buffer	char* getBuffer() const { return m_Buffer; }     // check if buffer is empty    bool isEmpty () const throw () { return m_Head == m_Tail; }    // get debug string    string toString () const throw ()    {        StringStream msg;        msg << "SocketOutputStream(m_BufferLen:"<<m_BufferLen<<",m_Head:"<<m_Head<<",m_Tail:"<<m_Tail<<")";        return msg.toString();    }//////////////////////////////////////////////////// attributes//////////////////////////////////////////////////private :		// socket	Socket* m_Socket;		// output buffer	char* m_Buffer;		// buffer length	uint m_BufferLen;		// buffer head/tail	uint m_Head;	uint m_Tail;};////////////////////////////////////////////////////////////////////////// write data to stream (output buffer)//// *Notes*//// ( ( m_Head = m_Tail + 1 ) ||  //   ( ( m_Head == 0 ) && ( m_Tail == m_BufferLen - 1 ) )//// 老 锭 滚欺 full 肺 埃林茄促绰 巴阑 镭瘤 富扼. 蝶扼辑, 滚欺狼 后// 傍埃狼 农扁绰 亲惑 1 阑 哗拎具 茄促绰 荤角!////////////////////////////////////////////////////////////////////////template<typename T>uint SocketOutputStream::write ( T buf )      throw ( Error ){	__BEGIN_TRY	uint len = sizeof(T);			// 泅犁 滚欺狼 后 康开阑 拌魂茄促.	// (!) m_Head > m_Tail牢 版快俊 m_Head - m_Tail - 1 肺 荐沥沁促. by sigi. 2002.9.16	// 辟单 buffer_resize啊 歹 磊林 老绢车促. 促弗单 巩力啊 乐绰单 老窜 给 茫摆栏骨肺.. back. by sigi. 2002.9.23	// 抛胶飘 秦焊聪鳖.. 沥惑利捞菌促. 角力肺 buffer resize啊 后锅洒 老绢唱绰 盔牢篮 够鳖? 促矫 荐沥. by sigi. 2002.9.27	uint nFree = ( ( m_Head <= m_Tail ) ?  m_BufferLen - m_Tail + m_Head - 1 : m_Head - m_Tail - 1 );					//m_Tail - m_Head - 1 );	// 镜妨绊 窍绰 单捞鸥狼 农扁啊 后 康开狼 农扁甫 檬苞且 版快 滚欺甫 刘啊矫挪促.	if ( len >= nFree )		resize( len - nFree + 1 );			if ( m_Head <= m_Tail )		// normal order	{		//		//    H   T		// 0123456789		// ...abcd...		//				if ( m_Head == 0 )		{			nFree = m_BufferLen - m_Tail - 1;			*((T*)(m_Buffer+m_Tail)) = buf;		}		else		{			nFree = m_BufferLen - m_Tail;			if ( len <= nFree )			{				*((T*)(m_Buffer+m_Tail)) = buf;			}			else			{				memcpy( &m_Buffer[m_Tail] , (char *)&buf , nFree );				memcpy( m_Buffer , (((char*)&buf)+nFree), len - nFree );			}		}	} 	else						// reversed order	{		//		//     T  H		// 0123456789		// abcd...efg		//		*((T*)(m_Buffer+m_Tail)) = buf;	}	// advance m_Tail	m_Tail = ( m_Tail + len ) % m_BufferLen;	return len;		__END_CATCH}#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?