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