11
11
package net .sf .j2s .ajax ;
12
12
13
13
import java .io .ByteArrayOutputStream ;
14
- import java .io .IOException ;
15
14
16
15
import net .sf .j2s .ajax .HttpRequest ;
17
16
import net .sf .j2s .ajax .SimpleRPCRequest ;
@@ -136,10 +135,13 @@ public void run() {
136
135
String pipeKey = runnable .pipeKey ;
137
136
String pipeMethod = runnable .getPipeMethod ();
138
137
String pipeURL = runnable .getPipeURL ();
139
-
140
- String pipeRequestData = constructRequest (pipeKey , PIPE_TYPE_NOTIFY , runnable . pipeSequence );
138
+ long sequence = runnable . pipeSequence ;
139
+ String pipeRequestData = constructRequest (pipeKey , PIPE_TYPE_NOTIFY , sequence );
141
140
sendRequest (request , pipeMethod , pipeURL , pipeRequestData , false );
142
141
String response = request .getResponseText ();
142
+ if (response != null && runnable .notifySequence < sequence && response .indexOf ("$p1p3b$" ) != 0 ) {
143
+ runnable .notifySequence = sequence ;
144
+ }
143
145
if (response != null && response .indexOf ("\" " + PIPE_STATUS_LOST + "\" " ) != -1 ) {
144
146
SWTHelper .syncExec (disp , new Runnable () {
145
147
public void run () {
@@ -150,6 +152,9 @@ public void run() {
150
152
SimplePipeHelper .removePipe (pipeKey );
151
153
// may need to inform user that connection is already lost!
152
154
break ;
155
+ } else {
156
+ runnable .lastLiveDetected = System .currentTimeMillis ();
157
+ runnable .updateStatus (true );
153
158
}
154
159
} else {
155
160
break ;
@@ -288,10 +293,30 @@ static void swtPipeContinuum(final SimplePipeRunnable runnable) {
288
293
HttpRequest pipeRequest = getRequestWithMonitor (new HttpRequest .IXHRReceiving () {
289
294
public boolean receiving (ByteArrayOutputStream baos , byte b [], int off , int len ) {
290
295
baos .write (b , off , len );
296
+ byte [] bytes = baos .toByteArray ();
297
+ int resetIndex = 0 ;
298
+ try {
299
+ resetIndex = swtParseReceivedBytes (bytes );
300
+ } catch (RuntimeException e ) { // invalid simple format
301
+ int length = bytes .length ;
302
+ if (length < 100 ) {
303
+ System .out .println ("[ERROR]: " + new String (bytes ));
304
+ } else {
305
+ System .out .println ("[ERROR]: " + new String (bytes , 0 , 100 ) + " .." );
306
+ }
307
+ throw e ;
308
+ }
309
+ if (resetIndex > 0 ) {
310
+ baos .reset ();
311
+ if (resetIndex < bytes .length ) {
312
+ baos .write (bytes , resetIndex , bytes .length - resetIndex );
313
+ }
314
+ }
315
+
291
316
/*
292
- * It is OK to convert to string as SimpleSerialize's
293
- * serialized string contains only ASCII chars.
294
- */
317
+ // It is OK to convert to string as SimpleSerialize's
318
+ // serialized string contains only ASCII chars.
319
+ // [20151228] ASCII characters only is broken after 2.0.0
295
320
String string = baos.toString();
296
321
String resetString = swtParseReceived(string);
297
322
if (resetString != null && resetString.length() > 0) {
@@ -315,7 +340,10 @@ public void run() {
315
340
} catch (IOException e) {
316
341
e.printStackTrace();
317
342
}
343
+ } else if (resetString != null && resetString.length() == 0) {
344
+ baos.reset();
318
345
}
346
+ // */
319
347
return true ;
320
348
}
321
349
@@ -370,19 +398,30 @@ static String swtParseReceived(String string) {
370
398
}
371
399
}
372
400
if ((ss = SimpleSerializable .parseInstance (string , end )) == null
401
+ || ss == SimpleSerializable .ERROR
373
402
|| !ss .deserialize (string , end )) {
374
403
break ;
375
404
}
376
405
String key = string .substring (start , end );
377
406
final SimplePipeRunnable runnable = SimplePipeHelper .getPipe (key );
378
407
if (runnable != null ) { // should be always fulfill this condition
379
- //runnable.deal(ss);
380
- final SimpleSerializable instance = ss ;
381
- SWTHelper .syncExec (Display .getDefault (), new Runnable () {
382
- public void run () {
383
- runnable .deal (instance );
408
+ runnable .lastPipeDataReceived = System .currentTimeMillis ();
409
+ if (ss != SimpleSerializable .UNKNOWN ) {
410
+ if (ss instanceof SimplePipeSequence ) {
411
+ long sequence = ((SimplePipeSequence ) ss ).sequence ;
412
+ if (sequence > runnable .pipeSequence ) {
413
+ runnable .pipeSequence = sequence ;
414
+ }
415
+ } else {
416
+ //runnable.deal(ss);
417
+ final SimpleSerializable instance = ss ;
418
+ SWTHelper .syncExec (Display .getDefault (), new Runnable () {
419
+ public void run () {
420
+ runnable .deal (instance );
421
+ }
422
+ });
384
423
}
385
- });
424
+ }
386
425
}
387
426
388
427
start = restStringIndex (string , start );
@@ -393,4 +432,83 @@ public void run() {
393
432
return string ;
394
433
}
395
434
435
+ @ J2SIgnore
436
+ static int swtParseReceivedBytes (final byte [] bytes ) {
437
+ if (bytes == null ) {
438
+ return -1 ;
439
+ }
440
+ SimpleSerializable ss = null ;
441
+ int start = 0 ;
442
+ while (bytes .length > start + PIPE_KEY_LENGTH ) { // should be bigger than 48 ( 32 + 6 + 1 + 8 + 1)
443
+ int end = start + PIPE_KEY_LENGTH ;
444
+ if (PIPE_STATUS_DESTROYED == bytes [end ]) {
445
+ final String key = new String (bytes , start , PIPE_KEY_LENGTH );
446
+ final SimplePipeRunnable pipe = SimplePipeHelper .getPipe (key );
447
+ if (pipe != null ) {
448
+ if (key .equals (pipe .pipeKey )) {
449
+ pipe .pipeAlive = false ;
450
+ //pipe.pipeClosed();
451
+ SWTHelper .syncExec (Display .getDefault (), new Runnable () {
452
+ public void run () {
453
+ pipe .pipeClosed ();
454
+ //SimplePipeHelper.removePipe(key);
455
+ }
456
+ });
457
+ }
458
+ SimplePipeHelper .removePipe (key );
459
+ }
460
+ return end + 1 ;
461
+ }
462
+ if (PIPE_STATUS_OK == bytes [end ]) {
463
+ String key = new String (bytes , start , PIPE_KEY_LENGTH );
464
+ SimplePipeRunnable runnable = SimplePipeHelper .getPipe (key );
465
+ if (runnable != null ) { // should always satisfy this condition
466
+ runnable .lastPipeDataReceived = System .currentTimeMillis ();
467
+ }
468
+ start = end + 1 ;
469
+ if (start == bytes .length ) {
470
+ return start ;
471
+ }
472
+ continue ;
473
+ }
474
+ ss = SimpleSerializable .parseInstance (bytes , end );
475
+ if (ss == null ) {
476
+ break ;
477
+ }
478
+ if (ss == SimpleSerializable .ERROR ) {
479
+ return -1 ; // error
480
+ }
481
+ if (!ss .deserializeBytes (bytes , end )) {
482
+ break ;
483
+ }
484
+ String key = new String (bytes , start , PIPE_KEY_LENGTH );
485
+ final SimplePipeRunnable runnable = SimplePipeHelper .getPipe (key );
486
+ if (runnable != null ) { // should always satisfy this condition
487
+ runnable .lastPipeDataReceived = System .currentTimeMillis ();
488
+ if (ss != SimpleSerializable .UNKNOWN ) {
489
+ if (ss instanceof SimplePipeSequence ) {
490
+ long sequence = ((SimplePipeSequence ) ss ).sequence ;
491
+ if (sequence > runnable .pipeSequence ) {
492
+ runnable .pipeSequence = sequence ;
493
+ }
494
+ } else {
495
+ //runnable.deal(ss);
496
+ final SimpleSerializable instance = ss ;
497
+ SWTHelper .syncExec (Display .getDefault (), new Runnable () {
498
+ public void run () {
499
+ runnable .deal (instance );
500
+ }
501
+ });
502
+ }
503
+ }
504
+ }
505
+
506
+ start = restBytesIndex (bytes , start );
507
+ }
508
+ if (start != 0 ) {
509
+ return start ;
510
+ }
511
+ return 0 ;
512
+ }
513
+
396
514
}
0 commit comments