src/proto2/NetworkServer.cc
author terom
Wed, 05 Nov 2008 20:39:44 +0000
changeset 8 2de58a6d0395
parent 6 faa4e777cc6e
child 13 3fbb927c6a27
permissions -rw-r--r--
fix to mostly compile
5
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
     1
#include "NetworkServer.hh"
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
     2
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
     3
#include <iostream>
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
     4
8
2de58a6d0395 fix to mostly compile
terom
parents: 6
diff changeset
     5
#include <ClanLib/network.h>
2de58a6d0395 fix to mostly compile
terom
parents: 6
diff changeset
     6
6
faa4e777cc6e fiddle with cmake, fix some compile errors
terom
parents: 5
diff changeset
     7
NetworkServer::NetworkServer (GameState *state, const CL_IPAddress &listen_ip) : NetworkBase(), state(state) {
5
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
     8
	socket.bind(listen_ip);
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
     9
	socket.listen(NETWORK_SERVER_BACKLOG);
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    10
	
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    11
	// add socket to listener
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    12
	listener.add_trigger(socket.get_read_trigger());
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    13
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    14
	// bind slot for recv
8
2de58a6d0395 fix to mostly compile
terom
parents: 6
diff changeset
    15
	slot_on_recv = socket.sig_read_triggered().connect(this, &NetworkServer::_onRecv);
5
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    16
}
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    17
8
2de58a6d0395 fix to mostly compile
terom
parents: 6
diff changeset
    18
NetworkServer *NetworkServer::newServer (void) {
5
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    19
	GameState *state = new GameState(/* XXX */);
8
2de58a6d0395 fix to mostly compile
terom
parents: 6
diff changeset
    20
	CL_IPAddress listen_ip(NETWORK_PORT);
5
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    21
	
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    22
	return new NetworkServer(state, listen_ip);
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    23
}		
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    24
8
2de58a6d0395 fix to mostly compile
terom
parents: 6
diff changeset
    25
void NetworkServer::run (void) {
5
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    26
	bool isRunning = true;
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    27
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    28
	while (isRunning) {
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    29
		/* XXX: all I need to do? */
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    30
		listener.wait();
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    31
		listener.reset_all();
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    32
	}
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    33
}
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    34
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    35
void NetworkServer::_onRecv (void) {
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    36
	char buf[NETWORK_PACKET_MAX];
8
2de58a6d0395 fix to mostly compile
terom
parents: 6
diff changeset
    37
	CL_IPAddress src;
5
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    38
	int ret;
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    39
8
2de58a6d0395 fix to mostly compile
terom
parents: 6
diff changeset
    40
	ret = socket.recv((void*) buf, NETWORK_PACKET_MAX, src);
5
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    41
	
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    42
	std::cout << src.get_address() << ":" << src.get_port() << " <- " << std::string(buf, NETWORK_PACKET_MAX) << std::endl;
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    43
}
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    44
		
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    45
NetworkServerClient::NetworkServerClient (NetworkServer &server) : server(server) {
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    46
	
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    47
}
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    48
		
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    49
void runNetworkServer (void) {
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    50
	NetworkServer *server = NULL;
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    51
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    52
	server = NetworkServer::newServer();
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    53
	server->run();
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    54
}
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    55
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    56
int main (int argc, char **argv) {
8
2de58a6d0395 fix to mostly compile
terom
parents: 6
diff changeset
    57
	// setup ClanLib components
2de58a6d0395 fix to mostly compile
terom
parents: 6
diff changeset
    58
	CL_SetupCore setup_core;
2de58a6d0395 fix to mostly compile
terom
parents: 6
diff changeset
    59
	CL_SetupNetwork setup_network;
2de58a6d0395 fix to mostly compile
terom
parents: 6
diff changeset
    60
	
2de58a6d0395 fix to mostly compile
terom
parents: 6
diff changeset
    61
	// create the gamestate and run the server
5
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    62
	runNetworkServer();
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    63
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    64
	return 0;
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    65
}
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    66