📄 warsocket.cpp
字号:
#include "StdAfx.h"#include "WarSocket.h" // class implemented#ifndef WAR_ASSERT_H_INCLUDED# define WAR_ASSERT_H_INCLUDED# include <assert.h>#endif#ifndef WAR_AUTO_LOCK_H# include "WarAutoLock.h"#endif#ifndef WAR_LOG_H# include "WarLog.h"#endif#ifndef WAR_PERFMON_DEF_H# include "WarPerfmonDef.h"#endif/////////////////////////////// PUBLIC /////////////////////////////////////////============================= LIFECYCLE ====================================WarSocket::WarSocket(): mpCompanion(NULL){ WAR_DB_PERFMON_INC(WAR_PRFDEBUG_NUM_SOCKETS); war_socket_io_ptr_t new_partner_ptr = new WarSocketIo; Initialize(new_partner_ptr);}// WarSocketWarSocket::WarSocket(war_socket_io_ptr_t& companionPtr){ WAR_DB_PERFMON_INC(WAR_PRFDEBUG_NUM_SOCKETS); Initialize(companionPtr);}WarSocket::~WarSocket(){ try { WarLog net_log(WARLOG_SOCKET, "WarSocket::~WarSocket()", this); if (net_log) { net_log << "The socket# " << GetSeqNumber() << " is done. " << GetBytesSent() << " bytes sent and " << GetBytesReceived() << " bytes received. "; if (!GetConnectTime().IsEmpty()) { net_log<< "The connection lasted for " << (int)(GetCloseTime().GetTime() - GetConnectTime().GetTime()) << " seconds."; } net_log << war_endl; } } catch(...) { ; // Who gives a shit } WAR_DB_PERFMON_DEC(WAR_PRFDEBUG_NUM_SOCKETS);}// ~WarSocket//============================= OPERATORS ====================================// OPERATORS// OPERATIONS void WarSocket::Close(){ WarLog net_log(WARLOG_SOCKET, "WarSocket::Close()", this); if (net_log) { net_log << "Closing socket " << GetSeqNumber() << war_endl; } assert(mpCompanion != NULL); mpCompanion->Close();}//void WarSocket::Connect(WarSocketTypesE commType, const WarNetAddress& rremoteHost, const WarNetAddress *plocalAddress) throw(WarException){ WarLog net_log(WARLOG_SOCKET, "WarSocket::Connect", this); if (net_log) { net_log << "Connecting socket " << GetSeqNumber() << ' '; if (plocalAddress) net_log << *plocalAddress; else net_log << "[unspecified]"; net_log << " --> " << rremoteHost.Explain() << war_endl; } mpCompanion->Connect(commType, rremoteHost, plocalAddress);}void WarSocket::Listen(WarNetAddress& rLocalAddress, int backlog, std::pair<war_port_t,war_port_t> *pRange) throw(WarException){ WarLog net_log(WARLOG_SOCKET, "WarSocket::Listen", this); if (net_log) { net_log << "Socket " << GetSeqNumber() << " Listening to " << rLocalAddress.Explain() << " backlog=" << backlog; if (pRange) net_log << " Range=" << pRange->first << '-' << pRange->second; net_log << war_endl; } mpCompanion->Listen(rLocalAddress, backlog, pRange);}int WarSocket::Send(war_ccstr_t buf, size_t len, int flags) throw(WarException){ WarLog net_log(WARLOG_SOCKET, "WarSocket::Send", this); if (net_log) { net_log << "Socket " << GetSeqNumber() << " Sending " << len << " bytes" << war_endl; } return mpCompanion->Send(buf, len, flags);}void WarSocket::SendWithCallback(war_transfer_buffer_ptr_t& outBuffer, size_t numSegments) throw(WarException){ WarLog net_log(WARLOG_SOCKET, "WarSocket::SendWithCallback", this); if (net_log) { net_log << "Sending with callback " << outBuffer->GetBytesUsed() << " bytes" << war_endl; } mpCompanion->SendWithCallback(outBuffer, numSegments);}int WarSocket::Recv(war_cstr_t buf, size_t len, int flags) throw(WarException){ WarLog net_log(WARLOG_SOCKET, "WarSocket::Recv", this); if (net_log) { net_log << "Socket " << GetSeqNumber() << " Receiving " << len << " bytes" << war_endl; } return mpCompanion->Recv(buf, len, flags);}void WarSocket::RecvWithCallback(war_transfer_buffer_ptr_t& inBuffer, size_t numSegments) throw(WarException){ WarLog net_log(WARLOG_SOCKET, "WarSocket::RecvWithCallback", this); if (net_log) { net_log << "Socket " << GetSeqNumber() << " Receiving with callback " << inBuffer->GetBytesUsed() << " bytes" << war_endl; } mpCompanion->RecvWithCallback(inBuffer, numSegments);}// Set blocking or non-blocking (async) modevoid WarSocket::SetAsyncMode(bool makeAsync, bool doForce) throw(WarException){ mpCompanion->SetAsyncMode(makeAsync, doForce);}// ACCESS// INQUIRYconst WarNetAddress& WarSocket::GetLocalAddress() const{ return mpCompanion->GetLocalAddress();}const WarNetAddress& WarSocket::GetRemoteAddress() const{ return mpCompanion->GetRemoteAddress();} bool WarSocket::IsClearToSend() const{ return mpCompanion->IsClearToSend();}bool WarSocket::IsClearToReceive() const{ return mpCompanion->IsClearToReceive();}bool WarSocket::IsAsync() const{ return mpCompanion->IsAsync();}bool WarSocket::IsOpen() const{ return mpCompanion->IsOpen();}war_uint64_t WarSocket::GetSeqNumber() const{ return mpCompanion->GetSeqNumber();}war_uint64_t WarSocket::GetBytesSent() const{ return mpCompanion->GetBytesSent();}war_uint64_t WarSocket::GetBytesReceived() const{ return mpCompanion->GetBytesReceived();}WarTime WarSocket::GetConnectTime() const{ return mpCompanion->GetConnectTime();}WarTime WarSocket::GetCloseTime() const{ return mpCompanion->GetCloseTime();}WarTime WarSocket::GetIdleTimeSince() const{ return mpCompanion->GetIdleTimeSince();}WarSocketStatesE WarSocket::GetCurrentState() const{ return mpCompanion->GetCurrentState();}war_time_t WarSocket::GetConnectedTime() const{ return mpCompanion->GetConnectedTime();}bool WarSocket::IsLocalAddress(const struct in_addr& addr) const{ // Enumerate IP addresses std::list<long> ip_list; if (htonl(INADDR_LOOPBACK) == addr.s_addr) return true; char ac[128]; if (gethostname(ac, sizeof(ac)) != -1) { struct hostent* phe = gethostbyname(ac); if (phe != 0) { for (int i = 0; phe->h_addr_list[i] != 0; ++i) { struct in_addr addr; memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr)); if (addr.s_addr == addr.s_addr) return true; } } } return false;}/////////////////////////////// PROTECTED ///////////////////////////////////// CALLBACKS FOR ASYNC IOvoid WarSocket::OnConnect(const WarError& status){}void WarSocket::OnAccept(const WarError& status, war_socket_t newSocket, const WarNetAddress& remoteAddress, const WarNetAddress& localAddress){}void WarSocket::OnReceived(const WarError& status, war_transfer_buffer_ptr_t& buffer){}void WarSocket::OnSent(const WarError& status, war_transfer_buffer_ptr_t& buffer){}void WarSocket::OnClose(const WarError& status){}void WarSocket::OnStateChanged(){}void WarSocket::PreOnConnect(const WarError& status){ OnConnect(status);}void WarSocket::PreOnAccept(const WarError& status, war_socket_t newSocket, const WarNetAddress& remoteAddress, const WarNetAddress& localAddress){ OnAccept(status, newSocket, remoteAddress, localAddress);}void WarSocket::PreOnReceived(const WarError& status, war_transfer_buffer_ptr_t& buffer){ OnReceived(status, buffer);}void WarSocket::PreOnSent(const WarError& status, war_transfer_buffer_ptr_t& buffer){ OnSent(status, buffer);}void WarSocket::PreOnClose(const WarError& status){ OnClose(status);}void WarSocket::PreOnStateChanged(){ OnStateChanged();}void WarSocket::SetCurrentState(WarSocketStatesE newState) const throw(WarException){ mpCompanion->SetCurrentState(newState);}/////////////////////////////// PRIVATE ///////////////////////////////////void WarSocket::Initialize(war_socket_io_ptr_t& companionPtr){ assert(!companionPtr.IsEmpty()); // Glue companions together mCompanionPtr = companionPtr; mpCompanion = &(*mCompanionPtr); mpCompanion->mpCompanion = this; WarCollector<char> identifier; identifier << "Socket(" << GetSeqNumber() << ")"; AddLogIdentifierTag(identifier.GetValue().c_str());}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -