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
|
const std = @import("std");
const testing = std.testing;
const transmission = @import("transmission.zig");
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
export fn add(a: i32, b: i32) i32 {
const clientOptions = transmission.ClientOptions{
.host = "192.168.0.2",
.port = 9091,
.https = false,
};
var client = transmission.Client.init(allocator, clientOptions);
defer client.deinit();
{
const body = transmission.session_get_raw(&client, null) catch |err| {
std.debug.print("error: {any}\n", .{err});
unreachable;
};
defer allocator.free(body);
std.debug.print("body: {s}\n", .{body});
}
//{
//const body = transmission.torrent_get_(&client, null) catch |err| {
//std.debug.print("error: {any}\n", .{err});
//unreachable;
//};
////defer allocator.free(body);
//for (body.arguments.torrent_get.torrents.?) |t| {
//std.debug.print("name: {any}\n", .{t});
//}
//}
//{
//const body = transmission.torrent_get_(&client, null) catch |err| {
//std.debug.print("error: {any}\n", .{err});
//unreachable;
//};
////defer allocator.free(body);
//std.debug.print("body: {any}\n", .{body});
//}
//{
//const body = transmission.session_set_raw(&client, .{ .peer_port = 51413 }) catch |err| {
//std.debug.print("error: {any}\n", .{err});
//unreachable;
//};
//defer allocator.free(body);
//std.debug.print("body: {s}\n", .{body});
//}
return a + b;
}
export fn c_client_init(clientOptions: transmission.ClientOptions) ?*anyopaque {
var client = allocator.create(transmission.Client) catch unreachable;
client.* = transmission.Client.init(allocator, clientOptions);
return @ptrCast(*anyopaque, client);
}
export fn c_client_deinit(client: ?*anyopaque) void {
var real_client: *transmission.Client = @ptrCast(*transmission.Client, @alignCast(
@alignOf(transmission.Client),
client.?,
));
real_client.*.deinit();
allocator.destroy(real_client);
}
export fn c_session_get(client: ?*anyopaque, buf: [*]u8) c_int {
var real_client: *transmission.Client = @ptrCast(*transmission.Client, @alignCast(
@alignOf(transmission.Client),
client.?,
));
const body = transmission.session_get_raw(real_client, null) catch |err| {
std.debug.print("error: {any}\n", .{err});
unreachable;
};
defer allocator.free(body);
std.debug.print("body: {s}\n", .{body});
_ = buf;
return 0;
}
export fn c_torrent_get(client: ?*anyopaque) [*:0]u8 {
var real_client: *transmission.Client = @ptrCast(*transmission.Client, @alignCast(
@alignOf(transmission.Client),
client.?,
));
const body = transmission.torrent_get_raw(real_client, null) catch |err| {
std.debug.print("error: {any}\n", .{err});
unreachable;
};
defer allocator.free(body);
// TODO: use the same pointer of body but gotta go fast :(
var foo = std.ArrayList(u8).init(allocator);
foo.insertSlice(0, body) catch unreachable;
//std.debug.print("body: {s}\n", .{body});
return foo.toOwnedSliceSentinel(0) catch unreachable;
}
test "c api" {
const clientOptions = transmission.ClientOptions{
.host = "192.168.0.2",
.port = 9091,
.https = false,
};
var foo = c_client_init(clientOptions);
_ = c_session_get(foo, undefined);
_ = c_torrent_get(foo);
c_client_deinit(foo);
}
test "basic add functionality" {
try testing.expect(add(3, 7) == 10);
}
|