src/proto2/NetworkServer.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
5
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
     1
#include "NetworkServer.hh"
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
     2
#include "Engine.hh"
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
     3
#include "Logger.hh"
5
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
     4
14
22e3bfb6720d NetworkServer.cc finally works as intended
terom
parents: 13
diff changeset
     5
#include <cassert>
5
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
     6
21
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents: 15
diff changeset
     7
NetworkServer::NetworkServer (GameState &state, const std::string &listen_port) : 
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
     8
	NetworkCore(state), pid_pool(0) {
5
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
     9
	
21
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents: 15
diff changeset
    10
	// connect slots
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents: 15
diff changeset
    11
	slots.connect(netsession.sig_computer_connected(), this, &NetworkServer::on_connect);
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents: 15
diff changeset
    12
	slots.connect(netsession.sig_computer_disconnected(), this, &NetworkServer::on_disconnect);
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents: 15
diff changeset
    13
	
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents: 15
diff changeset
    14
	// and then we listen
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents: 15
diff changeset
    15
	netsession.start_listen(listen_port);
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    16
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    17
	Engine::log(INFO, "server") << "running, listen_port=" << listen_port;
5
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    18
}
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    19
21
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents: 15
diff changeset
    20
void NetworkServer::on_connect (CL_NetComputer &computer) {
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    21
	// assign a pid
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    22
	uint16_t pid = ++pid_pool;
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    23
	
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    24
	// create the player object (it logs it)
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    25
	NetworkServerPlayer *player = new NetworkServerPlayer(*this, computer, pid);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    26
	
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    27
	// map computer to it
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    28
	players[computer] = player;
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    29
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    30
	// add to GameState
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    31
	state.newRemotePlayer(player);
5
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    32
}
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    33
21
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents: 15
diff changeset
    34
void NetworkServer::on_disconnect (CL_NetComputer &computer) {
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    35
	NetworkServerPlayer *player = players[computer];
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    36
	
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    37
	// remove from players
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    38
	players.erase(computer);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    39
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    40
	// remove from state
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    41
	state.removePlayer(player);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    42
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    43
	// remove from game
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    44
	player->disconnected();
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    45
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    46
	// delete
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    47
	delete player;
5
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    48
}
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    49
		
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    50
NetworkServerPlayer::NetworkServerPlayer (NetworkServer &server, CL_NetComputer &computer, uint16_t pid) : 
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    51
	RemotePlayer(Coordinate(0, 0), true), server(server), computer(computer), obj(&server.netobjs), pid(pid) {
5
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    52
	
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    53
	// log
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    54
	Engine::log(INFO, "server_player.connected") << "computer=" << computer << ", obj=" << obj;
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    55
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    56
	// messages
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    57
	slots.connect(obj.sig_received_message(NETMSG_CLIENT_MOVE), this, &NetworkServerPlayer::on_move);	
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    58
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    59
	// the initial NETMSG_PLAYER_HELLO
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    60
	CL_NetPacket hello_pkt;
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    61
	hello_pkt.output.write_uint32(position.x);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    62
	hello_pkt.output.write_uint32(position.y);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    63
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    64
	obj.send(computer, NETMSG_SERVER_HELLO, hello_pkt, true);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    65
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    66
	// send other player objects
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    67
	for (std::map<CL_NetComputer, NetworkServerPlayer*>::iterator it = server.players.begin(); it != server.players.end(); it++) {
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    68
		NetworkServerPlayer *player = it->second;
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    69
		CL_NetPacket player_pkt;
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    70
		
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    71
		// player is not in players list yet
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    72
		assert(player != this);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    73
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    74
		player_pkt.output.write_uint32(player->position.x);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    75
		player_pkt.output.write_uint32(player->position.y);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    76
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    77
		player->obj.send(computer, NETMSG_PLAYER_INFO, player_pkt, true);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    78
	}
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    79
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    80
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    81
	// broadcast NETMSG_PLAYER_JOIN to all clients
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    82
	obj.send(server.netsession.get_all(), NETMSG_PLAYER_JOIN, hello_pkt, true);
5
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    83
}
617813994ab1 move proto/p2 -> src/proto2
terom
parents:
diff changeset
    84
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    85
void NetworkServerPlayer::disconnected (void) {
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    86
	CL_NetPacket pkt;
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    87
	
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    88
	Engine::log(INFO, "server_player.disconnected") << "computer=" << computer << ", obj=" << obj;
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    89
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    90
	obj.send(server.netsession.get_all(), NETMSG_PLAYER_QUIT, pkt, true);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    91
}
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    92
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    93
void NetworkServerPlayer::on_move (CL_NetComputer &from, CL_NetPacket &pkt) {
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    94
	// sanity-check
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    95
	if (!(from == computer))
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    96
		return;
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    97
	
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    98
	// read packet
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    99
	uint32_t dx = pkt.input.read_uint32();
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   100
	uint32_t dy = pkt.input.read_uint32();
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   101
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   102
	// movement delta
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   103
	PositionDelta delta(dx, dy);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   104
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
   105
	Engine::log(INFO, "server_player.on_move") << "obj=" << obj << ", old_pos=" << position << ", delta=" << delta;
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
   106
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   107
	// apply movement
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   108
	position += delta;
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   109
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   110
	// send position update
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   111
	send_position_update();
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   112
}
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   113
		
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   114
void NetworkServerPlayer::send_position_update (void) {
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   115
	CL_NetPacket pkt;
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   116
	pkt.output.write_uint32(position.x);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   117
	pkt.output.write_uint32(position.y);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   118
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
   119
	Engine::log(INFO, "server_player.send_position_update") << "obj=" << obj << " -> " << position;
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
   120
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
   121
	obj.send(server.netsession.get_all(), NETMSG_PLAYER_POSITION, pkt, false);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   122
}
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   123