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