Skip to content

Commit f4e0047

Browse files
committed
Add breakwater-parser C bindings
1 parent 121986c commit f4e0047

File tree

9 files changed

+151
-2
lines changed

9 files changed

+151
-2
lines changed

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
[workspace]
2-
members = ["breakwater-parser", "breakwater", "breakwater-egui-overlay"]
2+
members = [
3+
"breakwater-parser",
4+
"breakwater",
5+
"breakwater-egui-overlay",
6+
"breakwater-parser-c-bindings"
7+
]
38
resolver = "2"
49

510
[workspace.package]

breakwater-f-stack/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ CFLAGS += -O3 -g -gdwarf-2 $(shell $(PKGCONF) --cflags libdpdk)
1616
LIBS+= $(shell $(PKGCONF) --static --libs libdpdk)
1717
LIBS+= -I${FF_PATH}/lib -L${FF_PATH}/lib -Wl,--whole-archive,-lfstack,--no-whole-archive
1818
LIBS+= -Wl,--no-whole-archive -lrt -lm -ldl -lcrypto -pthread -lnuma
19+
# WIP, start with "sudo LD_LIBRARY_PATH=../target/release build/breakwater-f-stack"
20+
LIBS += -l breakwater_parser_c_bindings -L ../target/release/
1921

2022
TARGET="breakwater-f-stack"
2123
all:

breakwater-f-stack/breakwater-f-stack.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
#include "framebuffer.h"
1919
#include "parser.h"
2020

21+
extern void breakwater_init_original_parser(int width, int height);
22+
extern size_t breakwater_original_parser_parse(const char* buffer, size_t buffer_len);
23+
2124
#define MAX_EVENTS 512
2225

2326
/* kevent set */
@@ -160,7 +163,9 @@ int loop(void *arg)
160163
ssize_t readlen = ff_read(clientfd, buf, sizeof(buf));
161164
client->bytes_parsed += readlen;
162165

163-
size_t bytes_parsed = parse(buf, readlen, framebuffer, clientfd);
166+
// size_t bytes_parsed = parse(buf, readlen, framebuffer, clientfd);
167+
long parsed = breakwater_original_parser_parse(buf, readlen);
168+
printf("Parse result: %ld\n", parsed);
164169
} else {
165170
printf("unknown event: %8.8X\n", event.flags);
166171
}
@@ -173,6 +178,8 @@ int main(int argc, char * argv[])
173178
{
174179
int err = 0;
175180

181+
breakwater_init_original_parser(WIDTH, HEIGHT);
182+
176183
struct framebuffer* framebuffer;
177184
if((err = create_fb(&framebuffer, WIDTH, HEIGHT, SHARED_MEMORY_NAME))) {
178185
fprintf(stderr, "Failed to allocate framebuffer: %s\n", strerror(err));
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "breakwater-parser-c-bindings"
3+
description = "Pixelflut parser C bindings"
4+
version.workspace = true
5+
authors.workspace = true
6+
license.workspace = true
7+
edition.workspace = true
8+
repository.workspace = true
9+
10+
[lib]
11+
crate-type = ["cdylib"]
12+
13+
[dependencies]
14+
breakwater-parser.workspace = true
15+
16+
libc = "0.2"
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use core::str;
2+
use std::{
3+
ffi::c_int,
4+
slice,
5+
sync::{Arc, Mutex, OnceLock},
6+
};
7+
8+
use breakwater_parser::{OriginalParser, Parser, SimpleFrameBuffer};
9+
use libc::size_t;
10+
11+
static ORIGINAL_PARSER: OnceLock<Mutex<OriginalParser<SimpleFrameBuffer>>> = OnceLock::new();
12+
13+
#[unsafe(no_mangle)]
14+
pub extern "C" fn breakwater_init_original_parser(width: c_int, height: c_int) {
15+
ORIGINAL_PARSER.get_or_init(|| {
16+
// let fb = Arc::new(NoopFrameBuffer::new(
17+
// width.try_into().unwrap(),
18+
// height.try_into().unwrap(),
19+
// ));
20+
let fb = Arc::new(SimpleFrameBuffer::new(
21+
width.try_into().unwrap(),
22+
height.try_into().unwrap(),
23+
));
24+
25+
Mutex::new(OriginalParser::new(fb))
26+
});
27+
}
28+
29+
#[unsafe(no_mangle)]
30+
pub extern "C" fn breakwater_original_parser_parser_lookahead() -> size_t {
31+
let parser = ORIGINAL_PARSER
32+
.get()
33+
.expect("Call breakwater_init_original_parser first!");
34+
35+
parser
36+
.lock()
37+
.unwrap()
38+
.parser_lookahead()
39+
.try_into()
40+
.unwrap()
41+
}
42+
43+
#[unsafe(no_mangle)]
44+
pub extern "C" fn breakwater_original_parser_parse(buffer: *mut u8, buffer_len: size_t) -> size_t {
45+
let parser = ORIGINAL_PARSER
46+
.get()
47+
.expect("Call breakwater_init_original_parser first!");
48+
49+
let buffer = unsafe { slice::from_raw_parts(buffer, buffer_len.try_into().unwrap()) };
50+
51+
// FIXME: Somehow return the response to the C side
52+
let mut response = Vec::new();
53+
let parsed = { parser.lock().unwrap().parse(buffer, &mut response) };
54+
dbg!(str::from_utf8(&response).unwrap());
55+
56+
parsed.try_into().unwrap()
57+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cd ../..
2+
cargo build --release -p breakwater-parser-c-bindings
3+
cd -
4+
gcc test.c -o test -l breakwater_parser_c_bindings -L../../target/release
5+
6+
LD_LIBRARY_PATH=../../target/release ./test
15.8 KB
Binary file not shown.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
extern void breakwater_init_original_parser(int width, int height);
6+
extern size_t breakwater_original_parser_parser_lookahead();
7+
extern size_t breakwater_original_parser_parse(const char* buffer, size_t buffer_len);
8+
9+
int main(void) {
10+
breakwater_init_original_parser(1920, 1080);
11+
size_t parser_lookahead = breakwater_original_parser_parser_lookahead();
12+
printf("Parser lookahead: %ld\n", parser_lookahead);
13+
14+
const char* text =
15+
"PX 0 0 123456\n"
16+
"PX 0 1 111111\n"
17+
"PX 0 2 222222\n"
18+
"PX 0 3 333333\n"
19+
"PX 0 4 444444\n"
20+
"PX 0 5 555555\n"
21+
"PX 0 6 666666\n"
22+
"PX 0 7 777777\n"
23+
"PX 0 8 888888\n"
24+
"PX 0 9 999999\n"
25+
"PX 0 0\n"
26+
"PX 0 1\n"
27+
"PX 0 2\n"
28+
"PX 0 3\n"
29+
"PX 0 4\n"
30+
"PX 0 5\n"
31+
"PX 0 6\n"
32+
"PX 0 7\n";
33+
34+
size_t text_len = strlen(text);
35+
size_t buffer_len = text_len + parser_lookahead;
36+
unsigned char* buffer = malloc(buffer_len);
37+
if (!buffer) {
38+
perror("malloc failed");
39+
return 1;
40+
}
41+
memcpy(buffer, text, text_len);
42+
43+
long parsed = breakwater_original_parser_parse(buffer, buffer_len);
44+
printf("Parse result: %ld\n", parsed);
45+
46+
free(buffer);
47+
return 0;
48+
}

0 commit comments

Comments
 (0)