File: netchannel_demo.cpp

package info (click to toggle)
clanlib0 0.4.4-8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,400 kB
  • ctags: 6,569
  • sloc: cpp: 45,626; ansic: 1,713; makefile: 695; asm: 250; sh: 48
file content (120 lines) | stat: -rw-r--r-- 2,495 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

#include "netchannel_demo.h"

NetChannelDemo app;

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

int NetChannelDemo::main(int argc, char **argv)
{
	// try server - if already one, go as client.
	if (run_server() == false) run_client();

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

	try
	{
		netgame = CL_Network::create_game("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.
			const CL_NetComputer *computer = netgame->receive_computer_join();
			if (computer != NULL)
			{
				std::cout << "Computer joined." << std::endl;
				netgame->set_access(0, computer);
			}
			
			// 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_NetGame *netgame = NULL;

	try
	{
//		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_game_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;
}