22
22
import io .rsocket .DuplexConnection ;
23
23
import io .rsocket .RSocketErrorException ;
24
24
import io .rsocket .frame .FrameHeaderCodec ;
25
- import io .rsocket .frame .FrameUtil ;
26
25
import io .rsocket .plugins .DuplexConnectionInterceptor .Type ;
27
26
import io .rsocket .plugins .InitializingInterceptorRegistry ;
28
27
import java .net .SocketAddress ;
29
28
import java .util .concurrent .atomic .AtomicIntegerFieldUpdater ;
30
29
import org .reactivestreams .Subscription ;
31
- import org .slf4j .Logger ;
32
- import org .slf4j .LoggerFactory ;
33
30
import reactor .core .CoreSubscriber ;
34
31
import reactor .core .publisher .Flux ;
35
32
import reactor .core .publisher .Mono ;
50
47
*/
51
48
class ClientServerInputMultiplexer implements CoreSubscriber <ByteBuf >, Closeable {
52
49
53
- private static final Logger LOGGER = LoggerFactory .getLogger ("io.rsocket.FrameLogger" );
54
- private static final InitializingInterceptorRegistry emptyInterceptorRegistry =
55
- new InitializingInterceptorRegistry ();
56
-
57
- private final InternalDuplexConnection setupReceiver ;
58
50
private final InternalDuplexConnection serverReceiver ;
59
51
private final InternalDuplexConnection clientReceiver ;
60
- private final DuplexConnection setupConnection ;
61
52
private final DuplexConnection serverConnection ;
62
53
private final DuplexConnection clientConnection ;
63
54
private final DuplexConnection source ;
64
55
private final boolean isClient ;
65
56
66
57
private Subscription s ;
67
- private boolean setupReceived ;
68
58
69
59
private Throwable t ;
70
60
71
61
private volatile int state ;
72
62
private static final AtomicIntegerFieldUpdater <ClientServerInputMultiplexer > STATE =
73
63
AtomicIntegerFieldUpdater .newUpdater (ClientServerInputMultiplexer .class , "state" );
74
64
75
- public ClientServerInputMultiplexer (DuplexConnection source ) {
76
- this (source , emptyInterceptorRegistry , false );
77
- }
78
-
79
65
public ClientServerInputMultiplexer (
80
66
DuplexConnection source , InitializingInterceptorRegistry registry , boolean isClient ) {
81
67
this .source = source ;
82
68
this .isClient = isClient ;
83
- source = registry .initConnection (Type .SOURCE , source );
84
69
85
- if (!isClient ) {
86
- setupReceiver = new InternalDuplexConnection (this , source );
87
- setupConnection = registry .initConnection (Type .SETUP , setupReceiver );
88
- } else {
89
- setupReceiver = null ;
90
- setupConnection = null ;
91
- }
92
- serverReceiver = new InternalDuplexConnection (this , source );
93
- clientReceiver = new InternalDuplexConnection (this , source );
94
- serverConnection = registry .initConnection (Type .SERVER , serverReceiver );
95
- clientConnection = registry .initConnection (Type .CLIENT , clientReceiver );
70
+ this .serverReceiver = new InternalDuplexConnection (this , source );
71
+ this .clientReceiver = new InternalDuplexConnection (this , source );
72
+ this .serverConnection = registry .initConnection (Type .SERVER , serverReceiver );
73
+ this .clientConnection = registry .initConnection (Type .CLIENT , clientReceiver );
96
74
}
97
75
98
- DuplexConnection asClientServerConnection () {
99
- return source ;
100
- }
101
-
102
- DuplexConnection asServerConnection () {
76
+ DuplexConnection asResponderConnection () {
103
77
return serverConnection ;
104
78
}
105
79
106
- DuplexConnection asClientConnection () {
80
+ DuplexConnection asRequesterConnection () {
107
81
return clientConnection ;
108
82
}
109
83
110
- DuplexConnection asSetupConnection () {
111
- return setupConnection ;
112
- }
113
-
114
84
@ Override
115
85
public void dispose () {
116
86
source .dispose ();
@@ -130,12 +100,7 @@ public Mono<Void> onClose() {
130
100
public void onSubscribe (Subscription s ) {
131
101
if (Operators .validate (this .s , s )) {
132
102
this .s = s ;
133
- if (isClient ) {
134
- s .request (Long .MAX_VALUE );
135
- } else {
136
- // request first SetupFrame
137
- s .request (1 );
138
- }
103
+ s .request (Long .MAX_VALUE );
139
104
}
140
105
}
141
106
@@ -145,12 +110,6 @@ public void onNext(ByteBuf frame) {
145
110
final Type type ;
146
111
if (streamId == 0 ) {
147
112
switch (FrameHeaderCodec .frameType (frame )) {
148
- case SETUP :
149
- case RESUME :
150
- case RESUME_OK :
151
- type = Type .SETUP ;
152
- setupReceived = true ;
153
- break ;
154
113
case LEASE :
155
114
case KEEPALIVE :
156
115
case ERROR :
@@ -164,19 +123,8 @@ public void onNext(ByteBuf frame) {
164
123
} else {
165
124
type = Type .CLIENT ;
166
125
}
167
- if (!isClient && type != Type .SETUP && !setupReceived ) {
168
- final IllegalStateException error =
169
- new IllegalStateException ("SETUP or LEASE frame must be received before any others." );
170
- this .s .cancel ();
171
- onError (error );
172
- }
173
126
174
127
switch (type ) {
175
- case SETUP :
176
- final InternalDuplexConnection setupReceiver = this .setupReceiver ;
177
- setupReceiver .onNext (frame );
178
- setupReceiver .onComplete ();
179
- break ;
180
128
case CLIENT :
181
129
clientReceiver .onNext (frame );
182
130
break ;
@@ -193,16 +141,6 @@ public void onComplete() {
193
141
return ;
194
142
}
195
143
196
- if (!isClient ) {
197
- if (!setupReceived ) {
198
- setupReceiver .onComplete ();
199
- }
200
-
201
- if (previousState == 1 ) {
202
- return ;
203
- }
204
- }
205
-
206
144
if (clientReceiver .isSubscribed ()) {
207
145
clientReceiver .onComplete ();
208
146
}
@@ -220,16 +158,6 @@ public void onError(Throwable t) {
220
158
return ;
221
159
}
222
160
223
- if (!isClient ) {
224
- if (!setupReceived ) {
225
- setupReceiver .onError (t );
226
- }
227
-
228
- if (previousState == 1 ) {
229
- return ;
230
- }
231
- }
232
-
233
161
if (clientReceiver .isSubscribed ()) {
234
162
clientReceiver .onError (t );
235
163
}
@@ -244,17 +172,8 @@ boolean notifyRequested() {
244
172
return false ;
245
173
}
246
174
247
- if (isClient ) {
248
- if (currentState == 2 ) {
249
- source .receive ().subscribe (this );
250
- }
251
- } else {
252
- if (currentState == 1 ) {
253
- source .receive ().subscribe (this );
254
- } else if (currentState == 3 ) {
255
- // means setup was consumed and we got request from client and server multiplexers
256
- s .request (Long .MAX_VALUE );
257
- }
175
+ if (currentState == 2 ) {
176
+ source .receive ().subscribe (this );
258
177
}
259
178
260
179
return true ;
@@ -280,7 +199,6 @@ private static class InternalDuplexConnection extends Flux<ByteBuf>
280
199
implements Subscription , DuplexConnection {
281
200
private final ClientServerInputMultiplexer clientServerInputMultiplexer ;
282
201
private final DuplexConnection source ;
283
- private final boolean debugEnabled ;
284
202
285
203
private volatile int state ;
286
204
static final AtomicIntegerFieldUpdater <InternalDuplexConnection > STATE =
@@ -292,7 +210,6 @@ public InternalDuplexConnection(
292
210
ClientServerInputMultiplexer clientServerInputMultiplexer , DuplexConnection source ) {
293
211
this .clientServerInputMultiplexer = clientServerInputMultiplexer ;
294
212
this .source = source ;
295
- this .debugEnabled = LOGGER .isDebugEnabled ();
296
213
}
297
214
298
215
@ Override
@@ -340,30 +257,18 @@ void onError(Throwable t) {
340
257
}
341
258
342
259
@ Override
343
- public void sendFrame (int streamId , ByteBuf frame , boolean prioritize ) {
344
- if (debugEnabled ) {
345
- LOGGER .debug ("sending -> " + FrameUtil .toString (frame ));
346
- }
347
-
348
- source .sendFrame (streamId , frame , prioritize );
260
+ public void sendFrame (int streamId , ByteBuf frame ) {
261
+ source .sendFrame (streamId , frame );
349
262
}
350
263
351
264
@ Override
352
- public void terminate (ByteBuf frame , RSocketErrorException terminalError ) {
353
- if (debugEnabled ) {
354
- LOGGER .debug ("sending -> " + FrameUtil .toString (frame ));
355
- }
356
-
357
- source .terminate (frame , terminalError );
265
+ public void sendErrorAndClose (RSocketErrorException e ) {
266
+ source .sendErrorAndClose (e );
358
267
}
359
268
360
269
@ Override
361
270
public Flux <ByteBuf > receive () {
362
- if (debugEnabled ) {
363
- return this .doOnNext (frame -> LOGGER .debug ("receiving -> " + FrameUtil .toString (frame )));
364
- } else {
365
- return this ;
366
- }
271
+ return this ;
367
272
}
368
273
369
274
@ Override
0 commit comments