1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     rdt_Sender.cpp
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  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   $Rev: 46347 $
14  *---------------------------------------------------------------------------*/
15 
16 #include "stdafx.h"
17 
18 #include <nn/rdt/CTR/rdt_Sender.h>
19 #include <nn/rdt/CTR/rdt_Result.h>
20 
21 #include "rdt_SenderImpl.h"
22 #include "rdt_Stopwatch.h"
23 
24 
25 namespace
26 {
27 }  // End of anonymous namespace
28 
29 namespace nn { namespace rdt { namespace CTR {
30 
31 
Sender(void)32 Sender::Sender(void)
33      :m_pImpl(NULL)
34 {
35 }
36 
37 
~Sender(void)38 Sender::~Sender(void)
39 {
40     Finalize();
41 }
42 
43 
Initialize(const struct SenderConfig & cfg)44 nn::Result Sender::Initialize(const struct SenderConfig &cfg)
45 {
46     if(m_pImpl)
47     {
48         return ResultAlreadyInitialized();
49     }
50     else if(cfg.pWorkBuf==NULL)
51     {
52         return ResultNullPointer();
53     }
54     else if(reinterpret_cast<u32>(cfg.pWorkBuf) % 8 != 0)
55     {
56         return ResultMisalignedAddress();
57     }
58     else
59     {
60         ASSERT(SENDER_WORKBUF_SIZE >= sizeof(SenderImpl));
61 
62         std::memset(cfg.pWorkBuf, 0, SENDER_WORKBUF_SIZE);  // For now, zero clear.
63         m_pImpl = new (cfg.pWorkBuf) SenderImpl();
64 #ifdef _WIN32
65         nn::Result result = m_pImpl->Initialize(cfg.sock, cfg.pSendBuf, cfg.sendBufSize);
66 #elif defined(NN_PLATFORM_CTR)
67         nn::Result result = m_pImpl->Initialize(cfg.nodeId, cfg.port, cfg.pSendBuf, cfg.sendBufSize);
68 #else
69     #error no platform selected
70 #endif
71         if(result.IsFailure())
72         {
73             m_pImpl->~SenderImpl();
74             m_pImpl = NULL;
75             return result;
76         }
77         else
78         {
79             return ResultSuccess();
80         }
81     }
82 }
83 
84 
Finalize(void)85 void Sender::Finalize(void)
86 {
87     if(m_pImpl)
88     {
89         m_pImpl->~SenderImpl();
90         m_pImpl = NULL;
91     }
92     else
93     {
94         // Do nothing.
95     }
96 }
97 
98 
Open(void)99 nn::Result Sender::Open(void)
100 {
101     if(!m_pImpl)
102     {
103         return ResultNotInitialized();
104     }
105 
106     return m_pImpl->Open();
107 }
108 
109 
Close(void)110 nn::Result Sender::Close(void)
111 {
112     if(!m_pImpl)
113     {
114         return ResultNotInitialized();
115     }
116 
117     return m_pImpl->Close();
118 }
119 
120 
Send(const void * pBuf,size_t bufSize)121 nn::Result Sender::Send(const void *pBuf, size_t bufSize)
122 {
123     if(!m_pImpl)
124     {
125         return ResultNotInitialized();
126     }
127 
128     return m_pImpl->Send(pBuf, bufSize);
129 }
130 
131 
Process(void)132 nn::Result Sender::Process(void)
133 {
134     if(!m_pImpl)
135     {
136         return ResultNotInitialized();
137     }
138 
139     static detail::Stopwatch s_sp("Sender::Process()");
140 
141     s_sp.Start();
142     nn::Result ret = m_pImpl->Process();
143     s_sp.Stop();
144 
145     return ret;
146 }
147 
148 
Cancel(void)149 void Sender::Cancel(void)
150 {
151     if(!m_pImpl)
152     {
153         LOG("Sender is not initialized.  Ignored %s.\n", __FUNCTION__);
154         return;
155     }
156 
157     m_pImpl->Cancel();
158 }
159 
160 
GetStatus(void) const161 enum SenderState Sender::GetStatus(void) const
162 {
163     if(!m_pImpl)
164     {
165         return SENDER_STATE_NOT_INITIALIZED;
166     }
167 
168     return m_pImpl->GetStatus();
169 }
170 
171 
SetPacketLossRatio(int per)172 void Sender::SetPacketLossRatio(int per)
173 {
174     if(!m_pImpl)
175     {
176         LOG("Sender is not initialized.  Ignored %s.\n", __FUNCTION__);
177         return;
178     }
179 
180     return m_pImpl->SetPacketLossRatio(per);
181 }
182 
183 
PrintDebugInfo(void) const184 void Sender::PrintDebugInfo(void) const
185 {
186     if(!m_pImpl)
187     {
188         LOG("Sender is not initialized.  Ignored %s.\n", __FUNCTION__);
189         return;
190     }
191 
192     m_pImpl->PrintDebugInfo();
193 
194     detail::Stopwatch::PrintAll();
195 }
196 
197 
198 }}} // namespace nn::rdt::CTR
199