-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommunication_handler.h
198 lines (156 loc) · 4.7 KB
/
communication_handler.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#ifndef COMMUNICATION_HANDLER
#define COMMUNICATION_HANDLER
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "declarations.h"
#include "message_handler.h"
size_t len_to_receive;
ssize_t received_count;
size_t received_total;
size_t len_to_send;
ssize_t send_count;
size_t send_total;
// PEER MANAGEMENT --------------------------------------------------
char *peer_get_addres_str(peer_struct *peer){
static char ret[INET_ADDRSTRLEN + 10];
char peer_ipv4_str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &peer->address.sin_addr, peer_ipv4_str, INET_ADDRSTRLEN);
sprintf(ret, "%s:%d", peer_ipv4_str, peer->address.sin_port);
wait();
return ret;
}
int peer_add_to_send(peer_struct *peer, message_data *message){
if(enqueue_message(&peer->send_buffer, message) == 0){
wait();
return 0;
}else{
return -1;
}
}
int delete_peer(peer_struct *peer){
close(peer->socket);
delete_message_queue(&peer->send_buffer);
wait();
}
int create_peer(peer_struct *peer){
create_message_queue(BUFFER_MAX_MSG_SIZE, &peer->send_buffer);
peer->current_sending_byte = -1;
peer->current_receiving_byte = 0;
wait();
return 0;
}
/* Receive message from peer and handle it with message_handler(). */
int receive_from_peer(peer_struct *peer, int (*message_handler)(message_data *)){
if (peer->current_receiving_byte >= sizeof(peer->receiving_buffer)) {
message_handler(&peer->receiving_buffer);
peer->current_receiving_byte = 0;
}
// Count bytes to send.
len_to_receive = sizeof(peer->receiving_buffer) - peer->current_receiving_byte;
if (len_to_receive > MAX_SIZE_TO_SEND){
len_to_receive = MAX_SIZE_TO_SEND;
}
received_count = recv(peer->socket, (char *)&peer->receiving_buffer + peer->current_receiving_byte, len_to_receive, MSG_DONTWAIT);
while(received_count > 0){
if (received_count < 0) {
if (errno == EAGAIN) {
//
}
else {
return -1;
}
}
else if (received_count < 0 && errno == EAGAIN) {
break;
}
else if (received_count == 0) {
return -1;
}
else if (received_count > 0) {
peer->current_receiving_byte += received_count;
received_total += received_count;
if (peer->current_receiving_byte >= sizeof(peer->receiving_buffer)) {
message_handler(&peer->receiving_buffer);
peer->current_receiving_byte = 0;
}
// Count bytes to send.
len_to_receive = sizeof(peer->receiving_buffer) - peer->current_receiving_byte;
if (len_to_receive > MAX_SIZE_TO_SEND)
len_to_receive = MAX_SIZE_TO_SEND;
received_count = recv(peer->socket, (char *)&peer->receiving_buffer + peer->current_receiving_byte, len_to_receive, MSG_DONTWAIT);
}
}
wait();
return 0;
}
int send_to_peer(peer_struct *peer){
do {
if (peer->current_sending_byte < 0 || peer->current_sending_byte >= sizeof(peer->sending_buffer)) {
if (dequeue_message(&peer->send_buffer, &peer->sending_buffer) != 0) {
peer->current_sending_byte = -1;
break;
}
peer->current_sending_byte = 0;
}
// Count bytes to send.
len_to_send = sizeof(peer->sending_buffer) - peer->current_sending_byte;
if (len_to_send > MAX_SIZE_TO_SEND)
len_to_send = MAX_SIZE_TO_SEND;
send_count = send(peer->socket, (char *)&peer->sending_buffer + peer->current_sending_byte, len_to_send, 0);
if (send_count < 0) {
if (errno == EAGAIN) {
//
}
else {
return -1;
}
}
// we have read as many as possible
else if (send_count < 0 && errno == EAGAIN) {
break;
}
else if (send_count == 0) {
break;
}
else if (send_count > 0) {
peer->current_sending_byte += send_count;
send_total += send_count;
}
}
while (send_count > 0);
wait();
return 0;
}
/* Reads from stdin and create new message. This message enqueue_messages to send queueu. */
int read_from_stdin(char *read_buffer, size_t max_len){
memset(read_buffer, 0, max_len);
ssize_t read_count = 0;
ssize_t total_read = 0;
do {
read_count = read(STDIN_FILENO, read_buffer, max_len);
if (read_count < 0 && errno != EAGAIN) {
return -1;
}
else if (read_count < 0 && errno == EAGAIN) {
break;
}
else if (read_count > 0) {
total_read = total_read + read_count;
if (total_read > max_len) {
fflush(STDIN_FILENO);
break;
}
}
} while (read_count > 0);
size_t len = strlen(read_buffer);
if (len > 0 && read_buffer[len - 1] == '\n')
read_buffer[len - 1] = '\0';
if(strcmp(read_buffer,"logout") == 0){
printf("%s\n","LOGGING OUT\n");
exit(1);
}
wait();
return 0;
}
#endif /* COMMUNICATION_HANDLER */