Dreamine.Communication.Core 1.0.2
Dreamine.Communication.Core 통신 기능과 관련 API를 제공합니다.
로딩중...
검색중...
일치하는것 없음
PlainTextProtocolAdapter.cs
이 파일의 문서화 페이지로 가기
1using System.Text;
2using Dreamine.Communication.Abstractions.Interfaces;
3using Dreamine.Communication.Abstractions.Models;
4
6
15public sealed class PlainTextProtocolAdapter : IMessageProtocolAdapter
16{
25 private readonly Encoding _encoding;
34 private readonly string _route;
43 private readonly string _name;
44
54 : this(new PlainTextProtocolOptions())
55 {
56 }
57
107 Encoding encoding,
108 string route,
109 string name)
110 : this(new PlainTextProtocolOptions
111 {
112 Encoding = encoding ?? throw new ArgumentNullException(nameof(encoding)),
113 Route = route,
114 Name = name
115 })
116 {
117 }
118
152 {
153 ArgumentNullException.ThrowIfNull(options);
154 ArgumentException.ThrowIfNullOrWhiteSpace(options.Route);
155 ArgumentException.ThrowIfNullOrWhiteSpace(options.Name);
156
157 _encoding = options.Encoding ?? throw new ArgumentNullException(nameof(options.Encoding));
158 _route = options.Route;
159 _name = options.Name;
161 }
162
171 public bool NormalizePayloadToUtf8 { get; }
172
213 public MessageEnvelope Decode(byte[] payload)
214 {
215 ArgumentNullException.ThrowIfNull(payload);
216
217 var text = _encoding.GetString(payload);
218
219 return new MessageEnvelope
220 {
221 Name = _name,
222 Route = _route,
223 Payload = NormalizePayloadToUtf8
224 ? Encoding.UTF8.GetBytes(text)
225 : _encoding.GetBytes(text),
226 Headers = new Dictionary<string, string>
227 {
228 ["ContentType"] = "text/plain",
229 ["Protocol"] = "PlainText",
230 ["ExternalEncoding"] = _encoding.WebName
231 }
232 };
233 }
234
267 public byte[] Encode(MessageEnvelope message)
268 {
269 ArgumentNullException.ThrowIfNull(message);
270
271 if (message.Payload.Length == 0)
272 {
273 return [];
274 }
275
276 var text = Encoding.UTF8.GetString(message.Payload);
277 return _encoding.GetBytes(text);
278 }
279}
PlainTextProtocolAdapter(Encoding encoding, string route, string name)