Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

on_msg_recv_callback #34

Open
harryCM opened this issue Jul 11, 2016 · 6 comments
Open

on_msg_recv_callback #34

harryCM opened this issue Jul 11, 2016 · 6 comments

Comments

@harryCM
Copy link

harryCM commented Jul 11, 2016

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

@tatsuhiro-t
Copy link
Owner

What error message did you get when you do this?

@harryCM
Copy link
Author

harryCM commented Jul 16, 2016

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
#34 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AO73Fm3YChYrOBkbc0HU0edmDMEzV5LCks5qWFKbgaJpZM4JI89v
.

@tatsuhiro-t
Copy link
Owner

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.

@harryCM
Copy link
Author

harryCM commented Jul 17, 2016

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
#34 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AO73FkfBpVzynamw_anb9CTkdjhxd4bzks5qWPrvgaJpZM4JI89v
.

@tatsuhiro-t
Copy link
Owner

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.

@harryCM
Copy link
Author

harryCM commented Jul 25, 2016

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
#34 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AO73FhhgJnDxkytle3yFgt0e3qtBIdh0ks5qXMADgaJpZM4JI89v
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants