author | terom |
Thu, 20 Nov 2008 19:25:56 +0000 | |
branch | no-netsession |
changeset 36 | 785d220fc6b7 |
parent 35 | e21cfda0edde |
child 38 | 4189b8bf3a5b |
permissions | -rw-r--r-- |
5 | 1 |
#include "NetworkServer.hh" |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
2 |
#include "Engine.hh" |
23
8d802b573cf0
fixed more network code, there's actually a high probability of it working now
terom
parents:
22
diff
changeset
|
3 |
#include "Logger.hh" |
5 | 4 |
|
14 | 5 |
#include <cassert> |
5 | 6 |
|
36 | 7 |
NetworkServer::NetworkServer (GameState &state, const NetworkAddress &listen_addr) : |
8 |
NetworkCore(state), netsession(NETWORK_MAGIC_ID), netobjs(netsession, NETCHAN_CORE) { |
|
24 | 9 |
|
10 |
// connect slots |
|
36 | 11 |
slots.connect(netsession.sig_node_connected(), this, &NetworkServer::on_node_connected); |
24 | 12 |
|
13 |
// and then we listen |
|
36 | 14 |
netsession.listen(listen_addr); |
23
8d802b573cf0
fixed more network code, there's actually a high probability of it working now
terom
parents:
22
diff
changeset
|
15 |
|
36 | 16 |
Engine::log(INFO, "server") << "running, listen_addr=" << listen_addr; |
5 | 17 |
} |
18 |
||
36 | 19 |
void NetworkServer::on_node_connected (NetworkNode *node) { |
24 | 20 |
// create the player object (it logs it) |
36 | 21 |
NetworkServerPlayer *player = new NetworkServerPlayer(*this, node); |
22 |
||
23 |
// add to players |
|
24 |
players.push_back(player); |
|
24 | 25 |
|
26 |
// add to GameState |
|
27 |
state.newRemotePlayer(player); |
|
5 | 28 |
} |
36 | 29 |
|
30 |
void NetworkServer::handle_disconnect (NetworkServerPlayer *player) { |
|
24 | 31 |
// remove from state |
32 |
state.removePlayer(player); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
33 |
|
36 | 34 |
// remove from list |
35 |
players.remove(player); |
|
24 | 36 |
} |
37 |
||
36 | 38 |
NetworkServerPlayer::NetworkServerPlayer (NetworkServer &server, NetworkNode *node) : |
39 |
RemotePlayer(server.state, Vector(PLAYER_INITIAL_X, PLAYER_INITIAL_Y), true), server(server), obj(server.netobjs), node(node) { |
|
24 | 40 |
|
41 |
// log |
|
36 | 42 |
Engine::log(INFO, "server_player.connected") << "node=" << node << ", obj=" << obj; |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
43 |
|
24 | 44 |
// messages |
36 | 45 |
slots.connect(node->sig_disconnected(), this, &NetworkServerPlayer::on_disconnected); |
46 |
slots.connect(obj.sig_message(NETMSG_CLIENT_MOVE), this, &NetworkServerPlayer::on_move); |
|
24 | 47 |
|
48 |
// the initial NETMSG_PLAYER_HELLO |
|
36 | 49 |
NetworkPacket hello_pkt; |
50 |
hello_pkt.write_vector(position); |
|
24 | 51 |
|
36 | 52 |
obj.send_to(node, NETMSG_SERVER_HELLO, hello_pkt, true); |
24 | 53 |
|
54 |
// send other player objects |
|
36 | 55 |
for (std::list<NetworkServerPlayer*>::iterator it = server.players.begin(); it != server.players.end(); it++) { |
56 |
NetworkServerPlayer *player = *it; |
|
57 |
NetworkPacket player_pkt; |
|
24 | 58 |
|
59 |
// player is not in players list yet |
|
60 |
assert(player != this); |
|
36 | 61 |
|
62 |
player_pkt.write_vector(player->position); |
|
24 | 63 |
|
36 | 64 |
player->obj.send_to(node, NETMSG_PLAYER_INFO, player_pkt, true); |
24 | 65 |
} |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
66 |
|
36 | 67 |
// broadcast NETMSG_PLAYER_JOIN to all clients except current |
68 |
obj.send_all_except(NETMSG_PLAYER_JOIN, hello_pkt, node, true); |
|
5 | 69 |
} |
70 |
||
36 | 71 |
void NetworkServerPlayer::on_disconnected (void) { |
72 |
NetworkPacket pkt; |
|
24 | 73 |
|
36 | 74 |
Engine::log(INFO, "server_player.disconnected") << "node=" << node << ", obj=" << obj; |
75 |
||
76 |
// remove from server |
|
77 |
server.handle_disconnect(this); |
|
78 |
||
79 |
// tell other clients |
|
80 |
obj.send_all(NETMSG_PLAYER_QUIT, pkt, true); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
81 |
|
36 | 82 |
// free |
83 |
delete this; |
|
22
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 |
|
36 | 86 |
void NetworkServerPlayer::on_move (NetworkNode *src, NetworkPacket &pkt) { |
24 | 87 |
// sanity-check |
36 | 88 |
if (src != node) |
24 | 89 |
return; |
90 |
||
36 | 91 |
Vector impulse_force = pkt.read_vector(); |
92 |
uint16_t impulse_ms = pkt.read_uint16(); |
|
23
8d802b573cf0
fixed more network code, there's actually a high probability of it working now
terom
parents:
22
diff
changeset
|
93 |
|
36 | 94 |
Engine::log(INFO, "server_player.on_move") << "player=" << obj << ", old_pos=" << position << ", impulse=" << impulse_force << "@" << impulse_ms << "ms"; |
35 | 95 |
|
96 |
// apply force |
|
97 |
applyForce(impulse_force, impulse_ms); |
|
24 | 98 |
|
99 |
// send position update |
|
100 |
send_position_update(); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
101 |
} |
24 | 102 |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
103 |
void NetworkServerPlayer::send_position_update (void) { |
36 | 104 |
NetworkPacket pkt; |
105 |
pkt.write_vector(position); |
|
106 |
pkt.write_vector(velocity); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
107 |
|
35 | 108 |
Engine::log(INFO, "server_player.send_position_update") << "obj=" << obj << " -> " << position << "+" << velocity; |
23
8d802b573cf0
fixed more network code, there's actually a high probability of it working now
terom
parents:
22
diff
changeset
|
109 |
|
36 | 110 |
obj.send_all(NETMSG_PLAYER_POSITION, pkt, false); |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
111 |
} |
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
112 |