wslay icon indicating copy to clipboard operation
wslay copied to clipboard

on_msg_recv_callback

Open harryCM opened this issue 9 years ago • 6 comments

I use fork_echoserv.c as a base to write a simple application. Instead of echoing back a frame received, I use on_msg_recv_callback function to send back a different frame using wslay_event_queue_msg(ctx, &msgNew) where the last argument contains my own frame structure. But the function does not allow me to do so. What other functions can allow me to send back a different frame based on the content of the frame just received? Please provide some examples. Thank you very much.

harry

harryCM avatar Jul 11 '16 00:07 harryCM

What error message did you get when you do this?

tatsuhiro-t avatar Jul 16 '16 03:07 tatsuhiro-t

Thank you very much for responding to my question. I like the simplicity of the overall design of your example codes for a basic echo app. In my simple test app, I just want to make a simple modification to the fork-echoserv.c code, instead of echoing back every byte to the peer, now sending back a short text message like "ok:". I am trying to use the same data structure (arg->msg) to store my reply message. But the underlying data structure for msgarg is a constant data structure (arg), it does not allow me to write. The following is my simple code modification:

// for test purpose, send back a simple 5-byte text msg: ok:123 arg->msg[0]=111; arg->msg[0]=107; arg->msg[0]=58; // 'o'=111 'k'=107 ':'=58 arg->msg[0]=49; arg->msg[0]=50; arg->msg[0]=51; // '1'=49, '2'=50 '3'=51

wslay_event_queue_msg(ctx, &msgarg);

When I compile the modified code (using gcc), I got the following compiler error:

myhost@UbuntuSRVS2S:~/ws-test/wslay-master/examples$ !gcc gcc -Wall -O2 -g -o fork-echoserv fork-echoserv.c -L../lib/.libs -I../lib/includes -lwslay -lnettle fork-echoserv.c: In function ‘on_msg_recv_callback’: fork-echoserv.c:883:5: error: assignment of read-only location ‘_arg->msg’ arg->msg[0]=111; arg->msg[0]=107; arg->msg[0]=58; // 'o'=111 'k'=107 ':'=58 ^ fork-echoserv.c:883:5: error: assignment of read-only location ‘_arg->msg’ fork-echoserv.c:883:5: error: assignment of read-only location ‘_arg->msg’ fork-echoserv.c:884:5: error: assignment of read-only location ‘_arg->msg’ arg->msg[0]=49; arg->msg[0]=50; arg->msg[0]=51; // '1'=49, '2'=50 '3'=51 ^ fork-echoserv.c:884:5: error: assignment of read-only location ‘_arg->msg’ fork-echoserv.c:884:5: error: assignment of read-only location ‘_arg->msg’

On Fri, Jul 15, 2016 at 10:40 PM, Tatsuhiro Tsujikawa < [email protected]> wrote:

What error message did you get when you do this?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/tatsuhiro-t/wslay/issues/34#issuecomment-233105926, or mute the thread https://github.com/notifications/unsubscribe-auth/AO73Fm3YChYrOBkbc0HU0edmDMEzV5LCks5qWFKbgaJpZM4JI89v .

harryCM avatar Jul 16 '16 15:07 harryCM

arg->msg is const pointer to uint8_t, so you cannot modify the pointed region directly. arg is self const pointer. Instead, create your own buffer, and write your data into it, and use it as message, like so:

    const uint8_t buf[] = "hello";
    struct wslay_event_msg msgarg = {arg->opcode, buf, sizeof(buf) - 1};
    wslay_event_queue_msg(ctx, &msgarg);

Although, manual does not say anything, but wslay_event_queue_msg makes a copy of msg of length msg_length.

tatsuhiro-t avatar Jul 16 '16 15:07 tatsuhiro-t

In my test case, the value returned to the peer in buf is dynamic. I only know the value at a run time. But gcc does not allow initializing a const using a variable such as follows:

char reply_message[256];

< application code that would change the value of variable: reply_message>

const uint8_t buf[] = reply_message

On Sat, Jul 16, 2016 at 10:38 AM, Tatsuhiro Tsujikawa < [email protected]> wrote:

arg->msg is const pointer to uint8_t, so you cannot modify the pointed region directly. arg is self const pointer. Instead, create your own buffer, and write your data into it, and use it as message, like so:

const uint8_t buf[] = "hello";
struct wslay_event_msg msgarg = {arg->opcode, buf, sizeof(buf) - 1};
wslay_event_queue_msg(ctx, &msgarg);

Although, manual does not say anything, but wslay_event_queue_msg makes a copy of msg of length msg_length.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/tatsuhiro-t/wslay/issues/34#issuecomment-233136068, or mute the thread https://github.com/notifications/unsubscribe-auth/AO73FkfBpVzynamw_anb9CTkdjhxd4bzks5qWPrvgaJpZM4JI89v .

harryCM avatar Jul 17 '16 23:07 harryCM

If you have message already in reply_message, you don't need to use const uint8_t buf[]. Just point wslay_event_msg's msg field to reply_message. Of course, you have to assign msg_length too.

tatsuhiro-t avatar Jul 19 '16 12:07 tatsuhiro-t

I did as you said by defining msgarg with my own data variables (see below):

struct wslay_event_msg msgarg = {arg->opcode, replyMsg, replyMsg_length};

Then, it works! Thanks a lot.

I have another question: how to tell the function (wslay_event_queue_msg) NOT to mask my reply message. I am trying to use a bitwise operator to force the left-most bit of the msg_length variable to be zero (using msg_length ^ 0x7F). But it does not work. I could not find any other function or some header files where it would allow to respond the client with a msg without masking.

I really appreciate if you could point out a way to do this. The reason for not masking is that the client app runs on a device with very little CPU cycles.

On Tue, Jul 19, 2016 at 7:16 AM, Tatsuhiro Tsujikawa < [email protected]> wrote:

If you have message already in reply_message, you don't need to use const uint8_t buf[]. Just point wslay_event_msg's msg field to reply_message. Of course, you have to assign msg_length too.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/tatsuhiro-t/wslay/issues/34#issuecomment-233614133, or mute the thread https://github.com/notifications/unsubscribe-auth/AO73FhhgJnDxkytle3yFgt0e3qtBIdh0ks5qXMADgaJpZM4JI89v .

harryCM avatar Jul 25 '16 22:07 harryCM