📄 sbillingplayer.cpp
字号:
//////////////////////////////////////////////////////////////////// filename : SBillingPlayer.cpp//////////////////////////////////////////////////////////////////// include files#include "SBillingPlayer.h"#include "Socket.h"#include "SocketInputStream.h"#include "SocketOutputStream.h"#include "Assert.h"#include "Packet.h"#include "SBillingPlayerManager.h"#include "CBillingPacketHeader.h"#include "CBillingPacketErrorBody.h"#include "PayUser.h"#include "PayUserManager.h"#include "CBillingPacketRequestLoginBody.h"#include "CBillingPacketResponseLoginBody.h"#include "CBillingPacketRequestMinusPointBody.h"#include "CBillingPacketRequestMinusMinuteBody.h"#include "CBillingPacketRequestLogoutBody.h"#include "CBillingPacketRequestIntervalValidationBody.h"#include "CBillingPacketResponseMinusPointBody.h"#include "CBillingPacketResponseMinusMinuteBody.h"#include "CBillingPacketResponseLogoutBody.h"#include "CBillingPacketResponseIntervalValidationBody.h"const int defaultSBillingPlayerInputStreamSize = 10240;const int defaultSBillingPlayerOutputStreamSize = 10240;// constructorSBillingPlayer::SBillingPlayer() throw ( Error ){}// constructorSBillingPlayer::SBillingPlayer( Socket* pSocket ) throw ( Error ){ __BEGIN_TRY Assert( pSocket != NULL ); m_pSocket = pSocket; // create socket input stream m_pInputStream = new SocketInputStream( m_pSocket, defaultSBillingPlayerInputStreamSize ); Assert( m_pInputStream != NULL ); // create socket output stream m_pOutputStream = new SocketOutputStream( m_pSocket, defaultSBillingPlayerOutputStreamSize ); Assert( m_pOutputStream != NULL ); __END_CATCH}// destructorSBillingPlayer::~SBillingPlayer() throw ( Error ){ __BEGIN_TRY // delete socket intput stream SAFE_DELETE( m_pInputStream ); // delete socket output stream SAFE_DELETE( m_pOutputStream ); // delete socket if ( m_pSocket != NULL ) { m_pSocket->close(); SAFE_DELETE( m_pSocket ); filelog( LOGFILE_CBILLING_PLAYER, "Close Socket" ); } __END_CATCH}void SBillingPlayer::processInput() throw ( IOException, Error ){ __BEGIN_TRY try { m_pInputStream->fill(); } catch ( NonBlockingIOException& nbie ) { } __END_CATCH}void SBillingPlayer::processOutput() throw ( IOException, Error ){ __BEGIN_TRY try { m_pOutputStream->flush(); } catch ( InvalidProtocolException& ) { throw DisconnectException( "捞惑茄 菩哦烙" ); } __END_CATCH}// parse packet and execute packet handlervoid SBillingPlayer::processCommand() throw ( IOException, Error ){ __BEGIN_TRY try { // 涝仿 滚欺俊 甸绢乐绰 肯傈茄 菩哦甸阑 葛炼府 贸府茄促. while ( true ) { // 庆歹甫 历厘且 滚欺 积己 CBillingPacketHeader header; // 老窜 庆歹甫 佬绰促. peek. 涝仿滚欺俊辑 瘤快瘤绰 臼绰促. // 庆歹啊 肯傈窍瘤 臼栏搁 菩胶 if ( !m_pInputStream->peek( (char*)&header, szCBillingPacketHeaderInfo ) ) return; header.Version_No = ntohl( header.Version_No ); header.Packet_Type = ntohl( header.Packet_Type ); header.Method_Code = ntohl( header.Method_Code ); header.Return_Code = ntohl( header.Return_Code ); header.Body_Length = ntohl( header.Body_Length ); //////////////////////////////////////////////////////////////////////////////////// // Packet_Type, Method_Code, Return_Code 俊 蝶扼 官叼啊 肯傈洒 粮犁窍绰瘤 犬牢茄促. //////////////////////////////////////////////////////////////////////////////////// if ( header.Packet_Type != CBILLING_PACKET_RESPONSE ) { cout << "error" << endl; cout << header.toString().c_str() << endl; return; // 俊矾促 扯菩~ } // 角菩老 版快 if ( header.Return_Code != CBILLING_RETURN_CODE_SUCCESS ) { // 官叼啊 肯傈洒 甸绢乐绰瘤 犬牢茄促. if ( m_pInputStream->length() < szCBillingPacketHeaderInfo + szCBillingPacketErrorBodyInfo ) return; // 捞力 角力肺 葛炼府 佬绢 郴磊. CBillingPacketErrorBody body; header.read( *m_pInputStream ); body.read( *m_pInputStream ); executeError( header, body ); } else // 己傍老 版快 { switch ( header.Method_Code ) { case CBILLING_METHOD_CODE_INTERVAL_VALIDATION: { // 官叼啊 肯傈洒 甸绢乐绰瘤 犬牢茄促. if ( m_pInputStream->length() < szCBillingPacketHeaderInfo + szCBillingPacketResponseIntervalValidationBodyInfo ) return; // 捞力 角力肺 葛炼府 佬绢 郴磊. CBillingPacketResponseIntervalValidationBody body; header.read( *m_pInputStream ); body.read( *m_pInputStream ); // 角青 executeIntervalValidation( header, body ); break; } case CBILLING_METHOD_CODE_LOGIN : { // 官叼啊 肯傈洒 甸绢乐绰瘤 犬牢茄促. if ( m_pInputStream->length() < szCBillingPacketHeaderInfo + szCBillingPacketResponseLoginBodyInfo ) return; // 捞力 角力肺 葛炼府 佬绢 郴磊. CBillingPacketResponseLoginBody body; header.read( *m_pInputStream ); body.read( *m_pInputStream ); // 角青 executeLogin( header, body ); break; } case CBILLING_METHOD_CODE_MINUS_POINT: { // 官叼啊 肯傈洒 甸绢乐绰瘤 犬牢茄促. if ( m_pInputStream->length() < szCBillingPacketHeaderInfo + szCBillingPacketResponseMinusPointBodyInfo ) return; // 捞力 角力肺 葛炼府 佬绢 郴磊. CBillingPacketResponseMinusPointBody body; header.read( *m_pInputStream ); body.read( *m_pInputStream ); // 角青 executeMinusPoint( header, body ); break; } case CBILLING_METHOD_CODE_MINUS_MINUTE: { // 官叼啊 肯傈洒 甸绢乐绰瘤 犬牢茄促. if ( m_pInputStream->length() < szCBillingPacketHeaderInfo + szCBillingPacketResponseMinusMinuteBodyInfo ) return; // 捞力 角力肺 葛炼府 佬绢 郴磊. CBillingPacketResponseMinusMinuteBody body; header.read( *m_pInputStream ); body.read( *m_pInputStream ); // 角青 executeMinusMinute( header, body ); break; } case CBILLING_METHOD_CODE_LOGOUT: { // 官叼啊 肯傈洒 甸绢乐绰瘤 犬牢茄促. if ( m_pInputStream->length() < szCBillingPacketHeaderInfo + szCBillingPacketResponseLogoutBodyInfo ) return; // 捞力 角力肺 葛炼府 佬绢 郴磊. CBillingPacketResponseLogoutBody body; header.read( *m_pInputStream ); body.read( *m_pInputStream ); // 角青 executeLogout( header, body ); break; } default: { throw Error( "Invaild Code" ); // 俊矾促. break; } } } } } catch ( NoSuchElementException& nsee ) { throw Error( nsee.toString() ); } catch ( InsufficientDataException ) { // do nothing } __END_CATCH}// send packet to player's output buffervoid SBillingPlayer::sendPacket( Packet* pPacket ) throw ( ProtocolException, Error ){ __BEGIN_TRY //pPacket->write( *m_pOutputStream ); __END_CATCH}// disconnectvoid SBillingPlayer::disconnect( bool bDisconnected ) throw ( InvalidProtocolException, Error ){ __BEGIN_TRY try { if ( bDisconnected == UNDISCONNECTED ) { m_pOutputStream->flush(); } m_pSocket->close(); } catch ( InvalidProtocolException& t ) { cerr << "SBillingPlayer::disconnect Exception occur!!" << endl; cerr << t.toString() << endl; m_pSocket->close(); } __END_CATCH}// set socketvoid SBillingPlayer::setSocket( Socket* pSocket ) throw (){ __BEGIN_TRY m_pSocket = pSocket; SAFE_DELETE( m_pInputStream ); m_pInputStream = new SocketInputStream( m_pSocket ); SAFE_DELETE( m_pOutputStream ); m_pOutputStream = new SocketOutputStream( m_pSocket );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -