@@ -108,6 +108,11 @@ public static interface IHttpPipeRequestFactory extends IHttpRequestFactory {
108108 * Query key for pipe: pipernd
109109 */
110110 public static final String FORM_PIPE_RANDOM = "r" ; // "pipernd";
111+
112+ /**
113+ * Query key for pipe: pipeseq
114+ */
115+ public static final String FORM_PIPE_SEQUENCE = "s" ; // "pipeseq";
111116
112117 static final int PIPE_KEY_LENGTH = 6 ;
113118
@@ -160,11 +165,12 @@ public static void switchToContinuumMode() {
160165 * @param pipeRequestType
161166 * @return request data for both GET and POST request.
162167 */
163- protected static String constructRequest (String pipeKey , String pipeRequestType ) {
168+ protected static String constructRequest (String pipeKey , String pipeRequestType , long pipeSequence ) {
164169 reqCount ++;
165170 return FORM_PIPE_KEY + "=" + pipeKey + "&"
166- + FORM_PIPE_TYPE + "=" + pipeRequestType
167- + "&" + FORM_PIPE_RANDOM + "=" + reqCount ;
171+ + FORM_PIPE_TYPE + "=" + pipeRequestType + "&"
172+ + FORM_PIPE_SEQUENCE + "=" + pipeSequence + "&"
173+ + FORM_PIPE_RANDOM + "=" + reqCount ;
168174 }
169175
170176 protected static void sendRequest (HttpRequest request , String method , String url ,
@@ -213,7 +219,7 @@ public void run() {
213219 @ J2SIgnore
214220 static void keepPipeLive (final SimplePipeRunnable runnable ) {
215221 runnable .updateStatus (true );
216- if (getRequstMode () != MODE_LOCAL_JAVA_THREAD ) {
222+ if (getRequstMode () != MODE_LOCAL_JAVA_THREAD && getPipeMode () == MODE_PIPE_QUERY ) {
217223 return ;
218224 }
219225 //*
@@ -227,7 +233,7 @@ public void run() {
227233 while (interval > 0 ) {
228234 try {
229235 Thread .sleep (Math .min (interval , 1000 ));
230- interval -= 1000 ;
236+ interval -= 1000 + ( runnable . pipeSequence - runnable . notifySequence ) * 1000 ;
231237 } catch (InterruptedException e ) {
232238 //e.printStackTrace();
233239 }
@@ -257,15 +263,18 @@ public void run() {
257263 }
258264 } else {
259265 SimplePipeRunnable r = SimplePipeHelper .getPipe (runnable .pipeKey );
260- if (r != null ) {
266+ if (r != null && r . pipeSequence != r . notifySequence ) {
261267 HttpRequest request = getRequest ();
262268 String pipeKey = runnable .pipeKey ;
263269 String pipeMethod = runnable .getPipeMethod ();
264270 String pipeURL = runnable .getPipeURL ();
265-
266- String pipeRequestData = constructRequest (pipeKey , PIPE_TYPE_NOTIFY );
271+ long sequence = runnable . pipeSequence ;
272+ String pipeRequestData = constructRequest (pipeKey , PIPE_TYPE_NOTIFY , sequence );
267273 sendRequest (request , pipeMethod , pipeURL , pipeRequestData , false );
268274 String response = request .getResponseText ();
275+ if (response != null && runnable .notifySequence < sequence ) {
276+ runnable .notifySequence = sequence ;
277+ }
269278 if (response != null && response .indexOf ("\" " + PIPE_STATUS_LOST + "\" " ) != -1 ) {
270279 runnable .pipeAlive = false ;
271280 runnable .pipeLost ();
@@ -491,9 +500,9 @@ public void onLoaded() {
491500html += " net.sf.j2s.ajax.SimplePipeRequest.parseReceived (string);\r\n";
492501html += " };\r\n";
493502html += "};\r\n";
494- html += "window[\"$p1p3b$\"] = function (key, result) {\r\n";
503+ html += "window[\"$p1p3b$\"] = function (key, result, sequence ) {\r\n";
495504html += " with (window.parent) {\r\n";
496- html += " net.sf.j2s.ajax.SimplePipeRequest.pipeNotifyCallBack (key, result);\r\n";
505+ html += " net.sf.j2s.ajax.SimplePipeRequest.pipeNotifyCallBack (key, result, sequence );\r\n";
497506html += " };\r\n";
498507html += "};\r\n";
499508html += "</scr" + "ipt></head><body><script type=\"text/javascript\">\r\n";
@@ -582,7 +591,7 @@ static void pipeScript(SimplePipeRunnable runnable) { // xss
582591 // only for JavaScript
583592 String url = runnable .getPipeURL ();
584593 String requestURL = url + (url .indexOf ('?' ) != -1 ? "&" : "?" )
585- + constructRequest (runnable .pipeKey , PIPE_TYPE_XSS );
594+ + constructRequest (runnable .pipeKey , PIPE_TYPE_XSS , runnable . pipeSequence );
586595 /**
587596 * @j2sNative
588597 * net.sf.j2s.ajax.SimplePipeRequest.pipeScriptMap[requestURL] = runnable;
@@ -627,7 +636,7 @@ static void pipeScript(SimplePipeRunnable runnable) { // xss
627636ifr.style.display = "none";
628637var url = runnable.getPipeURL();
629638var src = url + (url.indexOf('?') != -1 ? "&" : "?")
630- + spr.constructRequest(pipeKey, spr.PIPE_TYPE_SUBDOMAIN_QUERY)
639+ + spr.constructRequest(pipeKey, spr.PIPE_TYPE_SUBDOMAIN_QUERY, runnable.pipeSequence )
631640 + "&" + spr.FORM_PIPE_DOMAIN + "=" + domain;
632641ifr.id = "pipe-" + pipeKey;
633642ifr.src = src;
@@ -638,14 +647,17 @@ static void pipeScript(SimplePipeRunnable runnable) { // xss
638647 static void pipeNotify (SimplePipeRunnable runnable ) { // notifier
639648 String url = runnable .getPipeURL ();
640649 loadPipeScript (url + (url .indexOf ('?' ) != -1 ? "&" : "?" )
641- + constructRequest (runnable .pipeKey , PIPE_TYPE_NOTIFY ));
650+ + constructRequest (runnable .pipeKey , PIPE_TYPE_NOTIFY , runnable . pipeSequence ));
642651 // only for JavaScript
643652 }
644653
645- static void pipeNotifyCallBack (String key , String result ) {
646- if (PIPE_STATUS_LOST .equals (result )) {
647- SimplePipeRunnable pipe = SimplePipeHelper .getPipe (key );
648- if (pipe != null ) {
654+ static void pipeNotifyCallBack (String key , String result , long sequence ) {
655+ SimplePipeRunnable pipe = SimplePipeHelper .getPipe (key );
656+ if (pipe != null ) {
657+ if (pipe .notifySequence < sequence ) {
658+ pipe .notifySequence = sequence ;
659+ }
660+ if (PIPE_STATUS_LOST .equals (result )) {
649661 pipe .pipeAlive = false ;
650662 pipe .pipeLost ();
651663 SimplePipeHelper .removePipe (key );
@@ -699,7 +711,7 @@ public void onLoaded() {
699711
700712 });
701713
702- String pipeRequestData = constructRequest (pipeKey , PIPE_TYPE_QUERY );
714+ String pipeRequestData = constructRequest (pipeKey , PIPE_TYPE_QUERY , runnable . pipeSequence );
703715 boolean async = false ;
704716 /**
705717 * In JavaScript such queries are not wrapped inside Thread, so asynchronous mode is required!
@@ -767,7 +779,7 @@ protected IXHRReceiving initializeReceivingMonitor() {
767779 window["xss.domain.enabled"] = true;
768780}
769781ifr.src = url + (url.indexOf('?') != -1 ? "&" : "?")
770- + spr.constructRequest(pipeKey, spr.PIPE_TYPE_SCRIPT)
782+ + spr.constructRequest(pipeKey, spr.PIPE_TYPE_SCRIPT, runnable.pipeSequence )
771783 + (subdomain == null ? ""
772784 : "&" + spr.FORM_PIPE_DOMAIN + "=" + subdomain);
773785document.body.appendChild (ifr);
@@ -781,6 +793,10 @@ protected IXHRReceiving initializeReceivingMonitor() {
781793 var runnable = sph.getPipe(key);
782794 if (runnable != null) {
783795 var spr = net.sf.j2s.ajax.SimplePipeRequest;
796+ if (runnable.pipeSequence == runnable.notifySequence) {
797+ window.setTimeout (arguments.callee, spr.pipeLiveNotifyInterval);
798+ return;
799+ }
784800 var now = new Date ().getTime ();
785801 var last = runnable.lastPipeDataReceived;
786802 if (last <= 0) {
@@ -858,7 +874,7 @@ public void onLoaded() { // on case that no destroy event is sent to client
858874 String pipeMethod = runnable .getPipeMethod ();
859875 String pipeURL = runnable .getPipeURL ();
860876
861- String pipeRequestData = constructRequest (pipeKey , PIPE_TYPE_CONTINUUM );
877+ String pipeRequestData = constructRequest (pipeKey , PIPE_TYPE_CONTINUUM , runnable . pipeSequence );
862878 sendRequest (pipeRequest , pipeMethod , pipeURL , pipeRequestData , true );
863879 }
864880
@@ -990,7 +1006,14 @@ public static String parseReceived(final String string) {
9901006 if (runnable != null ) { // should always satisfy this condition
9911007 runnable .lastPipeDataReceived = System .currentTimeMillis ();
9921008 if (ss != SimpleSerializable .UNKNOWN ) {
993- runnable .deal (ss );
1009+ if (ss instanceof SimplePipeSequence ) {
1010+ long sequence = ((SimplePipeSequence ) ss ).sequence ;
1011+ if (sequence > runnable .pipeSequence ) {
1012+ runnable .pipeSequence = sequence ;
1013+ }
1014+ } else {
1015+ runnable .deal (ss );
1016+ }
9941017 }
9951018 }
9961019
@@ -1344,7 +1367,7 @@ public void run() {
13441367 method = runnable.getPipeMethod ();
13451368 url = runnable.getPipeURL ();
13461369 var spr = net.sf.j2s.ajax.SimplePipeRequest;
1347- data = spr.constructRequest(key, spr.PIPE_TYPE_QUERY);
1370+ data = spr.constructRequest(key, spr.PIPE_TYPE_QUERY, runnable.pipeSequence );
13481371 } catch (e) {
13491372 }
13501373 }
@@ -1357,7 +1380,7 @@ public void run() {
13571380 method = runnable.getPipeMethod ();
13581381 url = runnable.getPipeURL ();
13591382 var spr = net.sf.j2s.ajax.SimplePipeRequest;
1360- data = spr.constructRequest(key, spr.PIPE_TYPE_QUERY);
1383+ data = spr.constructRequest(key, spr.PIPE_TYPE_QUERY, runnable.pipeSequence );
13611384 } catch (e) {
13621385 }
13631386 }
0 commit comments