Is there a schedule concept that integrates UDP into it?
The only thing missing in libevent after so many years of use is that udp is not supported by default and you have to do your own integration.
Does the author still have the energy to add udp to it?
Hi @X-Crack
What integration do you mean here? Simple events for UDP does supported by libevent, you can take a look at evdns code for example
is the desire to integrate a simple version into libevent
Like the popular libuv, the tcp/udp support is packaged by their team.
I'm using UDP with libevent for years, including features like evbuffer. I don't think that libevent needs any special integration for UDP. The "libevent can't do UDP" misconception arises, because bufferevents are made for streaming and connection based network IO. People don't realize that they can just use read/write callbacks and evbuffers to use UDP. And own code for the socket stuff is also not needed, libevent provides functions like evutil_make_socket_nonblocking.
I think what libevent needs is a simple example for UDP so people find it easier to use.
Edit: Ok, some changes would make evbuffers and libevent more usable with UDP. I think the main problem is that you always need custom code to use evbuffers with non connection based sockets. This custom code often kills the efficiency that evbuffers introduce.
I think what libevent needs is a simple example for UDP so people find it easier to use.
Having such example will be great! Maybe you can generalize your code and submit it?
related with #654
@azat Should we offer how detail code for UDP?
For server, evconnlistener_new_bind for TCP, include following things:
- create socket, and set nonblocking, and other flags, UDP can do these things manually
- custom event
evconnlistener_eventand add it to event_base, UDP can do these things manually - read callback,
acceptand wrap toevconnlistener_cb, UDP can do these things manually- the difference is there is no
accept, and onlyrecvfromorrecvmsgor similar
- the difference is there is no
After read something, we may need use a hash map for state in UDP:
- convert fd to bufferevent,
bufferevent_socket_new, UDP can create it's ownbuffereventfrombufferevent_struct.h - for the new
bufferevent, we should do following things:input, if it's ok to write to remote or other, then write it without cache it ininput(it's udp, we have read them inrecvfrom/recvmsg)output, useevbuffer_add_cbto add a callback, then usesendtoev_read, assign a new event, then useevent_addfor timeout
evconnlistener_new_bind and bufferevent_socket_new do many things for us, if we know the internal detail, then use libevent for UDP should be ok. However, as bufferevent_xxx is for bufferevent_private, we cannot use these methods.
@liudongmiao can you submit a PR with your suggestion? It should include some abstraction for it, tests and also converts evdns to it
@azat
wss-proxy, is a proxy over websocket.
https://github.com/brevent/wss-proxy/blob/0a9a3b74187a5c623a64705cf65c8431afe8d934/wss-proxy-client.c#L500-L535
wss-proxy-client is the client mode, acts like websocket client and tcp and udp server, receive packets from tcp / udp and send to websocket, then receive packets from websocket and send back to tcp / udp.
https://github.com/brevent/wss-proxy/blob/0a9a3b74187a5c623a64705cf65c8431afe8d934/wss-proxy-server.c#L179-L191
https://github.com/brevent/wss-proxy/blob/0a9a3b74187a5c623a64705cf65c8431afe8d934/wss-proxy-server.c#L133-L177
wss-proxy-server is the server mode, acts like websocket server and tcp and udp client, receive packets from websockets and send to tcp / udp client, then read from tcp / udp client then send back to websocket.
For tcp part, it's like le-proxy, however much easier and without watermark.
For udp part, as I have written the proxy logic of tcp, most of the work is how to build a bufferevent, and the input / output evbuffer, and callback.
I'm still thinking about how to make an abstract of udp client / server, and websocket client / server.