summaryrefslogtreecommitdiff
path: root/tracker/AdminServer/serverpage.cpp
blob: 467771536654b36febf1e9439145d03ea07583da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// The copyright to the contents herein is the property of Valve, L.L.C.
// The contents may be used and/or copied only with the written permission of
// Valve, L.L.C., or in accordance with the terms and conditions stipulated in
// the agreement/contract under which the contents have been supplied.
//
// Purpose: 
//
// $Workfile:     $
// $Date:         $
//
//-----------------------------------------------------------------------------
// $Log: $
//
// $NoKeywords: $
//=============================================================================
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>

// base vgui interfaces
#include <VGUI_Controls.h>
#include <VGUI_IInput.h>
#include <VGUI_ISurface.h>
#include <VGUI_IScheme.h>
#include <VGUI_IVGui.h>
#include <VGUI_MouseCode.h>
#include "filesystem.h"


// vgui controls
#include <VGUI_Button.h>
#include <VGUI_CheckButton.h>
#include <VGUI_ComboBox.h>
#include <VGUI_FocusNavGroup.h>
#include <VGUI_Frame.h>
#include <VGUI_KeyValues.h>
#include <VGUI_ListPanel.h>
#include <VGUI_MessageBox.h>
#include <VGUI_Panel.h>
#include <VGUI_PropertySheet.h>
#include <VGUI_ToggleButton.h>
#include <VGUI_QueryBox.h>


// serverbrowser headers
#include "inetapi.h"
//#include "msgbuffer.h"
#include "proto_oob.h"
#include "ServerContextMenu.h"
#include "socket.h"
#include "util.h"
#include "vinternetdlg.h"
#include "dialogcvarchange.h"
//#include "ModList.h"
#include "DialogGameInfo.h"
#include "ConfigPanel.h"
 

// game list
#include "FavoriteGames.h"
#include "GamePanelInfo.h"

// tracker stuff
//#include "Tracker.h"
#include "TrackerProtocol.h"
//#include "OnlineStatus.h"


// interface to game engine / tracker
#include "IRunGameEngine.h"

using namespace vgui;

static VInternetDlg *s_InternetDlg = NULL;
CSysModule * g_hTrackerNetModule = NULL;

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
VInternetDlg::VInternetDlg( unsigned int userid ) : Frame(NULL, "VInternetDlg")
{
	s_InternetDlg = this;
	
	m_iUserID=userid;
	m_bLoggedIn=false;

	MakePopup();

	m_pSavedData = NULL;

	// create the controls
	m_pContextMenu = new CServerContextMenu(this);
//	m_pContextMenu->SetVisible(false);

	m_pFavoriteGames = new CFavoriteGames(this);

	SetMinimumSize(570, 550);

	m_pGameList = m_pFavoriteGames;

	// property sheet
	m_pTabPanel = new PropertySheet(this, "GameTabs");

	m_pTabPanel->SetTabWidth(150);
//	m_pTabPanel->SetScrolling(true);
	m_pTabPanel->AddPage(m_pFavoriteGames, "My Servers");
	m_pTabPanel->AddActionSignalTarget(this);

	m_pStatusLabel = new Label(this, "StatusLabel", "");

	LoadControlSettings("Admin\\DialogAdminServer.res");

	m_pStatusLabel->SetText("");


	// Setup tracker objects
	// tracker doc
	//g_pTrackerDoc = new CTrackerDoc();

	// create the networking
	/*m_pServerSession = new CServerSession();

	// load networking dll
	char szDLL[_MAX_PATH];

	// now load the net interface so we can use it
	g_pFullFileSystem->GetLocalPath("Friends/TrackerNET.dll", szDLL);
	g_pFullFileSystem->GetLocalCopy(szDLL);
	g_hTrackerNetModule = Sys_LoadModule(szDLL);

	CreateInterfaceFn netFactory = Sys_GetFactory(g_hTrackerNetModule);
	m_pNet = (ITrackerNET *)netFactory(TRACKERNET_INTERFACE_VERSION, NULL);

	m_pNet->Initialize(27030, 27100);
	m_iServerAddr=m_pNet->GetNetAddress("tracker3.valvesoftware.com:1200");
	
	// uncomment this to do the "tracker" magic
	//SendInitialLogin();
	*/

	// load filters
	LoadFilters();
	// load window settings
	LoadDialogState(this, "AdminServer");

	// let us be ticked every frame
	ivgui()->AddTickSignal(this->GetVPanel());


}

