diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/client.c | 11 | ||||
-rw-r--r-- | src/net.c | 7 | ||||
-rw-r--r-- | src/server.c | 55 |
3 files changed, 36 insertions, 37 deletions
diff --git a/src/client.c b/src/client.c index 3bf3649..d15ba8e 100644 --- a/src/client.c +++ b/src/client.c @@ -10,6 +10,7 @@ #include "net.h" #include "ticker.h" #include "sig.h" +#include "content.h" typedef struct { peer conn; @@ -80,10 +81,16 @@ int main() int socket = socket_create("127.0.0.1", "4560", false); if (socket < 0) client_exit(&c, EXIT_FAILURE); - peer_init(&c.conn, socket); + str server_name = S("server"); + peer_init(&c.conn, socket, &server_name); gfx_alt_buffer(true); + SEND_PKT(c.conn, SPKT_HI, + ser_str(&pkt, c.name); + ser_str(&pkt, c.pass); + ) + ticker t; ticker_init(&t, NANOS/60); @@ -122,7 +129,7 @@ int main() if (c.conn.disco) client_exit(&c, EXIT_SUCCESS); if (pkt.len > 0 && !handle_pkt(&c, pkt)) - invalid_pkt(S("server"), pkt); + invalid_pkt(&c.conn, pkt); uint64_t dtime; if (ticker_tick(&t, &dtime)) @@ -16,11 +16,11 @@ #include "net.h" -void invalid_pkt(str from, str pkt) +void invalid_pkt(peer *from, str pkt) { // TODO: maybe inform peer about failure? not sure // FIXME: hexdumping a gazillon bytes to stderr might be an issue - fprintf(stderr, "invalid pkt from %*s: ", PSTR(from)); + fprintf(stderr, "invalid pkt from %*s: ", PSTR(*from->name)); for (size_t i = 0; i < pkt.len; i++) fprintf(stderr, "%02x%c", (uint8_t) pkt.data[i], i+1 == pkt.len ? '\n' : ' '); } @@ -76,10 +76,11 @@ int socket_accept(int accept_fd) return socket; } -void peer_init(peer *p, int socket) +void peer_init(peer *p, int socket, str *name) { p->socket = socket; p->disco = false; + p->name = name; p->in.header = true; p->in.len = 0; 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); } } } |