src/proto2/Engine.cc
author terom
Sat, 08 Nov 2008 21:25:56 +0000
changeset 23 8d802b573cf0
parent 22 b70d30e1b0fe
child 24 b81cb670e6b2
permissions -rw-r--r--
fixed more network code, there's actually a high probability of it working now
21
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
     1
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
     2
#include "Engine.hh"
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
     3
#include "GameState.hh"
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
     4
#include "NetworkServer.hh"
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
     5
#include "NetworkClient.hh"
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
     6
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
     7
#include <iostream>
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
     8
21
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
     9
Engine::Engine (void) : is_running(true) {
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    10
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    11
}
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    12
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    13
void Engine::runNetworkServer (const std::string &listen_port) {
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    14
	// the engine
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    15
	Engine engine;
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    16
	
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    17
	// setup network
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    18
	CL_SetupNetwork setup_network;
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    19
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    20
	try {
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    21
		// create the server
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    22
		engine.net_server = new NetworkServer(engine.game_state, listen_port);
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    23
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    24
		// run the main loop
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    25
		engine.main_loop();
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    26
	
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    27
	} catch (CL_Error &e) {
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    28
		std::cerr << "NetworkServer::main: CL_Error:" << e.message << std::endl;
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    29
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    30
		throw;
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    31
	}
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    32
}
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    33
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    34
void Engine::runNetworkClient (const std::string &connect_host, const std::string &connect_port) {
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    35
	// the engine
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    36
	Engine engine;
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    37
	
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    38
	// setup network
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    39
	CL_SetupNetwork setup_network;
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    40
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    41
	// connect_to
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    42
	CL_IPAddress connect_addr(connect_host, connect_port);
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    43
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    44
	try {
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    45
		// create the server
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    46
		engine.net_client = new NetworkClient(engine.game_state, connect_addr);
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    47
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    48
		// run the main loop
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    49
		engine.main_loop();
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    50
	
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    51
	} catch (CL_Error &e) {
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    52
		std::cerr << "NetworkServer::main: CL_Error:" << e.message << std::endl;
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    53
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    54
		throw;
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    55
	}
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    56
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    57
}
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    58
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    59
void Engine::main_loop (void) {
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    60
	while (is_running) {
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    61
		// this does.... magical things
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    62
		CL_System::keep_alive();
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    63
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    64
		// if I can't find some better way to do this in ClanLib by next thursday, then it f*%!ing sucks
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    65
		// ideally, we should be able to have a main loop that does timed waits on I/O, fufilling some set of timers
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    66
		// but as far as I can tell, ClanLib doesn't have anything like that
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    67
		CL_System::sleep(100);
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    68
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    69
	}
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    70
}
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    71
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    72
Logger Engine::log (enum LogLevel level, const char *type) {
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    73
	return Logger(level <= WARN ? std::cerr : std::cout, level, type);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    74
}
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    75