-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProtocol.c
60 lines (56 loc) · 1.6 KB
/
Protocol.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
57
58
59
60
#include "Protocol.h"
void write_pack(int sd, char* c){
/* Method that writes the message pointed on by c.
*/
int size = strlen(c);
int packs = (size + PACK_SIZE)/PACK_SIZE;
int i;
char* char_size = malloc(20);
memset(char_size, 0, 20);
sprintf(char_size, "#%d", size);
//first write call with a number indicating how many bytes is coming in total:
write(sd, char_size, 20);
free(char_size);
for(i = 0; i<packs; i++){
//write i pack of size PACK_SIZE until the buffer is empty
write(sd, c, PACK_SIZE);
c += PACK_SIZE;
}
}
char* read_pack(int sd){
/* Method that reads and returns the incoming message. Returns an error
* message if something went wrong.
* PS: this method allocates memory, which must be free'd by the caller.
*/
char* size = malloc(20);
char* s;
memset(size, 0, 20);
int bytes, packs, i;
//first read gets a number which indicates the amount of bytes coming in:
read(sd, size, 20);
if(size[0] != '#'){
char* err = malloc(100);
free(size);
strcat(err, "/ERRprotocol not recognized");
return err;
}
s = size;
size += 1;
if((bytes = strtol(size, NULL, 10)) == 0){
fprintf(stderr,"error in strtol\n");
exit(-1);
}
packs = ((bytes + PACK_SIZE)/PACK_SIZE);
char* command = malloc(bytes + 1);
memset(command, 0, bytes + 1);
char* tmp = malloc(PACK_SIZE + 1);
for(i = 0; i < packs; i++){
memset(tmp, 0, PACK_SIZE +1);
//read the whole message by blocks of PACK_SIZE byte:
read(sd, tmp, PACK_SIZE);
strcat(command, tmp);
}
free(s);
free(tmp);
return command;
}