BruceChen7 / gitblog

My blog
6 stars 1 forks source link

golang实现HTTP2之协议处理 #46

Open BruceChen7 opened 1 year ago

BruceChen7 commented 1 year ago

参考资料

处理客户端发送

func (sc *serverConn) serve() {
    ....
    go sc.readFrame()
    ...
}
func (sc *serverConn) readFrames() {
    gate := make(gate)
    gateDone := gate.Done
    for {
        // 执行 IO 操作,并解析成一帧
        f, err := sc.framer.ReadFrame()
        select {
        case sc.readFrameCh <- readFrameResult{f, err, gateDone}:
        case <-sc.doneServing: // connection is finished
            return
        }
        select {
        case <-gate:
        case <-sc.doneServing:
            return
        }
        if terminalReadFrameError(err) {
            return
        }
    }
}

处理 setting 帧

func (sc *serverConn) processSettings(f *SettingsFrame) error {
    sc.serveG.check()
    // 是客户端对 server 的 ack
    if f.IsAck() {
        sc.unackedSettings--
        if sc.unackedSettings < 0 {
            // Why is the peer ACKing settings we never sent?
            // The spec doesn't mention this case, but
            // hang up on them anyway.
            return ConnectionError(ErrCodeProtocol)
        }
        return nil
    }
    // 数量不对
    if f.NumSettings() > 100 || f.HasDuplicates() {
        // This isn't actually in the spec, but hang up on
        // suspiciously large settings frames or those with
        // duplicate entries.
        return ConnectionError(ErrCodeProtocol)
    }
    if err := f.ForeachSetting(sc.processSetting); err != nil {
        return err
    }
    // TODO: judging by RFC 7540, Section 6.5.3 each SETTINGS frame should be
    // acknowledged individually, even if multiple are received before the ACK.
    // 需要应达 setting 的 ack
    sc.needToSendSettingsAck = true
    sc.scheduleFrameWrite()
    return nil
}

type/golang #public