From bcedd02ad95f6afd241e0759f5c3a33a1b26d32d Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 16 Apr 2020 20:43:36 +0200 Subject: [PATCH] libasync: add TcpSocket::{close, abort, keep_alive, timeout}() --- libasync/src/smoltcp/tcp_stream.rs | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/libasync/src/smoltcp/tcp_stream.rs b/libasync/src/smoltcp/tcp_stream.rs index c8146ae..9ea7d61 100644 --- a/libasync/src/smoltcp/tcp_stream.rs +++ b/libasync/src/smoltcp/tcp_stream.rs @@ -14,6 +14,7 @@ use smoltcp::{ SocketHandle, SocketRef, TcpSocketBuffer, TcpSocket, TcpState, }, + time::Duration, }; use crate::task; use super::Sockets; @@ -217,6 +218,38 @@ impl TcpStream { } }).await } + + /// Close the transmit half of the connection + pub async fn close(&self) { + self.with_socket(|mut socket| socket.close()); + + // Yield for one iface.poll() to send the packet + task::r#yield().await; + } + + /// Destroy the socket, sending the RST + pub async fn abort(self) { + self.with_socket(|mut socket| socket.abort()); + + // Yield for one iface.poll() to send the packet + task::r#yield().await; + } + + pub fn keep_alive(&self) -> Option { + self.with_socket(|socket| socket.keep_alive()) + } + + pub fn set_keep_alive(&mut self, interval: Option) { + self.with_socket(|mut socket| socket.set_keep_alive(interval)); + } + + pub fn timeout(&self) -> Option { + self.with_socket(|socket| socket.timeout()) + } + + pub fn set_timeout(&mut self, duration: Option) { + self.with_socket(|mut socket| socket.set_timeout(duration)); + } } impl Drop for TcpStream {