-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket.h
80 lines (62 loc) · 1.61 KB
/
packet.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#ifndef _PACKET_H
#define _PACKET_H
#include "board.h"
#include "util.h"
#include <stdint.h>
enum packet_type {
PKT_CLIENT_HELLO, // Sent from the client with a magic value
PKT_SERVER_HELLO, // Sent fromfrom the server to the client with a magic value
PKT_SERVER_READY, // Server is ready
PKT_SHIPS_READY, // A player's ships have been placed
PKT_BEGIN_GAME, // Sent from the server to indicate the game has begun.
PKT_MOVE, // Player has made a move
PKT_MOVE_RESULT, // Result of a move (hit, miss, ship sunk)
PKT_DISCONNECT, // Sent when an error is encountered.
};
enum peer_type {
PEER_CLIENT,
PEER_SERVER
};
enum net_move_result {
NET_HIT,
NET_MISS,
NET_SINK
};
struct packet_header {
enum packet_type type;
u16 length;
};
// weak attempt at writing BATTLE in hex
#define NET_MAGIC 0x00BA117E
struct pkt_begin_game {
// Who will go first.
enum peer_type first;
};
struct pkt_move {
int row, col;
};
struct pkt_move_result {
enum net_move_result result;
// Sink data. this only filled in if a ship was sunk
enum ship ship_type;
int ship_row, ship_col, ship_dir, ship_size;
// 1 if the move sunk the last ship.
u8 win;
};
struct pkt_end_game {
enum peer_type winner;
};
struct pkt_disconnect {
char reason[512];
};
struct packet {
enum packet_type type;
union {
struct pkt_begin_game begin_game;
struct pkt_move move;
struct pkt_move_result move_result;
struct pkt_end_game end_game;
struct pkt_disconnect disconnect;
};
};
#endif