Repository navigation
Conversation
- Get the peer address correctly - Implement us_udp_socket_close - Remove printf statement when creating udp socket Signed-off-by: Thiago Padilha <thiago@padilha.cc>
Also fix Poll.deinit to receive a loop pointer. Signed-off-by: Thiago Padilha <thiago@padilha.cc>
For now this only contains a basic implementation, but it should be enough to implement a node.js shim Signed-off-by: Thiago Padilha <thiago@padilha.cc>
|
Pr seams like a good idea but api wise we might want to integrate it into Bun.connect rather then a standalone function maybe, it might also maybe allow to reuse some code, although thats not mandatory i think but i think it def makes sense to integrate it as a option rather then a standalone api on the Bun global object. |
That might be possible, but note that UDP has a different programming model than TCP:
|
Signed-off-by: Thiago Padilha <thiago@padilha.cc>
It is not necessary to store these fields since they are only used during socket creation. Signed-off-by: Thiago Padilha <thiago@padilha.cc>
The previous code could result in non 0-terminated strings, fix that by using `sliceZBuf` which can write to a stack buffer. This is OK because we have a predictable max length of an ip address. Signed-off-by: Thiago Padilha <thiago@padilha.cc>
These seam to be mostly implementation details though, correct me if wrong but the user-facing js api does not need to really change for socket clients right, which is from where the idea came. |
It is not an implementation detail, it is simply a lower level protocol than TCP. When you send a UDP packet over the wire, it is not guaranteed the other end will receive it and the sender gets no confirmation. It is up to the application to deal with it, which is why UDP is more useful in real time chat apps and games. The concept of "streams" is a higher level abstraction created by TCP (upon which other protocols such as HTTP are built upon). When you send some data over a TCP connection, the other end is guaranteed to receive it because the TCP layer will resend packets which were not received. This is not something we can abstract away from users without reimplementing TCP and incurring significant performance loss (At which point the user would have no reason to use UDP in the first place). Here's some extra reading if you want to understand more: https://www.pluralsight.com/blog/it-ops/networking-basics-tcp-udp-tcpip-osi-models |
|
Im aware what UDP is and i in no point suggested that its needed to reimplement TCP over udp? |
const client = Bun.connect({
hostname: "localhost",
port: 41234,
protocol: "udp",
socket: {
open(socket) {}, // can be auto emitted
close(socket) {}, // can be emitted after the user calls client.close() or omitted
error(socket, error) {}, // same as data but for errors
},
});
const server = await Bun.listen({ // the await can be akain to the node listening event
hostname: "localhost",
port: 41234,
protocol: "udp",
socket: {
data(rinfo, data) {}, // message received from client
error(error) {}, // error handler
},
});This does not need to be final or complete but maybe it illustrates the idea better. |
|
It is possible to implement the way you suggest, but IMO it is going to be a forced consistency with the TCP constructors, and the semantics will be different:
Also, the fact that I'm providing a base API with a higher level means it will be more difficult to implement a node.js shim on top of it. For example, Node.js also doesn't make a distinction between client and server udp sockets (because there's no underlying distinction), and it is perfectly fine for a "server" to "connect" to another address/port. The API I've implemented here closely follows the uSockets API uSockets public API is also not ideal because it binds the socket immediatelly and requires you to specify an address and port. The abstraction I've made here is that if you don't specify bind address or port, it will default to "0.0.0.0" for address(because we want to have a global return address) and 0 for port (which means the OS will select a random port). We can modify uSockets, but so far I'm taking @paperdave suggestion to keep uSockets changes to a minimum. |
|
"connect" is a TCP concept - so it makes no sense in the context of UDP. |
https://nodejs.org/api/dgram.html#socketconnectport-address-callback can also refer to association, it does not need to be solely related to tcp. |
Right now, neither I'm going to wait for feedback from maintainers before making any extra changes though. |
|
heads up, seems this doesnt compile on MacOS right now, potential zig bug ziglang/translate-c#122 you can test a zig compile for macos target with |
| @@ -0,0 +1,477 @@ | |||
| const bun = @import("root").bun; | |||
| const std = @import("std"); | |||
| const c = @cImport({ | |||
There was a problem hiding this comment.
this is probably the root of the macos compilation error. an alternative to this is hand writing the extern fn stuff.
What does this PR do?
#1630
This implements a simple UDP programming API on the Bun namespace. It is still not fully optimized, but should be enough to implement a node.js
dgramshim (in a future PR).How did you verify your code works?
zig fmton the changed files