0

I'm using libwebsockets-2.1.0 with generic session & lwsws option enabled.

In case LWS_CALLBACK_SERVER_WRITEABLE I've got some code that opens a file, and output the content to websocket.

static const char* filename = "/tmp/loop.log";
#define MAX_STAT_LINE_LENGTH    256

unsigned char buf[LWS_PRE + 512];
unsigned char *p = &buf[LWS_PRE];

char line[MAX_STAT_LINE_LENGTH];

while ( fgets(line, sizeof(line), fp) != NULL) {
    int n = lws_snprintf((char *)p, sizeof(line), "%s", line);
    int m = lws_write(wsi, p, n, LWS_WRITE_TEXT);

    if (m < n) {
        printf("websocket write failed\n");
    }
}

In the terminal I'm getting a bunch of these:

lwsws[13778]: ****** 0x9230a50: Sending new 46 (/name?ag=z&abcd=011), pending truncated ...
It's illegal to do an lws_write outside of the writable callback: fix your code

Is there an explanation for this error? I mean, I have declared char line[1000] but it's still complaining.

I would like to point out that the final output of the websocket is inconsistent, sometimes it stops at line 30-something, sometimes it stops at line 400

FWIW, the total line of the file I'm reading is 1758 lines, with the longest character length of a line is 107 characters long.

Removing the fgets loop and replacing the value with my own generic value seems to work fine.

Thanks

deojeff
  • 377
  • 6
  • 19

1 Answers1

0

The fix is to increase the rx buf size.

#define LWS_PLUGIN_PROTOCOL_DUMB_INCREMENT \
{ 
    "protocol_dumb_increment", \
    callback_dumb_increment, \
    sizeof(struct per_session_data__dumb_increment), \
    4000, /* rx buf size must be >= permessage-deflate rx size */ \
}

Source: libwebsocket's github issue page

deojeff
  • 377
  • 6
  • 19