| author | saiam |
| Thu, 20 Nov 2008 21:25:09 +0000 | |
| changeset 83 | cbba9729e92b |
| parent 66 | 1415a2d45686 |
| child 89 | 825c4613e087 |
| 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 |
|
| 21 | 7 |
NetworkServer::NetworkServer (GameState &state, const std::string &listen_port) : |
| 24 | 8 |
NetworkCore(state), pid_pool(0) {
|
9 |
||
10 |
// connect slots |
|
11 |
slots.connect(netsession.sig_computer_connected(), this, &NetworkServer::on_connect); |
|
12 |
slots.connect(netsession.sig_computer_disconnected(), this, &NetworkServer::on_disconnect); |
|
13 |
||
14 |
// and then we listen |
|
15 |
netsession.start_listen(listen_port); |
|
|
23
8d802b573cf0
fixed more network code, there's actually a high probability of it working now
terom
parents:
22
diff
changeset
|
16 |
|
| 24 | 17 |
Engine::log(INFO, "server") << "running, listen_port=" << listen_port; |
| 5 | 18 |
} |
19 |
||
| 21 | 20 |
void NetworkServer::on_connect (CL_NetComputer &computer) {
|
| 24 | 21 |
// assign a pid |
22 |
uint16_t pid = ++pid_pool; |
|
23 |
||
24 |
// create the player object (it logs it) |
|
25 |
NetworkServerPlayer *player = new NetworkServerPlayer(*this, computer, pid); |
|
26 |
||
27 |
// map computer to it |
|
28 |
players[computer] = player; |
|
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
29 |
|
| 24 | 30 |
// add to GameState |
31 |
state.newRemotePlayer(player); |
|
| 5 | 32 |
} |
33 |
||
| 21 | 34 |
void NetworkServer::on_disconnect (CL_NetComputer &computer) {
|
| 24 | 35 |
NetworkServerPlayer *player = players[computer]; |
36 |
||
37 |
// remove from players |
|
38 |
players.erase(computer); |
|
|
23
8d802b573cf0
fixed more network code, there's actually a high probability of it working now
terom
parents:
22
diff
changeset
|
39 |
|
| 24 | 40 |
// remove from state |
41 |
state.removePlayer(player); |
|
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
42 |
|
| 24 | 43 |
// remove from game |
44 |
player->disconnected(); |
|
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
45 |
|
| 24 | 46 |
// delete |
47 |
delete player; |
|
48 |
} |
|
49 |
||
50 |
NetworkServerPlayer::NetworkServerPlayer (NetworkServer &server, CL_NetComputer &computer, uint16_t pid) : |
|
| 66 | 51 |
RemotePlayer(server.state, Vector(PLAYER_INITIAL_X, PLAYER_INITIAL_Y), true), server(server), computer(computer), obj(&server.netobjs), pid(pid) {
|
| 24 | 52 |
|
53 |
// log |
|
54 |
Engine::log(INFO, "server_player.connected") << "computer=" << computer << ", obj=" << obj; |
|
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
55 |
|
| 24 | 56 |
// messages |
57 |
slots.connect(obj.sig_received_message(NETMSG_CLIENT_MOVE), this, &NetworkServerPlayer::on_move); |
|
58 |
||
59 |
// the initial NETMSG_PLAYER_HELLO |
|
60 |
CL_NetPacket hello_pkt; |
|
| 66 | 61 |
writeVector(hello_pkt, position); |
| 24 | 62 |
|
63 |
obj.send(computer, NETMSG_SERVER_HELLO, hello_pkt, true); |
|
64 |
||
65 |
// send other player objects |
|
66 |
for (std::map<CL_NetComputer, NetworkServerPlayer*>::iterator it = server.players.begin(); it != server.players.end(); it++) {
|
|
67 |
NetworkServerPlayer *player = it->second; |
|
68 |
CL_NetPacket player_pkt; |
|
69 |
||
70 |
// player is not in players list yet |
|
71 |
assert(player != this); |
|
72 |
||
| 66 | 73 |
writeVector(player_pkt, player->position); |
| 24 | 74 |
|
75 |
player->obj.send(computer, NETMSG_PLAYER_INFO, player_pkt, true); |
|
76 |
} |
|
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
77 |
|
|
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
78 |
|
| 24 | 79 |
// broadcast NETMSG_PLAYER_JOIN to all clients |
80 |
obj.send(server.netsession.get_all(), NETMSG_PLAYER_JOIN, hello_pkt, true); |
|
| 5 | 81 |
} |
82 |
||
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
83 |
void NetworkServerPlayer::disconnected (void) {
|
| 24 | 84 |
CL_NetPacket pkt; |
85 |
||
86 |
Engine::log(INFO, "server_player.disconnected") << "computer=" << computer << ", obj=" << obj; |
|
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
87 |
|
| 24 | 88 |
obj.send(server.netsession.get_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
|
89 |
} |
|
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
90 |
|
|
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
91 |
void NetworkServerPlayer::on_move (CL_NetComputer &from, CL_NetPacket &pkt) {
|
| 24 | 92 |
// sanity-check |
93 |
if (!(from == computer)) |
|
94 |
return; |
|
95 |
||
| 66 | 96 |
Vector impulse_force = readVector(pkt); |
97 |
uint16_t impulse_ms = pkt.input.read_uint16(); |
|
|
23
8d802b573cf0
fixed more network code, there's actually a high probability of it working now
terom
parents:
22
diff
changeset
|
98 |
|
| 66 | 99 |
Engine::log(INFO, "server_player.on_move") << "obj=" << obj << ", old_pos=" << position << ", impulse=" << impulse_force << "@" << impulse_ms << "ms"; |
100 |
||
101 |
// apply force |
|
102 |
applyForce(impulse_force, impulse_ms); |
|
| 24 | 103 |
|
104 |
// send position update |
|
105 |
send_position_update(); |
|
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
106 |
} |
| 24 | 107 |
|
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
108 |
void NetworkServerPlayer::send_position_update (void) {
|
| 24 | 109 |
CL_NetPacket pkt; |
| 66 | 110 |
writeVector(pkt, position); |
111 |
writeVector(pkt, velocity); |
|
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
112 |
|
| 66 | 113 |
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
|
114 |
|
| 24 | 115 |
obj.send(server.netsession.get_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
|
116 |
} |
|
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
21
diff
changeset
|
117 |