//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
VInternetDlg::~VInternetDlg()
{
	// set a flag indicating the threads should kill themselves
//	m_pNet->Shutdown(false);
//	m_pNet->deleteThis();
//	m_pNet = NULL;

	Sys_UnloadModule(g_hTrackerNetModule);


}

//-----------------------------------------------------------------------------
// Purpose: Called once to set up
//-----------------------------------------------------------------------------
void VInternetDlg::Initialize()
{
	SetTitle("Admin", true);
	SetVisible(false);
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : serverID - 
// Output : serveritem_t
//-----------------------------------------------------------------------------
serveritem_t &VInternetDlg::GetServer(unsigned int serverID)
{
	return m_pGameList->GetServer(serverID);
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void VInternetDlg::Open( void )
{	
	m_pTabPanel->RequestFocus();
	// if serverbrowser file is not there we will try to transfer the favorites list.
	FileHandle_t f = g_pFullFileSystem->Open("AdminServer.vdf", "rb");
	if (f)
	{
		g_pFullFileSystem->Close( f );
	}

	
	surface()->SetMinimized(GetVPanel(), false);
	SetVisible(true);
	RequestFocus();
	m_pTabPanel->RequestFocus();
	MoveToFront();
}

//-----------------------------------------------------------------------------
// Purpose: relayouts the dialogs controls
//-----------------------------------------------------------------------------
void VInternetDlg::PerformLayout()
{
	BaseClass::PerformLayout();

	int x, y, wide, tall;
	GetClientArea(x, y, wide, tall);
	
	// game list in middle
	m_pTabPanel->SetBounds(8, y + 8, GetWide() - 16, tall - (28));
	x += 4;

	// status text along bottom
	m_pStatusLabel->SetBounds(x + 2, (tall - y) + 40, wide - 6, 20);
	m_pStatusLabel->SetContentAlignment(Label::a_northwest);

	Repaint();

}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void VInternetDlg::OnClose()
{
	// bug here if you exit before logging in.
	SaveDialogState(this, "AdminServer");
	SaveFilters();
	Frame::OnClose();	
	
}

//-----------------------------------------------------------------------------
// Purpose: Loads filter settings from disk
//-----------------------------------------------------------------------------
void VInternetDlg::LoadFilters()
{
  	// free any old filters
  	if (m_pSavedData)
  	{
  		m_pSavedData->deleteThis();
  	}

	m_pSavedData = new KeyValues ("Filters");
	if (!m_pSavedData->LoadFromFile(g_pFullFileSystem, "Admin\\AdminServer.vdf", true, "PLATFORM"))
	{
		// file not successfully loaded, create the default key
		m_pSavedData->FindKey("List", true);
		m_pSavedData->FindKey("List/Default", true);
		m_pSavedData->SetString("DefaultFilter", "Default");
	}

	// load favorite servers
	KeyValues *favorites = m_pSavedData->FindKey("Favorites", true);
	m_bSaveRcon= m_pSavedData->FindKey("SaveRcon", true)->GetInt();

	m_pFavoriteGames->LoadFavoritesList(favorites,m_bSaveRcon);

	m_bAutoRefresh= m_pSavedData->FindKey("AutoRefresh", true)->GetInt();
	if(!m_bAutoRefresh)
	{
		m_iRefreshTime=0;
	}
	else
	{
		m_iRefreshTime= m_pSavedData->FindKey("RefreshTime", true)->GetInt();
	}

	m_bGraphs= m_pSavedData->FindKey("ShowGraphs", true)->GetInt();
	if(!m_bGraphs)
	{
		m_iGraphsRefreshTime=0;
	}
	else
	{
		m_iGraphsRefreshTime= m_pSavedData->FindKey("GraphsRefreshTime", true)->GetInt();
	}

	m_bDoLogging= m_pSavedData->FindKey("GetLogs", true)->GetInt();


	m_pTabPanel->SetActivePage(m_pFavoriteGames);
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void VInternetDlg::SaveFilters()
{

	// get the favorites list
	KeyValues *favorites = m_pSavedData->FindKey("Favorites", true);
	m_pFavoriteGames->SaveFavoritesList(favorites,m_bSaveRcon);
	m_pSavedData->SaveToFile(g_pFullFileSystem, "Admin\\AdminServer.vdf", "PLATFORM");

}

void VInternetDlg::SetConfig(bool autorefresh,bool savercon,int refreshtime,bool graphs,int graphsrefreshtime,bool getlogs)
{
	m_bAutoRefresh=autorefresh;
	m_bDoLogging = getlogs;
	m_bSaveRcon=savercon;
	if(m_bAutoRefresh)
	{
		m_iRefreshTime=refreshtime;
	}
	else
	{
		m_iRefreshTime=0;
	}
	
	m_bGraphs = graphs;
	if(graphs)
	{
		m_iGraphsRefreshTime=graphsrefreshtime;
	}
	else
	{
		m_iGraphsRefreshTime=0;
	}

	m_pSavedData->SetInt("AutoRefresh",autorefresh);
	m_pSavedData->SetInt("SaveRcon",savercon);
	m_pSavedData->SetInt("RefreshTime",refreshtime);
	m_pSavedData->SetInt("GraphsRefreshTime",graphsrefreshtime);
	m_pSavedData->SetInt("ShowGraphs",graphs);
	m_pSavedData->SetInt("GetLogs",getlogs);

	
}


//-----------------------------------------------------------------------------
// Purpose: Updates status test at bottom of window
// Input  : *fmt - 
//			... - 
//-----------------------------------------------------------------------------
void VInternetDlg::UpdateStatusText(const char *fmt, ...)
{
	if ( !m_pStatusLabel )
		return;

	char str[ 1024 ];
	va_list argptr;
	va_start( argptr, fmt );
	vsprintf( str, fmt, argptr );
	va_end( argptr );

	m_pStatusLabel->SetText( str );
}

//-----------------------------------------------------------------------------
// Purpose: returns a pointer to a static instance of this dialog
// Output : VInternetDlg
//----------------------------------------------------------------------------
VInternetDlg *VInternetDlg::GetInstance()
{
	return s_InternetDlg;
}

//-----------------------------------------------------------------------------
// Purpose: 
// Output : CServerContextMenu
//-----------------------------------------------------------------------------
CServerContextMenu *VInternetDlg::GetContextMenu()
{
	return m_pContextMenu;
}

//-----------------------------------------------------------------------------
// Purpose: begins the process of joining a server from a game list
//			the game info dialog it opens will also update the game list
//-----------------------------------------------------------------------------
CDialogGameInfo *VInternetDlg::JoinGame(IGameList *gameList, unsigned int serverIndex)
{
	// open the game info dialog, then mark it to attempt to connect right away
	CDialogGameInfo *gameDialog = OpenGameInfoDialog(gameList, serverIndex);

	// set the dialog name to be the server name
	gameDialog->Connect();

	return gameDialog;
}

//-----------------------------------------------------------------------------
// Purpose: joins a game by a specified IP, not attached to any game list
//-----------------------------------------------------------------------------
CDialogGameInfo *VInternetDlg::JoinGame(int serverIP, int serverPort, const char *titleName)
{
	// open the game info dialog, then mark it to attempt to connect right away
	CDialogGameInfo *gameDialog = OpenGameInfoDialog(serverIP, serverPort, titleName);

	// set the dialog name to be the server name
	gameDialog->Connect();

	return gameDialog;
}

//-----------------------------------------------------------------------------
// Purpose: opens a game info dialog from a game list
//-----------------------------------------------------------------------------
CDialogGameInfo *VInternetDlg::OpenGameInfoDialog(IGameList *gameList, unsigned int serverIndex)
{
	CDialogGameInfo *gameDialog = new CDialogGameInfo(gameList, serverIndex);
	serveritem_t &server = gameList->GetServer(serverIndex);
	gameDialog->Run(server.name);
	return gameDialog;
}

//-----------------------------------------------------------------------------
// Purpose: opens a game info dialog by a specified IP, not attached to any game list
//-----------------------------------------------------------------------------
CDialogGameInfo *VInternetDlg::OpenGameInfoDialog(int serverIP, int serverPort, const char *titleName)
{
	CDialogGameInfo *gameDialog = new CDialogGameInfo(NULL, 0, serverIP, serverPort);
	gameDialog->Run(titleName);
	return gameDialog;
}

//-----------------------------------------------------------------------------
// Purpose: Save position and window size of a dialog from the .vdf file.
// Input  : *dialog - panel we are setting position and size 
//			*dialogName -  name of dialog in the .vdf file
//-----------------------------------------------------------------------------
void VInternetDlg::SaveDialogState(Panel *dialog, const char *dialogName)
{
	// write the size and position to the document
	int x, y, wide, tall;
	dialog->GetBounds(x, y, wide, tall);

	KeyValues *data;
	data = m_pSavedData->FindKey(dialogName, true);

	data->SetInt("x", x);
	data->SetInt("y", y);
	data->SetInt("w", wide);
	data->SetInt("t", tall);
}

//-----------------------------------------------------------------------------
// Purpose: Load position and window size of a dialog from the .vdf file.
// Input  : *dialog - panel we are setting position and size 
//			*dialogName -  name of dialog in the .vdf file
//-----------------------------------------------------------------------------
void VInternetDlg::LoadDialogState(Panel *dialog, const char *dialogName)
{						   
	// read the size and position from the document
	KeyValues *data;
	data = m_pSavedData->FindKey(dialogName, true);

	// calculate defaults, center of the screen
	int x, y, wide, tall, dwide, dtall;
	int nx, ny, nwide, ntall;
	vgui::surface()->GetScreenSize(wide, tall);
	dialog->GetSize(dwide, dtall);
	x = (int)((wide - dwide) * 0.5);
	y = (int)((tall - dtall) * 0.5);

	// set dialog
	nx = data->GetInt("x", x);
	ny = data->GetInt("y", y);
	nwide = data->GetInt("w", dwide);
	ntall = data->GetInt("t", dtall);

	// make sure it's on the screen. If it isn't, move it over so it is.
	if (nx + nwide > wide)
	{
		nx = wide - nwide;
	}
	if (ny + ntall > tall)
	{
		ny = tall - ntall;
	}
	if (nx < 0)
	{
		nx = 0;
	}
	if (ny < 0)
	{
		ny = 0;
	}

	dialog->SetBounds(nx, ny, nwide, ntall);
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *dest - 
//			*src - 
//			bufsize - 
//-----------------------------------------------------------------------------
void v_strncpy(char *dest, const char *src, int bufsize)
{
	if (src == dest)
		return;

	strncpy(dest, src, bufsize - 1);
	dest[bufsize - 1] = 0;
}

void VInternetDlg::ConfigPanel()
{
		CConfigPanel *config = new CConfigPanel(m_bAutoRefresh,m_bSaveRcon,m_iRefreshTime,m_bGraphs,m_iGraphsRefreshTime,m_bDoLogging);
		config->Run();
}

void VInternetDlg::OnManageServer(int serverID) 
{
	int i;
	serveritem_t &server = m_pFavoriteGames->GetServer(serverID); 
	netadr_t addr;
	memcpy(addr.ip,server.ip,4);
	addr.port=(server.port & 0xff) << 8 | (server.port & 0xff00) >> 8;
	addr.type=NA_IP;

	const char *netString = net->AdrToString(&addr);
	char tabName[20];

	for(i=0;i<m_pTabPanel->GetNumPages();i++)
	{

		m_pTabPanel->GetTabTitle(i,tabName,20);
		if(!stricmp(netString,tabName))
		{
			break;
		}
	}
	
	if(i==m_pTabPanel->GetNumPages())
	{

		if(m_bSaveRcon) 
		{ // rcons are being saved		
			if(strlen(server.rconPassword)>0) 
			{ // this rcon password is already saved :)
				ManageServer(serverID,server.rconPassword);
				return;
			}
		}

		// otherwise ask for an rcon password
		CDialogCvarChange *box = new CDialogCvarChange();
		char id[5];
		_snprintf(id,5,"%i",serverID);
		box->AddActionSignalTarget(this);
		box->SetTitle("Enter Rcon Password",true);
		box->SetLabelText("CvarNameLabel","");
		box->SetLabelText("PasswordLabel","Password:");

		box->MakePassword();
		box->Activate(id, "","rconpassword","Enter Rcon Password for this Server");
	}
	else
	{
		m_pTabPanel->SetActivePage(m_pTabPanel->GetPage(i));
	}
}

void VInternetDlg::OnPlayerDialog(vgui::KeyValues *data)
{
	const char *type=data->GetString("type");
	const char *playerName=data->GetString("player");
	if(!stricmp(type,"rconpassword")) 
	{
		const char *value=data->GetString("value");
		serveritem_t &server = m_pFavoriteGames->GetServer(atoi(playerName)); // we encode the serverid in the name field :)
		strncpy(server.rconPassword,value,sizeof(server.rconPassword)); // save this password

		ManageServer(atoi(playerName),value);
	}
}

void VInternetDlg::ManageServer(int serverID,const char *pass)
{
	serveritem_t &server = m_pFavoriteGames->GetServer(serverID); 
	netadr_t addr;
	memcpy(addr.ip,server.ip,4);
	addr.port=(server.port & 0xff) << 8 | (server.port & 0xff00) >> 8;
	addr.type=NA_IP;

	m_pGamePanelInfo = new CGamePanelInfo(this,"Current Server",server.gameDir,m_iRefreshTime,m_iGraphsRefreshTime,m_bDoLogging);

	m_pTabPanel->AddPage(m_pGamePanelInfo,net->AdrToString(&addr) );

	m_pGamePanelInfo->ChangeGame(server,pass);
	m_pTabPanel->SetActivePage(m_pGamePanelInfo);
}

void VInternetDlg::UpdateServer(serveritem_t &server)
{
	m_pFavoriteGames->UpdateServer(server); 
}

void VInternetDlg::OnDeleteServer(int chosenPanel)
{
	Panel *delPanel =m_pTabPanel->GetPage(chosenPanel);
	m_pTabPanel->DeletePage(delPanel);

	InvalidateLayout();
	Repaint();

}

vgui::PropertySheet *VInternetDlg::GetTabPanel() 
{
	return m_pTabPanel;
}


void VInternetDlg::OnOpenContextMenu()
{
//	CServerContextMenu *menu = VInternetDlg::GetInstance()->GetContextMenu();
		// no selected rows, so don't display default stuff in menu
	if( m_pTabPanel->GetActiveTab()->IsCursorOver() || 
		m_pFavoriteGames->IsCursorOver() )
	{
		m_pContextMenu->ShowMenu(this, -1, false, false, false,false);
	}
}	








void VInternetDlg::OnTick()
{

//FIX ME!!!
	return;

/*

	// get the latest raw messages
	IBinaryBuffer *buf;
	CNetAddress address;
	while ((buf = m_pNet->GetIncomingRawData(address)) != NULL)
	{
		//ReceivedRawData(buf, address);
		buf->Release();
	}

	// get all the latest messages
	IReceiveMessage *recv;
	while ((recv = m_pNet->GetIncomingData()) != NULL)
	{

		// make sure the message is valid
		if (!CheckMessageValidity(recv))
			return;

		// record the reception
	//	m_iLastReceivedTime = m_iTime;

		// find the message id in the dispatch table
		int dataName = recv->GetMsgID();



		switch(dataName) 
		{
		case TSVC_CHALLENGE:
			{
				int ChallengeKey;
				int status = COnlineStatus::ONLINE;
				int heartbeatRate =10000;//GetHeartBeatRate();
			
				recv->ReadInt("challenge", ChallengeKey);
				recv->ReadUInt("sessionID", m_iSessionID);
				// respond to the challenge
				ISendMessage *reply = CreateServerMessage(TCLS_RESPONSE);
				reply->SetSessionID( m_iSessionID );
			


				reply->WriteInt("challenge", ChallengeKey);
				reply->WriteUInt("sessionID", m_iSessionID);
				reply->WriteInt("status", status);
				reply->WriteInt("build", 1994);
				reply->WriteInt("hrate", heartbeatRate);	// heartbeat rate to expect

				//m_iPreviousHeartBeatRateSentToServer = heartbeatRate;

				// reset the login timeout
				//m_iLoginTimeout = system()->getTimeMillis() + COnlineStatus::SERVERCONNECT_TIMEOUT;

				m_pNet->SendMessage(reply, NET_RELIABLE);
				
				}
			break;

		case TSVC_LOGINOK:
			{
				int newStatus;
				recv->ReadInt("status", newStatus);
				m_bLoggedIn=true;

				SearchForFriend(0, "[email protected]", "", "", "");

			}
			break;
		case TSVC_FRIENDSFOUND:
			{
				//char name[60];

				int serverID,sessionID;
				recv->ReadInt("uid",m_iRemoteUID);	
				recv->ReadInt("serverid",serverID);
				recv->ReadInt("sessionID",sessionID);



				// create the message to the server
				ISendMessage *msg = CreateServerMessage(TCLS_ROUTETOFRIEND);

				// write in the redirection info
				msg->WriteInt("rID", TCL_MESSAGE);
				msg->WriteUInt("rUserID", m_iRemoteUID);
				msg->WriteUInt("rSessionID", sessionID);
				msg->WriteUInt("rServerID", serverID);
				msg->WriteBlob("rData", "Hello", 5);

				m_pNet->SendMessage(msg, NET_RELIABLE);

				// lets log off
				msg = CreateServerMessage(TCLS_HEARTBEAT);
				msg->WriteInt("status", COnlineStatus::OFFLINE);	
				m_pNet->SendMessage(msg, NET_RELIABLE);

			//	m_pNet->Shutdown(true);
			//	m_pNet->deleteThis();
	//			SendStatusToServer(COnlineStatus::OFFLINE);

			}
			break;

		default:
			{
				while(recv->AdvanceField()) 
				{
					char data[512];
					const char *nm=recv->GetFieldName();
					recv->ReadString(nm, data, 512);

				}
			}

			break;
	

		//	{ TSVC_CHALLENGE,	CServerSession::ReceivedMsg_Challenge },
		//	{ TSVC_LOGINOK,		CServerSession::ReceivedMsg_LoginOK },
		//	{ TSVC_LOGINFAIL,	CServerSession::ReceivedMsg_LoginFail },
		//	{ TSVC_DISCONNECT,	CServerSession::ReceivedMsg_Disconnect },
		//	{ TSVC_FRIENDS,		CServerSession::ReceivedMsg_Friends },
		//	{ TSVC_FRIENDUPDATE, CServerSession::ReceivedMsg_FriendUpdate },
		//	{ TSVC_GAMEINFO,	CServerSession::ReceivedMsg_GameInfo },
		//	{ TSVC_HEARTBEAT,	CServerSession::ReceivedMsg_Heartbeat },
		//	{ TSVC_PINGACK,		CServerSession::ReceivedMsg_PingAck },
		

		//default:
		//	break;

		}
		//ReceivedData(recv);
		m_pNet->ReleaseMessage(recv);
	}


	// get the latest fails
	while ((recv = m_pNet->GetFailedMessage()) != NULL)
	{
		

		m_pNet->ReleaseMessage(recv);
	}

	// now let it update itself
	m_pNet->RunFrame();	
	*/
}

void VInternetDlg::SearchForFriend(unsigned int uid, const char *email, const char *username, const char *firstname, const char *lastname)
{
	ISendMessage *msg = CreateServerMessage(TCLS_FRIENDSEARCH);
	msg->WriteUInt("uid", uid);
	msg->WriteString("Email", email);
	msg->WriteString("UserName", username);
	msg->WriteString("FirstName", firstname);
	msg->WriteString("LastName", lastname);

	m_pNet->SendMessage(msg, NET_RELIABLE);
}

ISendMessage *VInternetDlg::CreateServerMessage(int msgID)
{
	ISendMessage *msg = m_pNet->CreateMessage(msgID);
	msg->SetNetAddress(GetServerAddress());
	msg->SetSessionID(m_iSessionID);
	msg->SetEncrypted(true);

	return msg;
}

CNetAddress VInternetDlg::GetServerAddress()
{
	return m_iServerAddr;// m_pNet->GetNetAddress("tracker.valvesoftware.com:1200");
}


//-----------------------------------------------------------------------------
// Purpose: Sends the first pack in the login sequence
//-----------------------------------------------------------------------------
void VInternetDlg::SendInitialLogin()
{
//	assert(m_iLoginState == LOGINSTATE_WAITINGTORECONNECT || m_iLoginState == LOGINSTATE_DISCONNECTED);

	m_iSessionID = 0;

	// stop searching for alternate servers
//	m_bServerSearch = false;

//	int desiredStatus = COnlineStatus::ONLINE;


	// setup the login message
/*	ISendMessage *loginMsg = m_pNet->CreateMessage(TCLS_LOGIN);
	loginMsg->SetNetAddress(GetServerAddress());
	loginMsg->SetEncrypted(true);
	loginMsg->SetSessionID(0);

//	const char *adr= GetServerAddress().ToStaticString();

	loginMsg->WriteUInt("uid", 36283);
	loginMsg->WriteString("email", "[email protected]");
	loginMsg->WriteString("password", "mrorange");
	loginMsg->WriteInt("status", desiredStatus);

	m_pNet->SendMessage(loginMsg, NET_RELIABLE);
*/
	// set the current status to be a connecting message
//	m_iStatus = COnlineStatus::CONNECTING;

//	m_iLoginState = LOGINSTATE_AWAITINGCHALLENGE;
	// record the time (for timeouts)
//	m_iLoginTimeout = system()->getTimeMillis() + COnlineStatus::SERVERCONNECT_TIMEOUT;


}


//-----------------------------------------------------------------------------
// Purpose: Checks to see if the current message is valid
//			replies with a message telling the sender if it's not
//-----------------------------------------------------------------------------
bool VInternetDlg::CheckMessageValidity(IReceiveMessage *dataBlock)
{
	int msgID = dataBlock->GetMsgID();
	if (msgID == TSVC_FRIENDS || msgID == TSVC_GAMEINFO || msgID == TSVC_HEARTBEAT || msgID == TSVC_FRIENDUPDATE)
	{
		// see if the server really knows us
		if (/*m_iStatus < COnlineStatus::ONLINE ||*/ m_iSessionID != dataBlock->SessionID())
		{
			// the server thinks we're still logged on to it
			// tell the server we're actually logged off from it
			ISendMessage *msg = m_pNet->CreateReply(TCLS_HEARTBEAT, dataBlock);
			// tell it we're the sessionID it thinks we are
			msg->SetSessionID(dataBlock->SessionID());
			msg->WriteInt("status", 0);
			m_pNet->SendMessage(msg, NET_RELIABLE);
			return false;
		}
	}

	return true;
}




//-----------------------------------------------------------------------------
// Purpose: Message map
//-----------------------------------------------------------------------------
MessageMapItem_t VInternetDlg::m_MessageMap[] =
{
//	MAP_MESSAGE( VInternetDlg, "PageChanged", OnGameListChanged ),
	MAP_MESSAGE_INT( VInternetDlg, "Manage", OnManageServer, "serverID" ),
	MAP_MESSAGE_PARAMS( VInternetDlg, "CvarChangeValue", OnPlayerDialog ),
	MAP_MESSAGE_INT( VInternetDlg, "DeleteServer", OnDeleteServer, "panelid" ),
	MAP_MESSAGE( VInternetDlg, "OpenContextMenu", OnOpenContextMenu ),
};
IMPLEMENT_PANELMAP(VInternetDlg, vgui::Frame);