Giter Site home page Giter Site logo

Comments (7)

ryan-summers avatar ryan-summers commented on June 29, 2024 1

Disclaimer: After writing this, I've noticed that the code in #21 shows a nice approach for UDP as well.


Currently, it looks like the following functions are missing from the embedded-nal:

  • UDP bind()
  • TCP bind()
  • TCP listen()
  • TCP accept()

While on the UDP-side, the server only exposes 1 additional function, this gets more complex on the TCP-side. I think we should aim for symmetry in the implementations, so we should add all server-related functions in a symmetric manner for the TCP and UDP stacks.

UDP and TCP servers are generally a super-set of their client components (with the exception of TCP-connect), thus it would be desirable for having common core functionality for the shared components (e.g. open(), read(), write()), but then expose separate traits for TCP clients and servers.

One approach here is to have a core-trait, shown here:

pub trait UdpClient {
   fn open();
   fn close();
   fn read();
   fn write();
}

pub trait UdpServerCore {
    fn bind();
}

pub trait UdpServer {};
impl<T: UdpServerCore + UdpClient> UdpServer for T {}

Then, a user would implement the UdpServerCore as follows and have the UdpServer trait implemented for them (because them:

pub struct UdpStack {
};

impl UdpClient for UdpStack {
    // ....
}

impl UdpServerCore for UdpStack {
    // ...
}

And library could could require a UdpServer as follows:

pub fn my_function(stack: impl UdpServer) {
}

If your library only needed the client, it could do:

pub fn my_function(stack: impl UdpClient) {
}

from embedded-nal.

MathiasKoch avatar MathiasKoch commented on June 29, 2024

I am not sure this is that thoroughly used and tested wrt. UDP yet.

If you have any elegant ideas to fix your issues, i am very sure a PR would be welcomed, such that we can bring this up to a level where it gets the attention and usage it deserves.

from embedded-nal.

jonahbron avatar jonahbron commented on June 29, 2024

Okay. I think simply adding a port arg to the open function would do the job. I'll create a PR.

from embedded-nal.

ryan-summers avatar ryan-summers commented on June 29, 2024

In response to the discussions in #21, it has become apparent that in general, the embedded-nal traits do not support server-based embedded implementations and the crate is geared more towards embedded TCP/UDP clients. Hence, that's why there's no current way to specify a local UDP port (bind-like behavior). Similarly, this is not exposed for TCP either.

As part of a generic interface, I believe our design goal at this point is to aim for high adoption rates in using the embedded-nal, and I think the way we accomplish that is by keeping implementation bare-bones and simple.

In this case, an external library is likely operating as either a server or a client, but is unlikely to be both at the same time. As discussed in #21, it may make sense to create new traits for server-side behavior for the embedded-nal as well, as this means that network stacks that are either unable to or do not want to operate as a server do not need to implement the necessary traits.

from embedded-nal.

jonahd-g avatar jonahd-g commented on June 29, 2024

@ryan-summers I'm a bit confused on the benefit of having three separate traits, one of which is automatically implemented. How is that better than having a {Udp/Tcp}Client and a {Udp/Tcp}Server that extends it? It seems like your example code contradicts your explanation: explanation prescribes a shared "core" trait, but the example code instead has a "servercore" trait.

from embedded-nal.

MathiasKoch avatar MathiasKoch commented on June 29, 2024

I would personally much rather see the approach with server extending client, as the server without the rest of the functions doesn't make any sense at all.

I like the approach you have in your pr!

from embedded-nal.

ryan-summers avatar ryan-summers commented on June 29, 2024

@ryan-summers I'm a bit confused on the benefit of having three separate traits, one of which is automatically implemented. How is that better than having a {Udp/Tcp}Client and a {Udp/Tcp}Server that extends it?

After I saw your implementation, I was like "Oh right, that is exactly what I was trying to do" - I forgot the Rust syntax to express it, so I added the disclaimer to the top. I definitely think the PR has a better approach here as well after review.

I would personally much rather see the approach with server extending client, as the server without the rest of the functions doesn't make any sense at all.

I agree that the approach works well for UDP, but the TCP side of things gets a bit trickier because TCP servers aren't necessarily an extension of TCP clients. For example, a TCP server does not need to use connect(), but rather accept() and listen(). connect() is a client-only API. I'm curious to hear thoughts on the TCP side of things, but I'll open a new issue to track that, since it's out of scope of the UDP side of things.

from embedded-nal.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.