Skip to content

Instantly share code, notes, and snippets.

@pochi
Last active December 29, 2015 10:39
Show Gist options
  • Save pochi/7658269 to your computer and use it in GitHub Desktop.
Save pochi/7658269 to your computer and use it in GitHub Desktop.

Connectionクラス

http://http2.github.io/http2-spec/

Section 3.5によると Connection Headerは以下の文字列で識別する。

 CONNECTION_HEADER   = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"

このクラスは接続を管理するクラスでStreamではなくTCP上の接続を管理する。 priorityは低いほど優先度が低い。

変数

state

接続状態を保持。new or close

error

接続に最後あったエラー

window

flow control window(デフォルトは無限らしいけど、接続同士で更新できる模様)

stream_limit

接続によって許容される最大連続Stream数(接続同士によって自動的に更新される)

active_stream_count

現在有効なStream一覧

メソッド一覧

new_stream(priority: DEFAULT_PROPERTY, parent: nil)

新しいStreamを作成してそのstreamを返す。 [#TODO]なぜstream_idを2個足すのか。 active_streamでstreamを追加する。

active_stream(id, priority, parent = nil)

hash_keyにidがないかチェック。 Streamを新しく作成しStreamリストに\追加。 streamリスナーにactive,close,promise(Serverの時のみtokinomi),frameを定義する。

send(frame)

もしframeタイプがdataだった場合、send_dataでデータを送る。

ping(frame)

8byteのデータを送る。 socketに書き込むとこはクライアントでリスナーを追加しないけない。 要はframe受け取ったときネットワークバイトオーダはしてやっから後は好きにしなよというスタンスらしい。 終わった後pongリクエストの受付までやる。

 conn.on(:frame) do |bytes|
  puts "Sending bytes: #{bytes.inspect}"
  sock.print bytes
  sock.flush
end
goaway(error, payload=nil)

これもframe経由で送信。終わったらTCP接続ごと終了とするみたい。

settings(stream_limit: @stream_limit, window_limit: @window_limit)

接続のStream値とWindowサイズの変更を行うリクエスト。

receive(data)

ここが受け取り口全般を担当する。 まず@stateがnewの場合、受け取るFrameは接続HeaderかSettingリクエストなのでそれを解析している。 それ以外はHandShakeErrorを発火する。

Framerクラス

ネットワーク直近のFrameの扱いを行う。 こいつがいるからクライアントからは人間が読めるレベルでのメッセージのやり取りができる。

readCommonHeader(buf)

はじめの8byteをアンパックしてflag, length, type, streamを取得する。 細かいところは後で仕様書を確認。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment