1
1
using System . Buffers ;
2
- using System . Buffers . Text ;
3
2
using System . Collections . Concurrent ;
4
- using System . Text ;
5
3
6
4
namespace NATS . Client . Core . Internal ;
7
5
8
- internal sealed class ReplyTask < T > : ReplyTaskBase , IDisposable
6
+ internal sealed class ReplyTask : ReplyTaskBase , IDisposable
9
7
{
10
8
private readonly object _gate ;
11
9
private readonly ReplyTaskFactory _factory ;
12
- private readonly long _id ;
13
- private readonly NatsConnection _connection ;
14
- private readonly INatsDeserialize < T > _deserializer ;
10
+ private readonly long _subjectNum ;
15
11
private readonly TimeSpan _requestTimeout ;
16
12
private readonly TaskCompletionSource _tcs ;
17
- private NatsMsg < T > _msg ;
13
+ private NatsRecievedEvent _msg ;
18
14
19
- public ReplyTask ( ReplyTaskFactory factory , long id , string subject , NatsConnection connection , INatsDeserialize < T > deserializer , TimeSpan requestTimeout )
15
+ public ReplyTask ( ReplyTaskFactory factory , long subjectNum , TimeSpan requestTimeout )
20
16
{
21
17
_factory = factory ;
22
- _id = id ;
23
- Subject = subject ;
24
- _connection = connection ;
25
- _deserializer = deserializer ;
18
+ _subjectNum = subjectNum ;
26
19
_requestTimeout = requestTimeout ;
27
20
_tcs = new TaskCompletionSource ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
28
21
_gate = new object ( ) ;
29
22
}
30
23
31
- public string Subject { get ; }
32
-
33
- public async ValueTask < NatsMsg < T > > GetResultAsync ( CancellationToken cancellationToken )
24
+ public async ValueTask < NatsRecievedEvent > GetResultAsync ( CancellationToken cancellationToken )
34
25
{
35
26
try
36
27
{
@@ -53,13 +44,13 @@ public override void SetResult(NatsProcessProps props, ReadOnlySequence<byte> pa
53
44
{
54
45
lock ( _gate )
55
46
{
56
- _msg = NatsMsg < T > . Build ( Subject , props . ReplyTo , headersBuffer , payload , _connection , _connection . HeaderParser , _deserializer ) ;
47
+ _msg = new NatsRecievedEvent ( props , headersBuffer , payload ) ;
57
48
}
58
49
59
50
_tcs . TrySetResult ( ) ;
60
51
}
61
52
62
- public void Dispose ( ) => _factory . Return ( _id ) ;
53
+ public void Dispose ( ) => _factory . Return ( _subjectNum ) ;
63
54
}
64
55
65
56
internal abstract class ReplyTaskBase
@@ -69,55 +60,18 @@ internal abstract class ReplyTaskBase
69
60
70
61
internal sealed class ReplyTaskFactory
71
62
{
72
- private readonly byte [ ] _inboxPrefix ;
73
- private readonly string _inboxPrefixString ;
74
- private readonly NatsConnection _connection ;
75
63
private readonly ConcurrentDictionary < long , ReplyTaskBase > _replies ;
76
- private readonly INatsSerializerRegistry _serializerRegistry ;
77
- private readonly TimeSpan _requestTimeout ;
78
- private readonly int _subjectMaxLength ;
79
- private readonly bool _allocSubject ;
80
64
private long _nextId ;
81
65
82
- public ReplyTaskFactory ( NatsConnection connection )
66
+ public ReplyTaskFactory ( )
83
67
{
84
- _connection = connection ;
85
- _inboxPrefixString = _connection . InboxPrefix + "." ;
86
- _inboxPrefix = Encoding . UTF8 . GetBytes ( _inboxPrefixString ) ;
87
- _subjectMaxLength = _inboxPrefix . Length + 20 ; // 20 digits for long
88
- _allocSubject = _subjectMaxLength < 128 ;
89
- _serializerRegistry = _connection . Opts . SerializerRegistry ;
90
- _requestTimeout = _connection . Opts . RequestTimeout ;
91
68
_replies = new ConcurrentDictionary < long , ReplyTaskBase > ( ) ;
92
69
}
93
70
94
- public ReplyTask < TReply > CreateReplyTask < TReply > ( INatsDeserialize < TReply > ? deserializer , TimeSpan ? requestTimeout )
71
+ public ReplyTask CreateReplyTask ( TimeSpan requestTimeout )
95
72
{
96
- deserializer ??= _serializerRegistry . GetDeserializer < TReply > ( ) ;
97
73
var id = Interlocked . Increment ( ref _nextId ) ;
98
-
99
- string subject ;
100
- if ( _allocSubject )
101
- {
102
- Span < byte > buffer = stackalloc byte [ _subjectMaxLength ] ;
103
- _inboxPrefix . CopyTo ( buffer ) ;
104
- var idSpan = buffer . Slice ( _inboxPrefix . Length ) ;
105
- if ( Utf8Formatter . TryFormat ( id , idSpan , out var written ) )
106
- {
107
- var subjectSpan = buffer . Slice ( 0 , written + _inboxPrefix . Length ) ;
108
- subject = Encoding . UTF8 . GetString ( subjectSpan ) ;
109
- }
110
- else
111
- {
112
- subject = _inboxPrefixString + id ;
113
- }
114
- }
115
- else
116
- {
117
- subject = _inboxPrefixString + id ;
118
- }
119
-
120
- var rt = new ReplyTask < TReply > ( this , id , subject , _connection , deserializer , requestTimeout ?? _requestTimeout ) ;
74
+ var rt = new ReplyTask ( this , id , requestTimeout ) ;
121
75
_replies . TryAdd ( id , rt ) ;
122
76
return rt ;
123
77
}
@@ -126,7 +80,7 @@ public ReplyTask<TReply> CreateReplyTask<TReply>(INatsDeserialize<TReply>? deser
126
80
127
81
public bool TrySetResult ( NatsProcessProps props , in ReadOnlySequence < byte > payloadBuffer , in ReadOnlySequence < byte > ? headersBuffer )
128
82
{
129
- if ( _replies . TryGetValue ( props . InboxId , out var rt ) )
83
+ if ( _replies . TryGetValue ( props . SubjectNumber , out var rt ) )
130
84
{
131
85
rt . SetResult ( props , payloadBuffer , headersBuffer ) ;
132
86
return true ;
0 commit comments