-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorder_entry.c
More file actions
148 lines (103 loc) · 4.53 KB
/
Copy pathorder_entry.c
File metadata and controls
148 lines (103 loc) · 4.53 KB
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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
#define LATEST_VERSION 1
// Structure representing the packet header containing metadata
typedef struct{
uint32_t magic; // Magic number to identify the packet format (0xCAFEBABE)
uint16_t version; // Protocol version for compatibility checks
uint16_t count; // Number of Order entries following this header
} PacketHeader;
// Structure representing a single trade order (Payload)
typedef struct {
uint32_t id; // Unique identifier for the order
uint32_t Price; // Price of the item
uint32_t Quantity; // Quantity of items
} Order;
// Function to serialize (pack) an array of Order structs into a raw byte buffer
int serialize_orders(char *buffer, size_t buffer_size, Order *orders, int count, int version){
// Calculate the total size needed: Header size + (Size of one Order * Number of Orders)
size_t required_size = sizeof(PacketHeader) + (count * sizeof(Order));
// Safety Check: Ensure the provided buffer is large enough to hold the data
if (required_size > buffer_size){
printf("[MEMORY ERROR] ORDER SIZE IS TOO LARGE\n");
return -1;
}
// Cast the start of the raw char buffer to a PacketHeader pointer
// This allows us to write struct members directly into the buffer memory
PacketHeader *header = (PacketHeader*)buffer;
header -> magic = 0xCAFEBABE;
header -> count = count;
header -> version = version;
// Calculate the memory address where the payload (orders) should start
// We skip 'sizeof(PacketHeader)' bytes from the beginning of the buffer
Order *payload = (Order*)(buffer + sizeof(PacketHeader));
// Loop through the source orders and copy them into the buffer
for (int i = 0; i < count; i++){
payload -> id = orders[i].id;
payload -> Price = orders[i].Price;
payload -> Quantity = orders[i].Quantity;
payload++; // Move the pointer to the next Order slot in the buffer
}
return sizeof(PacketHeader) + (count * sizeof(Order));
}
// Function to parse (unpack) a raw buffer back into structured data
int parse_packet(char *buffer){
printf("--- Packet Received ---\n\n");
// Cast the buffer to a PacketHeader pointer to read metadata
PacketHeader *header = (PacketHeader*)buffer;
// Validation: Check if the Magic Number matches and Version is supported
if (header -> magic != 0xCAFEBABE || header -> version != LATEST_VERSION){
printf("[NETWORK ERROR] Invalid Packet Received. \n");
return -1;
}
printf("Magic: 0x%X\n", header -> magic);
printf("Version: %u\n", header -> version);
printf("Count: %u\n\n", header -> count);
Order *trades = (Order*)(buffer + sizeof(PacketHeader));
// Iterate through the number of trades specified in the header
for (int i = 0; i < header -> count; i++){
printf("Trade ID = %u Price = %u Quantity = %u\n", trades -> id, trades -> Price, trades -> Quantity);
// Move the pointer to the next Trade struct in memory
trades++;
}
printf("Packet Processing complete. \n");
return 0;
}
int main() {
// Seed the random number generator
srand(time(NULL));
// Generate a random number of orders between 1 and 50
int num_of_orders = (rand() % 50) + 1;
// Allocate a buffer large enough to hold the header and the orders
// Note: This uses a Variable Length Array (VLA)
size_t size = sizeof(PacketHeader) + (num_of_orders * sizeof(Order)) + 10;
Order my_orders[num_of_orders];
char buffer[size]; // The raw byte buffer simulating a network packet
// Populate the order array with random dummy data
while(1){
for (int i = 0; i < num_of_orders; i++){
my_orders[i].id = i + 1;
my_orders[i].Price = (rand() % 1000);
my_orders[i].Quantity = (rand() % 50);
}
// Serialize the structured data into the raw buffer
int total_bytes = serialize_orders(buffer, sizeof(buffer), my_orders, num_of_orders, 1);
printf("Total Bytes Written: %d (Expected: %d)\n", total_bytes, (sizeof(PacketHeader) + (num_of_orders * sizeof(Order))));
//Desrialize the data from the raw buffer into structured data
parse_packet(buffer);
printf("Waiting for next batch...\n");
#ifdef _WIN32
Sleep(500); // 1000 milliseconds
#else
sleep(0.5); // 1 second
#endif
}
return 0;
}