1package protocol 2 3import "sync" 4 5// Type determines connection protocol type. 6type Type string 7 8const ( 9 // TypeJSON means JSON protocol. 10 TypeJSON Type = "json" 11 // TypeProtobuf means Protobuf protocol. 12 TypeProtobuf Type = "protobuf" 13) 14 15var ( 16 jsonPushEncoder = NewJSONPushEncoder() 17 protobufPushEncoder = NewProtobufPushEncoder() 18) 19 20// GetPushEncoder ... 21func GetPushEncoder(protoType Type) PushEncoder { 22 if protoType == TypeJSON { 23 return jsonPushEncoder 24 } 25 return protobufPushEncoder 26} 27 28var ( 29 jsonReplyEncoder = NewJSONReplyEncoder() 30 protobufReplyEncoder = NewProtobufReplyEncoder() 31) 32 33// GetReplyEncoder ... 34func GetReplyEncoder(protoType Type) ReplyEncoder { 35 if protoType == TypeJSON { 36 return jsonReplyEncoder 37 } 38 return protobufReplyEncoder 39} 40 41var ( 42 jsonDataEncoderPool sync.Pool 43 protobufDataEncoderPool sync.Pool 44 jsonCommandDecoderPool sync.Pool 45 protobufCommandDecoderPool sync.Pool 46) 47 48// GetDataEncoder ... 49func GetDataEncoder(protoType Type) DataEncoder { 50 if protoType == TypeJSON { 51 e := jsonDataEncoderPool.Get() 52 if e == nil { 53 return NewJSONDataEncoder() 54 } 55 protoEncoder := e.(DataEncoder) 56 protoEncoder.Reset() 57 return protoEncoder 58 } 59 e := protobufDataEncoderPool.Get() 60 if e == nil { 61 return NewProtobufDataEncoder() 62 } 63 protoEncoder := e.(DataEncoder) 64 protoEncoder.Reset() 65 return protoEncoder 66} 67 68// PutDataEncoder ... 69func PutDataEncoder(protoType Type, e DataEncoder) { 70 if protoType == TypeJSON { 71 jsonDataEncoderPool.Put(e) 72 return 73 } 74 protobufDataEncoderPool.Put(e) 75} 76 77// GetCommandDecoder ... 78func GetCommandDecoder(protoType Type, data []byte) CommandDecoder { 79 if protoType == TypeJSON { 80 e := jsonCommandDecoderPool.Get() 81 if e == nil { 82 return NewJSONCommandDecoder(data) 83 } 84 commandDecoder := e.(*JSONCommandDecoder) 85 _ = commandDecoder.Reset(data) 86 return commandDecoder 87 } 88 e := protobufCommandDecoderPool.Get() 89 if e == nil { 90 return NewProtobufCommandDecoder(data) 91 } 92 commandDecoder := e.(*ProtobufCommandDecoder) 93 _ = commandDecoder.Reset(data) 94 return commandDecoder 95} 96 97// PutCommandDecoder ... 98func PutCommandDecoder(protoType Type, e CommandDecoder) { 99 if protoType == TypeJSON { 100 jsonCommandDecoderPool.Put(e) 101 return 102 } 103 protobufCommandDecoderPool.Put(e) 104} 105 106// GetResultEncoder ... 107func GetResultEncoder(protoType Type) ResultEncoder { 108 if protoType == TypeJSON { 109 return NewJSONResultEncoder() 110 } 111 return NewProtobufResultEncoder() 112} 113 114// PutResultEncoder ... 115func PutResultEncoder(_ Type, _ ReplyEncoder) {} 116 117// GetParamsDecoder ... 118func GetParamsDecoder(protoType Type) ParamsDecoder { 119 if protoType == TypeJSON { 120 return NewJSONParamsDecoder() 121 } 122 return NewProtobufParamsDecoder() 123} 124 125// PutParamsDecoder ... 126func PutParamsDecoder(_ Type, _ ParamsDecoder) {} 127