-
Notifications
You must be signed in to change notification settings - Fork 0
Cookbook
一组可直接接入驱动示例工程的生产级范例,演示 khttp 高层 API 在真实场景中资源/错误处理正确的用法。源码位于 src/KernelHttpExample_Cookbook。
English | 简体中文
samples/KhttpScopeGuard.h —— RAII 句柄守卫(仅头文件,可复用)
samples/CookbookSamples.h —— 范例集声明
samples/CookbookSamples.cpp —— 范例集实现(7 组范例)
tests/KhtTest.h —— 轻量测试框架(无异常/无 RTTI)
tests/CookbookMockTransport.h —— 确定性 mock 传输(HTTP + WebSocket 回显)
tests/cookbook_tests.cpp —— 测试套件(9 用例、51 断言)
tests/run-cookbook-tests.ps1 —— 一键编译并运行测试
| 范例 | 演示内容 | 关键 API |
|---|---|---|
| QuickGet | 最简正确 GET:调用 + 检查状态 + 守卫释放 |
Get / ResponseStatusCode / ResponseRelease
|
| SessionReuse | 同主机连续多请求,命中连接池 keep-alive |
Get(同一 Session) |
| PostJson | POST JSON + 自定义头 + 读响应头 |
RequestSetJsonBody / RequestSetHeader / Send / ResponseGetHeader
|
| StreamingDownload | 回调式流式接收,不缓存整包 | SendOptions.OnHeader/OnBody |
| HttpsTls | HTTPS + 显式 TLS:SNI、ALPN、始终开启证书校验 |
RequestSetTls / TlsConfig
|
| AsyncRequest | 异步发起 → 等待 → 取响应 |
GetAsync / AsyncWait / AsyncGetResponse
|
| AsyncCancel | 协作式取消(取消后仍需收尾等待) |
AsyncCancel / AsyncWait / AsyncGetStatus
|
| WebSocketEcho | 连接→发→收→关,含全双工时序说明 |
kws::Connect / kws::SendText / kws::Receive / kws::Close
|
每个范例独立运行,单个失败不阻断后续;入口返回首个失败状态用于汇总。
注:当前公开 WebSocket API 在命名空间
kws(kws::Connect/SendText/Receive/Close,见 WebSocket 协议)。若你的 Cookbook 源码包仍写作khttp::WsConnect/WsSendText/...,那是命名空间统一前的旧写法,请对照当前头文件kws/WebSocket.h调整。
| 守卫 | 管理对象 | 析构调用 |
|---|---|---|
SessionGuard |
khttp::Session |
SessionClose |
RequestGuard |
khttp::Request |
RequestRelease |
ResponseGuard |
khttp::Response |
ResponseRelease |
AsyncOpGuard |
khttp::AsyncOp |
AsyncRelease |
WebSocketGuard |
kws::WebSocket |
kws::Close |
常用成员:Receive()(取地址传给 _Out_ 句柄,接收前先释放已持有的)、Get()(取裸指针)、Detach()(放弃所有权)、Reset()(立即释放)、operator bool。守卫不可复制、可移动。析构调用 Release/Close,须在 PASSIVE_LEVEL。
- 把
samples/三个文件拷到src/KernelHttpExample/samples/。 - 在
.vcxproj加入CookbookSamples.cpp与两个头文件。 - 在
DriverEntry.cpp包含samples/CookbookSamples.h,在RunLoadHttpSamples()里追加RunCookbookSamples(g_wskClient, &cookbookResults)。 - 卸载路径无需改动——现有
DriverUnload已调用engine::KhEngineDrainAsync()。
-
IRQL:所有调用与守卫析构都在
PASSIVE_LEVEL。 -
所有权:
Response与Request/AsyncOp独立生命周期,分别释放。 -
卸载:用异步 API 后卸载前必须
KhEngineDrainAsync()。 -
WebSocket 全双工时序:
kws::Close不得与同句柄「新 I/O 发起」并发;最安全是单线程内 连接→发→收→关。 -
kws::Receive的message.Data指向内部缓冲,下次收/关前有效,关闭后勿引用。
详见仓库内 src/KernelHttpExample_Cookbook/README.md 与 tests/README.md。
A set of production-grade samples showing correct resource/error handling for the khttp high-level API. Source: src/KernelHttpExample_Cookbook.
| Sample | Demonstrates | Key APIs |
|---|---|---|
| QuickGet | Minimal correct GET + status check + guarded release |
Get / ResponseStatusCode / ResponseRelease
|
| SessionReuse | Multiple requests to same host hitting pool keep-alive |
Get (same Session) |
| PostJson | POST JSON + custom headers + read response headers |
RequestSetJsonBody / RequestSetHeader / Send
|
| StreamingDownload | Callback streaming without buffering whole body | SendOptions.OnHeader/OnBody |
| HttpsTls | HTTPS + explicit TLS: SNI, ALPN, always-on cert verify |
RequestSetTls / TlsConfig
|
| AsyncRequest | Async issue → wait → fetch response |
GetAsync / AsyncWait / AsyncGetResponse
|
| AsyncCancel | Cooperative cancel (still wait for drain) |
AsyncCancel / AsyncWait / AsyncGetStatus
|
| WebSocketEcho | connect→send→recv→close with full-duplex timing |
kws::Connect / kws::SendText / kws::Receive / kws::Close
|
SessionGuard, RequestGuard, ResponseGuard, AsyncOpGuard, WebSocketGuard — each calls the matching release/close on destruction (at PASSIVE_LEVEL). Members: Receive(), Get(), Detach(), Reset(), operator bool. Non-copyable, movable.
-
IRQL: all calls and guard destruction at
PASSIVE_LEVEL. -
Ownership:
Responselifetime is independent ofRequest/AsyncOp. -
Unload: call
KhEngineDrainAsync()before unload after using async APIs. -
WebSocket full-duplex: never run
kws::Closeconcurrently with new I/O on the same handle; safest is single-threaded connect→send→recv→close. -
kws::Receive'smessage.Datapoints to an internal buffer valid until the next receive/close.
See src/KernelHttpExample_Cookbook/README.md and tests/README.md in the repo.
-
开始使用 / Get Started
-
概念与行为 / Concepts
-
协议指南 / Protocols
-
API 参考 / API Reference
-
贡献与项目 / Contribute