Skip to content

add accept method #292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/asynchronous/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ impl Server {
Ok(())
}

pub async fn accept(&mut self, conn: Socket) -> std::io::Result<()> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Why does this need to be &mut self, and not just &self?

Also, should this be an async function? or a normal function that returns a Connection (or some kind or wrapper)
As an async function, &mut self is captured until the connection ends.
IIUC, returning the connection would allow accepting more than one connection.

Copy link
Author

@KarstenB KarstenB Jul 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean something like this:

    pub fn accept(&self, conn: Socket) -> AcceptedConnection {
        let delegate = ServerBuilder {
            services: self.services.clone(),
            streams: Arc::default(),
            shutdown_waiter: self.shutdown.subscribe(),
        };
        AcceptedConnection(Connection::new(conn, delegate))
    }
    //...
    
pub struct AcceptedConnection(Connection<ServerBuilder>);

impl AcceptedConnection {
    pub async fn run(self) -> std::io::Result<()>{
        self.0.run().await
    }
}

This would also work for me. The point of the accept method in my use case is through I only have single connection anyhow. So I wasn't bothered by the &mut. Anyhow, a newtype wrapper is necessary otherwise a whole bunch of things would need to become public to be usable, which was the issue with your first proposal.

I have this implementation on my accepted branch https://github.com/KarstenB/ttrpc-rust/blob/accepted/src/asynchronous/server.rs#L174

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KarstenB So why don't we change the &mut self to &self?

let delegate = ServerBuilder {
services: self.services.clone(),
streams: Arc::default(),
shutdown_waiter: self.shutdown.subscribe(),
};
Connection::new(conn, delegate).run().await
}

pub async fn shutdown(&mut self) -> Result<()> {
self.stop_listen().await;
self.disconnect().await;
Expand Down
Loading