-
Notifications
You must be signed in to change notification settings - Fork 1
/
send.c
56 lines (49 loc) · 1.48 KB
/
send.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "boss.h"
#include "list.h"
#include "send.h"
#include "write.h"
// Network is big endian, for 0xa1cf 0xa1 is transmitted first (MSB first)
static pthread_t sendThread;
static List* out_list;
struct addrinfo* sinRemote; // All address info for who to send to
void* sendMessage(void* unused) {
while(1) {
Write_signalMsg(); // sendThread waits until it is told a message is ready to be sent
int socketDescriptor = Boss_getSocket();
char* message = List_first(out_list); // Grab message from list
int len = strlen(message);
if (sendto(socketDescriptor, message,
len + 1, 0, sinRemote->ai_addr,
sinRemote->ai_addrlen) == -1) {
printf("ERROR: Message not successfully sent\n");
}
// Has to check message + 2 since ENTER included '\n' and fgets
// puts EOF after that
if (*(message) == '!' && *(message + 2) == '\0') { // If user sendss a solo '!' gracefully exits
Boss_shutdown(); // Any malloc'd message in list will be freed by shutdown
}
else {
free(message);
Boss_removeNode(out_list);
}
}
}
void Send_init(List* list, struct addrinfo** remoteAddress) {
out_list = list;
sinRemote = *remoteAddress;
pthread_create(&sendThread, NULL, sendMessage, NULL);
}
void Send_shutdown(void) {
pthread_cancel(sendThread);
pthread_join(sendThread, NULL);
free(sinRemote);
}