-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_TCP.h
74 lines (59 loc) · 1.75 KB
/
custom_TCP.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
#ifndef _CUSTOM_TCP_H
#define _CUSTOM_TCP_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <poll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iterator>
#include <list>
#include <unordered_map>
using namespace std;
#define HEADER_SIZE 24
#define BUFLEN 1580
#define SUBSCRIBE_SF 0
#define SUBSCRIBE 1
#define UNSUBSCRIBE 2
#define PUBLISH 3
#define ID 4
#define SOCKET_CLOSED 0x10
#define DUPLICATE_CLIENT 0x11
#define ABORT(condition, message) \
if (condition) { \
fprintf(stderr, "%s", message); \
exit(EXIT_FAILURE); \
}
#define WARNING(condition, message) \
if (condition) { \
fprintf(stderr, "%s", message); \
}
struct TCP_msg {
u_int32_t length;
u_int8_t type;
char id[13];
u_int16_t UDP_port;
struct in_addr UDP_addr;
char payload[1556];
};
typedef struct subscriber {
int socket;
bool connected;
} *Subscriber;
typedef struct subscription {
char subscriber_id[13];
bool fs;
} *Subscription;
void create_unsubscribe_msg(char *id, char* topic, struct TCP_msg *msg);
void create_subscribe_msg(u_int8_t SF, char* id, char* topic, struct TCP_msg *msg);
void create_id_msg(char *id, struct TCP_msg *msg);
void send_msg(int socket, struct TCP_msg *msg);
int receive_msg(int socket, struct TCP_msg *msg, unordered_map <string, list <Subscription>> &topics_table,
unordered_map <string, Subscriber> &clients_table, unordered_map <string, list<struct TCP_msg *>> &unsent_table);
void publish_message(char *content, int content_size, unordered_map <string, Subscriber> &clients_table,
unordered_map <string, list <Subscription>> &topics_table, unordered_map <string, list<struct TCP_msg *>> &unsent_table,
struct sockaddr_in UDP_cli_addr);
#endif