Skip to content

Commit 852e2ac

Browse files
chore: add protocol doc
1 parent d4e0ed8 commit 852e2ac

File tree

4 files changed

+115
-4
lines changed

4 files changed

+115
-4
lines changed

PROTOCOL

-3
This file was deleted.

PROTOCOL.md

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# **CacheX Protocol: Request and Response Format**
2+
3+
## **Request Format**
4+
CacheX follows a **binary protocol** for efficient parsing and communication. Each request consists of **multiple arguments** encoded in a structured format.
5+
6+
### **Structure**
7+
| Field | Type | Description |
8+
|--------|--------|-------------|
9+
| `nstr` | `uint32_t` | Number of arguments (e.g., `SET key value` has 3 arguments) |
10+
| `len1` | `uint32_t` | Length of the first argument |
11+
| `str1` | `char[]` | First argument (e.g., `"SET"`) |
12+
| `len2` | `uint32_t` | Length of the second argument |
13+
| `str2` | `char[]` | Second argument (e.g., `"key"`) |
14+
| `lenN` | `uint32_t` | Length of the Nth argument |
15+
| `strN` | `char[]` | Nth argument |
16+
17+
### **Binary Layout**
18+
```
19+
+------+-----+------+-----+------+-----+-----+------+
20+
| nstr | len | str1 | len | str2 | ... | len | strN |
21+
+------+-----+------+-----+------+-----+-----+------+
22+
4B 4B ...
23+
```
24+
25+
### **Example**
26+
#### **SET Request**
27+
**Command:** `SET key value`
28+
**Binary Format (Network Order)**
29+
```
30+
[ 0x00000003 ] // Number of arguments (3)
31+
[ 0x00000003 ] // Length of "SET"
32+
[ "SET" ] // Command
33+
[ 0x00000003 ] // Length of "key"
34+
[ "key" ] // Key
35+
[ 0x00000005 ] // Length of "value"
36+
[ "value" ] // Value
37+
```
38+
39+
#### **GET Request**
40+
**Command:** `GET key`
41+
**Binary Format**
42+
```
43+
[ 0x00000002 ] // Number of arguments (2)
44+
[ 0x00000003 ] // Length of "GET"
45+
[ "GET" ] // Command
46+
[ 0x00000003 ] // Length of "key"
47+
[ "key" ] // Key
48+
```
49+
50+
---
51+
52+
## **Response Format**
53+
The response contains a **status code** followed by optional **data**.
54+
55+
### **Structure**
56+
| Field | Type | Description |
57+
|-----------|--------|-------------|
58+
| `length` | `uint32_t` | Total response length (excluding itself) |
59+
| `status` | `uint32_t` | Response status code |
60+
| `data` | `char[]` | (Optional) Response data |
61+
62+
### **Binary Layout**
63+
```
64+
+--------+-----------+
65+
| length | status |
66+
+--------+-----------+
67+
| data |(optional) |
68+
+--------------------+
69+
4B ...
70+
```
71+
72+
### **Response Status Codes**
73+
| Code | Meaning |
74+
|-------|---------|
75+
| `0` (`RES_OK`) | Success |
76+
| `1` (`RES_ERR`) | Error |
77+
| `2` (`RES_NX`) | Key not found |
78+
79+
---
80+
81+
### **Example Responses**
82+
#### **SET Response**
83+
```
84+
[ 0x00000004 ] // Response length (4 bytes for status)
85+
[ 0x00000000 ] // Status: RES_OK (0)
86+
```
87+
88+
#### **GET Response (Key Found)**
89+
```
90+
[ 0x00000009 ] // Response length (4 bytes for status + 5 bytes data)
91+
[ 0x00000000 ] // Status: RES_OK (0)
92+
[ "value" ] // Data: "value"
93+
```
94+
95+
#### **GET Response (Key Not Found)**
96+
```
97+
[ 0x00000004 ] // Response length (4 bytes for status)
98+
[ 0x00000002 ] // Status: RES_NX (2)
99+
```
100+
101+
#### **Invalid Command Response**
102+
```
103+
[ 0x00000004 ] // Response length (4 bytes for status)
104+
[ 0x00000001 ] // Status: RES_ERR (1)
105+
```
106+
107+
---
108+
109+
Handles large requests with up to **200,000 arguments** safely.

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
# CacheX
22

33
CacheX is a high-performance cache server written in **C and C++**, designed to explore **low-level programming, data structures, and scalable network systems**. This project is a hands-on approach to learning **efficient memory management, high-throughput networking, and system-level optimizations** while building a fast and lightweight caching solution.
4+
5+
## CacheX Protocol
6+
7+
[PROTOCOL](./PROTOCOL.md)

include/cacheX_protocol.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
constexpr size_t kHeaderSize = 4;
1212
constexpr size_t kMaxPayloadSize = 32 * 1024 * 1024; // 32 << 20 (32MB)
13+
constexpr size_t kMaxArgs = 200 * 1000;
1314

1415
static inline int32_t read_all(int fd, uint8_t *buf, size_t n) {
1516
size_t total_read = 0;
@@ -40,7 +41,7 @@ static inline int32_t write_all(int fd, const uint8_t *buf, size_t n) {
4041
}
4142

4243
// Append to the back
43-
static void buffer_append(std::vector<uint8_t> &buf, const uint8_t *data, size_t len) {
44+
static inline void buffer_append(std::vector<uint8_t> &buf, const uint8_t *data, size_t len) {
4445
buf.insert(buf.end(), data, data + len);
4546
}
4647

0 commit comments

Comments
 (0)