dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/dubbo/impl/const.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  	"reflect"
    22  	"regexp"
    23  )
    24  
    25  import (
    26  	"github.com/pkg/errors"
    27  )
    28  
    29  const (
    30  	DUBBO = "dubbo"
    31  )
    32  
    33  const (
    34  	// Zero : byte zero
    35  	Zero = byte(0x00)
    36  )
    37  
    38  const (
    39  	TAG_READ        = int32(-1)
    40  	ASCII_GAP       = 32
    41  	CHUNK_SIZE      = 4096
    42  	BC_BINARY       = byte('B') // final chunk
    43  	BC_BINARY_CHUNK = byte('A') // non-final chunk
    44  
    45  	BC_BINARY_DIRECT  = byte(0x20) // 1-byte length binary
    46  	BINARY_DIRECT_MAX = byte(0x0f)
    47  	BC_BINARY_SHORT   = byte(0x34) // 2-byte length binary
    48  	BINARY_SHORT_MAX  = 0x3ff      // 0-1023 binary
    49  
    50  	BC_DATE        = byte(0x4a) // 64-bit millisecond UTC date
    51  	BC_DATE_MINUTE = byte(0x4b) // 32-bit minute UTC date
    52  
    53  	BC_DOUBLE = byte('D') // IEEE 64-bit double
    54  
    55  	BC_DOUBLE_ZERO  = byte(0x5b)
    56  	BC_DOUBLE_ONE   = byte(0x5c)
    57  	BC_DOUBLE_BYTE  = byte(0x5d)
    58  	BC_DOUBLE_SHORT = byte(0x5e)
    59  	BC_DOUBLE_MILL  = byte(0x5f)
    60  
    61  	BC_FALSE = byte('F') // boolean false
    62  
    63  	BC_INT = byte('I') // 32-bit int
    64  
    65  	INT_DIRECT_MIN = -0x10
    66  	INT_DIRECT_MAX = byte(0x2f)
    67  	BC_INT_ZERO    = byte(0x90)
    68  
    69  	INT_BYTE_MIN     = -0x800
    70  	INT_BYTE_MAX     = 0x7ff
    71  	BC_INT_BYTE_ZERO = byte(0xc8)
    72  
    73  	BC_END = byte('Z')
    74  
    75  	INT_SHORT_MIN     = -0x40000
    76  	INT_SHORT_MAX     = 0x3ffff
    77  	BC_INT_SHORT_ZERO = byte(0xd4)
    78  
    79  	BC_LIST_VARIABLE         = byte(0x55)
    80  	BC_LIST_FIXED            = byte('V')
    81  	BC_LIST_VARIABLE_UNTYPED = byte(0x57)
    82  	BC_LIST_FIXED_UNTYPED    = byte(0x58)
    83  
    84  	BC_LIST_DIRECT         = byte(0x70)
    85  	BC_LIST_DIRECT_UNTYPED = byte(0x78)
    86  	LIST_DIRECT_MAX        = byte(0x7)
    87  
    88  	BC_LONG         = byte('L') // 64-bit signed integer
    89  	LONG_DIRECT_MIN = -0x08
    90  	LONG_DIRECT_MAX = byte(0x0f)
    91  	BC_LONG_ZERO    = byte(0xe0)
    92  
    93  	LONG_BYTE_MIN     = -0x800
    94  	LONG_BYTE_MAX     = 0x7ff
    95  	BC_LONG_BYTE_ZERO = byte(0xf8)
    96  
    97  	LONG_SHORT_MIN     = -0x40000
    98  	LONG_SHORT_MAX     = 0x3ffff
    99  	BC_LONG_SHORT_ZERO = byte(0x3c)
   100  
   101  	BC_LONG_INT = byte(0x59)
   102  
   103  	BC_MAP         = byte('M')
   104  	BC_MAP_UNTYPED = byte('H')
   105  
   106  	BC_NULL = byte('N') // x4e
   107  
   108  	BC_OBJECT     = byte('O')
   109  	BC_OBJECT_DEF = byte('C')
   110  
   111  	BC_OBJECT_DIRECT  = byte(0x60)
   112  	OBJECT_DIRECT_MAX = byte(0x0f)
   113  
   114  	BC_REF = byte(0x51)
   115  
   116  	BC_STRING       = byte('S') // final string
   117  	BC_STRING_CHUNK = byte('R') // non-final string
   118  
   119  	BC_STRING_DIRECT  = byte(0x00)
   120  	STRING_DIRECT_MAX = byte(0x1f)
   121  	BC_STRING_SHORT   = byte(0x30)
   122  	STRING_SHORT_MAX  = 0x3ff
   123  
   124  	BC_TRUE = byte('T')
   125  
   126  	P_PACKET_CHUNK = byte(0x4f)
   127  	P_PACKET       = byte('P')
   128  
   129  	P_PACKET_DIRECT   = byte(0x80)
   130  	PACKET_DIRECT_MAX = byte(0x7f)
   131  
   132  	P_PACKET_SHORT   = byte(0x70)
   133  	PACKET_SHORT_MAX = 0xfff
   134  	ARRAY_STRING     = "[string"
   135  	ARRAY_INT        = "[int"
   136  	ARRAY_DOUBLE     = "[double"
   137  	ARRAY_FLOAT      = "[float"
   138  	ARRAY_BOOL       = "[boolean"
   139  	ARRAY_LONG       = "[long"
   140  
   141  	PATH_KEY      = "path"
   142  	GROUP_KEY     = "group"
   143  	INTERFACE_KEY = "interface"
   144  	VERSION_KEY   = "version"
   145  	TIMEOUT_KEY   = "timeout"
   146  
   147  	STRING_NIL   = ""
   148  	STRING_TRUE  = "true"
   149  	STRING_FALSE = "false"
   150  	STRING_ZERO  = "0.0"
   151  	STRING_ONE   = "1.0"
   152  )
   153  
   154  // ResponsePayload related consts
   155  const (
   156  	Response_OK                byte = 20
   157  	Response_CLIENT_TIMEOUT    byte = 30
   158  	Response_SERVER_TIMEOUT    byte = 31
   159  	Response_BAD_REQUEST       byte = 40
   160  	Response_BAD_RESPONSE      byte = 50
   161  	Response_SERVICE_NOT_FOUND byte = 60
   162  	Response_SERVICE_ERROR     byte = 70
   163  	Response_SERVER_ERROR      byte = 80
   164  	Response_CLIENT_ERROR      byte = 90
   165  
   166  	// According to "java dubbo" There are two cases of response:
   167  	// 		1. with attachments
   168  	// 		2. no attachments
   169  	RESPONSE_WITH_EXCEPTION                  int32 = 0
   170  	RESPONSE_VALUE                           int32 = 1
   171  	RESPONSE_NULL_VALUE                      int32 = 2
   172  	RESPONSE_WITH_EXCEPTION_WITH_ATTACHMENTS int32 = 3
   173  	RESPONSE_VALUE_WITH_ATTACHMENTS          int32 = 4
   174  	RESPONSE_NULL_VALUE_WITH_ATTACHMENTS     int32 = 5
   175  )
   176  
   177  /**
   178   * the dubbo protocol header length is 16 Bytes.
   179   * the first 2 Bytes is magic code '0xdabb'
   180   * the next 1 Byte is message flags, in which its 16-20 bit is serial id, 21 for event, 22 for two way, 23 for request/response flag
   181   * the next 1 Bytes is response state.
   182   * the next 8 Bytes is package DI.
   183   * the next 4 Bytes is package length.
   184   **/
   185  const (
   186  	// header length.
   187  	HEADER_LENGTH = 16
   188  
   189  	// magic header
   190  	MAGIC      = uint16(0xdabb)
   191  	MAGIC_HIGH = byte(0xda)
   192  	MAGIC_LOW  = byte(0xbb)
   193  
   194  	// message flag.
   195  	FLAG_REQUEST = byte(0x80)
   196  	FLAG_TWOWAY  = byte(0x40)
   197  	FLAG_EVENT   = byte(0x20) // for heartbeat
   198  	SERIAL_MASK  = 0x1f
   199  
   200  	DUBBO_VERSION                          = "2.5.4"
   201  	DUBBO_VERSION_KEY                      = "dubbo"
   202  	DEFAULT_DUBBO_PROTOCOL_VERSION         = "2.0.2" // Dubbo RPC protocol version, for compatibility, it must not be between 2.0.10 ~ 2.6.2
   203  	LOWEST_VERSION_FOR_RESPONSE_ATTACHMENT = 2000200
   204  	DEFAULT_LEN                            = 8388608 // 8 * 1024 * 1024 default body max length
   205  )
   206  
   207  // regular
   208  const (
   209  	JAVA_IDENT_REGEX = "(?:[_$a-zA-Z][_$a-zA-Z0-9]*)"
   210  	CLASS_DESC       = "(?:L" + JAVA_IDENT_REGEX + "(?:\\/" + JAVA_IDENT_REGEX + ")*;)"
   211  	ARRAY_DESC       = "(?:\\[+(?:(?:[VZBCDFIJS])|" + CLASS_DESC + "))"
   212  	DESC_REGEX       = "(?:(?:[VZBCDFIJS])|" + CLASS_DESC + "|" + ARRAY_DESC + ")"
   213  )
   214  
   215  // Dubbo request response related consts
   216  var (
   217  	DubboRequestHeaderBytesTwoWay = [HEADER_LENGTH]byte{MAGIC_HIGH, MAGIC_LOW, FLAG_REQUEST | FLAG_TWOWAY}
   218  	DubboRequestHeaderBytes       = [HEADER_LENGTH]byte{MAGIC_HIGH, MAGIC_LOW, FLAG_REQUEST}
   219  	DubboResponseHeaderBytes      = [HEADER_LENGTH]byte{MAGIC_HIGH, MAGIC_LOW, Zero, Response_OK}
   220  	DubboRequestHeartbeatHeader   = [HEADER_LENGTH]byte{MAGIC_HIGH, MAGIC_LOW, FLAG_REQUEST | FLAG_TWOWAY | FLAG_EVENT}
   221  	DubboResponseHeartbeatHeader  = [HEADER_LENGTH]byte{MAGIC_HIGH, MAGIC_LOW, FLAG_EVENT}
   222  )
   223  
   224  // Error part
   225  var (
   226  	// the following errors has already existed in github.com/apache/dubbo-go-hessian2.
   227  	// Please do not use them.
   228  	ErrHeaderNotEnough = errors.New("header buffer too short")
   229  	ErrBodyNotEnough   = errors.New("body buffer too short")
   230  	ErrJavaException   = errors.New("got java exception")
   231  	ErrIllegalPackage  = errors.New("illegal package!")
   232  )
   233  
   234  // DescRegex ...
   235  var DescRegex, _ = regexp.Compile(DESC_REGEX)
   236  
   237  var NilValue = reflect.Zero(reflect.TypeOf((*interface{})(nil)).Elem())
   238  
   239  // Body map keys
   240  var (
   241  	DubboVersionKey = "dubboVersion"
   242  	ArgsTypesKey    = "argsTypes"
   243  	ArgsKey         = "args"
   244  	ServiceKey      = "service"
   245  	AttachmentsKey  = "attachments"
   246  )