Thank you very much for your helpful reply fft and Ralf !
This week i’m in OpenAT training course. So, i ask how works context. Here an explanation :
In fact, context is stored in a part of WIP library. So, when you use *ctx, the “msg_copy” array will be copied in a space array dedicated to the wip_channel_t returned by the “tcpClientCreate” function.
So, you don’t need to save or copy the message in any buffer. when the “event_handler” will be called, data will be retrieved automatically.
But, as i describe above, ctx is stored in a part of library. So, your code will crashed fft because you release a part of the WIP dedicated array in your sample. if you do that :
So, it will crashed !
you should release the memory allocated to the “msg_copy” after tcpClientCreate function. the right code will be :
// warning, untested code
static void evh_msg( wip_event_t *ev, void *ctx);
void send_msg( char *peer_addr, u16 peer_port, char *msg) {
char *msg_copy = adl_memGet( strlen( msg) + 1);
wip_tcpClientCreate( peer_addr, peer_port, evh_msg, (void*) msg_copy);
adl_memRelease( msg_copy);
}
static void evh_msg( wip_event_t *ev, void *ctx) {
switch( ev->kind) {
case WIP_CEV_OPEN: {
char *msg = (char*) ctx; // cast back to real type
wip_write( ev->channel, msg, strlen( msg));
// cleanup:
wip_close( ev->channel);
break;
}
case WIP_CEV_ERROR: {
wip_debug( "[SAMPLE] error, can't connect\n");
break;
to modify context specified on tcpClientCreate() function use wip_setCtx() function before event_handler will be called !
I hope this would help anybody which will be worried about *ctx !
Thank you fft and Ralf for your help
(Please let me known if you have any remarks)