File: netchannel_demo.cpp

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (140 lines) | stat: -rw-r--r-- 2,967 bytes parent folder | download
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
// Run this example twice to create both a server and a client

#include "netchannel_demo.h"

NetChannelDemo app;

char *NetChannelDemo::get_title()
{
	return "NetChannel demo.";
}

int NetChannelDemo::main(int argc, char **argv)
{
	// Create a console window for text-output if not available
	CL_ConsoleWindow console("My console");
	console.redirect_stdio();

	CL_SetupCore::init();
	CL_SetupNetwork::init();

	// try server - if already one, go as client.
	if (run_server() == false) run_client();

	CL_SetupNetwork::deinit();
	CL_SetupCore::deinit();

	// Display console close message and wait for a key
	console.display_close_message();

	return 1;
}
	
bool NetChannelDemo::run_server()
{
	CL_NetSession *netgame = NULL;

	try
	{
		netgame = new CL_NetSession("netchannel demo", 7667);
	}
	catch(CL_Error err)
	{
		std::cout << "Could not create server: " << err.message.c_str() << std::endl;
		std::cout << "We will try to be client instead." << std::endl;
		return false;
	}

	try
	{
		std::cout << "*** server running ***" << std::endl;
		float start_time = CL_System::get_time() / 1000.0f;
		while (true)
		{
			// Send Hello ping to clients each and every while:
			float cur_time = CL_System::get_time() / 1000.0f;
			if (cur_time - start_time > 2) // every two sec
			{
				static char *string = "Ping!...";
			
				CL_NetMessage msg;
				msg.data = string;

				netgame->send(
					0,
					netgame->get_all(),
					msg);

				start_time = cur_time; // reset timer.
			}
		
			// check network events (new computers, etc.):

			// check for joining computers - give full access to channel zero.
			try
			{
				CL_NetComputer computer = netgame->receive_computer_join();

				std::cout << "Computer joined." << std::endl;
				netgame->set_access(0, computer);
			}
			catch (CL_Error err)
			{
			}
			
			// keep ClanLib happy:
			CL_System::keep_alive();
			
			// Save some cpu power:
			CL_System::sleep(100);
		}
	}
	catch (CL_Error err)
	{
		std::cout << "Fatal server error: " << err.message.c_str() << std::endl;
	}
	
	delete netgame;

	return true;
}

void NetChannelDemo::run_client()
{
	CL_NetSession *netgame = NULL;

	try
	{
		netgame = new CL_NetSession("netchannel demo", "localhost", 7667);
//		CL_Network::find_game_at("netchannel demo", "localhost", 7667);
//		CL_Network::find_games_broadcast("netchannel demo", 7667);
//		netgame = CL_Network::receive_game_found(5000); // 5 sec timeout.

		std::cout << "*** client running ***" << std::endl;
		
		while (!netgame->receive_session_closed())
		{
			try
			{
				CL_NetMessage msg = netgame->receive(0);
				std::cout << "Server said: " << msg.data.c_str() << std::endl;
			}
			catch (...)
			{
			}
		
			// keep ClanLib happy:
			CL_System::keep_alive();

			// Save some cpu power:
			CL_System::sleep(100);
		}
		std::cout << "Game closed" << std::endl;
	}
	catch (CL_Error err)
	{
		std::cout << "Fatal client error: " << err.message.c_str() << std::endl;
	}
	
	delete netgame;
}