-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathstream.zig
More file actions
160 lines (135 loc) · 4.55 KB
/
stream.zig
File metadata and controls
160 lines (135 loc) · 4.55 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
149
150
151
152
153
154
155
156
157
158
159
160
const fs = @import("fs.zig");
const log = @import("log.zig");
const uart = @import("uart.zig");
const tcpip = @import("tcpip.zig");
const sync = @import("sync.zig");
const Directory = fs.Directory;
const Socket = tcpip.Socket;
const SpinLock = sync.SpinLock;
const OpenedFile = fs.OpenedFile;
const STREAM_NUM = 2048;
var streams_internal: [STREAM_NUM]?Stream = init: {
var initial_fd_table: [STREAM_NUM]?Stream = undefined;
// set stdin, stdout, and stderr to the uart
initial_fd_table[0] = Stream{ .uart = void{} };
initial_fd_table[1] = Stream{ .uart = void{} };
initial_fd_table[2] = Stream{ .uart = void{} };
break :init initial_fd_table;
};
// thread safe file descriptor table
pub var fd_table = FdTable{ .streams = SpinLock([STREAM_NUM]?Stream).new(&streams_internal) };
const FdTable = struct {
streams: SpinLock([STREAM_NUM]?Stream),
index: usize = 0,
const Self = @This();
pub fn get(self: *Self, fd: usize) ?*Stream {
const streams = self.streams.acquire();
defer self.streams.release();
const s = &streams.*[fd];
if (s.* == null) {
return null;
}
return @as(*Stream, @ptrCast(s));
}
// If the stream has fd field, it will be set to the new fd
pub fn set(self: *Self, stream: Stream) Stream.Error!usize {
const streams = self.streams.acquire();
defer self.streams.release();
var i = (self.index + 1) % STREAM_NUM;
defer self.index = i;
while (i != self.index) : (i = (i + 1) % STREAM_NUM) {
if (streams.*[i] == null) {
streams.*[i] = stream;
const set_stream = &streams.*[i];
// const new_fd = @as(i32, @intCast(i));
switch (set_stream.*.?) {
Stream.uart => {},
Stream.socket => |*sock| {
sock.setFd(i);
},
Stream.opened_file => {},
Stream.dir => {},
}
return @intCast(i);
}
}
return Stream.Error.FdFull;
}
pub fn remove(self: *Self, fd: usize) void {
const streams = self.streams.acquire();
defer self.streams.release();
streams.*[fd] = null;
}
};
pub const Stream = union(enum) {
uart: void,
socket: Socket,
opened_file: OpenedFile,
dir: Directory,
const Self = @This();
pub const Error = error{FdFull} || Socket.Error;
pub fn read(self: *Self, buffer: []u8) Error!usize {
return switch (self.*) {
Self.uart => @panic("unimplemented"),
Self.socket => |*sock| sock.read(buffer),
Self.opened_file => |*f| f.read(buffer),
Self.dir => @panic("unimplemented"),
};
}
pub fn write(self: *Self, buffer: []u8) Error!usize {
return switch (self.*) {
Self.uart => uart.write(buffer),
Self.socket => |*sock| sock.send(buffer),
Self.opened_file => @panic("unimplemented"),
Self.dir => @panic("unimplemented"),
};
}
pub fn close(self: *Self) Error!void {
return switch (self.*) {
Self.uart => @panic("unimplemented"),
Self.socket => |*sock| sock.close(),
Self.opened_file => {},
Self.dir => {},
};
}
pub fn flags(self: *Self) u16 {
return switch (self.*) {
Self.uart => 0,
Self.socket => 0,
Self.opened_file => 0,
Self.dir => 0,
};
}
pub fn setFlags(self: *Self, f: u16) void {
switch (self.*) {
Self.uart => @panic("unimplemented"),
Self.socket => |*sock| sock.*.flags |= f,
Self.opened_file => @panic("unimplemented"),
Self.dir => @panic("unimplemented"),
}
}
pub fn bytesCanRead(self: *Self) ?usize {
return switch (self.*) {
Self.uart => 1,
Self.socket => |*sock| sock.bytesCanRead(),
Self.opened_file => |*f| f.inner.data.len - f.pos,
Self.dir => 0,
};
}
pub fn bytesCanWrite(self: *Self) ?usize {
return switch (self.*) {
Self.uart => 1,
Self.socket => |*sock| sock.bytesCanWrite(),
Self.opened_file => 0,
Self.dir => 0,
};
}
pub fn size(self: *Self) usize {
return switch (self.*) {
Self.uart => 0,
Self.socket => 0,
Self.opened_file => |*f| f.inner.data.len,
Self.dir => 0,
};
}
};