libevent icon indicating copy to clipboard operation
libevent copied to clipboard

Is there a schedule concept that integrates UDP into it?

Open 0xdddddddd opened this issue 4 years ago • 8 comments

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?

0xdddddddd avatar Dec 04 '21 11:12 0xdddddddd

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

azat avatar Dec 07 '21 07:12 azat

is the desire to integrate a simple version into libevent

Like the popular libuv, the tcp/udp support is packaged by their team.

0xdddddddd avatar Dec 07 '21 15:12 0xdddddddd

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.

SunnyPaprika avatar Aug 17 '22 09:08 SunnyPaprika

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?

azat avatar Aug 28 '22 18:08 azat

related with #654

liudongmiao avatar Apr 02 '24 00:04 liudongmiao

@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_event and add it to event_base, UDP can do these things manually
  • read callback, accept and wrap to evconnlistener_cb, UDP can do these things manually
    • the difference is there is no accept, and only recvfrom or recvmsg or similar

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 own bufferevent from bufferevent_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 in input (it's udp, we have read them in recvfrom / recvmsg)
    • output, use evbuffer_add_cb to add a callback, then use sendto
    • ev_read, assign a new event, then use event_add for 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 avatar Apr 03 '24 01:04 liudongmiao

@liudongmiao can you submit a PR with your suggestion? It should include some abstraction for it, tests and also converts evdns to it

azat avatar Apr 07 '24 15:04 azat

@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.

liudongmiao avatar Apr 08 '24 09:04 liudongmiao