1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     rdt_ReceiverImpl.h
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 #ifndef NN_LIBRARIES_RDT_CTR_RDT_RECEIVER_IMPL_H_
19 #define NN_LIBRARIES_RDT_CTR_RDT_RECEIVER_IMPL_H_
20 
21 #include "rdt_HostBase.h"
22 #include "rdt_ReceiveBuffer.h"
23 #include "rdt_Segment.h"
24 
25 
26 namespace nn { namespace rdt { namespace CTR {
27 
28 // Forward declaration
29 class ReceiverStateBase;
30 
31 /* Please see man pages for details
32 
33 */
34 class ReceiverImpl : public HostBase{
35 public:
36 /* Please see man pages for details
37 
38 */
39     ReceiverImpl(void) throw();
40 
41 /* Please see man pages for details
42 
43 */
44     virtual ~ReceiverImpl(void);
45 
46 /* Please see man pages for details
47 
48 */
49 #ifdef _WIN32
50     nn::Result Initialize(SOCKET sock, void *pRecvBuf, u16 recvBufSize);
51 #elif defined(NN_PLATFORM_CTR)
52     nn::Result Initialize(u16 nodeId, u8 port, void *pRecvBuf, u16 recvBufSize);
53 #endif
54 
55 /* Please see man pages for details
56 
57 */
58     void Finalize(void);
59 
60 /* Please see man pages for details
61 
62 */
63     nn::Result Wait(void);
64 
65 /* Please see man pages for details
66 
67 
68 */
69     nn::Result Close(void);
70 
71 /* Please see man pages for details
72 
73 
74 
75 */
76     nn::Result Receive(void *pBuf, size_t *recvSize, size_t bufSize);
77 
78 /* Please see man pages for details
79 
80 
81 
82 */
83     nn::Result Process(void);
84 
85 /* Please see man pages for details
86 
87 */
88     void Cancel(void);
89 
90 /* Please see man pages for details
91 
92 */
93     enum ReceiverState GetStatus(void) const;
94 
95 /* Please see man pages for details
96 
97 */
new(size_t size,void * pBuf)98     static void* operator new(size_t size, void *pBuf) throw()
99     {
100         if(size!=sizeof(ReceiverImpl))
101         {
102             PANIC("Wrong size.\n");
103             return NULL;
104         }
105 
106         if(pBuf==NULL)
107         {
108             PANIC("NULL pointer is detected.\n");
109             return NULL;
110         }
111 
112         ALIGN_ASSERT(pBuf, 8);
113 
114         return pBuf;
115     }
116 
delete(void * p)117     static void  operator delete (void *p) throw()
118     {
119         PANIC("Do not call this delete!  Please call destructor manually.\n");
120         (void)p;
121     }
122 
123 
124 /* Please see man pages for details
125 
126 */
127     void PrintDebugInfo(void) const;
128 
129 private:
130 /* Please see man pages for details
131 
132 */
133     ReceiverImpl           (const ReceiverImpl&);
134 
135 /* Please see man pages for details
136 
137 */
138     ReceiverImpl& operator=(const ReceiverImpl&);
139 
140     // Constants
141     static const MSEC_T FIN_TIMEOUT = 1000;  // Parameter that needs to be adjusted with a CTR device.
142 
143     // Private function group.
144     void changeState(void);          // Calling this function causes a state transition.
145     void setNextState(ReceiverStateBase *p); // Set the next state.
146 
147     // Call when creating a response for a SYN.
148     void putSynAckSegment(u32 ack);
149 
150     // Call when creating an ACK segment.
151     void putAckSegment(void);
152 
153     // Call when sending an ACK segment for a FIN.
154     // At this time, the ACK send time is also recorded.
155     void putFinAckSegment(u32 ack);
156 
157     // Are you confident that enough time has passed since sending an ACK for a FIN and that the partner has closed?
158     //
159     bool isSenderClosed(void) const;
160 
161     // Is the receive buffer empty?
isReceiveBufferEmpty(void)162     bool isReceiveBufferEmpty(void) const { return m_recvBuf.IsEmpty(); }
163 
164     // Assumes that it is called when the Closed state is entered.
165     // Except for the members that maintain a state in the State pattern, all member variables are restored to the new state.
166     //
167     void clear(void);
168 
169     // Member variables.
170     ReceiveBuffer m_recvBuf;    // Receive buffer. The data actually received from the Sender is stored here.
171     bool   m_initialized;       // Initialization completed?
172     u8     m_padding[7];        // Notice that the MSEC_T type is 64-bit!
173     MSEC_T m_finAckSentTime;    // Time when the ACK corresponding to the FIN segment was sent last.
174 
175     // State pattern implementation.
176     ReceiverStateBase *m_pState;
177     ReceiverStateBase *m_pNextState;
178     friend class ReceiverStateWaiting;
179     friend class ReceiverStateOpened;
180     friend class ReceiverStateWaitingFinished;
181     friend class ReceiverStateFinished;
182     friend class ReceiverStateClosed;
183 };
184 
185 }}} // namespace nn::rdt::CTR
186 
187 #endif  // end of NN_LIBRARIES_RDT_CTR_RDT_RECEIVER_IMPL_H_
188