1 /*--------------------------------------------------------------------------
2   Project:  HorizonSDK
3   File:     rdt_Receiver.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 
21 #include <nn/rdt/CTR/rdt_Receiver.h>
22 #include <nn/rdt/CTR/rdt_Result.h>
23 
24 #include "rdt_ReceiverImpl.h"
25 #include "rdt_Stopwatch.h"
26 
27 namespace
28 {
29 
30 }  // end of anonymous namespace
31 
32 
33 namespace nn { namespace rdt { namespace CTR {
34 
35 
Receiver(void)36 Receiver::Receiver(void)
37      :m_pImpl(NULL)
38 {
39     ASSERT(RECEIVER_WORKBUF_SIZE >= sizeof(ReceiverImpl));
40 }
41 
42 
~Receiver(void)43 Receiver::~Receiver(void)
44 {
45     Finalize();
46 }
47 
48 
Initialize(const struct ReceiverConfig & cfg)49 nn::Result Receiver::Initialize(const struct ReceiverConfig &cfg)
50 {
51     if(m_pImpl)
52     {
53         return ResultAlreadyInitialized();
54     }
55     else if(cfg.pWorkBuf==NULL)
56     {
57         return ResultNullPointer();
58     }
59     else if(reinterpret_cast<u32>(cfg.pWorkBuf) % 8 != 0)
60     {
61         return ResultMisalignedAddress();
62     }
63     else
64     {
65         ASSERT(RECEIVER_WORKBUF_SIZE >= sizeof(ReceiverImpl));
66 
67         std::memset(cfg.pWorkBuf, 0, RECEIVER_WORKBUF_SIZE);  // 念のため、ゼロクリア。
68         m_pImpl = new (cfg.pWorkBuf) ReceiverImpl();
69 #ifdef _WIN32
70         nn::Result result = m_pImpl->Initialize(cfg.sock, cfg.pRecvBuf, cfg.recvBufSize);
71 #elif defined(NN_PLATFORM_CTR)
72         nn::Result result = m_pImpl->Initialize(cfg.nodeId, cfg.port, cfg.pRecvBuf, cfg.recvBufSize);
73 #else
74     #error no platform selected
75 #endif
76         if(result.IsFailure())
77         {
78             m_pImpl->~ReceiverImpl();
79             m_pImpl = NULL;
80             return result;
81         }
82         else
83         {
84             return ResultSuccess();
85         }
86     }
87 }
88 
89 
Finalize(void)90 void Receiver::Finalize(void)
91 {
92     if(m_pImpl)
93     {
94         m_pImpl->~ReceiverImpl();
95         m_pImpl = NULL;
96     }
97     else
98     {
99         // Do nothing.
100     }
101 }
102 
103 
Wait(void)104 nn::Result Receiver::Wait(void)
105 {
106     if(!m_pImpl)
107     {
108         return ResultNotInitialized();
109     }
110 
111     return m_pImpl->Wait();
112 }
113 
114 
Close(void)115 nn::Result Receiver::Close(void)
116 {
117     if(!m_pImpl)
118     {
119         return ResultNotInitialized();
120     }
121 
122     return m_pImpl->Close();
123 }
124 
125 
Receive(void * pBuf,size_t * pRecvSize,size_t bufSize)126 nn::Result Receiver::Receive(void *pBuf, size_t *pRecvSize, size_t bufSize)
127 {
128     if(!m_pImpl)
129     {
130         return ResultNotInitialized();
131     }
132 
133     return m_pImpl->Receive(pBuf, pRecvSize, bufSize);
134 }
135 
136 
Process(void)137 nn::Result Receiver::Process(void)
138 {
139     if(!m_pImpl)
140     {
141         return ResultNotInitialized();
142     }
143 
144     static detail::Stopwatch s_sp("Receiver::Process()");
145 
146     s_sp.Start();
147     nn::Result ret = m_pImpl->Process();
148     s_sp.Stop();
149 
150     return ret;
151 }
152 
153 
Cancel(void)154 void Receiver::Cancel(void)
155 {
156     if(!m_pImpl)
157     {
158         LOG("Receiver is not initialized.  Ignored %s.\n", __FUNCTION__);
159         return;
160     }
161 
162     m_pImpl->Cancel();
163 }
164 
165 
GetStatus(void) const166 enum ReceiverState Receiver::GetStatus(void) const
167 {
168     if(!m_pImpl)
169     {
170         return RECEIVER_STATE_NOT_INITIALIZED;
171     }
172 
173     return m_pImpl->GetStatus();
174 }
175 
176 
SetPacketLossRatio(int per)177 void Receiver::SetPacketLossRatio(int per)
178 {
179     if(!m_pImpl)
180     {
181         LOG("Receiver is not initialized.  Ignored %s.\n", __FUNCTION__);
182         return;
183     }
184 
185     m_pImpl->SetPacketLossRatio(per);
186 }
187 
188 
PrintDebugInfo(void) const189 void Receiver::PrintDebugInfo(void) const
190 {
191     if(!m_pImpl)
192     {
193         LOG("Receiver is not initialized.  Ignored %s.\n", __FUNCTION__);
194         return;
195     }
196 
197     m_pImpl->PrintDebugInfo();
198 
199     detail::Stopwatch::PrintAll();
200 }
201 
202 
203 }}} // namespace nn::rdt::CTR
204