socketoutputstream.cpp
来自「天之炼狱1服务器端源文件游戏服务端不完整」· C++ 代码 · 共 386 行
CPP
386 行
//////////////////////////////////////////////////////////////////////// // SocketOutputStream.cpp// // by Reiot// //////////////////////////////////////////////////////////////////////#include "SocketOutputStream.h"#include "Assert.h"#include "Packet.h"#include "VSDateTime.h"//////////////////////////////////////////////////////////////////////// constructor//////////////////////////////////////////////////////////////////////SocketOutputStream::SocketOutputStream ( Socket * sock , uint BufferLen ) throw ( Error ): m_Socket(sock), m_Buffer(NULL), m_BufferLen(BufferLen), m_Head(0), m_Tail(0){ __BEGIN_TRY// Assert( m_Socket != NULL ); Assert( m_BufferLen > 0 ); m_Buffer = new char[ m_BufferLen ]; __END_CATCH} //////////////////////////////////////////////////////////////////////// destructor//////////////////////////////////////////////////////////////////////SocketOutputStream::~SocketOutputStream () throw ( Error ){ __BEGIN_TRY if ( m_Buffer != NULL ) { // 楷搬捞 谗败辑 ConnectException 阑 罐酒 辆丰等 惑怕俊辑 // flush甫 且 版快 SIGPIPE 啊 抄促. 蝶扼辑, 公矫窍磊~ // flush(); delete [] m_Buffer; m_Buffer = NULL; } __END_CATCH}////////////////////////////////////////////////////////////////////////// write data to stream (output buffer)//// *Notes*//// ( ( m_Head = m_Tail + 1 ) || // ( ( m_Head == 0 ) && ( m_Tail == m_BufferLen - 1 ) )//// 老 锭 滚欺 full 肺 埃林茄促绰 巴阑 镭瘤 富扼. 蝶扼辑, 滚欺狼 后// 傍埃狼 农扁绰 亲惑 1 阑 哗拎具 茄促绰 荤角!////////////////////////////////////////////////////////////////////////uint SocketOutputStream::write ( const char * buf , uint len ) throw ( Error ){ __BEGIN_TRY // 泅犁 滚欺狼 后 康开阑 拌魂茄促. // (!) 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; memcpy( &m_Buffer[m_Tail] , buf , len ); } else { nFree = m_BufferLen - m_Tail; if ( len <= nFree ) memcpy( &m_Buffer[m_Tail] , buf , len ); else { memcpy( &m_Buffer[m_Tail] , buf , nFree ); memcpy( m_Buffer , &buf[nFree] , len - nFree ); } } } else { // reversed order // // T H // 0123456789 // abcd...efg // memcpy( &m_Buffer[m_Tail] , buf , len ); } // advance m_Tail m_Tail = ( m_Tail + len ) % m_BufferLen; return len; __END_CATCH} //////////////////////////////////////////////////////////////////////// write packet to stream (output buffer)//////////////////////////////////////////////////////////////////////void SocketOutputStream::writePacket ( const Packet * pPacket ) throw ( ProtocolException , Error ){ __BEGIN_TRY // 快急 菩哦酒捞叼客 菩哦农扁甫 免仿滚欺肺 敬促. PacketID_t packetID = pPacket->getPacketID(); write( (char*)&packetID , szPacketID ); PacketSize_t packetSize = pPacket->getPacketSize(); write( (char*)&packetSize , szPacketSize ); // 捞力 菩哦官叼甫 免仿滚欺肺 敬促. pPacket->write( *this ); /* if( packetID == Packet::PACKET_GC_UPDATE_INFO ) { ofstream file("flush.txt", ios::out | ios::app); file << "SEND GCUPDATE INFO" << endl; file.close(); } */// flush(); __END_CATCH}//////////////////////////////////////////////////////////////////////// flush stream (output buffer) to socket//////////////////////////////////////////////////////////////////////uint SocketOutputStream::flush () throw (IOException, ProtocolException, InvalidProtocolException, Error){ __BEGIN_TRY Assert( m_Socket != NULL ); uint nFlushed = 0; uint nSent = 0; uint nLeft; try { if ( m_Head < m_Tail ) { // // H T // 0123456789 // ...abcd... // nLeft = m_Tail - m_Head; while ( nLeft > 0 ) { nSent = m_Socket->send( &m_Buffer[m_Head] , nLeft , MSG_NOSIGNAL ); // NonBlockException力芭. by sigi.2002.5.17 if (nSent==0) return 0; nFlushed += nSent; nLeft -= nSent; m_Head += nSent; } Assert( nLeft == 0 ); } else if ( m_Head > m_Tail ) { // // T H // 0123456789 // abcd...efg // nLeft = m_BufferLen - m_Head; while ( nLeft > 0 ) { nSent = m_Socket->send( &m_Buffer[m_Head] , nLeft , MSG_NOSIGNAL ); // NonBlockException力芭. by sigi.2002.5.17 if (nSent==0) return 0; nFlushed += nSent; nLeft -= nSent; m_Head += nSent; } Assert( m_Head == m_BufferLen ); m_Head = 0; nLeft = m_Tail; while ( nLeft > 0 ) { nSent = m_Socket->send( &m_Buffer[m_Head] , nLeft , MSG_NOSIGNAL ); // NonBlockException力芭. by sigi.2002.5.17 if (nSent==0) return 0; nFlushed += nSent; nLeft -= nSent; m_Head += nSent; } Assert( nLeft == 0 ); } if ( m_Head != m_Tail ) { cout << "m_Head : " << m_Head << endl; cout << "m_Tail : " << m_Tail << endl; Assert( m_Head == m_Tail ); } } catch ( NonBlockingIOException& ) { // 老何父 send登绊 付绰 版快 // by sigi. 2002.9.27 if (nSent>0) { m_Head += nSent; } cerr << "SocketOutputStream NonBlockingIOException Check! " << endl; throw NonBlockingIOException( "SocketOutputStream NonBlockingIOException Check"); } catch ( InvalidProtocolException & t ) { // 老何父 send登绊 付绰 版快 // by sigi. 2002.9.27 if (nSent>0) { m_Head += nSent; } cerr << "SocketOutputStream Exception Check! " << endl; cerr << t.toString() << endl; throw InvalidProtocolException( "SocketOutputStream Exception Check"); } /* ofstream file("flush.txt", ios::out | ios::app); file << "flush send size: " << nFlushed << " left size : " << nLeft << endl; file.close(); */ // 梅何磐 促矫.. by sigi. 2002.9.26 m_Head = m_Tail = 0; return nFlushed; __END_CATCH}//////////////////////////////////////////////////////////////////////// resize buffer//////////////////////////////////////////////////////////////////////void SocketOutputStream::resize ( int size ) throw ( IOException , Error ){ __BEGIN_TRY //Assert( m_Socket != NULL ); Assert( size != 0 ); int orgSize = size; // 梨篮 resize甫 规瘤窍扁 困秦辑.. 泅犁 滚欺狼 1/2父怒 疵妨杭鳖 by sigi. 2002.9.26 size = max(size, (int)(m_BufferLen>>1)); uint newBufferLen = m_BufferLen + size; uint len = length(); if ( size < 0 ) { // 父距 农扁甫 临捞妨绰单 滚欺俊 甸绢乐绰 单捞鸥甫 // 促 给淬酒尘 版快 if ( newBufferLen < 0 || newBufferLen < len ) throw IOException("new buffer is too small!"); } // 货 滚欺甫 且寸罐绰促. char * newBuffer = new char[ newBufferLen ]; // 盔贰 滚欺狼 郴侩阑 汗荤茄促. if ( m_Head < m_Tail ) { // // H T // 0123456789 // ...abcd... // memcpy( newBuffer , &m_Buffer[m_Head] , m_Tail - m_Head ); } else if ( m_Head > m_Tail ) { // // T H // 0123456789 // abcd...efg // memcpy( newBuffer , &m_Buffer[m_Head] , m_BufferLen - m_Head ); memcpy( &newBuffer[ m_BufferLen - m_Head ] , m_Buffer , m_Tail ); } // 盔贰 滚欺甫 昏力茄促. delete [] m_Buffer; // 滚欺 棺 滚欺 农扁甫 犁汲沥茄促. m_Buffer = newBuffer; m_BufferLen = newBufferLen; m_Head = 0; m_Tail = len; VSDateTime current = VSDateTime::currentDateTime(); if ( m_Socket == NULL ) { // m_Socket 捞 NULL 捞扼绰 巴篮 捞 胶飘覆捞 宏肺靛 某胶飘侩 胶飘覆捞扼绰 富捞促. // resize 啊 阂啡促绰 富篮 菩哦狼 getPacketSize() 窃荐啊 肋给登绢 乐促绰 富捞促. filelog( "packetsizeerror.txt", "PacketID = %u", *(PacketID_t*)m_Buffer ); } else { ofstream ofile("buffer_resized.log",ios::app); ofile << "[" << current.toString().c_str() << "] " << m_Socket->getHost().c_str() << " - SocketOutputStream resized: " << orgSize << " / " << size << "/" << m_BufferLen << " bytes!" << endl; ofile.close(); } __END_CATCH}//////////////////////////////////////////////////////////////////////// get data's size in buffer//////////////////////////////////////////////////////////////////////uint SocketOutputStream::length () const throw (){ if ( m_Head < m_Tail ) return m_Tail - m_Head; else if ( m_Head > m_Tail ) return m_BufferLen - m_Head + m_Tail; return 0;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?