author | terom |
Tue, 18 Nov 2008 22:58:50 +0000 | |
branch | no-netsession |
changeset 35 | e21cfda0edde |
parent 28 | 3da59a3bc92e |
child 36 | 785d220fc6b7 |
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 |
35 | 41 |
Vector position = readVector(pkt); |
24 | 42 |
|
35 | 43 |
Engine::log(INFO, "client.on_server_hello") << "obj=" << obj << ", pos=" << position; |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
44 |
|
24 | 45 |
// create the LocalPlayer object |
35 | 46 |
NetworkClientLocalPlayer *player = new NetworkClientLocalPlayer(*this, obj, position); |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
47 |
|
24 | 48 |
// inform state |
49 |
state.newLocalPlayer(player); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
50 |
} |
24 | 51 |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
52 |
void NetworkClient::on_player_info (CL_NetObject_Client &obj, CL_NetPacket &pkt) { |
24 | 53 |
// read the packet |
35 | 54 |
Vector position = readVector(pkt); |
24 | 55 |
|
35 | 56 |
Engine::log(INFO, "client.on_player_info") << "obj=" << obj << ", pos=" << position; |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
57 |
|
24 | 58 |
// create the LocalPlayer object |
35 | 59 |
NetworkClientRemotePlayer *player = new NetworkClientRemotePlayer(*this, obj, position); |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
60 |
|
24 | 61 |
// inform state |
62 |
state.newRemotePlayer(player); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
63 |
|
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
64 |
} |
24 | 65 |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
66 |
void NetworkClient::on_player_join (CL_NetObject_Client &obj, CL_NetPacket &pkt) { |
24 | 67 |
// read the packet |
35 | 68 |
Vector position = readVector(pkt); |
24 | 69 |
|
35 | 70 |
Engine::log(INFO, "client.on_player_join") << "obj=" << obj << ", pos=" << position; |
24 | 71 |
|
72 |
// create the RemotePlayer object |
|
35 | 73 |
NetworkClientRemotePlayer *player = new NetworkClientRemotePlayer(*this, obj, position); |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
74 |
|
24 | 75 |
// inform state |
76 |
state.newRemotePlayer(player); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
77 |
} |
24 | 78 |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
79 |
void NetworkClient::player_quit (NetworkClientRemotePlayer *player) { |
24 | 80 |
// inform state |
81 |
state.removePlayer(player); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
82 |
|
24 | 83 |
// delete |
84 |
// XXX: leak because deleting the slot while it's being called breaks ClanLib |
|
85 |
// delete player; |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
86 |
} |
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
87 |
|
35 | 88 |
NetworkClientLocalPlayer::NetworkClientLocalPlayer (NetworkClient &client, CL_NetObject_Client &obj, Vector position) : |
89 |
LocalPlayer(client.state, position, true), client(client), obj(obj) { |
|
24 | 90 |
|
91 |
// receive messages |
|
92 |
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
|
93 |
} |
24 | 94 |
|
35 | 95 |
void NetworkClientLocalPlayer::applyForce (Vector force, uint16_t dt) { |
25 | 96 |
// always send move, in all cases |
24 | 97 |
CL_NetPacket pkt; |
35 | 98 |
writeVector(pkt, force); |
99 |
pkt.output.write_uint16(dt); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
100 |
|
24 | 101 |
obj.send(NETMSG_CLIENT_MOVE, pkt, false); |
35 | 102 |
|
103 |
// do not handle locally |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
104 |
} |
24 | 105 |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
106 |
void NetworkClientLocalPlayer::on_position (CL_NetPacket &pkt) { |
35 | 107 |
Vector position = readVector(pkt); |
108 |
Vector velocity = readVector(pkt); |
|
23
8d802b573cf0
fixed more network code, there's actually a high probability of it working now
terom
parents:
22
diff
changeset
|
109 |
|
35 | 110 |
Engine::log(INFO, "client_player.on_position") << "obj=" << obj << ", position=" << position << ", velocity=" << velocity; |
24 | 111 |
|
35 | 112 |
// just update... |
113 |
updatePhysics(position, velocity); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
114 |
} |
24 | 115 |
|
35 | 116 |
NetworkClientRemotePlayer::NetworkClientRemotePlayer (NetworkClient &client, CL_NetObject_Client &obj, Vector position) : |
117 |
RemotePlayer(client.state, position, true), client(client), obj(obj) { |
|
24 | 118 |
|
119 |
// receive messages |
|
120 |
slots.connect(obj.sig_received_message(NETMSG_PLAYER_POSITION), this, &NetworkClientRemotePlayer::on_position); |
|
121 |
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
|
122 |
} |
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 |
void NetworkClientRemotePlayer::on_position (CL_NetPacket &pkt) { |
35 | 125 |
Vector position = readVector(pkt); |
126 |
Vector velocity = readVector(pkt); |
|
127 |
||
128 |
Engine::log(INFO, "client_player.on_position") << "obj=" << obj << ", position=" << position << ", velocity=" << velocity; |
|
24 | 129 |
|
35 | 130 |
// just update... |
131 |
updatePhysics(position, velocity); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
132 |
} |
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 |
void NetworkClientRemotePlayer::on_quit (CL_NetPacket &pkt) { |
24 | 135 |
// pkt is empty |
136 |
(void) pkt; |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
137 |
|
24 | 138 |
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
|
139 |
|
24 | 140 |
client.player_quit(this); |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
141 |
} |