dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/dubbo/impl/package.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package impl
    19  
    20  import (
    21  	"bufio"
    22  	"bytes"
    23  	"fmt"
    24  	"time"
    25  )
    26  
    27  import (
    28  	"github.com/pkg/errors"
    29  )
    30  
    31  type PackageType int
    32  
    33  // enum part
    34  const (
    35  	PackageError              = PackageType(0x01)
    36  	PackageRequest            = PackageType(0x02)
    37  	PackageResponse           = PackageType(0x04)
    38  	PackageHeartbeat          = PackageType(0x08)
    39  	PackageRequest_TwoWay     = PackageType(0x10)
    40  	PackageResponse_Exception = PackageType(0x20)
    41  	PackageType_BitSize       = 0x2f
    42  )
    43  
    44  type DubboHeader struct {
    45  	SerialID       byte
    46  	Type           PackageType
    47  	ID             int64
    48  	BodyLen        int
    49  	ResponseStatus byte
    50  }
    51  
    52  // Service defines service instance
    53  type Service struct {
    54  	Path      string
    55  	Interface string
    56  	Group     string
    57  	Version   string
    58  	Method    string
    59  	Timeout   time.Duration // request timeout
    60  }
    61  
    62  type DubboPackage struct {
    63  	Header  DubboHeader
    64  	Service Service
    65  	Body    interface{}
    66  	Err     error
    67  	Codec   *ProtocolCodec
    68  }
    69  
    70  func (p DubboPackage) String() string {
    71  	return fmt.Sprintf("HessianPackage: Header-%v, Path-%v, Body-%v", p.Header, p.Service, p.Body)
    72  }
    73  
    74  func (p *DubboPackage) ReadHeader() error {
    75  	return p.Codec.ReadHeader(&p.Header)
    76  }
    77  
    78  func (p *DubboPackage) Marshal() (*bytes.Buffer, error) {
    79  	if p.Codec == nil {
    80  		return nil, errors.New("Codec is nil")
    81  	}
    82  	pkg, err := p.Codec.Encode(*p)
    83  	if err != nil {
    84  		return nil, errors.WithStack(err)
    85  	}
    86  	return bytes.NewBuffer(pkg), nil
    87  }
    88  
    89  func (p *DubboPackage) Unmarshal() error {
    90  	if p.Codec == nil {
    91  		return errors.New("Codec is nil")
    92  	}
    93  	return p.Codec.Decode(p)
    94  }
    95  
    96  func (p DubboPackage) IsHeartBeat() bool {
    97  	return p.Header.Type&PackageHeartbeat != 0
    98  }
    99  
   100  func (p DubboPackage) IsRequest() bool {
   101  	return p.Header.Type&(PackageRequest_TwoWay|PackageRequest) != 0
   102  }
   103  
   104  func (p DubboPackage) IsResponse() bool {
   105  	return p.Header.Type == PackageResponse
   106  }
   107  
   108  func (p DubboPackage) IsResponseWithException() bool {
   109  	flag := PackageResponse | PackageResponse_Exception
   110  	return p.Header.Type&flag == flag
   111  }
   112  
   113  func (p DubboPackage) GetBodyLen() int {
   114  	return p.Header.BodyLen
   115  }
   116  
   117  func (p DubboPackage) GetLen() int {
   118  	return HEADER_LENGTH + p.Header.BodyLen
   119  }
   120  
   121  func (p DubboPackage) GetBody() interface{} {
   122  	return p.Body
   123  }
   124  
   125  func (p *DubboPackage) SetBody(body interface{}) {
   126  	p.Body = body
   127  }
   128  
   129  func (p *DubboPackage) SetHeader(header DubboHeader) {
   130  	p.Header = header
   131  }
   132  
   133  func (p *DubboPackage) SetService(svc Service) {
   134  	p.Service = svc
   135  }
   136  
   137  func (p *DubboPackage) SetID(id int64) {
   138  	p.Header.ID = id
   139  }
   140  
   141  func (p DubboPackage) GetHeader() DubboHeader {
   142  	return p.Header
   143  }
   144  
   145  func (p DubboPackage) GetService() Service {
   146  	return p.Service
   147  }
   148  
   149  func (p *DubboPackage) SetResponseStatus(status byte) {
   150  	p.Header.ResponseStatus = status
   151  }
   152  
   153  func (p *DubboPackage) SetSerializer(serializer Serializer) {
   154  	p.Codec.SetSerializer(serializer)
   155  }
   156  
   157  func NewDubboPackage(data *bytes.Buffer) *DubboPackage {
   158  	var codec *ProtocolCodec
   159  	if data == nil {
   160  		codec = NewDubboCodec(nil)
   161  	} else {
   162  		codec = NewDubboCodec(bufio.NewReaderSize(data, len(data.Bytes())))
   163  	}
   164  	return &DubboPackage{
   165  		Header:  DubboHeader{},
   166  		Service: Service{},
   167  		Body:    nil,
   168  		Err:     nil,
   169  		Codec:   codec,
   170  	}
   171  }