src/proto2/NetworkClient.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 "NetworkClient.hh"
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
     3
#include "Engine.hh"
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
     4
#include "Logger.hh"
21
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
     5
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
     6
NetworkClient::NetworkClient (GameState &state, const CL_IPAddress &connect_to) : 
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
     7
	NetworkCore(state), server(netsession.connect(connect_to)) {
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
     8
	
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
     9
	// connect slots
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    10
	slots.connect(netobjs.sig_create_object(), this, &NetworkClient::on_create_object);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    11
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    12
	// XXX: sig_disconnected
21
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    13
}
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    14
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    15
void NetworkClient::on_create_object (CL_NetObject_Client &obj, int msg_type, CL_NetPacket &pkt) {
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    16
	switch (msg_type) {
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    17
		case NETMSG_SERVER_HELLO:
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    18
			on_server_hello(obj, pkt);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    19
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    20
			break;
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    21
		
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    22
		case NETMSG_PLAYER_INFO:
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    23
			on_player_info(obj, pkt);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    24
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    25
			break;
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
		case NETMSG_PLAYER_JOIN:
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    28
			on_player_join(obj, pkt);
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
			break;
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    31
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    32
		default:
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    33
			Engine::log(WARN, "client.on_create_object") << "unknown msg_type=" << msg_type << " for obj=" << obj;
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    34
	}
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    35
}
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
void NetworkClient::on_server_hello (CL_NetObject_Client &obj, CL_NetPacket &pkt) {
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    38
	// read the packet
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    39
	uint32_t x = pkt.input.read_uint32();
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    40
	uint32_t y = pkt.input.read_uint32();
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    41
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    42
	Coordinate initial_position(x, y);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    43
	
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    44
	Engine::log(INFO, "client.on_server_hello") << "obj=" << obj << ", pos=" << initial_position;
22
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
	// create the LocalPlayer object
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    47
	NetworkClientLocalPlayer *player = new NetworkClientLocalPlayer(*this, obj, initial_position);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    48
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    49
	// inform state
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    50
	state.newLocalPlayer(player);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    51
}
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    52
		
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    53
void NetworkClient::on_player_info (CL_NetObject_Client &obj, CL_NetPacket &pkt) {
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    54
	// read the packet
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    55
	uint32_t x = pkt.input.read_uint32();
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    56
	uint32_t y = pkt.input.read_uint32();
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    57
	
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    58
	Coordinate initial_position(x, y);
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    59
	
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    60
	Engine::log(INFO, "client.on_player_info") << "obj=" << obj << ", pos=" << initial_position;
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    61
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    62
	// create the LocalPlayer object
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    63
	NetworkClientRemotePlayer *player = new NetworkClientRemotePlayer(*this, obj, initial_position);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    64
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    65
	// inform state
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    66
	state.newRemotePlayer(player);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    67
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    68
}
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    69
		
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    70
void NetworkClient::on_player_join (CL_NetObject_Client &obj, CL_NetPacket &pkt) {
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    71
	// read the packet
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    72
	uint32_t x = pkt.input.read_uint32();
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    73
	uint32_t y = pkt.input.read_uint32();
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
	Coordinate initial_position(x, 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
	Engine::log(INFO, "client.on_player_join") << "obj=" << obj << ", pos=" << initial_position;
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    78
	
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    79
	// create the RemotePlayer object
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    80
	NetworkClientRemotePlayer *player = new NetworkClientRemotePlayer(*this, obj, initial_position);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    81
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    82
	// inform state
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    83
	state.newRemotePlayer(player);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    84
}
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    85
		
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    86
void NetworkClient::player_quit (NetworkClientRemotePlayer *player) {
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    87
	// inform state
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    88
	state.removePlayer(player);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    89
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    90
	// delete
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    91
	// XXX: leak because deleting the slot while it's being called breaks ClanLib
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    92
	//	delete player;
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    93
}
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    94
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    95
NetworkClientLocalPlayer::NetworkClientLocalPlayer (NetworkClient &client, CL_NetObject_Client &obj, Coordinate initial_position) :
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    96
	LocalPlayer(initial_position, true), client(client), obj(obj) {
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
	// receive messages
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    99
	slots.connect(obj.sig_received_message(NETMSG_PLAYER_POSITION), this, &NetworkClientLocalPlayer::on_position);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   100
}
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
void NetworkClientLocalPlayer::move (PositionDelta d) {
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   103
	CL_NetPacket pkt;
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   104
	pkt.output.write_uint32(d.dx);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   105
	pkt.output.write_uint32(d.dy);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   106
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   107
	obj.send(NETMSG_CLIENT_MOVE, pkt, false);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   108
}
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
void NetworkClientLocalPlayer::on_position (CL_NetPacket &pkt) {
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   111
	uint32_t x = pkt.input.read_uint32();
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   112
	uint32_t y = pkt.input.read_uint32();
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
   113
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
   114
	Coordinate pos (x, y);
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
   115
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
   116
	Engine::log(INFO, "client_player.on_position") << "obj=" << obj << ", pos=" << pos;
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   117
	
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
   118
	updatePosition(pos);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   119
}
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   120
		
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   121
NetworkClientRemotePlayer::NetworkClientRemotePlayer (NetworkClient &client, CL_NetObject_Client &obj, Coordinate initial_position) :
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   122
	RemotePlayer(initial_position, true), client(client), obj(obj) {
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   123
	
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   124
	// receive messages
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   125
	slots.connect(obj.sig_received_message(NETMSG_PLAYER_POSITION), this, &NetworkClientRemotePlayer::on_position);
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
   126
	slots.connect(obj.sig_received_message(NETMSG_PLAYER_QUIT), this, &NetworkClientRemotePlayer::on_quit);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   127
}
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   128
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   129
void NetworkClientRemotePlayer::on_position (CL_NetPacket &pkt) {
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   130
	uint32_t x = pkt.input.read_uint32();
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   131
	uint32_t y = pkt.input.read_uint32();
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   132
	
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
   133
	Coordinate pos (x, y);
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
   134
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
   135
	Engine::log(INFO, "client_player.on_position") << "obj=" << obj << ", pos=" << pos;
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
   136
	
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
   137
	updatePosition(pos);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   138
}
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   139
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   140
void NetworkClientRemotePlayer::on_quit (CL_NetPacket &pkt) {
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   141
	// pkt is empty
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   142
	(void) pkt;
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   143
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
   144
	Engine::log(INFO, "client_player.on_quit") << "obj=" << obj;
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
   145
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   146
	client.player_quit(this);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   147
}