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_RDT_RECEIVERIMPL_H_
19 #define NN_RDT_RECEIVERIMPL_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 executes a state transition.
145     void setNextState(ReceiverStateBase *p); // Set the next state.
146 
147     // Called when creating a response to SYN.
148     void putSynAckSegment(u32 ack);
149 
150     // Called when creating an ACK segment.
151     void putAckSegment(void);
152 
153     // Called when sending an ACK segment for FIN.
154     // At this time, the ACK send time is also recorded.
155     void putFinAckSegment(u32 ack);
156 
157     // After a sufficient amount of time passes from sending an ACK for FIN, can be confident that the other party 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 system enters the Closed state.
165     // Excluding members maintaining a state for the State pattern, each member variable is restored to a new state.
166     //
167     void clear(void);
168 
169     // Member variable group.
170     ReceiveBuffer m_recvBuf;    // Receive buffer. Stores here the actual data from the Sender.
171     bool   m_initialized;       // Initialization complete?
172     u8     m_padding[7];        // Note that MSEC_T format is 64-bit!
173     MSEC_T m_finAckSentTime;    // Time when ACK was sent last in response to FIN segment.
174 
175     // Applies State pattern.
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_RDT_RECEIVERIMPL_H_
188