Clients are working.
parent
79f9fdc3e2
commit
2ad505b305
|
@ -97,7 +97,6 @@ pub const Context = struct {
|
||||||
const newfd = stream.handle;
|
const newfd = stream.handle;
|
||||||
errdefer std.os.closeSocket(newfd);
|
errdefer std.os.closeSocket(newfd);
|
||||||
var newcon = Connection.init(ctype, path);
|
var newcon = Connection.init(ctype, path);
|
||||||
newcon.client = stream;
|
|
||||||
try self.connections.append(newcon);
|
try self.connections.append(newcon);
|
||||||
try self.pollfd.append(.{ .fd = newfd
|
try self.pollfd.append(.{ .fd = newfd
|
||||||
, .events = std.os.linux.POLL.IN
|
, .events = std.os.linux.POLL.IN
|
||||||
|
@ -254,29 +253,26 @@ pub const Context = struct {
|
||||||
print("read index {}\n", .{index});
|
print("read index {}\n", .{index});
|
||||||
|
|
||||||
var buffer: [2000000]u8 = undefined; // TODO: FIXME??
|
var buffer: [2000000]u8 = undefined; // TODO: FIXME??
|
||||||
var origin: i32 = undefined;
|
|
||||||
var packet_size: usize = undefined;
|
var packet_size: usize = undefined;
|
||||||
|
|
||||||
// TODO: this is a problem from the network API in Zig,
|
// TODO: this is a problem from the network API in Zig,
|
||||||
// servers and clients are different, they aren't just fds.
|
// servers and clients are different, they aren't just fds.
|
||||||
// Maybe there is something to change in the API.
|
// Maybe there is something to change in the API.
|
||||||
if (self.connections.items[index].t == .IPC) {
|
if (self.connections.items[index].t == .SERVER) {
|
||||||
var client = self.connections.items[index].client
|
|
||||||
orelse return error.NoClientHere;
|
|
||||||
var stream: net.Stream = client.stream;
|
|
||||||
origin = stream.handle;
|
|
||||||
packet_size = try stream.read(buffer[0..]);
|
|
||||||
}
|
|
||||||
else if (self.connections.items[index].t == .SERVER) {
|
|
||||||
return error.messageOnServer;
|
return error.messageOnServer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This may be kinda hacky, idk.
|
||||||
|
var fd = self.pollfd.items[index].fd;
|
||||||
|
var stream: net.Stream = .{ .handle = fd };
|
||||||
|
packet_size = try stream.read(buffer[0..]);
|
||||||
|
|
||||||
// Let's handle this as a disconnection.
|
// Let's handle this as a disconnection.
|
||||||
if (packet_size <= 4) {
|
if (packet_size <= 4) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return try Message.read(origin, buffer[0..], self.allocator);
|
return try Message.read(fd, buffer[0..], self.allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait an event.
|
// Wait an event.
|
||||||
|
@ -359,7 +355,14 @@ pub const Context = struct {
|
||||||
else {
|
else {
|
||||||
// TODO: handle incoming message
|
// TODO: handle incoming message
|
||||||
// TODO: handle_new_message
|
// TODO: handle_new_message
|
||||||
var maybe_message = try self.read(i);
|
var maybe_message = self.read(i) catch |err| switch(err) {
|
||||||
|
error.ConnectionResetByPeer => {
|
||||||
|
print("connection reset by peer\n", .{});
|
||||||
|
try self.close(i);
|
||||||
|
return Event.init(Event.Type.DISCONNECTION, i, fd.fd, null);
|
||||||
|
},
|
||||||
|
else => { return err; },
|
||||||
|
};
|
||||||
if (maybe_message) |m| {
|
if (maybe_message) |m| {
|
||||||
return Event.init(Event.Type.MESSAGE, i, fd.fd, m);
|
return Event.init(Event.Type.MESSAGE, i, fd.fd, m);
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue