diff options
Diffstat (limited to 'src/server.c')
-rw-r--r-- | src/server.c | 55 |
1 files changed, 23 insertions, 32 deletions
diff --git a/src/server.c b/src/server.c index 5355620..0addcf2 100644 --- a/src/server.c +++ b/src/server.c @@ -192,37 +192,26 @@ void ser_node(strbuf *w, node *n) ser_color(w, n->col); } -bool player_auth(player *p) -{ - return p->auth; -} - -#define SEND_PKT(TYPE, PEERS, NPEERS, FILTER, ...) \ - { strbuf pkt = NILSBUF; \ - ser_pkt_type(&pkt, TYPE); \ - __VA_ARGS__ \ - for (size_t i = 0; i < NPEERS; i++) { \ - if (FILTER(&(PEERS)[i]) && !peer_send(&(PEERS)[i].conn, pkt.data, pkt.len)) \ - fprintf(stderr, "failed to send " #TYPE " to %*s\n", PSTR((PEERS)[i].name));} \ - free(pkt.data); } - void send_players(game *g) { - SEND_PKT(CPKT_PLAYERS, g->players.data, g->players.len, player_auth, - ser_u16(&pkt, g->players.len); - for (size_t i = 0; i < g->players.len; i++) { - player *p = &g->players.data[i]; - if (!p->auth) continue; - ser_str(&pkt, p->name); - ser_u64(&pkt, p->id); - } - ) + for (size_t i = 0; i < g->players.len; i++) { + player *p = &g->players.data[i]; + if (p->auth) SEND_PKT(p->conn, CPKT_PLAYERS, + ser_u16(&pkt, g->players.len); + for (size_t i = 0; i < g->players.len; i++) { + player *p = &g->players.data[i]; + if (!p->auth) continue; + ser_str(&pkt, p->name); + ser_u64(&pkt, p->id); + } + ) + } } // send_nudes when void send_nodes(player *p, game *g, int8_t z, int32_t x, int32_t y, uint32_t w, uint32_t h) { - SEND_PKT(CPKT_NODES, p, 1,, + SEND_PKT(p->conn, CPKT_NODES, ser_i8(&pkt, z); ser_i32(&pkt, x); ser_i32(&pkt, y); @@ -275,25 +264,27 @@ bool handle_hi(str pkt, player *p, game *g) return false; if (str_cmp(g->passphrase, pass) != 0) { - fprintf(stderr, "wrong passphrase from %*s\n", PSTR(name)); // TODO: log ip ? - SEND_PKT(CPKT_FAIL, p, 1,, ser_fail_reason(&pkt, FAIL_WRONG_PASS);) + printf("wrong passphrase from %*s\n", PSTR(name)); // TODO: log ip ? + SEND_PKT(p->conn, CPKT_FAIL, ser_fail_reason(&pkt, FAIL_WRONG_PASS);) return true; // valid pkt, but invalid passphrase } for (size_t i = 0; i < g->players.len; i++) { player *p2 = &g->players.data[i]; if (p2->auth && str_cmp(p2->name, name) == 0) { - SEND_PKT(CPKT_FAIL, p, 1,, ser_fail_reason(&pkt, FAIL_ALREADY_ONLINE);) + SEND_PKT(p->conn, CPKT_FAIL, ser_fail_reason(&pkt, FAIL_ALREADY_ONLINE);) return true; } } + printf("player authenticated as %*s\n", PSTR(name)); + p->auth = true; p->name = str_clone(name); p->x = p->y = 0; p->z = 0; - SEND_PKT(CPKT_HI, p, 1,, ser_str(&pkt, g->motd);) + SEND_PKT(p->conn, CPKT_HI, ser_str(&pkt, g->motd);) send_players(g); uint32_t range = 10; @@ -354,7 +345,7 @@ int main() for (size_t i = 0; i < g.players.len;) { player *p = &g.players.data[i]; if (p->conn.disco) { - printf("player disco\n"); + printf("player %*s disconnected\n", PSTR(p->name)); player_remove(p, &g); continue; } @@ -382,7 +373,7 @@ int main() if (p->conn.disco || pkt.len == 0) continue; if (!handle_pkt(pkt, p, &g)) - invalid_pkt(p->name, pkt); + invalid_pkt(&p->conn, pkt); } if (fds[g.players.len].revents) { @@ -394,8 +385,8 @@ int main() player *p = ARR_APPEND(g.players); p->auth = false; p->id = g.entity_id++; - p->name = S("unauthenticated"); - peer_init(&p->conn, socket); + p->name = S("(unauthenticated)"); + peer_init(&p->conn, socket, &p->name); } } } |