1 /*--------------------------------------------------------------------------
2   Project:  HorizonSDK
3   File:     rdt_ReceiverImpl.h
4   Copyright 2009 Nintendo. All rights reserved.
5   These coded instructions, statements, and computer programs contain
6   proprietary information of Nintendo of America Inc. and/or Nintendo
7   Company Ltd., and are protected by Federal copyright law. They may
8   not be disclosed to third parties or copied or duplicated in any form,
9   in whole or in part, without the prior written consent of Nintendo.
10   $Date:: 2010-09-13#$
11   $Rev: 25720 $
12   $Author: hiratsu_daisuke $
13  *-------------------------------------------------------------------------
14 
15 
16 */
17 
18 #include "stdafx.h"
19 
20 #ifndef NN_RDT_RECEIVERIMPL_H_
21 #define NN_RDT_RECEIVERIMPL_H_
22 
23 #include "rdt_HostBase.h"
24 #include "rdt_ReceiveBuffer.h"
25 #include "rdt_Segment.h"
26 
27 
28 namespace nn { namespace rdt { namespace CTR {
29 
30 // Forward declaration
31 class ReceiverStateBase;
32 
33 /* Please see man pages for details
34 
35 */
36 class ReceiverImpl : public HostBase{
37 public:
38 /* Please see man pages for details
39 
40 */
41     ReceiverImpl(void) throw();
42 
43 /* Please see man pages for details
44 
45 */
46     virtual ~ReceiverImpl(void);
47 
48 /* Please see man pages for details
49 
50 */
51 #ifdef _WIN32
52     nn::Result Initialize(SOCKET sock, void *pRecvBuf, u16 recvBufSize);
53 #elif defined(NN_PLATFORM_CTR)
54     nn::Result Initialize(u16 nodeId, u8 port, void *pRecvBuf, u16 recvBufSize);
55 #endif
56 
57 /* Please see man pages for details
58 
59 */
60     void Finalize(void);
61 
62 /* Please see man pages for details
63 
64 */
65     nn::Result Wait(void);
66 
67 /* Please see man pages for details
68 
69 
70 */
71     nn::Result Close(void);
72 
73 /* Please see man pages for details
74 
75 
76 
77 */
78     nn::Result Receive(void *pBuf, size_t *recvSize, size_t bufSize);
79 
80 /* Please see man pages for details
81 
82 
83 
84 */
85     nn::Result Process(void);
86 
87 /* Please see man pages for details
88 
89 */
90     void Cancel(void);
91 
92 /* Please see man pages for details
93 
94 */
95     enum ReceiverState GetStatus(void) const;
96 
97 /* Please see man pages for details
98 
99 */
new(size_t size,void * pBuf)100     static void* operator new(size_t size, void *pBuf) throw()
101     {
102         if(size!=sizeof(ReceiverImpl))
103         {
104             PANIC("Wrong size.\n");
105             return NULL;
106         }
107 
108         if(pBuf==NULL)
109         {
110             PANIC("NULL pointer is detected.\n");
111             return NULL;
112         }
113 
114         ALIGN_ASSERT(pBuf, 8);
115 
116         return pBuf;
117     }
118 
delete(void * p)119     static void  operator delete (void *p) throw()
120     {
121         PANIC("Do not call this delete!  Please call destructor manually.\n");
122         (void)p;
123     }
124 
125 
126 /* Please see man pages for details
127 
128 */
129     void PrintDebugInfo(void) const;
130 
131 private:
132 /* Please see man pages for details
133 
134 */
135     ReceiverImpl           (const ReceiverImpl&);
136 
137 /* Please see man pages for details
138 
139 */
140     ReceiverImpl& operator=(const ReceiverImpl&);
141 
142     // Constants
143     static const MSEC_T FIN_TIMEOUT = 1000;  // Parameter that needs to be adjusted with a CTR device.
144 
145     // Private Function Group.
146     void changeState(void);          // Calling this function executes a state transition.
147     void setNextState(ReceiverStateBase *p); // Set the next state.
148 
149     // Called when creating a response to SYN.
150     void putSynAckSegment(u32 ack);
151 
152     // Called when creating an ACK segment.
153     void putAckSegment(void);
154 
155     // Called when sending an ACK segment for FIN.
156     // At this time, the ACK send time is also recorded.
157     void putFinAckSegment(u32 ack);
158 
159     // After a sufficient amount of time passes from sending an ACK for FIN, can be confident that the other party has closed?
160     //
161     bool isSenderClosed(void) const;
162 
163     // Is the receive buffer empty?
isReceiveBufferEmpty(void)164     bool isReceiveBufferEmpty(void) const { return m_recvBuf.IsEmpty(); }
165 
166     // Assumes that it is called when the system enters the Closed state.
167     // Excluding members maintaining a state for the State pattern, each member variable is restored to a new state.
168     //
169     void clear(void);
170 
171     // Member variable group.
172     ReceiveBuffer m_recvBuf;    // Receive buffer. Stores here the actual data from the Sender.
173     bool   m_initialized;       // Initialization complete?
174     u8     m_padding[7];        // Note that MSEC_T format is 64-bit!
175     MSEC_T m_finAckSentTime;    // Time when ACK was sent last in response to FIN segment.
176 
177     // Applies State pattern.
178     ReceiverStateBase *m_pState;
179     ReceiverStateBase *m_pNextState;
180     friend class ReceiverStateWaiting;
181     friend class ReceiverStateOpened;
182     friend class ReceiverStateWaitingFinished;
183     friend class ReceiverStateFinished;
184     friend class ReceiverStateClosed;
185 };
186 
187 }}} // namespace nn::rdt::CTR
188 
189 #endif  // end of NN_RDT_RECEIVERIMPL_H_
190