19
19
import static io .rsocket .frame .FrameHeaderFlyweight .FLAGS_M ;
20
20
21
21
import io .netty .buffer .*;
22
+ import io .netty .util .AbstractReferenceCounted ;
22
23
import io .netty .util .IllegalReferenceCountException ;
23
24
import io .netty .util .Recycler ;
24
25
import io .netty .util .Recycler .Handle ;
33
34
import io .rsocket .frame .VersionFlyweight ;
34
35
import io .rsocket .framing .FrameType ;
35
36
import java .nio .charset .StandardCharsets ;
36
- import java .util .Objects ;
37
37
import javax .annotation .Nullable ;
38
38
import org .slf4j .Logger ;
39
39
import org .slf4j .LoggerFactory ;
43
43
*
44
44
* <p>This provides encoding, decoding and field accessors.
45
45
*/
46
- public class Frame implements Payload , ByteBufHolder {
46
+ public class Frame extends AbstractReferenceCounted implements Payload , ByteBufHolder {
47
47
private static final Recycler <Frame > RECYCLER =
48
48
new Recycler <Frame >() {
49
49
protected Frame newObject (Handle <Frame > handle ) {
@@ -58,12 +58,6 @@ private Frame(final Handle<Frame> handle) {
58
58
this .handle = handle ;
59
59
}
60
60
61
- /** Clear and recycle this instance. */
62
- private void recycle () {
63
- content = null ;
64
- handle .recycle (this );
65
- }
66
-
67
61
/** Return the content which is held by this {@link Frame}. */
68
62
@ Override
69
63
public ByteBuf content () {
@@ -105,26 +99,17 @@ public Frame replace(ByteBuf content) {
105
99
return from (content );
106
100
}
107
101
108
- /**
109
- * Returns the reference count of this object. If {@code 0}, it means this object has been
110
- * deallocated.
111
- */
112
- @ Override
113
- public int refCnt () {
114
- return content .refCnt ();
115
- }
116
-
117
102
/** Increases the reference count by {@code 1}. */
118
103
@ Override
119
104
public Frame retain () {
120
- content .retain ();
105
+ super .retain ();
121
106
return this ;
122
107
}
123
108
124
109
/** Increases the reference count by the specified {@code increment}. */
125
110
@ Override
126
111
public Frame retain (int increment ) {
127
- content .retain (increment );
112
+ super .retain (increment );
128
113
return this ;
129
114
}
130
115
@@ -150,36 +135,12 @@ public Frame touch(@Nullable Object hint) {
150
135
return this ;
151
136
}
152
137
153
- /**
154
- * Decreases the reference count by {@code 1} and deallocates this object if the reference count
155
- * reaches at {@code 0}.
156
- *
157
- * @return {@code true} if and only if the reference count became {@code 0} and this object has
158
- * been deallocated
159
- */
160
- @ Override
161
- public boolean release () {
162
- if (content != null && content .release ()) {
163
- recycle ();
164
- return true ;
165
- }
166
- return false ;
167
- }
168
-
169
- /**
170
- * Decreases the reference count by the specified {@code decrement} and deallocates this object if
171
- * the reference count reaches at {@code 0}.
172
- *
173
- * @return {@code true} if and only if the reference count became {@code 0} and this object has
174
- * been deallocated
175
- */
138
+ /** Called once {@link #refCnt()} is equals 0. */
176
139
@ Override
177
- public boolean release (int decrement ) {
178
- if (content != null && content .release (decrement )) {
179
- recycle ();
180
- return true ;
181
- }
182
- return false ;
140
+ protected void deallocate () {
141
+ content .release ();
142
+ content = null ;
143
+ handle .recycle (this );
183
144
}
184
145
185
146
/**
@@ -239,6 +200,7 @@ public int flags() {
239
200
*/
240
201
public static Frame from (final ByteBuf content ) {
241
202
final Frame frame = RECYCLER .get ();
203
+ frame .setRefCnt (1 );
242
204
frame .content = content ;
243
205
244
206
return frame ;
@@ -281,6 +243,7 @@ public static Frame from(
281
243
final ByteBuf data = payload .sliceData ();
282
244
283
245
final Frame frame = RECYCLER .get ();
246
+ frame .setRefCnt (1 );
284
247
frame .content =
285
248
ByteBufAllocator .DEFAULT .buffer (
286
249
SetupFrameFlyweight .computeFrameLength (
@@ -347,6 +310,7 @@ public static Frame from(int streamId, final Throwable throwable, ByteBuf dataBu
347
310
348
311
final int code = ErrorFrameFlyweight .errorCodeFromException (throwable );
349
312
final Frame frame = RECYCLER .get ();
313
+ frame .setRefCnt (1 );
350
314
frame .content =
351
315
ByteBufAllocator .DEFAULT .buffer (
352
316
ErrorFrameFlyweight .computeFrameLength (dataBuffer .readableBytes ()));
@@ -378,6 +342,7 @@ private Lease() {}
378
342
379
343
public static Frame from (int ttl , int numberOfRequests , ByteBuf metadata ) {
380
344
final Frame frame = RECYCLER .get ();
345
+ frame .setRefCnt (1 );
381
346
frame .content =
382
347
ByteBufAllocator .DEFAULT .buffer (
383
348
LeaseFrameFlyweight .computeFrameLength (metadata .readableBytes ()));
@@ -411,6 +376,7 @@ public static Frame from(int streamId, int requestN) {
411
376
}
412
377
413
378
final Frame frame = RECYCLER .get ();
379
+ frame .setRefCnt (1 );
414
380
frame .content = ByteBufAllocator .DEFAULT .buffer (RequestNFrameFlyweight .computeFrameLength ());
415
381
frame .content .writerIndex (RequestNFrameFlyweight .encode (frame .content , streamId , requestN ));
416
382
return frame ;
@@ -438,6 +404,7 @@ public static Frame from(int streamId, FrameType type, Payload payload, int init
438
404
final ByteBuf data = payload .sliceData ();
439
405
440
406
final Frame frame = RECYCLER .get ();
407
+ frame .setRefCnt (1 );
441
408
frame .content =
442
409
ByteBufAllocator .DEFAULT .buffer (
443
410
RequestFrameFlyweight .computeFrameLength (
@@ -464,6 +431,7 @@ public static Frame from(int streamId, FrameType type, Payload payload, int init
464
431
465
432
public static Frame from (int streamId , FrameType type , int flags ) {
466
433
final Frame frame = RECYCLER .get ();
434
+ frame .setRefCnt (1 );
467
435
frame .content =
468
436
ByteBufAllocator .DEFAULT .buffer (RequestFrameFlyweight .computeFrameLength (type , null , 0 ));
469
437
frame .content .writerIndex (
@@ -480,6 +448,7 @@ public static Frame from(
480
448
int initialRequestN ,
481
449
int flags ) {
482
450
final Frame frame = RECYCLER .get ();
451
+ frame .setRefCnt (1 );
483
452
frame .content =
484
453
ByteBufAllocator .DEFAULT .buffer (
485
454
RequestFrameFlyweight .computeFrameLength (
@@ -543,6 +512,7 @@ public static Frame from(int streamId, FrameType type, Payload payload, int flag
543
512
public static Frame from (
544
513
int streamId , FrameType type , @ Nullable ByteBuf metadata , ByteBuf data , int flags ) {
545
514
final Frame frame = RECYCLER .get ();
515
+ frame .setRefCnt (1 );
546
516
frame .content =
547
517
ByteBufAllocator .DEFAULT .buffer (
548
518
FrameHeaderFlyweight .computeFrameHeaderLength (
@@ -559,6 +529,7 @@ private Cancel() {}
559
529
560
530
public static Frame from (int streamId ) {
561
531
final Frame frame = RECYCLER .get ();
532
+ frame .setRefCnt (1 );
562
533
frame .content =
563
534
ByteBufAllocator .DEFAULT .buffer (
564
535
FrameHeaderFlyweight .computeFrameHeaderLength (FrameType .CANCEL , null , 0 ));
@@ -575,6 +546,7 @@ private Keepalive() {}
575
546
576
547
public static Frame from (ByteBuf data , boolean respond ) {
577
548
final Frame frame = RECYCLER .get ();
549
+ frame .setRefCnt (1 );
578
550
frame .content =
579
551
ByteBufAllocator .DEFAULT .buffer (
580
552
KeepaliveFrameFlyweight .computeFrameLength (data .readableBytes ()));
@@ -611,12 +583,12 @@ public boolean equals(Object o) {
611
583
return false ;
612
584
}
613
585
final Frame frame = (Frame ) o ;
614
- return Objects .equals (content , frame .content );
586
+ return content .equals (frame .content () );
615
587
}
616
588
617
589
@ Override
618
590
public int hashCode () {
619
- return Objects . hash ( content );
591
+ return content . hashCode ( );
620
592
}
621
593
622
594
@ Override
0 commit comments