@@ -148,31 +148,33 @@ public synchronized void onUnbound(Status reason) {
148148 @ Override
149149 public synchronized Runnable start (Listener clientTransportListener ) {
150150 this .clientTransportListener = checkNotNull (clientTransportListener );
151- return () -> {
152- synchronized (BinderClientTransport .this ) {
153- if (inState (TransportState .NOT_STARTED )) {
154- setState (TransportState .SETUP );
155- try {
156- if (preAuthorizeServer ) {
157- preAuthorize (serviceBinding .resolve ());
158- } else {
159- serviceBinding .bind ();
160- }
161- } catch (StatusException e ) {
162- shutdownInternal (e .getStatus (), true );
163- return ;
164- }
165- if (readyTimeoutMillis >= 0 ) {
166- readyTimeoutFuture =
167- getScheduledExecutorService ()
168- .schedule (
169- BinderClientTransport .this ::onReadyTimeout ,
170- readyTimeoutMillis ,
171- MILLISECONDS );
172- }
173- }
151+ return this ::postStartRunnable ;
152+ }
153+
154+ private synchronized void postStartRunnable () {
155+ if (!inState (TransportState .NOT_STARTED )) {
156+ return ;
157+ }
158+
159+ setState (TransportState .SETUP );
160+
161+ try {
162+ if (preAuthorizeServer ) {
163+ preAuthorize (serviceBinding .resolve ());
164+ } else {
165+ serviceBinding .bind ();
174166 }
175- };
167+ } catch (StatusException e ) {
168+ shutdownInternal (e .getStatus (), true );
169+ return ;
170+ }
171+
172+ if (readyTimeoutMillis >= 0 ) {
173+ readyTimeoutFuture =
174+ getScheduledExecutorService ()
175+ .schedule (
176+ BinderClientTransport .this ::onReadyTimeout , readyTimeoutMillis , MILLISECONDS );
177+ }
176178 }
177179
178180 @ GuardedBy ("this" )
@@ -204,13 +206,16 @@ public void onFailure(Throwable t) {
204206 }
205207
206208 private synchronized void handlePreAuthResult (Status authorization ) {
207- if (inState (TransportState .SETUP )) {
208- if (!authorization .isOk ()) {
209- shutdownInternal (authorization , true );
210- } else {
211- serviceBinding .bind ();
212- }
209+ if (!inState (TransportState .SETUP )) {
210+ return ;
211+ }
212+
213+ if (!authorization .isOk ()) {
214+ shutdownInternal (authorization , true );
215+ return ;
213216 }
217+
218+ serviceBinding .bind ();
214219 }
215220
216221 private synchronized void onReadyTimeout () {
@@ -252,17 +257,17 @@ public synchronized ClientStream newStream(
252257 Status failure = Status .INTERNAL .withDescription ("Clashing call IDs" );
253258 shutdownInternal (failure , true );
254259 return newFailingClientStream (failure , attributes , headers , tracers );
260+ }
261+
262+ if (inbound .countsForInUse () && numInUseStreams .getAndIncrement () == 0 ) {
263+ clientTransportListener .transportInUse (true );
264+ }
265+ Outbound .ClientOutbound outbound =
266+ new Outbound .ClientOutbound (this , callId , method , headers , statsTraceContext );
267+ if (method .getType ().clientSendsOneMessage ()) {
268+ return new SingleMessageClientStream (inbound , outbound , attributes );
255269 } else {
256- if (inbound .countsForInUse () && numInUseStreams .getAndIncrement () == 0 ) {
257- clientTransportListener .transportInUse (true );
258- }
259- Outbound .ClientOutbound outbound =
260- new Outbound .ClientOutbound (this , callId , method , headers , statsTraceContext );
261- if (method .getType ().clientSendsOneMessage ()) {
262- return new SingleMessageClientStream (inbound , outbound , attributes );
263- } else {
264- return new MultiMessageClientStream (inbound , outbound , attributes );
265- }
270+ return new MultiMessageClientStream (inbound , outbound , attributes );
266271 }
267272 }
268273
@@ -314,39 +319,46 @@ void notifyTerminated() {
314319 @ Override
315320 @ GuardedBy ("this" )
316321 protected void handleSetupTransport (Parcel parcel ) {
317- int remoteUid = Binder .getCallingUid ();
318- if (inState (TransportState .SETUP )) {
319- int version = parcel .readInt ();
320- IBinder binder = parcel .readStrongBinder ();
321- if (version != WIRE_FORMAT_VERSION ) {
322- shutdownInternal (Status .UNAVAILABLE .withDescription ("Wire format version mismatch" ), true );
323- } else if (binder == null ) {
324- shutdownInternal (
325- Status .UNAVAILABLE .withDescription ("Malformed SETUP_TRANSPORT data" ), true );
326- } else if (!setOutgoingBinder (OneWayBinderProxy .wrap (binder , offloadExecutor ))) {
327- shutdownInternal (
328- Status .UNAVAILABLE .withDescription ("Failed to observe outgoing binder" ), true );
329- } else {
330- restrictIncomingBinderToCallsFrom (remoteUid );
331- attributes = setSecurityAttrs (attributes , remoteUid );
332- ListenableFuture <Status > authResultFuture =
333- register (checkServerAuthorizationAsync (remoteUid ));
334- Futures .addCallback (
335- authResultFuture ,
336- new FutureCallback <Status >() {
337- @ Override
338- public void onSuccess (Status result ) {
339- handleAuthResult (result );
340- }
341-
342- @ Override
343- public void onFailure (Throwable t ) {
344- handleAuthResult (t );
345- }
346- },
347- offloadExecutor );
348- }
322+ if (!inState (TransportState .SETUP )) {
323+ return ;
324+ }
325+
326+ int version = parcel .readInt ();
327+ if (version != WIRE_FORMAT_VERSION ) {
328+ shutdownInternal (Status .UNAVAILABLE .withDescription ("Wire format version mismatch" ), true );
329+ return ;
330+ }
331+
332+ IBinder binder = parcel .readStrongBinder ();
333+ if (binder == null ) {
334+ shutdownInternal (Status .UNAVAILABLE .withDescription ("Malformed SETUP_TRANSPORT data" ), true );
335+ return ;
336+ }
337+
338+ if (!setOutgoingBinder (OneWayBinderProxy .wrap (binder , offloadExecutor ))) {
339+ shutdownInternal (
340+ Status .UNAVAILABLE .withDescription ("Failed to observe outgoing binder" ), true );
341+ return ;
349342 }
343+
344+ int remoteUid = Binder .getCallingUid ();
345+ restrictIncomingBinderToCallsFrom (remoteUid );
346+ attributes = setSecurityAttrs (attributes , remoteUid );
347+ ListenableFuture <Status > authResultFuture = register (checkServerAuthorizationAsync (remoteUid ));
348+ Futures .addCallback (
349+ authResultFuture ,
350+ new FutureCallback <Status >() {
351+ @ Override
352+ public void onSuccess (Status result ) {
353+ handleAuthResult (result );
354+ }
355+
356+ @ Override
357+ public void onFailure (Throwable t ) {
358+ handleAuthResult (t );
359+ }
360+ },
361+ offloadExecutor );
350362 }
351363
352364 private ListenableFuture <Status > checkServerAuthorizationAsync (int remoteUid ) {
@@ -356,18 +368,21 @@ private ListenableFuture<Status> checkServerAuthorizationAsync(int remoteUid) {
356368 }
357369
358370 private synchronized void handleAuthResult (Status authorization ) {
359- if (inState (TransportState .SETUP )) {
360- if (!authorization .isOk ()) {
361- shutdownInternal (authorization , true );
362- } else {
363- setState (TransportState .READY );
364- attributes = clientTransportListener .filterTransport (attributes );
365- clientTransportListener .transportReady ();
366- if (readyTimeoutFuture != null ) {
367- readyTimeoutFuture .cancel (false );
368- readyTimeoutFuture = null ;
369- }
370- }
371+ if (!inState (TransportState .SETUP )) {
372+ return ;
373+ }
374+
375+ if (!authorization .isOk ()) {
376+ shutdownInternal (authorization , true );
377+ return ;
378+ }
379+
380+ setState (TransportState .READY );
381+ attributes = clientTransportListener .filterTransport (attributes );
382+ clientTransportListener .transportReady ();
383+ if (readyTimeoutFuture != null ) {
384+ readyTimeoutFuture .cancel (false );
385+ readyTimeoutFuture = null ;
371386 }
372387 }
373388
0 commit comments