author | terom |
Sun, 09 Nov 2008 20:40:46 +0000 | |
changeset 24 | b81cb670e6b2 |
parent 23 | 8d802b573cf0 |
child 25 | af75a1894a32 |
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 |
|
6 |
NetworkClient::NetworkClient (GameState &state, const CL_IPAddress &connect_to) : |
|
24 | 7 |
NetworkCore(state), server(netsession.connect(connect_to)) { |
8 |
||
9 |
// connect slots |
|
10 |
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
|
11 |
|
24 | 12 |
// XXX: sig_disconnected |
21 | 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) { |
24 | 16 |
switch (msg_type) { |
17 |
case NETMSG_SERVER_HELLO: |
|
18 |
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
|
19 |
|
24 | 20 |
break; |
21 |
||
22 |
case NETMSG_PLAYER_INFO: |
|
23 |
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
|
24 |
|
24 | 25 |
break; |
26 |
||
27 |
case NETMSG_PLAYER_JOIN: |
|
28 |
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
|
29 |
|
24 | 30 |
break; |
31 |
||
32 |
default: |
|
33 |
Engine::log(WARN, "client.on_create_object") << "unknown msg_type=" << msg_type << " for obj=" << obj; |
|
34 |
} |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
35 |
} |
24 | 36 |
|
22
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) { |
24 | 38 |
// read the packet |
39 |
uint32_t x = pkt.input.read_uint32(); |
|
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 |
|
24 | 42 |
Coordinate initial_position(x, y); |
43 |
||
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 |
|
24 | 46 |
// create the LocalPlayer object |
47 |
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
|
48 |
|
24 | 49 |
// inform state |
50 |
state.newLocalPlayer(player); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
51 |
} |
24 | 52 |
|
22
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) { |
24 | 54 |
// read the packet |
55 |
uint32_t x = pkt.input.read_uint32(); |
|
56 |
uint32_t y = pkt.input.read_uint32(); |
|
57 |
||
58 |
Coordinate initial_position(x, y); |
|
59 |
||
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 |
|
24 | 62 |
// create the LocalPlayer object |
63 |
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
|
64 |
|
24 | 65 |
// inform state |
66 |
state.newRemotePlayer(player); |
|
22
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 |
} |
24 | 69 |
|
22
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) { |
24 | 71 |
// read the packet |
72 |
uint32_t x = pkt.input.read_uint32(); |
|
73 |
uint32_t y = pkt.input.read_uint32(); |
|
74 |
||
75 |
Coordinate initial_position(x, y); |
|
76 |
||
77 |
Engine::log(INFO, "client.on_player_join") << "obj=" << obj << ", pos=" << initial_position; |
|
78 |
||
79 |
// create the RemotePlayer object |
|
80 |
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
|
81 |
|
24 | 82 |
// inform state |
83 |
state.newRemotePlayer(player); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
84 |
} |
24 | 85 |
|
22
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) { |
24 | 87 |
// inform state |
88 |
state.removePlayer(player); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
89 |
|
24 | 90 |
// delete |
91 |
// XXX: leak because deleting the slot while it's being called breaks ClanLib |
|
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) : |
24 | 96 |
LocalPlayer(initial_position, true), client(client), obj(obj) { |
97 |
||
98 |
// receive messages |
|
99 |
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
|
100 |
} |
24 | 101 |
|
22
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) { |
24 | 103 |
CL_NetPacket pkt; |
104 |
pkt.output.write_uint32(d.dx); |
|
105 |
pkt.output.write_uint32(d.dy); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
106 |
|
24 | 107 |
obj.send(NETMSG_CLIENT_MOVE, pkt, false); |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
108 |
} |
24 | 109 |
|
22
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) { |
24 | 111 |
uint32_t x = pkt.input.read_uint32(); |
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 |
|
24 | 114 |
Coordinate pos (x, y); |
115 |
||
116 |
Engine::log(INFO, "client_player.on_position") << "obj=" << obj << ", pos=" << pos; |
|
117 |
||
118 |
updatePosition(pos); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
119 |
} |
24 | 120 |
|
22
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) : |
24 | 122 |
RemotePlayer(initial_position, true), client(client), obj(obj) { |
123 |
||
124 |
// receive messages |
|
125 |
slots.connect(obj.sig_received_message(NETMSG_PLAYER_POSITION), this, &NetworkClientRemotePlayer::on_position); |
|
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) { |
24 | 130 |
uint32_t x = pkt.input.read_uint32(); |
131 |
uint32_t y = pkt.input.read_uint32(); |
|
132 |
||
133 |
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
|
134 |
|
24 | 135 |
Engine::log(INFO, "client_player.on_position") << "obj=" << obj << ", pos=" << pos; |
136 |
||
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) { |
24 | 141 |
// pkt is empty |
142 |
(void) pkt; |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
143 |
|
24 | 144 |
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
|
145 |
|
24 | 146 |
client.player_quit(this); |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
147 |
} |