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