1 /*--------------------------------------------------------------------------
2   Project:  HorizonSDK
3   File:     rdt_Sender.cpp
4 
5   Copyright 2009 Nintendo.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2010-08-06#$
14   $Rev: 22868 $
15   $Author: hiratsu_daisuke $
16  *-------------------------------------------------------------------------*/
17 
18 #include "stdafx.h"
19 
20 #include <nn/rdt/CTR/rdt_Sender.h>
21 #include <nn/rdt/CTR/rdt_Result.h>
22 
23 #include "rdt_SenderImpl.h"
24 #include "rdt_Stopwatch.h"
25 
26 
27 namespace
28 {
29 }  // end of anonymous namespace
30 
31 namespace nn { namespace rdt { namespace CTR {
32 
33 
Sender(void)34 Sender::Sender(void)
35      :m_pImpl(NULL)
36 {
37 }
38 
39 
~Sender(void)40 Sender::~Sender(void)
41 {
42     Finalize();
43 }
44 
45 
Initialize(const struct SenderConfig & cfg)46 nn::Result Sender::Initialize(const struct SenderConfig &cfg)
47 {
48     if(m_pImpl)
49     {
50         return ResultAlreadyInitialized();
51     }
52     else if(cfg.pWorkBuf==NULL)
53     {
54         return ResultNullPointer();
55     }
56     else if(reinterpret_cast<u32>(cfg.pWorkBuf) % 8 != 0)
57     {
58         return ResultMisalignedAddress();
59     }
60     else
61     {
62         ASSERT(SENDER_WORKBUF_SIZE >= sizeof(SenderImpl));
63 
64         std::memset(cfg.pWorkBuf, 0, SENDER_WORKBUF_SIZE);  // とりあえず、ゼロクリア。
65         m_pImpl = new (cfg.pWorkBuf) SenderImpl();
66 #ifdef _WIN32
67         nn::Result result = m_pImpl->Initialize(cfg.sock, cfg.pSendBuf, cfg.sendBufSize);
68 #elif defined(NN_PLATFORM_CTR)
69         nn::Result result = m_pImpl->Initialize(cfg.nodeId, cfg.port, cfg.pSendBuf, cfg.sendBufSize);
70 #else
71     #error no platform selected
72 #endif
73         if(result.IsFailure())
74         {
75             m_pImpl->~SenderImpl();
76             m_pImpl = NULL;
77             return result;
78         }
79         else
80         {
81             return ResultSuccess();
82         }
83     }
84 }
85 
86 
Finalize(void)87 void Sender::Finalize(void)
88 {
89     if(m_pImpl)
90     {
91         m_pImpl->~SenderImpl();
92         m_pImpl = NULL;
93     }
94     else
95     {
96         // Do nothing.
97     }
98 }
99 
100 
Open(void)101 nn::Result Sender::Open(void)
102 {
103     if(!m_pImpl)
104     {
105         return ResultNotInitialized();
106     }
107 
108     return m_pImpl->Open();
109 }
110 
111 
Close(void)112 nn::Result Sender::Close(void)
113 {
114     if(!m_pImpl)
115     {
116         return ResultNotInitialized();
117     }
118 
119     return m_pImpl->Close();
120 }
121 
122 
Send(const void * pBuf,size_t bufSize)123 nn::Result Sender::Send(const void *pBuf, size_t bufSize)
124 {
125     if(!m_pImpl)
126     {
127         return ResultNotInitialized();
128     }
129 
130     return m_pImpl->Send(pBuf, bufSize);
131 }
132 
133 
Process(void)134 nn::Result Sender::Process(void)
135 {
136     if(!m_pImpl)
137     {
138         return ResultNotInitialized();
139     }
140 
141     static detail::Stopwatch s_sp("Sender::Process()");
142 
143     s_sp.Start();
144     nn::Result ret = m_pImpl->Process();
145     s_sp.Stop();
146 
147     return ret;
148 }
149 
150 
Cancel(void)151 void Sender::Cancel(void)
152 {
153     if(!m_pImpl)
154     {
155         LOG("Sender is not initialized.  Ignored %s.\n", __FUNCTION__);
156         return;
157     }
158 
159     m_pImpl->Cancel();
160 }
161 
162 
GetStatus(void) const163 enum SenderState Sender::GetStatus(void) const
164 {
165     if(!m_pImpl)
166     {
167         return SENDER_STATE_NOT_INITIALIZED;
168     }
169 
170     return m_pImpl->GetStatus();
171 }
172 
173 
SetPacketLossRatio(int per)174 void Sender::SetPacketLossRatio(int per)
175 {
176     if(!m_pImpl)
177     {
178         LOG("Sender is not initialized.  Ignored %s.\n", __FUNCTION__);
179         return;
180     }
181 
182     return m_pImpl->SetPacketLossRatio(per);
183 }
184 
185 
PrintDebugInfo(void) const186 void Sender::PrintDebugInfo(void) const
187 {
188     if(!m_pImpl)
189     {
190         LOG("Sender is not initialized.  Ignored %s.\n", __FUNCTION__);
191         return;
192     }
193 
194     m_pImpl->PrintDebugInfo();
195 
196     detail::Stopwatch::PrintAll();
197 }
198 
199 
200 }}} // namespace nn::rdt::CTR
201