author | terom |
Thu, 20 Nov 2008 19:25:56 +0000 | |
branch | no-netsession |
changeset 36 | 785d220fc6b7 |
parent 35 | e21cfda0edde |
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 |
||
36 | 8 |
NetworkClient::NetworkClient (GameState &state, const NetworkAddress &connect_to) : |
9 |
NetworkCore(state), netsession(NETWORK_MAGIC_ID), server(netsession.connect(connect_to)), netobjs(netsession, NETCHAN_CORE, server) { |
|
24 | 10 |
|
11 |
// connect slots |
|
36 | 12 |
slots.connect(netobjs.sig_create(), this, &NetworkClient::on_create); |
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 |
|
36 | 17 |
void NetworkClient::on_create (NetworkObject_Client *obj, NetworkMessageID msg_id, NetworkPacket &pkt) { |
18 |
switch (msg_id) { |
|
24 | 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: |
|
36 | 35 |
Engine::log(WARN, "client.on_create_object") << "unknown msg_id=" << msg_id << " for obj=" << obj; |
24 | 36 |
} |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
37 |
} |
24 | 38 |
|
36 | 39 |
void NetworkClient::on_server_hello (NetworkObject_Client *obj, NetworkPacket &pkt) { |
24 | 40 |
// read the packet |
36 | 41 |
Vector position = pkt.read_vector(); |
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 |
|
36 | 52 |
void NetworkClient::on_player_info (NetworkObject_Client *obj, NetworkPacket &pkt) { |
24 | 53 |
// read the packet |
36 | 54 |
Vector position = pkt.read_vector(); |
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 |
|
36 | 66 |
void NetworkClient::on_player_join (NetworkObject_Client *obj, NetworkPacket &pkt) { |
24 | 67 |
// read the packet |
36 | 68 |
Vector position = pkt.read_vector(); |
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 |
|
36 | 88 |
NetworkClientLocalPlayer::NetworkClientLocalPlayer (NetworkClient &client, NetworkObject_Client *obj, Vector position) : |
35 | 89 |
LocalPlayer(client.state, position, true), client(client), obj(obj) { |
24 | 90 |
|
91 |
// receive messages |
|
36 | 92 |
slots.connect(obj->sig_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 |
36 | 97 |
NetworkPacket pkt; |
98 |
pkt.write_vector(force); |
|
99 |
pkt.write_uint16(dt); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
100 |
|
36 | 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 |
|
36 | 106 |
void NetworkClientLocalPlayer::on_position (NetworkPacket &pkt) { |
107 |
Vector position = pkt.read_vector(); |
|
108 |
Vector velocity = pkt.read_vector(); |
|
23
8d802b573cf0
fixed more network code, there's actually a high probability of it working now
terom
parents:
22
diff
changeset
|
109 |
|
36 | 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 |
|
36 | 116 |
NetworkClientRemotePlayer::NetworkClientRemotePlayer (NetworkClient &client, NetworkObject_Client *obj, Vector position) : |
35 | 117 |
RemotePlayer(client.state, position, true), client(client), obj(obj) { |
24 | 118 |
|
119 |
// receive messages |
|
36 | 120 |
slots.connect(obj->sig_message(NETMSG_PLAYER_POSITION), this, &NetworkClientRemotePlayer::on_position); |
121 |
slots.connect(obj->sig_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 |
|
36 | 124 |
void NetworkClientRemotePlayer::on_position (NetworkPacket &pkt) { |
125 |
Vector position = pkt.read_vector(); |
|
126 |
Vector velocity = pkt.read_vector(); |
|
35 | 127 |
|
36 | 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 |
|
36 | 134 |
void NetworkClientRemotePlayer::on_quit (NetworkPacket &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 |
|
36 | 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 |
} |
36 | 142 |