Move connection to conn.zig
This commit is contained in:
parent
f5af54e5d1
commit
8ba0ebd84c
3 changed files with 40 additions and 32 deletions
33
src/http/conn.zig
Normal file
33
src/http/conn.zig
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
pub const Connection = struct {
|
||||||
|
pub const Id = u64;
|
||||||
|
|
||||||
|
id: Id,
|
||||||
|
address: std.net.Address,
|
||||||
|
stream: std.net.Stream,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const ConnectionServer = struct {
|
||||||
|
pub const Handler = fn (Connection) callconv(.Async) void;
|
||||||
|
|
||||||
|
alloc: std.mem.Allocator,
|
||||||
|
next_conn_id: std.atomic.Atomic(Connection.Id) = std.atomic.Atomic(Connection.Id).init(1),
|
||||||
|
handler: Handler,
|
||||||
|
|
||||||
|
// todo accept is a bad name
|
||||||
|
pub fn accept(self: *ConnectionServer, stream: std.net.Stream, address: std.net.Address) void {
|
||||||
|
const conn = Connection{
|
||||||
|
.id = self.next_conn_id.fetchAdd(1, .SeqCst),
|
||||||
|
.address = address,
|
||||||
|
.stream = stream,
|
||||||
|
};
|
||||||
|
|
||||||
|
defer conn.stream.close();
|
||||||
|
std.log.debug("new connection conn_id={}", .{conn.id});
|
||||||
|
|
||||||
|
async self.handler(conn);
|
||||||
|
|
||||||
|
std.log.debug("terminating connection conn_id={}", .{conn.id});
|
||||||
|
}
|
||||||
|
};
|
|
@ -1,14 +1,16 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const ciutf8 = @import("util").ciutf8;
|
const ciutf8 = @import("util").ciutf8;
|
||||||
|
|
||||||
|
const conn = @import("./conn.zig");
|
||||||
|
const response_stream = @import("./response_stream.zig");
|
||||||
const routing = @import("./routing.zig");
|
const routing = @import("./routing.zig");
|
||||||
const server = @import("./server.zig");
|
const server = @import("./server.zig");
|
||||||
const response_stream = @import("./response_stream.zig");
|
|
||||||
|
|
||||||
pub const Status = std.http.Status;
|
pub const Connection = conn.Connection;
|
||||||
pub const Method = std.http.Method;
|
pub const Method = std.http.Method;
|
||||||
pub const ResponseStream = response_stream.ResponseStream;
|
pub const ResponseStream = response_stream.ResponseStream;
|
||||||
//pub const Router = routing.Router(Context);
|
//pub const Router = routing.Router(Context);
|
||||||
|
pub const Status = std.http.Status;
|
||||||
|
|
||||||
pub const Headers = std.HashMap([]const u8, []const u8, struct {
|
pub const Headers = std.HashMap([]const u8, []const u8, struct {
|
||||||
pub fn eql(_: @This(), a: []const u8, b: []const u8) bool {
|
pub fn eql(_: @This(), a: []const u8, b: []const u8) bool {
|
||||||
|
@ -28,7 +30,8 @@ pub const Request = struct {
|
||||||
};
|
};
|
||||||
|
|
||||||
test {
|
test {
|
||||||
_ = ResponseStream;
|
_ = conn;
|
||||||
|
_ = response_stream;
|
||||||
_ = routing;
|
_ = routing;
|
||||||
_ = server;
|
_ = server;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,38 +2,10 @@ const std = @import("std");
|
||||||
const util = @import("util");
|
const util = @import("util");
|
||||||
const http = @import("./lib.zig");
|
const http = @import("./lib.zig");
|
||||||
|
|
||||||
const Address = std.net.Address;
|
const Connection = http.Connection;
|
||||||
const Method = http.Method;
|
const Method = http.Method;
|
||||||
const Request = http.Request;
|
const Request = http.Request;
|
||||||
const Headers = http.Headers;
|
const Headers = http.Headers;
|
||||||
const Id = u64;
|
|
||||||
|
|
||||||
const Connection = struct {
|
|
||||||
id: Id,
|
|
||||||
address: Address,
|
|
||||||
stream: std.net.Stream,
|
|
||||||
};
|
|
||||||
|
|
||||||
const ConnectionServer = struct {
|
|
||||||
alloc: std.mem.Allocator,
|
|
||||||
next_conn_id: std.atomic.Atomic(Id) = std.atomic.Atomic(Id).init(1),
|
|
||||||
|
|
||||||
// todo accept is a bad name
|
|
||||||
fn accept(self: *ConnectionServer, stream: std.net.Stream, address: Address) void {
|
|
||||||
const conn = Connection{
|
|
||||||
.id = self.next_conn_id.fetchAdd(1, .SeqCst),
|
|
||||||
.address = address,
|
|
||||||
.stream = stream,
|
|
||||||
};
|
|
||||||
|
|
||||||
defer conn.stream.close();
|
|
||||||
std.log.debug("new connection conn_id={}", .{conn.id});
|
|
||||||
|
|
||||||
async handleConnection(conn);
|
|
||||||
|
|
||||||
std.log.debug("terminating connection conn_id={}", .{conn.id});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const request_buf_size = 1 << 16;
|
const request_buf_size = 1 << 16;
|
||||||
const max_path_len = 1 << 10;
|
const max_path_len = 1 << 10;
|
||||||
|
|
Loading…
Reference in a new issue