Skip to content
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

fix: fix bind twice #347

Merged
merged 1 commit into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
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
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 <driftluo@foxmail.com>", "Nervos Core Dev <dev@nervos.org>"]
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