github.com/cloudwego/kitex@v0.9.0/pkg/remote/trans/netpollmux/mux_transport.go (about)

     1  /*
     2   * Copyright 2021 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package netpollmux
    18  
    19  import (
    20  	"encoding/binary"
    21  	"fmt"
    22  
    23  	"github.com/cloudwego/netpoll"
    24  
    25  	"github.com/cloudwego/kitex/pkg/remote/codec"
    26  )
    27  
    28  // 0-4Byte length, 4-8Byte version check, 8-12Byte seqID
    29  func parseHeader(reader netpoll.Reader) (length int, seqID int32, err error) {
    30  	buf, err := reader.Peek(3 * codec.Size32)
    31  	if err != nil {
    32  		return 0, 0, err
    33  	}
    34  	// add length self 4 bytes
    35  	length = int(binary.BigEndian.Uint32(buf[:codec.Size32])) + 4
    36  	if length <= 4 {
    37  		return 0, 0, fmt.Errorf("mux read head length[%d] invalid", length-4)
    38  	}
    39  
    40  	// check version
    41  	if !codec.IsTTHeader(buf[:2*codec.Size32]) {
    42  		return 0, 0, fmt.Errorf("mux read check protocol failed, just support TTHeader")
    43  	}
    44  	seqID = int32(binary.BigEndian.Uint32(buf[2*codec.Size32 : 3*codec.Size32]))
    45  	return length, seqID, nil
    46  }