From 407b95bfd52e83e29685311d46fb0bef52f92e77 Mon Sep 17 00:00:00 2001 From: driftluo Date: Tue, 11 Jan 2022 17:36:02 +0800 Subject: [PATCH] fix: fix bind twice --- CHANGELOG.md | 5 +++++ tentacle/Cargo.toml | 2 +- tentacle/src/builder.rs | 18 ++++++------------ tentacle/src/context.rs | 2 +- tentacle/src/runtime/async_runtime.rs | 11 ++++++++++- tentacle/src/runtime/tokio_runtime.rs | 12 +++++++++++- 6 files changed, 34 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3957a0b..50ba115f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.4.0-beta.2 + +### Bug Fix +- Fix `bind` on listen can't return Error(#347) + ## 0.4.0-beta.1 ### Bug Fix diff --git a/tentacle/Cargo.toml b/tentacle/Cargo.toml index 2c864696..6a268b92 100644 --- a/tentacle/Cargo.toml +++ b/tentacle/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tentacle" -version = "0.4.0-beta.1" +version = "0.4.0-beta.2" license = "MIT" description = "Minimal implementation for a multiplexed p2p network framework." authors = ["piaoliu ", "Nervos Core Dev "] diff --git a/tentacle/src/builder.rs b/tentacle/src/builder.rs index 659995b5..a08216fb 100644 --- a/tentacle/src/builder.rs +++ b/tentacle/src/builder.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, io, sync::Arc, time::Duration}; +use std::{io, sync::Arc, time::Duration}; use nohash_hasher::IntMap; use tokio_util::codec::LengthDelimitedCodec; @@ -18,6 +18,7 @@ use crate::{ }; /// Builder for Service +#[derive(Default)] pub struct ServiceBuilder { inner: IntMap, key_pair: Option, @@ -177,6 +178,10 @@ impl ServiceBuilder { /// Ok(socket.into()) /// }); /// ``` + /// + /// ## Note + /// + /// User use `listen(2)` or `connect(2)` on this closure will cause abnormal behavior #[cfg(not(target_arch = "wasm32"))] pub fn tcp_config(mut self, f: F) -> Self where @@ -222,17 +227,6 @@ impl ServiceBuilder { } } -impl Default for ServiceBuilder { - fn default() -> Self { - ServiceBuilder { - inner: HashMap::default(), - key_pair: None, - forever: false, - config: ServiceConfig::default(), - } - } -} - pub(crate) type NameFn = Box String + Send + Sync>; pub(crate) type CodecFn = Box Box + Send + Sync>; pub(crate) type SessionHandleFn = diff --git a/tentacle/src/context.rs b/tentacle/src/context.rs index 5fec5f81..5bef9db4 100644 --- a/tentacle/src/context.rs +++ b/tentacle/src/context.rs @@ -428,6 +428,6 @@ impl<'a> Deref for ProtocolContextMutRef<'a> { impl<'a> DerefMut for ProtocolContextMutRef<'a> { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.inner + self.inner } } diff --git a/tentacle/src/runtime/async_runtime.rs b/tentacle/src/runtime/async_runtime.rs index 0062ee0c..2f53cd2f 100644 --- a/tentacle/src/runtime/async_runtime.rs +++ b/tentacle/src/runtime/async_runtime.rs @@ -145,7 +145,16 @@ mod os { let t = tcp_config(socket.into())?; t.inner }; - socket.bind(&addr.into())?; + // `bind` twice will return error + // + // code 22 means: + // EINVAL The socket is already bound to an address. + // ref: https://man7.org/linux/man-pages/man2/bind.2.html + if let Err(e) = socket.bind(&addr.into()) { + if Some(22) != e.raw_os_error() { + return Err(e); + } + } socket.listen(1024)?; let listen = std::net::TcpListener::from(socket); diff --git a/tentacle/src/runtime/tokio_runtime.rs b/tentacle/src/runtime/tokio_runtime.rs index feca89ba..2a2b8d8a 100644 --- a/tentacle/src/runtime/tokio_runtime.rs +++ b/tentacle/src/runtime/tokio_runtime.rs @@ -75,7 +75,17 @@ pub(crate) fn listen(addr: SocketAddr, tcp_config: TcpSocketConfig) -> io::Resul socket } }; - socket.bind(addr)?; + // `bind` twice will return error + // + // code 22 means: + // EINVAL The socket is already bound to an address. + // ref: https://man7.org/linux/man-pages/man2/bind.2.html + if let Err(e) = socket.bind(addr) { + if Some(22) != e.raw_os_error() { + return Err(e); + } + } + socket.listen(1024) }