-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared.h
139 lines (120 loc) · 3.02 KB
/
shared.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
/**
* definition of message and serialization functions
* Author: Jabez Wilson (z5027406)
*
*/
#ifndef SHARED_H
#pragma pack(0)
#define SHARED_H
#include <assert.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#define STRING_SIZE 24
#define SIZE_INT sizeof(int)
#define SIZE_UINT sizeof(unsigned int)
#define SIZE_USHORT sizeof(unsigned short)
#define SIZE_ULONG sizeof(unsigned long)
//#######################################
// Custom Type Definition
//#######################################
struct packet {
char from;
char to;
int cost;
};
//#######################################
// Basic Type Serialisation
//#######################################
char* serialize_int(char* buf,int a) {
char* p = (char*)&a;
for(int i=0;i<SIZE_INT;i++,p++,buf++)
*buf = *p;
return buf;
}
char* serialize_uint(char* buf,unsigned int a) {
char* p = (char*)&a;
for(int i=0;i<SIZE_UINT;i++,p++,buf++)
*buf = *p;
return buf;
}
char* serialize_ushort(char* buf,unsigned short a) {
char* p = (char*)&a;
for(int i=0;i<SIZE_USHORT;i++,p++,buf++)
*buf = *p;
return buf;
}
char* serialize_ulong(char* buf,unsigned long a) {
char* p = (char*)&a;
for(int i=0;i<SIZE_ULONG;i++,p++,buf++)
*buf = *p;
return buf;
}
char* serialize_string(char* buf, char* a, int size) {
char* p = a;
for(int i=0;i<size;i++,p++,buf++)
*buf = *p;
return buf;
}
char* serialize_char(char* buf, char a) {
*buf = a;
return buf+1;
}
char* serialize_uchar(char* buf, unsigned char a) {
*buf = (char)a;
return buf+1;
}
char* deserialize_int(char* buf,int* a) {
char* p = (char*)a;
for(int i=0;i<SIZE_INT;i++,p++,buf++)
*p = *buf;
return buf;
}
char* deserialize_uint(char* buf,unsigned int* a) {
char* p = (char*)a;
for(int i=0;i<SIZE_UINT;i++,p++,buf++)
*p = *buf;
return buf;
}
char* deserialize_ushort(char* buf,unsigned short* a) {
char* p = (char*)a;
for(int i=0;i<SIZE_USHORT;i++,p++,buf++)
*p = *buf;
return buf;
}
char* deserialize_ulong(char* buf,unsigned long* a) {
char* p = (char*)a;
for(int i=0;i<SIZE_ULONG;i++,p++,buf++)
*p = *buf;
return buf;
}
char* deserialize_string(char* buf, char* a, int size) {
char* p = a;
for(int i=0;i<size;i++,p++,buf++)
*p = *buf;
return buf;
}
char* deserialize_char(char* buf, char* a) {
*a = *buf;
return buf+1;
}
char* deserialize_uchar(char* buf, unsigned char* a) {
*a = (unsigned char)*buf;
return buf+1;
}
//#######################################
// Custom Type Serialisation
//#######################################
char* serialize_packet(char* buf, struct packet a) {
buf = serialize_char(buf,a.from);
buf = serialize_char(buf,a.to);
buf = serialize_int(buf,a.cost);
return buf;
}
char* deserialize_packet(char* buf, struct packet *a) {
buf = deserialize_char(buf,&(a->from));
buf = deserialize_char(buf,&(a->to));
buf = deserialize_int(buf,&(a->cost));
return buf;
}
#endif