[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [Libevent-users] unsend data in output buffer when close connection
Mark Ellzey:
I have seen your reply in mail archive but I can't receive the mail from mail list
so I reply the mail sent by myself
thank you for the reply!
>I honeslty have no idea how you're using th same bufferevent for a new
>connection, are you just re-using the same thing?
My prog is the client side of connection
I just use this code to close socket in bufferevent
evutil_socket_t sock=bufferevent_getfd(bev);
if(sock>=0){
evutil_closesocket(sock);
}
bufferevent_setfd(bev,-1);
and reconnect the new socket using:
bufferevent_socket_connect
It seems the bufferevent can be reused by this way
but the data already in bufferevent before close old socket will be send to new socket
and maybe the message boundary is broken in the buffer
>get the output buffer from the bufferevent, then evbuffer_drain()
I can get the output buffer , but can't drain the output buffer
I check the source code, the output buffer 's freeze_start is set to true, so it can't be read ,copy out or drain
So, it seems that the best I can do is free the bufferevent and create new one
if I don't want lost the data in output buffer, I must use some confirmation message from server to confirm receive just like the ACK in TCP stack
if the client side didn't receive the confirm message ,it should resend the data
so the data will be store in two place:
one is output buffer of bufferevent
the other is somewhere in my code to wait confirm message from server
is it necessary and is it the best way to do?
Thank you!
2015-01-21
slump
发件人: slump
发送时间: 2015-01-16 15:50:48
收件人: libevent-users
抄送:
主题: [Libevent-users] unsend data in output buffer when close connection
I have used bufferevent to send data to socket
the application use variable length message, the first 4 bytes in message is the length
all thing is OK but for socket reconnect
if the socket is closed by peer and my app reconnect
peer may receive the wrong msg, the message length is wrong
it seems the unsent data in bufferevent's output buffer will be send when the socket reconnect
but the message boundary is broken in output buffer
I guess it is because when I write message use bufferevent_write, one message is splited into two chunks in output buffer
one chunk is send to peer before socket closed
and the next chunk is sent to peer after socket reconnect, so the message is broken
if it is true, can I get the data from output buffer and find the next message begin in data
so I can discard the broken message and send the message follow it
but I find I can't get the data from output buffer, the evbuffer_copyout function return -1
and I can't find the function to clear the output buffer to discard all message in buffer
the only thing I can do is to free the bufferevent and create a new bufferevent when reconnect socket
is it the right way to do such thing?
Thank you!
2015-01-16
slump
N?叉??z千u┺?ザ??茛j??鳔?p?n??殊m?m朕????^n?