Skip to content

Commit

Permalink
Merge pull request #347 from nervosnetwork/fix-bind-twice
Browse files Browse the repository at this point in the history
fix: fix bind twice
  • Loading branch information
driftluo authored Jan 11, 2022
2 parents 3f1e0a4 + 407b95b commit 496fe8e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion tentacle/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>", "Nervos Core Dev <[email protected]>"]
Expand Down
18 changes: 6 additions & 12 deletions tentacle/src/builder.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,6 +18,7 @@ use crate::{
};

/// Builder for Service
#[derive(Default)]
pub struct ServiceBuilder {
inner: IntMap<ProtocolId, ProtocolMeta>,
key_pair: Option<SecioKeyPair>,
Expand Down Expand Up @@ -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<F>(mut self, f: F) -> Self
where
Expand Down Expand Up @@ -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<dyn Fn(ProtocolId) -> String + Send + Sync>;
pub(crate) type CodecFn = Box<dyn Fn() -> Box<dyn Codec + Send + 'static> + Send + Sync>;
pub(crate) type SessionHandleFn =
Expand Down
2 changes: 1 addition & 1 deletion tentacle/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
11 changes: 10 additions & 1 deletion tentacle/src/runtime/async_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 11 additions & 1 deletion tentacle/src/runtime/tokio_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 496fe8e

Please sign in to comment.