コンテンツにスキップ

サーバーが開いたストリームを閉じずに保つ

ストリーム のページの wired_server_wt_open_uni は、 ペイロード全体を送り、すぐに FIN(ストリームの終わりを示す印)で閉じます。 wired_server_wt_open_uni_stream は同じ種類の単方向(uni)ストリームを開きますが、閉じずに残します。 アプリは、あとから wired_server_wt_stream_send でラウンド(1 回分の送信)を追加し、 FIN を送るラウンドも自分で決められます。

コード

main.c
#define WIRED_MAIN
#include "wired.h"
/* The one server-opened uni stream this single-session demo keeps open
* across several rounds, appended to from on_stream_data below. */
static i64 g_uni = -1;
/* Open it on connect, WITHOUT closing: wired_server_wt_open_uni_stream
* (unlike wired_server_wt_open_uni) leaves the stream open for further
* wired_server_wt_stream_send rounds. */
static void on_session(
void* ctx, wired_wt_session* s, wired_span path, wired_span protocol) {
(void)ctx;
(void)path;
(void)protocol;
u8 buf[16];
usz n = wired_wtwire_signal_put(buf, sizeof buf, 0, s->connect_stream_id);
memcpy(buf + n, "chunk1 ", 7);
g_uni = wired_server_wt_open_uni_stream(s, wired_span_of(buf, n + 7));
}
/* The client drives the open stream over its own bidi requests: "more"
* appends a round (fin=0, stays open), "bye" appends the closing round
* (fin=1). Each request gets a short reply once applied. */
static void on_stream_data(
void* ctx, wired_wt_session* s, u64 stream_id, wired_span data, int fin) {
(void)ctx;
if (!fin) return;
if (wired_span_eq_cstr(data, "more"))
wired_server_wt_stream_send(s, (u64)g_uni, wired_span_cstr("chunk2 "), 0);
else if (wired_span_eq_cstr(data, "bye"))
wired_server_wt_stream_send(s, (u64)g_uni, wired_span_cstr("chunk3"), 1);
wired_server_wt_stream_reply(s, stream_id, wired_span_cstr("ok"));
}
int wired_main(int argc, char** argv) {
/* The same fixed demo identity as the HTTP/3 hello page. */
static wired_srvboot_demo_keys keys;
wired_srvboot_id id;
wired_srvboot_demo(&id, &keys, 0x50, "guide-wt");
id.max_datagram_frame_size = 65535; /* WebTransport requires DATAGRAM */
wired_srvrun_opt opt = {0};
opt.incoming_cpu = -1;
opt.wt_on_session = on_session;
opt.wt_on_stream_data = on_stream_data;
u16 port = (u16)wired_cliargs_int(argc, argv, "--port", 4433);
wired_srvrun_handler h = {0};
wired_srvrun_obs obs = {0};
return wired_server_run_opt(port, &id, h, obs, &opt) ? 0 : 1;
}

クライアント

client.go
package main
import (
"context"
"crypto/tls"
"fmt"
"io"
"net/http"
"os"
"github.com/quic-go/quic-go"
"github.com/quic-go/webtransport-go"
)
func main() {
ctx := context.Background()
var conn *quic.Conn // kept so main can close it before exiting
d := webtransport.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
QUICConfig: &quic.Config{EnableDatagrams: true, EnableStreamResetPartialDelivery: true},
DialAddr: func(ctx context.Context, addr string, t *tls.Config, q *quic.Config) (*quic.Conn, error) {
c, err := quic.DialAddrEarly(ctx, addr, t, q)
conn = c
return c, err
},
}
_, s, err := d.Dial(ctx, os.Args[1]+"/open", http.Header{})
if err != nil {
panic(err)
}
defer conn.CloseWithError(0, "")
uni, err := s.AcceptUniStream(ctx)
if err != nil {
panic(err)
}
done := make(chan []byte, 1)
go func() {
b, _ := io.ReadAll(uni)
done <- b
}()
// Each word drives the server-opened stream one round further, and we
// wait for its ack before sending the next -- so "bye" only reaches the
// server after "more" was fully applied.
for _, word := range []string{"more", "bye"} {
str, err := s.OpenStreamSync(ctx)
if err != nil {
panic(err)
}
str.Write([]byte(word))
str.Close()
ack, _ := io.ReadAll(str)
fmt.Printf("%s: %q\n", word, ack)
}
fmt.Printf("uni stream: %q\n", <-done)
}

実行結果

Terminal window
$ ./wt-open-streams --port 4433
conn closed by peer (CONNECTION_CLOSE)
Terminal window
$ go run . https://127.0.0.1:4433
more: "ok"
bye: "ok"
uni stream: "chunk1 chunk2 chunk3"
  • on_session は wired_server_wt_open_uni_stream で uni ストリームを開き、 その ID を g_uni に保持します。 この呼び出しでは fin を設定しないので、最初のラウンドの後もストリームは開いたままです。
  • クライアントからのリクエストごとに 1 ラウンドを追加します。 wired_server_wt_stream_send(s, g_uni, payload, fin) は、fin=0 なら開いたままにし、 fin=1 ならこのラウンドのバイトの後でストリームを終えます。
  • クライアントの uni ストリームの読み取り(io.ReadAll)は、 サーバーが fin=1 のラウンドを送るまで返りません。 それまでは、データを待つ他のストリーム読み取りと同じようにブロックします。