/*-------------------------------------------------------------------------- Project: HorizonSDK File: rdt_HostBase.cpp Copyright 2009 Nintendo. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Date:: 2010-09-16#$ $Rev: 26004 $ $Author: hiratsu_daisuke $ *-------------------------------------------------------------------------*/ #include "stdafx.h" #include "rdt_HostBase.h" #include "rdt_Utility.h" #include // rand()用。 #include namespace { } // end of anonymous namespace namespace nn { namespace rdt { namespace CTR { HostBase::HostBase(void) :m_initialized(false) { } ///< デストラクタ HostBase::~HostBase(void) { Finalize(); } nn::Result HostBase::Initialize( #ifdef _WIN32 SOCKET &sock #elif defined(NN_PLATFORM_CTR) u16 nodeId, u8 port #endif ) { if(m_initialized) { return ResultAlreadyInitialized(); } else { nn::Result result = m_transceiver.Initialize( #ifdef _WIN32 sock #elif defined(NN_PLATFORM_CTR) nodeId, port #endif ); // TORIAEZU。CTRでsrand()を実行しないと、rand()で得られる値がオール0だった…。 std::srand(1234); if(result.IsFailure()) { return result; } else { m_packetLossRatio = 0; m_resultCode = ResultSuccess(); m_initialized = true; return ResultSuccess(); } } } void HostBase::Finalize(void) { if(m_initialized) { m_initialized = false; m_transceiver.Finalize(); } else { // Do nothing. } } void HostBase::SetPacketLossRatio(int per) { ASSERT(m_initialized); ASSERT((0<=per) && (per<=100)); m_packetLossRatio = per; } void HostBase::PrintProperty(void) { LOG("m_transceiver: %d (%d)\n", offsetof(HostBase, m_transceiver), sizeof(Transceiver)); LOG("m_packetLossRatio: %d (%d)\n", offsetof(HostBase, m_packetLossRatio), sizeof(int)); LOG("m_resultCode: %d (%d)\n", offsetof(HostBase, m_resultCode), sizeof(Result)); LOG("sizeof(HostBase)=%ld\n", (long) sizeof(HostBase)); } void HostBase::putSegment(const Segment &seg) { // パケロスのエミュレーションはここで行う。 // パケロス設定が無いときに、高コストな剰余計算を // 行わせたくなかったので、以下のような書き方をしている。 int r = (m_packetLossRatio==0) ? 100 : std::rand()%100; if(r >= m_packetLossRatio) { nn::Result result = m_transceiver.Put(seg); if(result.IsFailure()) { errorHandling(result); } #if 0 LOG("Sent segment is:\n"); seg.PrintDebugInfo(); #endif } else { LOG("Packet loss happened! Lost segment is:\n"); seg.PrintDebugInfo(); } } nn::Result HostBase::pullSegment(Segment *pSeg) { ASSERT(pSeg!=NULL); nn::Result result = m_transceiver.Pull(pSeg); if(result.IsFailure() && result!=ResultNoData()) { errorHandling(result); } return result; } void HostBase::sendRstSegment(u32 seq) { VERBOSE("%s called.\n", __FUNCTION__); Segment seg; seg.ClearHeader(); seg.SetRst(); seg.SetSeqNumber(seq); putSegment(seg); } void HostBase::sendRstAckSegment(u32 seq, u32 ack) { VERBOSE("%s called.\n", __FUNCTION__); Segment seg; seg.ClearHeader(); seg.SetRst(); seg.SetSeqNumber(seq); seg.SetAckNumber(ack); putSegment(seg); } void HostBase::errorHandling(Result resultCode) { if(resultCode.IsFailure()) { LOG("Failure result code will be set...\n"); } m_resultCode = resultCode; } }}} // namespace nn::rdt::CTR