introduce lws_partial_buffered
authorAndy Green <andy.green@linaro.org>
Mon, 18 Aug 2014 14:49:39 +0000 (22:49 +0800)
committerAndy Green <andy.green@linaro.org>
Mon, 18 Aug 2014 14:49:39 +0000 (22:49 +0800)
Signed-off-by: Andy Green <andy.green@linaro.org>
changelog
lib/libwebsockets.c
lib/libwebsockets.h
test-server/test-server.c

index 95e6194..f50d87b 100644 (file)
--- a/changelog
+++ b/changelog
@@ -21,6 +21,12 @@ over ssl or not.  If LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT is used, both
 ssl and non-ssl connections are possible and may need to be treated differently
 in the user code.
 
+int lws_partial_buffered(wsi) added... should be checked after any
+libwebsocket_write that will be followed by another libwebsocket_write inside
+the same writeable callback.  If set, you can't do any more writes until the
+writeable callback is called again.  If you only do one write per writeable callback,
+you can ignore this.
+
 
 User api removal
 ----------------
index a5ec698..166dd3c 100644 (file)
@@ -769,3 +769,26 @@ lws_is_ssl(struct libwebsocket *wsi)
 {
        return wsi->use_ssl;
 }
+
+/**
+ * lws_partial_buffered() - find out if lws buffered the last write
+ * @wsi:       websocket connection to check
+ *
+ * Returns 1 if you cannot use libwebsocket_write because the last
+ * write on this connection is still buffered, and can't be cleared without
+ * returning to the service loop and waiting for the connection to be
+ * writeable again.
+ * 
+ * If you will try to do >1 libwebsocket_write call inside a single
+ * WRITEABLE callback, you must check this after every write and bail if
+ * set, ask for a new writeable callback and continue writing from there.
+ * 
+ * This is never set at the start of a writeable callback, but any write
+ * may set it.
+ */
+
+LWS_VISIBLE int
+lws_partial_buffered(struct libwebsocket *wsi)
+{
+       return !!wsi->truncated_send_len;       
+}
index a0c500a..7b5e697 100644 (file)
@@ -1170,6 +1170,9 @@ LWS_VISIBLE LWS_EXTERN int
 lws_send_pipe_choked(struct libwebsocket *wsi);
 
 LWS_VISIBLE LWS_EXTERN int
+lws_partial_buffered(struct libwebsocket *wsi);
+
+LWS_VISIBLE LWS_EXTERN int
 lws_frame_is_binary(struct libwebsocket *wsi);
 
 LWS_VISIBLE LWS_EXTERN int
index 682bb5d..c43b9c4 100644 (file)
@@ -386,13 +386,17 @@ static int callback_http(struct libwebsocket_context *context,
                        if (m) /* while still active, extend timeout */
                                libwebsocket_set_timeout(wsi,
                                        PENDING_TIMEOUT_HTTP_CONTENT, 5);
+                       
+                       /* if he has indigestion, let him clear it before eating more */
+                       if (lws_partial_buffered(wsi))
+                               break;
 
                } while (!lws_send_pipe_choked(wsi));
                libwebsocket_callback_on_writable(context, wsi);
                break;
 flush_bail:
                /* true if still partial pending */
-               if (lws_send_pipe_choked(wsi)) {
+               if (lws_partial_buffered(wsi)) {
                        libwebsocket_callback_on_writable(context, wsi);
                        break;
                }
@@ -632,7 +636,7 @@ callback_lws_mirror(struct libwebsocket_context *context,
 
                        // lwsl_debug("tx fifo %d\n", (ringbuffer_head - pss->ringbuffer_tail) & (MAX_MESSAGE_QUEUE - 1));
 
-                       if (lws_send_pipe_choked(wsi)) {
+                       if (lws_partial_buffered(wsi) || lws_send_pipe_choked(wsi)) {
                                libwebsocket_callback_on_writable(context, wsi);
                                break;
                        }