rocketmq_protocol/protocol/remoting_command/
constructors.rs1use bytes::Bytes;
16use cheetah_string::CheetahString;
17
18use super::next_request_id;
19use super::ExtensionFields;
20use super::RemotingCommand;
21use super::SerializeType;
22use crate::protocol::command_custom_header::CommandCustomHeader;
23use crate::protocol::header_codec::BinaryHeaderFields;
24use crate::protocol::remoting_command_defaults::application_remoting_command_factory;
25use crate::protocol::remoting_command_defaults::RemotingCommandDefaults;
26use crate::protocol::LanguageCode;
27
28impl RemotingCommand {
29 pub fn with_resolved_defaults(version: i32, serialize_type: SerializeType) -> Self {
34 let opaque = next_request_id();
35 RemotingCommand {
36 code: 0,
37 language: LanguageCode::RUST, version,
39 opaque,
40 flag: 0,
41 remark: None,
42 ext_fields: ExtensionFields::default(),
43 body: None,
44 suspended: false,
45 command_custom_header: None,
46 custom_header_to_net: false,
47 serialize_type,
48 }
49 }
50
51 pub(crate) fn from_binary_wire_parts(
52 code: i32,
53 language: LanguageCode,
54 version: i32,
55 opaque: i32,
56 flag: i32,
57 remark: Option<CheetahString>,
58 ext_fields: BinaryHeaderFields,
59 ) -> Self {
60 Self {
61 code,
62 language,
63 version,
64 opaque,
65 flag,
66 remark,
67 ext_fields: ExtensionFields::from_rocketmq_raw(ext_fields),
68 body: None,
69 suspended: false,
70 command_custom_header: None,
71 custom_header_to_net: false,
72 serialize_type: SerializeType::ROCKETMQ,
73 }
74 }
75
76 pub fn new_request(code: impl Into<i32>, body: impl Into<Bytes>) -> Self {
77 application_remoting_command_factory().create_request(code, body)
78 }
79
80 pub fn create_request_command<T>(code: impl Into<i32>, header: T) -> Self
81 where
82 T: CommandCustomHeader + Sync + Send + 'static,
83 {
84 application_remoting_command_factory().create_request_command(code, header)
85 }
86
87 pub fn create_request_command_with_defaults<T>(
89 code: impl Into<i32>,
90 header: T,
91 version: i32,
92 serialize_type: SerializeType,
93 ) -> Self
94 where
95 T: CommandCustomHeader + Sync + Send + 'static,
96 {
97 crate::protocol::remoting_command_defaults::RemotingCommandFactory::new(RemotingCommandDefaults::new(
98 version,
99 serialize_type,
100 ))
101 .create_request_command(code, header)
102 }
103
104 pub fn create_remoting_command(code: impl Into<i32>) -> Self {
105 application_remoting_command_factory().create_remoting_command(code)
106 }
107
108 pub fn get_and_add() -> i32 {
109 next_request_id()
110 }
111
112 pub fn create_response_command_with_code(code: impl Into<i32>) -> Self {
113 application_remoting_command_factory().create_response_command_with_code(code)
114 }
115
116 pub fn create_response_command_with_code_and_header(
118 code: impl Into<i32>,
119 header: impl CommandCustomHeader + Sync + Send + 'static,
120 ) -> Self {
121 application_remoting_command_factory().create_response_command_with_code_and_header(code, header)
122 }
123
124 pub fn create_response_command_with_code_remark(code: impl Into<i32>, remark: impl Into<CheetahString>) -> Self {
125 application_remoting_command_factory().create_response_command_with_code_remark(code, remark)
126 }
127
128 pub fn create_success_response_command() -> Self {
130 application_remoting_command_factory().create_success_response_command()
131 }
132
133 pub fn create_success_response_command_with_header(
135 header: impl CommandCustomHeader + Sync + Send + 'static,
136 ) -> Self {
137 application_remoting_command_factory().create_success_response_command_with_header(header)
138 }
139
140 pub fn create_java_default_error_response_command() -> Self {
142 application_remoting_command_factory().create_java_default_error_response_command()
143 }
144
145 pub fn create_java_default_error_response_command_with_header(
147 header: impl CommandCustomHeader + Sync + Send + 'static,
148 ) -> Self {
149 application_remoting_command_factory().create_java_default_error_response_command_with_header(header)
150 }
151
152 pub fn create_new_request_id() -> i32 {
153 next_request_id()
154 }
155}