CTR Pia  4.11.3
Game Communication Engine
transport_ProtocolManager.h
1 /*---------------------------------------------------------------------------*
2  Project: Pia
3  File: transport_ProtocolManager.h
4 
5  Copyright 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 
14 
15 #pragma once
16 
17 #include <pia/transport/transport_definitions.h>
18 
19 #include <pia/common/common_OffsetList.h>
20 #include <pia/transport/transport_ProtocolID.h>
21 #include <pia/transport/transport_ProtocolEvent.h>
22 #include <pia/transport/transport_Protocol.h>
23 
24 namespace nn
25 {
26 namespace pia
27 {
28 namespace transport
29 {
30 
31 class PacketHandler;
32 
33 /*!
34 @cond PRIVATE
35 @brief Manages protocols.
36 
37 @date 2012-04-06 Initial version.
38 */
39 class ProtocolManager : public common::RootObject
40 {
41 public:
42 /*!
43 @brief Instantiates the object with default parameters (default constructor).
44 */
45  ProtocolManager(void);
46 
47 
48 /*!
49 @brief Destructor.
50 */
51  ~ProtocolManager(void);
52 
53 
54 /*!
55 @brief Initialization.
56 @return <tt>nn::ResultSuccess</tt>.
57 */
58  nn::Result Initialize();
59 
60 
61 /*!
62 @brief Finalizes.
63 */
64  void Finalize();
65 
66 
67 /*!
68 @brief Generates an instance of a protocol of type <tt>T</tt>.
69 
70 @param[in] port The value of the protocol port.
71 
72 @return A handle to the generated protocol. If the same protocol ID already exists, 0 is returned.
73 Also returns zero after the \c Startup function has been called, because the object cannot be created in this case.
74 */
75  template <typename T>
76  u32 CreateProtocol(u16 port = 0)
77  {
78  if (m_IsRunning)
79  {
80  return 0;
81  }
82 
83  transport::Protocol* pProtocol = new (AllocProtocol(sizeof(T))) T();
84  transport::ProtocolId id(T::GetProtocolTypeStatic(), port);
85  return CreateProtocolImpl(pProtocol, id);
86  }
87 
88 
89 /*!
90 @brief Destroys the protocol instance.
91 
92 @param[in] protocolHandle The handle for the protocol to delete.
93 */
94  void DestroyProtocol(u32 protocolHandle);
95 
96 
97 /*!
98 @brief Gets an instance of a protocol of type <tt>T</tt>.
99 
100 @param[in] protocolHandle The handle for the protocol to get.
101 
102 @return The address of the protocol instance. Returns \c NULL if the protocol for the specified handle does not exist or is not of type <tt>T</tt>.
103 */
104  template <typename T>
105  T* GetProtocol(u32 protocolHandle)
106  {
107  return static_cast<T*>(SearchProtocol(ProtocolId(protocolHandle), T::GetProtocolTypeStatic()));
108  }
109 
110 
111 /*!
112 @cond PRIVATE
113 @brief Starts communication.
114 
115 @param[in] pPacketHandler The \c PacketHandler instance to use.
116 
117 @return On success, returns a \c Result value for which the \c IsSuccess function will return \c true.
118 */
119  nn::Result Startup(PacketHandler* pPacketHandler);
120  //! @endcond
121 
122 
123 /*!
124 @cond PRIVATE
125 @brief Ends communication.
126 */
127  void Cleanup();
128  //! @endcond
129 
130 
131 /*!
132 @cond PRIVATE
133 @brief Calls the \c Protocol::Startup function of each protocol.
134 
135 @param[in] myStationIndex The <tt>StationIndex</tt> value of the local station.
136 
137 @return On success, returns a <tt>Result</tt> value for which the <tt>IsSuccess</tt> function returns <tt>true</tt>.
138 */
139  nn::Result StartupProtocols(StationIndex myStationIndex);
140  //! @endcond
141 
142 
143 /*!
144 @cond PRIVATE
145 @brief Calls the \c Protocol::Cleanup function of each protocol.
146 */
147  void CleanupProtocols();
148  //! @endcond
149 
150 
151 /*!
152 @cond PRIVATE
153 @brief The main process.
154 
155 @return On success, returns a <tt>Result</tt> value for which the <tt>IsSuccess</tt> function returns <tt>true</tt>.
156 */
157  nn::Result Dispatch();
158  //! @endcond
159 
160 
161 /*!
162 @cond PRIVATE
163 @brief Switches connection status.
164 
165 @param[in] event Event representing the operation.
166 
167 @return On success, returns a <tt>Result</tt> value for which the <tt>IsSuccess</tt> function returns <tt>true</tt>.
168 */
169  nn::Result UpdateProtocolEvent(const ProtocolEvent& event);
170  //! @endcond
171 
172 
173 /*!
174 @brief Prints information that is useful for debugging.
175 
176 @param[in] flag Bitwise OR of trace flags. For more information, see the <tt>@ref TraceFlag</tt> type.
177 */
178  virtual void Trace(u64 flag) const;
179 
180 #if PIA_STATISTIC_PROTOCOL_BLOCK_TIME_ENABLE
181  void ResetProtocolStatistic();
182  void PrintProtocolStatistic(u32 printMaxDataNum);
183 #endif
184 
185 private:
186  void* AllocProtocol(u32 size);
187 
188  u32 CreateProtocolImpl(Protocol* pProtocol, ProtocolId id);
189 
190  Protocol* SearchProtocol(ProtocolId protocolId, u16 protocolType);
191 
192 private:
193  typedef common::OffsetList<Protocol> ProtocolList;
194 
195  ProtocolList m_ProtocolList;
196 
197  bool m_IsRunning;
198 };
199 //! @endcond
200 }
201 }
202 } // end of namespace nn::pia::transport
StationIndex
Enumerates StationIndex values.
Definition: platformCtr.h:44
Definition: assert.h:115
virtual void Trace(u64 flag) const
Prints information that is useful for debugging.
void Finalize(void)
Initializes the transport module. This function does nothing and returns if it is called before the I...
nn::Result Initialize(void)
Initializes the transport module.