From 499c01a7e5c196135327e8d5b39f49548fbf84b9 Mon Sep 17 00:00:00 2001
From: wuyingfengsui <wuyingfengsui55@sina.com>
Date: Sun, 17 Sep 2017 13:39:54 +0800
Subject: [PATCH] new_with_specific_source

---
 src/client.rs | 36 ++++++++++++++++++++----------------
 1 file changed, 20 insertions(+), 16 deletions(-)

diff --git a/src/client.rs b/src/client.rs
index dcac775..390a320 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -18,31 +18,35 @@ pub struct CoAPClient {
 }
 
 impl CoAPClient {
-    /// Create a CoAP client with the peer address.
-    pub fn new<A: ToSocketAddrs>(addr: A) -> Result<CoAPClient> {
-        addr.to_socket_addrs().and_then(|mut iter| {
+    /// Create a CoAP client with the specific source and peer address.
+    pub fn new_with_specific_source<A: ToSocketAddrs, B: ToSocketAddrs>(bind_addr: A, peer_addr: B) -> Result<CoAPClient> {
+        peer_addr.to_socket_addrs().and_then(|mut iter| {
             match iter.next() {
-                Some(SocketAddr::V4(a)) => {
-                    UdpSocket::bind("0.0.0.0:0").and_then(|s| {
+                Some(paddr) => {
+                    UdpSocket::bind(bind_addr).and_then(|s| {
                         s.set_read_timeout(Some(Duration::new(DEFAULT_RECEIVE_TIMEOUT, 0)))
                             .and_then(|_| {
                                 Ok(CoAPClient {
                                     socket: s,
-                                    peer_addr: SocketAddr::V4(a),
+                                    peer_addr: paddr,
                                 })
                             })
                     })
                 }
-                Some(SocketAddr::V6(a)) => {
-                    UdpSocket::bind(":::0").and_then(|s| {
-                        s.set_read_timeout(Some(Duration::new(DEFAULT_RECEIVE_TIMEOUT, 0)))
-                            .and_then(|_| {
-                                Ok(CoAPClient {
-                                    socket: s,
-                                    peer_addr: SocketAddr::V6(a),
-                                })
-                            })
-                    })
+                None => Err(Error::new(ErrorKind::Other, "no address")),
+            }
+        })
+    }
+
+    /// Create a CoAP client with the peer address.
+    pub fn new<A: ToSocketAddrs>(addr: A) -> Result<CoAPClient> {
+        addr.to_socket_addrs().and_then(|mut iter| {
+            match iter.next() {
+                Some(SocketAddr::V4(_)) => {
+                    Self::new_with_specific_source("0.0.0.0:0", addr)
+                }
+                Some(SocketAddr::V6(_)) => {
+                    Self::new_with_specific_source(":::0", addr)
                 }
                 None => Err(Error::new(ErrorKind::Other, "no address")),
             }
-- 
GitLab