╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
235 // what a listener does with a port that is already taken 236 const PortPolicy = enum { 237 exact, 238 // leave it to whoever holds it and take an OS-assigned port instead 239 fallback, 240 }; 241 242 fn listen(io: std.Io, listen_address: ListenAddress, policy: PortPolicy) !std.Io.net.Server { 243 const address = try std.Io.net.IpAddress.parseIp4(listen_address.host, listen_address.port); 244 return switch (policy) { 245 .exact => try address.listen(io, .{}), 246 .fallback => address.listen(io, .{}) catch |listen_err| switch (listen_err) { 247 error.AddressInUse => blk: { 248 const any_port = try std.Io.net.IpAddress.parseIp4(listen_address.host, 0); 249 break :blk try any_port.listen(io, .{}); 250 }, 251 else => |e| return e, 252 }, 253 }; 254 } 255 256 fn parseListenAddress(value: []const u8) !ListenAddress { 257 const colon = std.mem.lastIndexOfScalar(u8, value, ':') orelse return error.InvalidListenAddress; 258 if (colon == 0 or colon + 1 >= value.len) return error.InvalidListenAddress; 259 const port = try std.fmt.parseInt(u16, value[colon + 1 ..], 10); 260 return .{ .host = value[0..colon], .port = port }; 261 } 262