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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//! Memory-based communication channel between host and guest
use std::sync::{Arc, Mutex};
use std::collections::{VecDeque, HashMap};
use crate::error::{Error, Result};
use crate::communication::{CommunicationChannel, RpcChannel, StringHandlerFunction, ByteHandlerFunction};
use crate::communication::memory::SharedMemoryRegion;
use crate::utils::logging;
/// Memory channel configuration
pub struct MemoryChannelConfig {
/// Channel name
pub name: String,
/// Data region size
pub data_size: usize,
/// Control region size
pub control_size: usize,
/// Message queue capacity
pub queue_capacity: usize,
}
impl Default for MemoryChannelConfig {
fn default() -> Self {
Self {
name: "default".to_string(),
data_size: 64 * 1024, // 64KB data region
control_size: 1024, // 1KB control region
queue_capacity: 32, // 32 messages in queue
}
}
}
/// Channel control flags
#[repr(u8)]
#[derive(PartialEq)]
pub enum ControlFlag {
/// Ready for reading
Ready = 1,
/// Reading in progress
Reading = 2,
/// Ready for writing
WritingReady = 3,
/// Writing in progress
Writing = 4,
/// Channel closed
Closed = 5,
}
/// Memory-based communication channel
pub struct MemoryChannel {
/// Channel name
name: String,
/// Data region for message data
data_region: Arc<SharedMemoryRegion>,
/// Control region for synchronization
control_region: Arc<SharedMemoryRegion>,
/// Message queue for bookkeeping
message_queue: Mutex<VecDeque<(usize, usize)>>, // (offset, length)
/// Is channel closed
closed: Mutex<bool>,
/// Queue capacity
capacity: usize,
}
impl MemoryChannel {
/// Create a new memory channel with shared memory regions
pub fn new(
config: &MemoryChannelConfig,
data_region: Arc<SharedMemoryRegion>,
control_region: Arc<SharedMemoryRegion>,
) -> Self {
Self {
name: config.name.clone(),
data_region,
control_region,
message_queue: Mutex::new(VecDeque::with_capacity(config.queue_capacity)),
closed: Mutex::new(false),
capacity: config.queue_capacity,
}
}
/// Get the channel name
pub fn name(&self) -> &str {
&self.name
}
/// Set control flag
fn set_control_flag(&self, flag: ControlFlag) -> Result<()> {
let mut buffer = [0u8; 1];
buffer[0] = flag as u8;
self.control_region.write(0, &buffer)?;
Ok(())
}
/// Get control flag
fn get_control_flag(&self) -> Result<ControlFlag> {
let mut buffer = [0u8; 1];
self.control_region.read(0, &mut buffer)?;
match buffer[0] {
1 => Ok(ControlFlag::Ready),
2 => Ok(ControlFlag::Reading),
3 => Ok(ControlFlag::WritingReady),
4 => Ok(ControlFlag::Writing),
5 => Ok(ControlFlag::Closed),
_ => Err(Error::Communication {
channel: "memory_channel".to_string(),
reason: "Invalid control flag".to_string(),
instance_id: None,
}),
}
}
/// Write message length to control region
fn write_message_length(&self, length: usize) -> Result<()> {
let length_bytes = (length as u32).to_le_bytes();
self.control_region.write(1, &length_bytes)?;
Ok(())
}
/// Read message length from control region
fn read_message_length(&self) -> Result<usize> {
let mut length_bytes = [0u8; 4];
self.control_region.read(1, &mut length_bytes)?;
Ok(u32::from_le_bytes(length_bytes) as usize)
}
}
impl CommunicationChannel for MemoryChannel {
fn send_to_guest(&self, message: &[u8]) -> Result<()> {
// Check if channel is closed
if *self.closed.lock().unwrap() {
return Err(Error::Communication {
channel: "memory_channel".to_string(),
reason: "Channel is closed".to_string(),
instance_id: None,
});
}
// Check message queue capacity
let mut queue = self.message_queue.lock().unwrap();
if queue.len() >= self.capacity {
return Err(Error::Communication {
channel: "memory_channel".to_string(),
reason: "Channel is full".to_string(),
instance_id: None,
});
}
// Wait for control region to be ready for writing
let mut retries = 0;
while self.get_control_flag()? != ControlFlag::WritingReady && retries < 100 {
std::thread::sleep(std::time::Duration::from_millis(10));
retries += 1;
}
if retries >= 100 {
return Err(Error::Communication {
channel: "memory_channel".to_string(),
reason: "Timeout waiting for channel to be ready".to_string(),
instance_id: None,
});
}
// Set control flag to writing
self.set_control_flag(ControlFlag::Writing)?;
// Write message length
self.write_message_length(message.len())?;
// Find next free offset in data region
let offset = if let Some((last_offset, last_len)) = queue.back() {
last_offset + last_len
} else {
0
};
// Write message to data region
self.data_region.write(offset, message)?;
// Add message to queue
queue.push_back((offset, message.len()));
// Set control flag to ready
self.set_control_flag(ControlFlag::Ready)?;
// Log the event
logging::log_communication_event(&self.name, "sent", message.len());
Ok(())
}
fn receive_from_guest(&self) -> Result<Vec<u8>> {
// Check if channel is closed
if *self.closed.lock().unwrap() {
return Err(Error::Communication {
channel: "memory_channel".to_string(),
reason: "Channel is closed".to_string(),
instance_id: None,
});
}
// Wait for control region to be ready for reading
let mut retries = 0;
while self.get_control_flag()? != ControlFlag::Ready && retries < 100 {
std::thread::sleep(std::time::Duration::from_millis(10));
retries += 1;
}
if retries >= 100 {
return Err(Error::Communication {
channel: "memory_channel".to_string(),
reason: "Timeout waiting for message".to_string(),
instance_id: None
});
}
// Set control flag to reading
self.set_control_flag(ControlFlag::Reading)?;
// Read message length
let length = self.read_message_length()?;
// Read message from data region
let mut message = vec![0u8; length];
self.data_region.read(0, &mut message)?;
// Set control flag back to writing ready
self.set_control_flag(ControlFlag::WritingReady)?;
// Log the event
logging::log_communication_event(&self.name, "received", message.len());
Ok(message)
}
fn has_messages(&self) -> bool {
// Check if channel is closed
if *self.closed.lock().unwrap() {
return false;
}
// Check if control region is ready for reading
if let Ok(flag) = self.get_control_flag() {
return flag == ControlFlag::Ready;
}
false
}
fn close(&self) -> Result<()> {
// Set closed flag
*self.closed.lock().unwrap() = true;
// Set control flag to closed
self.set_control_flag(ControlFlag::Closed)?;
Ok(())
}
}
/// Memory RPC channel
pub struct MemoryRpcChannel {
/// Communication channel for RPC
channel: Arc<MemoryChannel>,
/// RPC function registry
functions: Mutex<HashMap<String, ByteHandlerFunction>>,
}
impl MemoryRpcChannel {
/// Create a new memory RPC channel
pub fn new(channel: Arc<MemoryChannel>) -> Self {
Self {
channel,
functions: Mutex::new(HashMap::new()),
}
}
}
impl RpcChannel for MemoryRpcChannel {
fn register_host_function_json(
&mut self,
name: &str,
function: StringHandlerFunction,
) -> Result<()> {
let name = name.to_string();
let func = Box::new(move |data: &[u8]| -> Result<Vec<u8>> {
// Convert bytes to string
let params_json = String::from_utf8_lossy(data);
// Call the function
let result_json = function(¶ms_json)?;
// Convert result back to bytes
Ok(result_json.into_bytes())
});
// Register the function
self.functions.lock().unwrap().insert(name, func);
Ok(())
}
fn call_guest_function_json(
&self,
function_name: &str,
params_json: &str,
) -> Result<String> {
// Create RPC message
let mut message = Vec::with_capacity(function_name.len() + params_json.len() + 5);
// Add function name length (u8)
message.push(function_name.len() as u8);
// Add function name
message.extend_from_slice(function_name.as_bytes());
// Add parameters as bytes
message.extend_from_slice(params_json.as_bytes());
// Send message to guest
self.channel.send_to_guest(&message)?;
// Receive response from guest
let response_bytes = self.channel.receive_from_guest()?;
// Convert response back to string
let response = String::from_utf8_lossy(&response_bytes).to_string();
Ok(response)
}
fn register_host_function_msgpack(
&mut self,
name: &str,
function: ByteHandlerFunction,
) -> Result<()> {
let name = name.to_string();
// Register the function directly
self.functions.lock().unwrap().insert(name, function);
Ok(())
}
fn call_guest_function_msgpack(
&self,
function_name: &str,
params_msgpack: &[u8],
) -> Result<Vec<u8>> {
// Create RPC message
let mut message = Vec::with_capacity(function_name.len() + params_msgpack.len() + 5);
// Add function name length (u8)
message.push(function_name.len() as u8);
// Add function name
message.extend_from_slice(function_name.as_bytes());
// Add parameters
message.extend_from_slice(params_msgpack);
// Send message to guest
self.channel.send_to_guest(&message)?;
// Receive response from guest
let response_bytes = self.channel.receive_from_guest()?;
Ok(response_bytes)
}
}