net.h (1184B)
1 2 #ifndef PROTOVERSE_NET_H 3 #define PROTOVERSE_NET_H 4 5 #include <sys/socket.h> 6 #include <arpa/inet.h> 7 8 #include "cursor.h" 9 #include "env.h" 10 11 enum packet_type { 12 PKT_FETCH_DATA, 13 PKT_FETCH_DATA_RESPONSE, 14 PKT_MESSAGE, 15 PKT_NUM_TYPES, 16 }; 17 18 enum message_type { 19 MSG_CHAT, 20 MSG_INTERACT, 21 }; 22 23 struct fetch_packet { 24 const char *path; 25 }; 26 27 struct fetch_response_packet { 28 const char *path; 29 int data_len; 30 unsigned char *data; 31 }; 32 33 struct message_packet { 34 int receiver; 35 int type; 36 const char *message; 37 }; 38 39 union packet_data { 40 struct fetch_packet fetch; 41 struct fetch_response_packet fetch_response; 42 struct message_packet message; 43 }; 44 45 struct packet { 46 enum packet_type type; 47 union packet_data data; 48 }; 49 50 int send_packet(int fd, struct sockaddr_in *to_addr, struct packet *packet); 51 int recv_packet(int sockfd, struct cursor *buf, struct sockaddr_in *from, struct packet *packet); 52 53 int push_packet(unsigned char *buf, int bufsize, struct packet *packet); 54 int pull_packet(struct cursor *c, struct cursor *buf, struct packet *packet, int received_bytes); 55 56 int packet_eq(struct packet *a, struct packet *b); 57 void print_packet(struct env *env, struct packet *packet); 58 59 #endif /* PROTOVERSE_NET_H */