@@ -117,8 +117,10 @@ secstream_ws(struct lws *wsi, enum lws_callback_reasons reason, void *user,
117117 if (!h ) {
118118 return LWSSSSRET_DISCONNECT_ME ;
119119 }
120- h -> retry = 0 ;
121- h -> seqstate = SSSEQ_CONNECTED ;
120+ h -> retry = 0 ;
121+ h -> seqstate = SSSEQ_CONNECTED ;
122+ wsi -> ws -> last_valid = 0 ;
123+ wsi -> ws -> last_fin = 0 ;
122124 lws_sul_cancel (& h -> sul );
123125#if defined(LWS_WITH_SYS_METRICS )
124126 /*
@@ -162,13 +164,41 @@ secstream_ws(struct lws *wsi, enum lws_callback_reasons reason, void *user,
162164 }
163165
164166 buflen = sizeof (buf ) - LWS_PRE ;
167+
168+ /*
169+ * Let's prepare *flags with information about the channel ws message state
170+ * which the callback can simply overwrite. But if you are tracing problems
171+ * with your own flags state blowing assert()s below, you can use this for
172+ * debugging the illegal state while still in your ss tx callback; otherwise
173+ * it can be harder to understand what you're doing wrong after we returned
174+ * from the callback and blow chunks here.
175+ *
176+ * b1 of flags is set if we know the last fin state. If b1 is set, b0 is
177+ * the last FIN state. You can use this info to see if your flags will
178+ * cause us to assert when you return.
179+ */
180+
181+ f = (!!wsi -> ws -> last_valid << 1 ) | (!!wsi -> ws -> last_fin );
165182 r = h -> info .tx (ss_to_userobj (h ), h -> txord ++ , buf + LWS_PRE ,
166183 & buflen , & f );
167184 if (r == LWSSSSRET_TX_DONT_SEND )
168185 return 0 ;
169186 if (r != LWSSSSRET_OK )
170187 return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE (r , wsi , & h );
171188
189+ if ((f & LWSSS_FLAG_SOM ) && wsi -> ws -> last_valid && !wsi -> ws -> last_fin ) {
190+ lwsl_ss_err (h , "%s TX: Illegal LWSSS_FLAG_SOM after previous frame without LWSSS_FLAG_EOM" , h -> policy ? h -> policy -> streamtype : "unknown" );
191+ assert (0 );
192+ }
193+ if (!(f & LWSSS_FLAG_SOM ) && wsi -> ws -> last_valid && wsi -> ws -> last_fin ) {
194+ lwsl_ss_err (h , "%s TX: Missing LWSSS_FLAG_SOM after previous frame with LWSSS_FLAG_EOM" , h -> policy ? h -> policy -> streamtype : "unknown" );
195+ assert (0 );
196+ }
197+ if (!(f & LWSSS_FLAG_SOM ) && !wsi -> ws -> last_valid ) {
198+ lwsl_ss_err (h , "%s TX: Missing LWSSS_FLAG_SOM on first frame" , h -> policy ? h -> policy -> streamtype : "unknown" );
199+ assert (0 );
200+ }
201+
172202 f1 = lws_write_ws_flags (h -> policy -> u .http .u .ws .binary ?
173203 LWS_WRITE_BINARY : LWS_WRITE_TEXT ,
174204 !!(f & LWSSS_FLAG_SOM ),
0 commit comments