// Run an upstream HTTP server on 127.0.0.1:8001, then request /busy, /wait and // /fast on port 8000. The window keeps drawing while the handlers share turns. import core::raylib; import core::http; record State { frames: i64, busy: bool, fast: bool, conflict: bool } fn outbound(ctx: http::Context, saved: i64) -> http::ServerResponse { let snapshot = http::snapshot_state(ctx); let request = http::client_request("GET", http::read_config(ctx))::value; let reply = http::request(request, http::client_options()); if not reply::ok { return http::text_response(502, reply::error::code, "text/plain")::value; } if saved not matches 7 { return http::response(500, [])::value; } let published = http::replace_state_if_current(ctx, snapshot, snapshot::value)::value; var current = http::read_state(ctx); current::conflict = not published; http::replace_state(ctx, current); return http::text_response(200, "conflict", "text/plain")::value; } fn handler(req: http::IncomingRequest, ctx: http::Context) -> http::ServerResponse { if req::path matches "/busy" { var state = http::read_state(ctx); state::busy = true; http::replace_state(ctx, state); while true {} } if req::path matches "/wait" { return outbound(ctx, 7); } var current = http::read_state(ctx); current::fast = raylib::IsWindowReady(); http::replace_state(ctx, current); return http::text_response(200, "fast", "text/plain")::value; } fn start_main() -> i64 { let opened = raylib::InitWindow(480, 240, "ASPL HTTP and drawing"); if not opened::ok { print(opened::error::code); return 1; } let pacing = raylib::SetTargetFPS(60); if not pacing::ok { return 9; } var server = http::server_config(); server::listeners = [http::listener("127.0.0.1", 8000)::value]; let routes = http::router([ http::route("GET", "/busy", handler)::value, http::route("GET", "/wait", handler)::value, http::route("GET", "/fast", handler)::value ])::value; let started = http::start(server, routes, "http://127.0.0.1:8001/", State {frames: 0, busy: false, fast: false, conflict: false}); if not started::ok { print(started::error::code); return 2; } let owner = started::value; print("ready\n"); var budget = http::progress_budget(); budget::max_instructions = 128; var frames: i32 = 0; while frames < 180 and not raylib::WindowShouldClose()::value { if not http::progress(owner, budget)::ok { return 3; } if not raylib::BeginDrawing()::ok { return 4; } if not raylib::ClearBackground(raylib::RAYWHITE)::ok { return 10; } var state = http::snapshot_session_state(owner)::value::value; state::frames = state::frames + 1; if not http::replace_session_state(owner, state)::ok { return 5; } if not raylib::DrawRectangle(frames, 90, 40, 40, raylib::BLUE)::ok { return 11; } if not raylib::EndDrawing()::ok { return 6; } frames = frames + 1; } let final = http::session_state(owner)::value; if not http::close_session(owner)::ok { return 7; } if not raylib::CloseWindow()::ok { return 8; } if final::busy and final::fast and final::conflict { print("busy fast conflict\n"); } return 0; }