@@ -121,10 +121,11 @@ void CCommunityRegistration::Open ( void )
121121 if ( m_ulStartTime == 0 )
122122 {
123123 // Create the URL
124- std::string strURL = std::string ( REGISTRATION_URL ) + " ?action=request" ;
124+ SString strURL = std::string ( REGISTRATION_URL ) + " ?action=request" ;
125125
126126 // Perform the HTTP request
127- m_HTTP.Get ( strURL );
127+ GetHTTP ()->Reset ();
128+ GetHTTP ()->QueueFile ( strURL, NULL , 0 , NULL , 0 , false , this , StaticDownloadFinished, false , 1 /* uiConnectionAttempts*/ );
128129
129130 // Store the start time
130131 m_ulStartTime = CClientTime::GetTime ();
@@ -154,81 +155,101 @@ void CCommunityRegistration::DoPulse ( void )
154155{
155156 if ( m_ulStartTime > 0 )
156157 {
157- CHTTPBuffer buffer;
158- if ( m_HTTP.GetData ( buffer ) )
159- {
160- char * szBuffer = buffer.GetData ();
161- unsigned int uiBufferLength = buffer.GetSize ();
158+ GetHTTP ()->ProcessQueuedFiles ();
159+ }
160+ }
161+
162+
163+ // Get the HTTP download manager used for community stuff
164+ CNetHTTPDownloadManagerInterface* CCommunityRegistration::GetHTTP ( void )
165+ {
166+ return g_pCore->GetNetwork ()->GetHTTPDownloadManager ( EDownloadMode::CORE_UPDATER );
167+ }
168+
169+
170+ // Handle server response
171+ bool CCommunityRegistration::StaticDownloadFinished ( double dDownloadNow, double dDownloadTotal, char * pCompletedData, size_t completedLength, void *pObj, bool bSuccess, int iErrorCode )
172+ {
173+ if ( bSuccess )
174+ ((CCommunityRegistration*)pObj)->DownloadSuccess ( pCompletedData, completedLength );
175+ else
176+ ((CCommunityRegistration*)pObj)->DownloadFailed ( iErrorCode );
177+ return true ;
178+ }
179+
180+ void CCommunityRegistration::DownloadSuccess ( char * szBuffer, size_t uiBufferLength )
181+ {
182+ // Succeed, deal with the response
183+ m_ulStartTime = 0 ;
184+
185+ // ID
186+ eRegistrationResult Result = REGISTRATION_ERROR_UNEXPECTED;
187+ if ( uiBufferLength > 0 )
188+ (eRegistrationResult)(szBuffer[0 ] - 48 );
189+
190+ if ( Result == REGISTRATION_ERROR_REQUEST )
191+ {
192+ CGUI *pManager = g_pCore->GetGUI ();
162193
163- // Succeed, deal with the response
164- m_ulStartTime = 0 ;
194+ // Sure we have it all right?
195+ if ( uiBufferLength > 32 )
196+ {
197+ // Get the hash
198+ m_strCommunityHash = std::string ( &szBuffer[1 ], 32 );
165199
166- // ID
167- eRegistrationResult Result = (eRegistrationResult)(szBuffer[0 ] - 48 );
200+ // TODO: Load it without a temp file
168201
169- if ( Result == REGISTRATION_ERROR_REQUEST )
202+ // Create a temp file for the png
203+ FILE * fp = fopen ( CalcMTASAPath ( REGISTRATION_TEMP_FILE ), " wb" );
204+ if ( fp )
170205 {
171- CGUI *pManager = g_pCore->GetGUI ();
172-
173- // Sure we have it all right?
174- if ( uiBufferLength > 32 )
175- {
176- // Get the hash
177- m_strCommunityHash = std::string ( &szBuffer[1 ], 32 );
178-
179- // TODO: Load it without a temp file
180-
181- // Create a temp file for the png
182- FILE * fp = fopen ( CalcMTASAPath ( REGISTRATION_TEMP_FILE ), " wb" );
183- if ( fp )
184- {
185- fwrite ( &szBuffer[33 ], uiBufferLength, 1 , fp );
186- fclose ( fp );
187-
188- m_pImageCode->LoadFromFile ( " temp.png" );
189- m_pImageCode->SetSize ( CVector2D ( 65 .0f , 20 .0f ), false );
190- m_pWindow->SetVisible ( true );
191- m_pWindow->BringToFront ();
192-
193- // Delete the temp file
194- remove ( CalcMTASAPath ( REGISTRATION_TEMP_FILE ) );
195- return ;
196- }
197- }
198- g_pCore->ShowMessageBox ( _ (" Error" )+_E (" CC04" ), _ (" Services currently unavailable" ), MB_BUTTON_OK | MB_ICON_ERROR );
199- }
200- else if ( Result == REGISTRATION_ERROR_SUCCESS )
201- {
202- g_pCore->ShowMessageBox ( _ (" Success" )+_E (" CC05" ), _ (" Successfully registered!" ), MB_BUTTON_OK | MB_ICON_INFO );
203-
204- m_pWindow->SetVisible ( false );
205- SetFrozen ( false );
206- m_strCommunityHash.clear ();
207- m_pImageCode->Clear ();
206+ fwrite ( &szBuffer[33 ], uiBufferLength, 1 , fp );
207+ fclose ( fp );
208+
209+ m_pImageCode->LoadFromFile ( " temp.png" );
210+ m_pImageCode->SetSize ( CVector2D ( 65 .0f , 20 .0f ), false );
211+ m_pWindow->SetVisible ( true );
212+ m_pWindow->BringToFront ();
213+
214+ // Delete the temp file
215+ remove ( CalcMTASAPath ( REGISTRATION_TEMP_FILE ) );
216+ return ;
208217 }
209- else if ( Result == REGISTRATION_ERROR_ERROR )
210- {
211- if ( strlen ( &szBuffer[1 ] ) > 0 )
212- g_pCore->ShowMessageBox ( _ (" Error" )+_E (" CC06" ), &szBuffer[1 ], MB_BUTTON_OK | MB_ICON_ERROR );
213- else
214- g_pCore->ShowMessageBox ( _ (" Error" )+_E (" CC07" ), " Unexpected error" , MB_BUTTON_OK | MB_ICON_ERROR );
215-
216- SetFrozen ( false );
217- }
218- else
219- {
220- g_pCore->ShowMessageBox ( _ (" Error" )+_E (" CC08" ), _ (" Services currently unavailable" ), MB_BUTTON_OK | MB_ICON_ERROR );
221- SetFrozen ( false );
222- }
223- }
224- else if ( ( CClientTime::GetTime () - m_ulStartTime ) > REGISTRATION_DELAY )
225- {
226- g_pCore->ShowMessageBox ( _ (" Error" )+_E (" CC10" ), _ (" Services currently unavailable" ), MB_BUTTON_OK | MB_ICON_ERROR );
227- SetFrozen ( false );
228- // Timed out
229- m_ulStartTime = 0 ;
230218 }
219+ g_pCore->ShowMessageBox ( _ (" Error" )+_E (" CC04" ), _ (" Services currently unavailable" ), MB_BUTTON_OK | MB_ICON_ERROR );
220+ }
221+ else if ( Result == REGISTRATION_ERROR_SUCCESS )
222+ {
223+ g_pCore->ShowMessageBox ( _ (" Success" )+_E (" CC05" ), _ (" Successfully registered!" ), MB_BUTTON_OK | MB_ICON_INFO );
224+
225+ m_pWindow->SetVisible ( false );
226+ SetFrozen ( false );
227+ m_strCommunityHash.clear ();
228+ m_pImageCode->Clear ();
229+ }
230+ else if ( Result == REGISTRATION_ERROR_ERROR )
231+ {
232+ if ( strlen ( &szBuffer[1 ] ) > 0 )
233+ g_pCore->ShowMessageBox ( _ (" Error" )+_E (" CC06" ), &szBuffer[1 ], MB_BUTTON_OK | MB_ICON_ERROR );
234+ else
235+ g_pCore->ShowMessageBox ( _ (" Error" )+_E (" CC07" ), " Unexpected error" , MB_BUTTON_OK | MB_ICON_ERROR );
236+
237+ SetFrozen ( false );
231238 }
239+ else
240+ {
241+ g_pCore->ShowMessageBox ( _ (" Error" )+_E (" CC08" ), _ (" Services currently unavailable" ), MB_BUTTON_OK | MB_ICON_ERROR );
242+ SetFrozen ( false );
243+ }
244+ }
245+
246+
247+ void CCommunityRegistration::DownloadFailed ( int iErrorCode )
248+ {
249+ g_pCore->ShowMessageBox ( _ (" Error" )+_E (" CC10" ), _ (" Services currently unavailable" ), MB_BUTTON_OK | MB_ICON_ERROR );
250+ SetFrozen ( false );
251+ // Timed out
252+ m_ulStartTime = 0 ;
232253}
233254
234255
@@ -269,7 +290,7 @@ bool CCommunityRegistration::OnButtonRegisterClick ( CGUIElement* pElement )
269290 Md5HashString ( m_pEditPassword->GetText ().c_str (), strPassword );
270291
271292 // Create the URL
272- std::string strURL =
293+ SString strURL =
273294 std::string ( REGISTRATION_URL ) +
274295 " ?action=register" +
275296 " &username=" + m_pEditUsername->GetText () +
@@ -279,7 +300,8 @@ bool CCommunityRegistration::OnButtonRegisterClick ( CGUIElement* pElement )
279300 " &hash=" + m_strCommunityHash;
280301
281302 // Perform the HTTP request
282- m_HTTP.Get ( strURL );
303+ GetHTTP ()->Reset ();
304+ GetHTTP ()->QueueFile ( strURL, NULL , 0 , NULL , 0 , false , this , StaticDownloadFinished, false , 1 /* uiConnectionAttempts*/ );
283305
284306 // Store the start time
285307 m_ulStartTime = CClientTime::GetTime ();
0 commit comments