2020import io .netty .buffer .ByteBufAllocator ;
2121import io .rsocket .Closeable ;
2222import io .rsocket .DuplexConnection ;
23+ import io .rsocket .RSocketErrorException ;
2324import io .rsocket .frame .FrameHeaderCodec ;
24- import io .rsocket .frame .FrameUtil ;
2525import io .rsocket .plugins .DuplexConnectionInterceptor .Type ;
2626import io .rsocket .plugins .InitializingInterceptorRegistry ;
2727import java .net .SocketAddress ;
2828import java .util .concurrent .atomic .AtomicIntegerFieldUpdater ;
29- import org .reactivestreams .Publisher ;
3029import org .reactivestreams .Subscription ;
31- import org .slf4j .Logger ;
32- import org .slf4j .LoggerFactory ;
3330import reactor .core .CoreSubscriber ;
3431import reactor .core .publisher .Flux ;
3532import reactor .core .publisher .Mono ;
5047 */
5148class ClientServerInputMultiplexer implements CoreSubscriber <ByteBuf >, Closeable {
5249
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 ;
5850 private final InternalDuplexConnection serverReceiver ;
5951 private final InternalDuplexConnection clientReceiver ;
60- private final DuplexConnection setupConnection ;
6152 private final DuplexConnection serverConnection ;
6253 private final DuplexConnection clientConnection ;
6354 private final DuplexConnection source ;
6455 private final boolean isClient ;
6556
6657 private Subscription s ;
67- private boolean setupReceived ;
6858
6959 private Throwable t ;
7060
7161 private volatile int state ;
7262 private static final AtomicIntegerFieldUpdater <ClientServerInputMultiplexer > STATE =
7363 AtomicIntegerFieldUpdater .newUpdater (ClientServerInputMultiplexer .class , "state" );
7464
75- public ClientServerInputMultiplexer (DuplexConnection source ) {
76- this (source , emptyInterceptorRegistry , false );
77- }
78-
7965 public ClientServerInputMultiplexer (
8066 DuplexConnection source , InitializingInterceptorRegistry registry , boolean isClient ) {
8167 this .source = source ;
8268 this .isClient = isClient ;
83- source = registry .initConnection (Type .SOURCE , source );
8469
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 );
9674 }
9775
98- public DuplexConnection asClientServerConnection () {
99- return source ;
100- }
101-
102- public DuplexConnection asServerConnection () {
76+ DuplexConnection asServerConnection () {
10377 return serverConnection ;
10478 }
10579
106- public DuplexConnection asClientConnection () {
80+ DuplexConnection asClientConnection () {
10781 return clientConnection ;
10882 }
10983
110- public DuplexConnection asSetupConnection () {
111- return setupConnection ;
112- }
113-
11484 @ Override
11585 public void dispose () {
11686 source .dispose ();
@@ -130,12 +100,7 @@ public Mono<Void> onClose() {
130100 public void onSubscribe (Subscription s ) {
131101 if (Operators .validate (this .s , s )) {
132102 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 );
139104 }
140105 }
141106
@@ -145,12 +110,6 @@ public void onNext(ByteBuf frame) {
145110 final Type type ;
146111 if (streamId == 0 ) {
147112 switch (FrameHeaderCodec .frameType (frame )) {
148- case SETUP :
149- case RESUME :
150- case RESUME_OK :
151- type = Type .SETUP ;
152- setupReceived = true ;
153- break ;
154113 case LEASE :
155114 case KEEPALIVE :
156115 case ERROR :
@@ -164,19 +123,8 @@ public void onNext(ByteBuf frame) {
164123 } else {
165124 type = Type .CLIENT ;
166125 }
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- }
173126
174127 switch (type ) {
175- case SETUP :
176- final InternalDuplexConnection setupReceiver = this .setupReceiver ;
177- setupReceiver .onNext (frame );
178- setupReceiver .onComplete ();
179- break ;
180128 case CLIENT :
181129 clientReceiver .onNext (frame );
182130 break ;
@@ -193,16 +141,6 @@ public void onComplete() {
193141 return ;
194142 }
195143
196- if (!isClient ) {
197- if (!setupReceived ) {
198- setupReceiver .onComplete ();
199- }
200-
201- if (previousState == 1 ) {
202- return ;
203- }
204- }
205-
206144 if (clientReceiver .isSubscribed ()) {
207145 clientReceiver .onComplete ();
208146 }
@@ -220,16 +158,6 @@ public void onError(Throwable t) {
220158 return ;
221159 }
222160
223- if (!isClient ) {
224- if (!setupReceived ) {
225- setupReceiver .onError (t );
226- }
227-
228- if (previousState == 1 ) {
229- return ;
230- }
231- }
232-
233161 if (clientReceiver .isSubscribed ()) {
234162 clientReceiver .onError (t );
235163 }
@@ -244,17 +172,8 @@ boolean notifyRequested() {
244172 return false ;
245173 }
246174
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 );
258177 }
259178
260179 return true ;
@@ -280,7 +199,6 @@ private static class InternalDuplexConnection extends Flux<ByteBuf>
280199 implements Subscription , DuplexConnection {
281200 private final ClientServerInputMultiplexer clientServerInputMultiplexer ;
282201 private final DuplexConnection source ;
283- private final boolean debugEnabled ;
284202
285203 private volatile int state ;
286204 static final AtomicIntegerFieldUpdater <InternalDuplexConnection > STATE =
@@ -292,7 +210,6 @@ public InternalDuplexConnection(
292210 ClientServerInputMultiplexer clientServerInputMultiplexer , DuplexConnection source ) {
293211 this .clientServerInputMultiplexer = clientServerInputMultiplexer ;
294212 this .source = source ;
295- this .debugEnabled = LOGGER .isDebugEnabled ();
296213 }
297214
298215 @ Override
@@ -340,32 +257,18 @@ void onError(Throwable t) {
340257 }
341258
342259 @ Override
343- public Mono <Void > send (Publisher <ByteBuf > frame ) {
344- if (debugEnabled ) {
345- return Flux .from (frame )
346- .doOnNext (f -> LOGGER .debug ("sending -> " + FrameUtil .toString (f )))
347- .as (source ::send );
348- }
349-
350- return source .send (frame );
260+ public void sendFrame (int streamId , ByteBuf frame ) {
261+ source .sendFrame (streamId , frame );
351262 }
352263
353264 @ Override
354- public Mono <Void > sendOne (ByteBuf frame ) {
355- if (debugEnabled ) {
356- LOGGER .debug ("sending -> " + FrameUtil .toString (frame ));
357- }
358-
359- return source .sendOne (frame );
265+ public void sendErrorAndClose (RSocketErrorException e ) {
266+ source .sendErrorAndClose (e );
360267 }
361268
362269 @ Override
363270 public Flux <ByteBuf > receive () {
364- if (debugEnabled ) {
365- return this .doOnNext (frame -> LOGGER .debug ("receiving -> " + FrameUtil .toString (frame )));
366- } else {
367- return this ;
368- }
271+ return this ;
369272 }
370273
371274 @ Override
0 commit comments