Browse Source

how to await tokio_timer::sleep?

open-trade 5 years ago
parent
commit
f7cb0cfde6
6 changed files with 826 additions and 74 deletions
  1. 46 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 15 2
      protos/message.proto
  4. 0 2
      protos/mod.rs
  5. 687 58
      src/protos/message.rs
  6. 77 12
      src/rendezvous_server.rs

+ 46 - 0
Cargo.lock

@@ -23,6 +23,11 @@ dependencies = [
23 23
  "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
24 24
 ]
25 25
 
26
+[[package]]
27
+name = "autocfg"
28
+version = "1.0.0"
29
+source = "registry+https://github.com/rust-lang/crates.io-index"
30
+
26 31
 [[package]]
27 32
 name = "bitflags"
28 33
 version = "1.2.1"
@@ -38,6 +43,16 @@ name = "cfg-if"
38 43
 version = "0.1.10"
39 44
 source = "registry+https://github.com/rust-lang/crates.io-index"
40 45
 
46
+[[package]]
47
+name = "crossbeam-utils"
48
+version = "0.7.2"
49
+source = "registry+https://github.com/rust-lang/crates.io-index"
50
+dependencies = [
51
+ "autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
52
+ "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
53
+ "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
54
+]
55
+
41 56
 [[package]]
42 57
 name = "env_logger"
43 58
 version = "0.7.1"
@@ -69,6 +84,11 @@ name = "fuchsia-zircon-sys"
69 84
 version = "0.3.3"
70 85
 source = "registry+https://github.com/rust-lang/crates.io-index"
71 86
 
87
+[[package]]
88
+name = "futures"
89
+version = "0.1.29"
90
+source = "registry+https://github.com/rust-lang/crates.io-index"
91
+
72 92
 [[package]]
73 93
 name = "futures"
74 94
 version = "0.3.4"
@@ -162,6 +182,7 @@ dependencies = [
162 182
  "protobuf 2.10.2 (registry+https://github.com/rust-lang/crates.io-index)",
163 183
  "protobuf-codegen-pure 2.10.2 (registry+https://github.com/rust-lang/crates.io-index)",
164 184
  "tokio 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
185
+ "tokio-timer 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
165 186
  "tokio-util 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
166 187
 ]
167 188
 
@@ -462,6 +483,15 @@ dependencies = [
462 483
  "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
463 484
 ]
464 485
 
486
+[[package]]
487
+name = "tokio-executor"
488
+version = "0.1.10"
489
+source = "registry+https://github.com/rust-lang/crates.io-index"
490
+dependencies = [
491
+ "crossbeam-utils 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
492
+ "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)",
493
+]
494
+
465 495
 [[package]]
466 496
 name = "tokio-macros"
467 497
 version = "0.2.5"
@@ -472,6 +502,17 @@ dependencies = [
472 502
  "syn 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
473 503
 ]
474 504
 
505
+[[package]]
506
+name = "tokio-timer"
507
+version = "0.2.13"
508
+source = "registry+https://github.com/rust-lang/crates.io-index"
509
+dependencies = [
510
+ "crossbeam-utils 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
511
+ "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)",
512
+ "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
513
+ "tokio-executor 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
514
+]
515
+
475 516
 [[package]]
476 517
 name = "tokio-util"
477 518
 version = "0.3.0"
@@ -541,13 +582,16 @@ dependencies = [
541 582
 "checksum aho-corasick 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)" = "d5e63fd144e18ba274ae7095c0197a870a7b9468abc801dd62f190d80817d2ec"
542 583
 "checksum arc-swap 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d7b8a9123b8027467bce0099fe556c628a53c8d83df0507084c31e9ba2e39aff"
543 584
 "checksum atty 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
585
+"checksum autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
544 586
 "checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
545 587
 "checksum bytes 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "130aac562c0dd69c56b3b1cc8ffd2e17be31d0b6c25b61c96b76231aa23e39e1"
546 588
 "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
589
+"checksum crossbeam-utils 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8"
547 590
 "checksum env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
548 591
 "checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3"
549 592
 "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
550 593
 "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
594
+"checksum futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)" = "1b980f2816d6ee8673b6517b52cb0e808a180efc92e5c19d02cdda79066703ef"
551 595
 "checksum futures 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "5c329ae8753502fb44ae4fc2b622fa2a94652c41e795143765ba0927f92ab780"
552 596
 "checksum futures-channel 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f0c77d04ce8edd9cb903932b608268b3fffec4163dc053b3b402bf47eac1f1a8"
553 597
 "checksum futures-core 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f25592f769825e89b92358db00d26f965761e094951ac44d3663ef25b7ac464a"
@@ -592,7 +636,9 @@ dependencies = [
592 636
 "checksum termcolor 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f"
593 637
 "checksum thread_local 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"
594 638
 "checksum tokio 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "0fa5e81d6bc4e67fe889d5783bd2a128ab2e0cfa487e0be16b6a8d177b101616"
639
+"checksum tokio-executor 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "fb2d1b8f4548dbf5e1f7818512e9c406860678f29c300cdf0ebac72d1a3a1671"
595 640
 "checksum tokio-macros 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f0c3acc6aa564495a0f2e1d59fab677cd7f81a19994cfc7f3ad0e64301560389"
641
+"checksum tokio-timer 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "93044f2d313c95ff1cb7809ce9a7a05735b012288a888b62d4434fd58c94f296"
596 642
 "checksum tokio-util 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "af67cdce2b40f8dffb0ee04c853a24217b5d0d3e358f0f5ccc0b5332174ed9a8"
597 643
 "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
598 644
 "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"

+ 1 - 0
Cargo.toml

@@ -10,6 +10,7 @@ edition = "2018"
10 10
 tokio = { version = "0.2", features = ["full"] }
11 11
 protobuf = "2.10"
12 12
 tokio-util = { version = "0.3", features = ["full"] }
13
+tokio-timer = "0.2"
13 14
 log = "0.4"
14 15
 env_logger = "0.7"
15 16
 futures = "0.3"

+ 15 - 2
protos/message.proto

@@ -1,9 +1,22 @@
1 1
 syntax = "proto3";
2 2
 package hbb;
3 3
 
4
+message RegisterPeer {
5
+  string hbb_addr = 1;
6
+}
7
+
8
+message PeekPeer {
9
+  string hbb_addr = 1;
10
+}
11
+
12
+message PeekPeerResponse {
13
+  bytes socket_addr = 1;
14
+}
15
+
4 16
 message Message {
5
-  string addr = 1; // hbb address
6 17
   oneof union {
7
-    bytes socket_addr = 6;
18
+    RegisterPeer register_peer = 6;
19
+    PeekPeer peek_peer = 7;
20
+    PeekPeerResponse peek_peer_response = 8;
8 21
   }
9 22
 }

+ 0 - 2
protos/mod.rs

@@ -1,2 +0,0 @@
1
-mod message;
2
-pub use message::*;

+ 687 - 58
src/protos/message.rs

@@ -27,9 +27,514 @@ use protobuf::ProtobufEnum as ProtobufEnum_imported_for_functions;
27 27
 // const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_10_2;
28 28
 
29 29
 #[derive(PartialEq,Clone,Default)]
30
-pub struct Message {
30
+pub struct RegisterPeer {
31
+    // message fields
32
+    pub hbb_addr: ::std::string::String,
33
+    // special fields
34
+    pub unknown_fields: ::protobuf::UnknownFields,
35
+    pub cached_size: ::protobuf::CachedSize,
36
+}
37
+
38
+impl<'a> ::std::default::Default for &'a RegisterPeer {
39
+    fn default() -> &'a RegisterPeer {
40
+        <RegisterPeer as ::protobuf::Message>::default_instance()
41
+    }
42
+}
43
+
44
+impl RegisterPeer {
45
+    pub fn new() -> RegisterPeer {
46
+        ::std::default::Default::default()
47
+    }
48
+
49
+    // string hbb_addr = 1;
50
+
51
+
52
+    pub fn get_hbb_addr(&self) -> &str {
53
+        &self.hbb_addr
54
+    }
55
+    pub fn clear_hbb_addr(&mut self) {
56
+        self.hbb_addr.clear();
57
+    }
58
+
59
+    // Param is passed by value, moved
60
+    pub fn set_hbb_addr(&mut self, v: ::std::string::String) {
61
+        self.hbb_addr = v;
62
+    }
63
+
64
+    // Mutable pointer to the field.
65
+    // If field is not initialized, it is initialized with default value first.
66
+    pub fn mut_hbb_addr(&mut self) -> &mut ::std::string::String {
67
+        &mut self.hbb_addr
68
+    }
69
+
70
+    // Take field
71
+    pub fn take_hbb_addr(&mut self) -> ::std::string::String {
72
+        ::std::mem::replace(&mut self.hbb_addr, ::std::string::String::new())
73
+    }
74
+}
75
+
76
+impl ::protobuf::Message for RegisterPeer {
77
+    fn is_initialized(&self) -> bool {
78
+        true
79
+    }
80
+
81
+    fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream<'_>) -> ::protobuf::ProtobufResult<()> {
82
+        while !is.eof()? {
83
+            let (field_number, wire_type) = is.read_tag_unpack()?;
84
+            match field_number {
85
+                1 => {
86
+                    ::protobuf::rt::read_singular_proto3_string_into(wire_type, is, &mut self.hbb_addr)?;
87
+                },
88
+                _ => {
89
+                    ::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?;
90
+                },
91
+            };
92
+        }
93
+        ::std::result::Result::Ok(())
94
+    }
95
+
96
+    // Compute sizes of nested messages
97
+    #[allow(unused_variables)]
98
+    fn compute_size(&self) -> u32 {
99
+        let mut my_size = 0;
100
+        if !self.hbb_addr.is_empty() {
101
+            my_size += ::protobuf::rt::string_size(1, &self.hbb_addr);
102
+        }
103
+        my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields());
104
+        self.cached_size.set(my_size);
105
+        my_size
106
+    }
107
+
108
+    fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream<'_>) -> ::protobuf::ProtobufResult<()> {
109
+        if !self.hbb_addr.is_empty() {
110
+            os.write_string(1, &self.hbb_addr)?;
111
+        }
112
+        os.write_unknown_fields(self.get_unknown_fields())?;
113
+        ::std::result::Result::Ok(())
114
+    }
115
+
116
+    fn get_cached_size(&self) -> u32 {
117
+        self.cached_size.get()
118
+    }
119
+
120
+    fn get_unknown_fields(&self) -> &::protobuf::UnknownFields {
121
+        &self.unknown_fields
122
+    }
123
+
124
+    fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields {
125
+        &mut self.unknown_fields
126
+    }
127
+
128
+    fn as_any(&self) -> &dyn (::std::any::Any) {
129
+        self as &dyn (::std::any::Any)
130
+    }
131
+    fn as_any_mut(&mut self) -> &mut dyn (::std::any::Any) {
132
+        self as &mut dyn (::std::any::Any)
133
+    }
134
+    fn into_any(self: Box<Self>) -> ::std::boxed::Box<dyn (::std::any::Any)> {
135
+        self
136
+    }
137
+
138
+    fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor {
139
+        Self::descriptor_static()
140
+    }
141
+
142
+    fn new() -> RegisterPeer {
143
+        RegisterPeer::new()
144
+    }
145
+
146
+    fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor {
147
+        static mut descriptor: ::protobuf::lazy::Lazy<::protobuf::reflect::MessageDescriptor> = ::protobuf::lazy::Lazy {
148
+            lock: ::protobuf::lazy::ONCE_INIT,
149
+            ptr: 0 as *const ::protobuf::reflect::MessageDescriptor,
150
+        };
151
+        unsafe {
152
+            descriptor.get(|| {
153
+                let mut fields = ::std::vec::Vec::new();
154
+                fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeString>(
155
+                    "hbb_addr",
156
+                    |m: &RegisterPeer| { &m.hbb_addr },
157
+                    |m: &mut RegisterPeer| { &mut m.hbb_addr },
158
+                ));
159
+                ::protobuf::reflect::MessageDescriptor::new::<RegisterPeer>(
160
+                    "RegisterPeer",
161
+                    fields,
162
+                    file_descriptor_proto()
163
+                )
164
+            })
165
+        }
166
+    }
167
+
168
+    fn default_instance() -> &'static RegisterPeer {
169
+        static mut instance: ::protobuf::lazy::Lazy<RegisterPeer> = ::protobuf::lazy::Lazy {
170
+            lock: ::protobuf::lazy::ONCE_INIT,
171
+            ptr: 0 as *const RegisterPeer,
172
+        };
173
+        unsafe {
174
+            instance.get(RegisterPeer::new)
175
+        }
176
+    }
177
+}
178
+
179
+impl ::protobuf::Clear for RegisterPeer {
180
+    fn clear(&mut self) {
181
+        self.hbb_addr.clear();
182
+        self.unknown_fields.clear();
183
+    }
184
+}
185
+
186
+impl ::std::fmt::Debug for RegisterPeer {
187
+    fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
188
+        ::protobuf::text_format::fmt(self, f)
189
+    }
190
+}
191
+
192
+impl ::protobuf::reflect::ProtobufValue for RegisterPeer {
193
+    fn as_ref(&self) -> ::protobuf::reflect::ProtobufValueRef {
194
+        ::protobuf::reflect::ProtobufValueRef::Message(self)
195
+    }
196
+}
197
+
198
+#[derive(PartialEq,Clone,Default)]
199
+pub struct PeekPeer {
31 200
     // message fields
32
-    pub addr: ::std::string::String,
201
+    pub hbb_addr: ::std::string::String,
202
+    // special fields
203
+    pub unknown_fields: ::protobuf::UnknownFields,
204
+    pub cached_size: ::protobuf::CachedSize,
205
+}
206
+
207
+impl<'a> ::std::default::Default for &'a PeekPeer {
208
+    fn default() -> &'a PeekPeer {
209
+        <PeekPeer as ::protobuf::Message>::default_instance()
210
+    }
211
+}
212
+
213
+impl PeekPeer {
214
+    pub fn new() -> PeekPeer {
215
+        ::std::default::Default::default()
216
+    }
217
+
218
+    // string hbb_addr = 1;
219
+
220
+
221
+    pub fn get_hbb_addr(&self) -> &str {
222
+        &self.hbb_addr
223
+    }
224
+    pub fn clear_hbb_addr(&mut self) {
225
+        self.hbb_addr.clear();
226
+    }
227
+
228
+    // Param is passed by value, moved
229
+    pub fn set_hbb_addr(&mut self, v: ::std::string::String) {
230
+        self.hbb_addr = v;
231
+    }
232
+
233
+    // Mutable pointer to the field.
234
+    // If field is not initialized, it is initialized with default value first.
235
+    pub fn mut_hbb_addr(&mut self) -> &mut ::std::string::String {
236
+        &mut self.hbb_addr
237
+    }
238
+
239
+    // Take field
240
+    pub fn take_hbb_addr(&mut self) -> ::std::string::String {
241
+        ::std::mem::replace(&mut self.hbb_addr, ::std::string::String::new())
242
+    }
243
+}
244
+
245
+impl ::protobuf::Message for PeekPeer {
246
+    fn is_initialized(&self) -> bool {
247
+        true
248
+    }
249
+
250
+    fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream<'_>) -> ::protobuf::ProtobufResult<()> {
251
+        while !is.eof()? {
252
+            let (field_number, wire_type) = is.read_tag_unpack()?;
253
+            match field_number {
254
+                1 => {
255
+                    ::protobuf::rt::read_singular_proto3_string_into(wire_type, is, &mut self.hbb_addr)?;
256
+                },
257
+                _ => {
258
+                    ::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?;
259
+                },
260
+            };
261
+        }
262
+        ::std::result::Result::Ok(())
263
+    }
264
+
265
+    // Compute sizes of nested messages
266
+    #[allow(unused_variables)]
267
+    fn compute_size(&self) -> u32 {
268
+        let mut my_size = 0;
269
+        if !self.hbb_addr.is_empty() {
270
+            my_size += ::protobuf::rt::string_size(1, &self.hbb_addr);
271
+        }
272
+        my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields());
273
+        self.cached_size.set(my_size);
274
+        my_size
275
+    }
276
+
277
+    fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream<'_>) -> ::protobuf::ProtobufResult<()> {
278
+        if !self.hbb_addr.is_empty() {
279
+            os.write_string(1, &self.hbb_addr)?;
280
+        }
281
+        os.write_unknown_fields(self.get_unknown_fields())?;
282
+        ::std::result::Result::Ok(())
283
+    }
284
+
285
+    fn get_cached_size(&self) -> u32 {
286
+        self.cached_size.get()
287
+    }
288
+
289
+    fn get_unknown_fields(&self) -> &::protobuf::UnknownFields {
290
+        &self.unknown_fields
291
+    }
292
+
293
+    fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields {
294
+        &mut self.unknown_fields
295
+    }
296
+
297
+    fn as_any(&self) -> &dyn (::std::any::Any) {
298
+        self as &dyn (::std::any::Any)
299
+    }
300
+    fn as_any_mut(&mut self) -> &mut dyn (::std::any::Any) {
301
+        self as &mut dyn (::std::any::Any)
302
+    }
303
+    fn into_any(self: Box<Self>) -> ::std::boxed::Box<dyn (::std::any::Any)> {
304
+        self
305
+    }
306
+
307
+    fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor {
308
+        Self::descriptor_static()
309
+    }
310
+
311
+    fn new() -> PeekPeer {
312
+        PeekPeer::new()
313
+    }
314
+
315
+    fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor {
316
+        static mut descriptor: ::protobuf::lazy::Lazy<::protobuf::reflect::MessageDescriptor> = ::protobuf::lazy::Lazy {
317
+            lock: ::protobuf::lazy::ONCE_INIT,
318
+            ptr: 0 as *const ::protobuf::reflect::MessageDescriptor,
319
+        };
320
+        unsafe {
321
+            descriptor.get(|| {
322
+                let mut fields = ::std::vec::Vec::new();
323
+                fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeString>(
324
+                    "hbb_addr",
325
+                    |m: &PeekPeer| { &m.hbb_addr },
326
+                    |m: &mut PeekPeer| { &mut m.hbb_addr },
327
+                ));
328
+                ::protobuf::reflect::MessageDescriptor::new::<PeekPeer>(
329
+                    "PeekPeer",
330
+                    fields,
331
+                    file_descriptor_proto()
332
+                )
333
+            })
334
+        }
335
+    }
336
+
337
+    fn default_instance() -> &'static PeekPeer {
338
+        static mut instance: ::protobuf::lazy::Lazy<PeekPeer> = ::protobuf::lazy::Lazy {
339
+            lock: ::protobuf::lazy::ONCE_INIT,
340
+            ptr: 0 as *const PeekPeer,
341
+        };
342
+        unsafe {
343
+            instance.get(PeekPeer::new)
344
+        }
345
+    }
346
+}
347
+
348
+impl ::protobuf::Clear for PeekPeer {
349
+    fn clear(&mut self) {
350
+        self.hbb_addr.clear();
351
+        self.unknown_fields.clear();
352
+    }
353
+}
354
+
355
+impl ::std::fmt::Debug for PeekPeer {
356
+    fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
357
+        ::protobuf::text_format::fmt(self, f)
358
+    }
359
+}
360
+
361
+impl ::protobuf::reflect::ProtobufValue for PeekPeer {
362
+    fn as_ref(&self) -> ::protobuf::reflect::ProtobufValueRef {
363
+        ::protobuf::reflect::ProtobufValueRef::Message(self)
364
+    }
365
+}
366
+
367
+#[derive(PartialEq,Clone,Default)]
368
+pub struct PeekPeerResponse {
369
+    // message fields
370
+    pub socket_addr: ::std::vec::Vec<u8>,
371
+    // special fields
372
+    pub unknown_fields: ::protobuf::UnknownFields,
373
+    pub cached_size: ::protobuf::CachedSize,
374
+}
375
+
376
+impl<'a> ::std::default::Default for &'a PeekPeerResponse {
377
+    fn default() -> &'a PeekPeerResponse {
378
+        <PeekPeerResponse as ::protobuf::Message>::default_instance()
379
+    }
380
+}
381
+
382
+impl PeekPeerResponse {
383
+    pub fn new() -> PeekPeerResponse {
384
+        ::std::default::Default::default()
385
+    }
386
+
387
+    // bytes socket_addr = 1;
388
+
389
+
390
+    pub fn get_socket_addr(&self) -> &[u8] {
391
+        &self.socket_addr
392
+    }
393
+    pub fn clear_socket_addr(&mut self) {
394
+        self.socket_addr.clear();
395
+    }
396
+
397
+    // Param is passed by value, moved
398
+    pub fn set_socket_addr(&mut self, v: ::std::vec::Vec<u8>) {
399
+        self.socket_addr = v;
400
+    }
401
+
402
+    // Mutable pointer to the field.
403
+    // If field is not initialized, it is initialized with default value first.
404
+    pub fn mut_socket_addr(&mut self) -> &mut ::std::vec::Vec<u8> {
405
+        &mut self.socket_addr
406
+    }
407
+
408
+    // Take field
409
+    pub fn take_socket_addr(&mut self) -> ::std::vec::Vec<u8> {
410
+        ::std::mem::replace(&mut self.socket_addr, ::std::vec::Vec::new())
411
+    }
412
+}
413
+
414
+impl ::protobuf::Message for PeekPeerResponse {
415
+    fn is_initialized(&self) -> bool {
416
+        true
417
+    }
418
+
419
+    fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream<'_>) -> ::protobuf::ProtobufResult<()> {
420
+        while !is.eof()? {
421
+            let (field_number, wire_type) = is.read_tag_unpack()?;
422
+            match field_number {
423
+                1 => {
424
+                    ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.socket_addr)?;
425
+                },
426
+                _ => {
427
+                    ::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?;
428
+                },
429
+            };
430
+        }
431
+        ::std::result::Result::Ok(())
432
+    }
433
+
434
+    // Compute sizes of nested messages
435
+    #[allow(unused_variables)]
436
+    fn compute_size(&self) -> u32 {
437
+        let mut my_size = 0;
438
+        if !self.socket_addr.is_empty() {
439
+            my_size += ::protobuf::rt::bytes_size(1, &self.socket_addr);
440
+        }
441
+        my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields());
442
+        self.cached_size.set(my_size);
443
+        my_size
444
+    }
445
+
446
+    fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream<'_>) -> ::protobuf::ProtobufResult<()> {
447
+        if !self.socket_addr.is_empty() {
448
+            os.write_bytes(1, &self.socket_addr)?;
449
+        }
450
+        os.write_unknown_fields(self.get_unknown_fields())?;
451
+        ::std::result::Result::Ok(())
452
+    }
453
+
454
+    fn get_cached_size(&self) -> u32 {
455
+        self.cached_size.get()
456
+    }
457
+
458
+    fn get_unknown_fields(&self) -> &::protobuf::UnknownFields {
459
+        &self.unknown_fields
460
+    }
461
+
462
+    fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields {
463
+        &mut self.unknown_fields
464
+    }
465
+
466
+    fn as_any(&self) -> &dyn (::std::any::Any) {
467
+        self as &dyn (::std::any::Any)
468
+    }
469
+    fn as_any_mut(&mut self) -> &mut dyn (::std::any::Any) {
470
+        self as &mut dyn (::std::any::Any)
471
+    }
472
+    fn into_any(self: Box<Self>) -> ::std::boxed::Box<dyn (::std::any::Any)> {
473
+        self
474
+    }
475
+
476
+    fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor {
477
+        Self::descriptor_static()
478
+    }
479
+
480
+    fn new() -> PeekPeerResponse {
481
+        PeekPeerResponse::new()
482
+    }
483
+
484
+    fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor {
485
+        static mut descriptor: ::protobuf::lazy::Lazy<::protobuf::reflect::MessageDescriptor> = ::protobuf::lazy::Lazy {
486
+            lock: ::protobuf::lazy::ONCE_INIT,
487
+            ptr: 0 as *const ::protobuf::reflect::MessageDescriptor,
488
+        };
489
+        unsafe {
490
+            descriptor.get(|| {
491
+                let mut fields = ::std::vec::Vec::new();
492
+                fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>(
493
+                    "socket_addr",
494
+                    |m: &PeekPeerResponse| { &m.socket_addr },
495
+                    |m: &mut PeekPeerResponse| { &mut m.socket_addr },
496
+                ));
497
+                ::protobuf::reflect::MessageDescriptor::new::<PeekPeerResponse>(
498
+                    "PeekPeerResponse",
499
+                    fields,
500
+                    file_descriptor_proto()
501
+                )
502
+            })
503
+        }
504
+    }
505
+
506
+    fn default_instance() -> &'static PeekPeerResponse {
507
+        static mut instance: ::protobuf::lazy::Lazy<PeekPeerResponse> = ::protobuf::lazy::Lazy {
508
+            lock: ::protobuf::lazy::ONCE_INIT,
509
+            ptr: 0 as *const PeekPeerResponse,
510
+        };
511
+        unsafe {
512
+            instance.get(PeekPeerResponse::new)
513
+        }
514
+    }
515
+}
516
+
517
+impl ::protobuf::Clear for PeekPeerResponse {
518
+    fn clear(&mut self) {
519
+        self.socket_addr.clear();
520
+        self.unknown_fields.clear();
521
+    }
522
+}
523
+
524
+impl ::std::fmt::Debug for PeekPeerResponse {
525
+    fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
526
+        ::protobuf::text_format::fmt(self, f)
527
+    }
528
+}
529
+
530
+impl ::protobuf::reflect::ProtobufValue for PeekPeerResponse {
531
+    fn as_ref(&self) -> ::protobuf::reflect::ProtobufValueRef {
532
+        ::protobuf::reflect::ProtobufValueRef::Message(self)
533
+    }
534
+}
535
+
536
+#[derive(PartialEq,Clone,Default)]
537
+pub struct Message {
33 538
     // message oneof groups
34 539
     pub union: ::std::option::Option<Message_oneof_union>,
35 540
     // special fields
@@ -45,7 +550,9 @@ impl<'a> ::std::default::Default for &'a Message {
45 550
 
46 551
 #[derive(Clone,PartialEq,Debug)]
47 552
 pub enum Message_oneof_union {
48
-    socket_addr(::std::vec::Vec<u8>),
553
+    register_peer(RegisterPeer),
554
+    peek_peer(PeekPeer),
555
+    peek_peer_response(PeekPeerResponse),
49 556
 }
50 557
 
51 558
 impl Message {
@@ -53,84 +560,171 @@ impl Message {
53 560
         ::std::default::Default::default()
54 561
     }
55 562
 
56
-    // string addr = 1;
563
+    // .hbb.RegisterPeer register_peer = 6;
57 564
 
58 565
 
59
-    pub fn get_addr(&self) -> &str {
60
-        &self.addr
566
+    pub fn get_register_peer(&self) -> &RegisterPeer {
567
+        match self.union {
568
+            ::std::option::Option::Some(Message_oneof_union::register_peer(ref v)) => v,
569
+            _ => RegisterPeer::default_instance(),
570
+        }
61 571
     }
62
-    pub fn clear_addr(&mut self) {
63
-        self.addr.clear();
572
+    pub fn clear_register_peer(&mut self) {
573
+        self.union = ::std::option::Option::None;
574
+    }
575
+
576
+    pub fn has_register_peer(&self) -> bool {
577
+        match self.union {
578
+            ::std::option::Option::Some(Message_oneof_union::register_peer(..)) => true,
579
+            _ => false,
580
+        }
64 581
     }
65 582
 
66 583
     // Param is passed by value, moved
67
-    pub fn set_addr(&mut self, v: ::std::string::String) {
68
-        self.addr = v;
584
+    pub fn set_register_peer(&mut self, v: RegisterPeer) {
585
+        self.union = ::std::option::Option::Some(Message_oneof_union::register_peer(v))
69 586
     }
70 587
 
71 588
     // Mutable pointer to the field.
72
-    // If field is not initialized, it is initialized with default value first.
73
-    pub fn mut_addr(&mut self) -> &mut ::std::string::String {
74
-        &mut self.addr
589
+    pub fn mut_register_peer(&mut self) -> &mut RegisterPeer {
590
+        if let ::std::option::Option::Some(Message_oneof_union::register_peer(_)) = self.union {
591
+        } else {
592
+            self.union = ::std::option::Option::Some(Message_oneof_union::register_peer(RegisterPeer::new()));
593
+        }
594
+        match self.union {
595
+            ::std::option::Option::Some(Message_oneof_union::register_peer(ref mut v)) => v,
596
+            _ => panic!(),
597
+        }
75 598
     }
76 599
 
77 600
     // Take field
78
-    pub fn take_addr(&mut self) -> ::std::string::String {
79
-        ::std::mem::replace(&mut self.addr, ::std::string::String::new())
601
+    pub fn take_register_peer(&mut self) -> RegisterPeer {
602
+        if self.has_register_peer() {
603
+            match self.union.take() {
604
+                ::std::option::Option::Some(Message_oneof_union::register_peer(v)) => v,
605
+                _ => panic!(),
606
+            }
607
+        } else {
608
+            RegisterPeer::new()
609
+        }
80 610
     }
81 611
 
82
-    // bytes socket_addr = 6;
612
+    // .hbb.PeekPeer peek_peer = 7;
83 613
 
84 614
 
85
-    pub fn get_socket_addr(&self) -> &[u8] {
615
+    pub fn get_peek_peer(&self) -> &PeekPeer {
86 616
         match self.union {
87
-            ::std::option::Option::Some(Message_oneof_union::socket_addr(ref v)) => v,
88
-            _ => &[],
617
+            ::std::option::Option::Some(Message_oneof_union::peek_peer(ref v)) => v,
618
+            _ => PeekPeer::default_instance(),
89 619
         }
90 620
     }
91
-    pub fn clear_socket_addr(&mut self) {
621
+    pub fn clear_peek_peer(&mut self) {
92 622
         self.union = ::std::option::Option::None;
93 623
     }
94 624
 
95
-    pub fn has_socket_addr(&self) -> bool {
625
+    pub fn has_peek_peer(&self) -> bool {
96 626
         match self.union {
97
-            ::std::option::Option::Some(Message_oneof_union::socket_addr(..)) => true,
627
+            ::std::option::Option::Some(Message_oneof_union::peek_peer(..)) => true,
98 628
             _ => false,
99 629
         }
100 630
     }
101 631
 
102 632
     // Param is passed by value, moved
103
-    pub fn set_socket_addr(&mut self, v: ::std::vec::Vec<u8>) {
104
-        self.union = ::std::option::Option::Some(Message_oneof_union::socket_addr(v))
633
+    pub fn set_peek_peer(&mut self, v: PeekPeer) {
634
+        self.union = ::std::option::Option::Some(Message_oneof_union::peek_peer(v))
105 635
     }
106 636
 
107 637
     // Mutable pointer to the field.
108
-    pub fn mut_socket_addr(&mut self) -> &mut ::std::vec::Vec<u8> {
109
-        if let ::std::option::Option::Some(Message_oneof_union::socket_addr(_)) = self.union {
638
+    pub fn mut_peek_peer(&mut self) -> &mut PeekPeer {
639
+        if let ::std::option::Option::Some(Message_oneof_union::peek_peer(_)) = self.union {
110 640
         } else {
111
-            self.union = ::std::option::Option::Some(Message_oneof_union::socket_addr(::std::vec::Vec::new()));
641
+            self.union = ::std::option::Option::Some(Message_oneof_union::peek_peer(PeekPeer::new()));
112 642
         }
113 643
         match self.union {
114
-            ::std::option::Option::Some(Message_oneof_union::socket_addr(ref mut v)) => v,
644
+            ::std::option::Option::Some(Message_oneof_union::peek_peer(ref mut v)) => v,
115 645
             _ => panic!(),
116 646
         }
117 647
     }
118 648
 
119 649
     // Take field
120
-    pub fn take_socket_addr(&mut self) -> ::std::vec::Vec<u8> {
121
-        if self.has_socket_addr() {
650
+    pub fn take_peek_peer(&mut self) -> PeekPeer {
651
+        if self.has_peek_peer() {
122 652
             match self.union.take() {
123
-                ::std::option::Option::Some(Message_oneof_union::socket_addr(v)) => v,
653
+                ::std::option::Option::Some(Message_oneof_union::peek_peer(v)) => v,
124 654
                 _ => panic!(),
125 655
             }
126 656
         } else {
127
-            ::std::vec::Vec::new()
657
+            PeekPeer::new()
658
+        }
659
+    }
660
+
661
+    // .hbb.PeekPeerResponse peek_peer_response = 8;
662
+
663
+
664
+    pub fn get_peek_peer_response(&self) -> &PeekPeerResponse {
665
+        match self.union {
666
+            ::std::option::Option::Some(Message_oneof_union::peek_peer_response(ref v)) => v,
667
+            _ => PeekPeerResponse::default_instance(),
668
+        }
669
+    }
670
+    pub fn clear_peek_peer_response(&mut self) {
671
+        self.union = ::std::option::Option::None;
672
+    }
673
+
674
+    pub fn has_peek_peer_response(&self) -> bool {
675
+        match self.union {
676
+            ::std::option::Option::Some(Message_oneof_union::peek_peer_response(..)) => true,
677
+            _ => false,
678
+        }
679
+    }
680
+
681
+    // Param is passed by value, moved
682
+    pub fn set_peek_peer_response(&mut self, v: PeekPeerResponse) {
683
+        self.union = ::std::option::Option::Some(Message_oneof_union::peek_peer_response(v))
684
+    }
685
+
686
+    // Mutable pointer to the field.
687
+    pub fn mut_peek_peer_response(&mut self) -> &mut PeekPeerResponse {
688
+        if let ::std::option::Option::Some(Message_oneof_union::peek_peer_response(_)) = self.union {
689
+        } else {
690
+            self.union = ::std::option::Option::Some(Message_oneof_union::peek_peer_response(PeekPeerResponse::new()));
691
+        }
692
+        match self.union {
693
+            ::std::option::Option::Some(Message_oneof_union::peek_peer_response(ref mut v)) => v,
694
+            _ => panic!(),
695
+        }
696
+    }
697
+
698
+    // Take field
699
+    pub fn take_peek_peer_response(&mut self) -> PeekPeerResponse {
700
+        if self.has_peek_peer_response() {
701
+            match self.union.take() {
702
+                ::std::option::Option::Some(Message_oneof_union::peek_peer_response(v)) => v,
703
+                _ => panic!(),
704
+            }
705
+        } else {
706
+            PeekPeerResponse::new()
128 707
         }
129 708
     }
130 709
 }
131 710
 
132 711
 impl ::protobuf::Message for Message {
133 712
     fn is_initialized(&self) -> bool {
713
+        if let Some(Message_oneof_union::register_peer(ref v)) = self.union {
714
+            if !v.is_initialized() {
715
+                return false;
716
+            }
717
+        }
718
+        if let Some(Message_oneof_union::peek_peer(ref v)) = self.union {
719
+            if !v.is_initialized() {
720
+                return false;
721
+            }
722
+        }
723
+        if let Some(Message_oneof_union::peek_peer_response(ref v)) = self.union {
724
+            if !v.is_initialized() {
725
+                return false;
726
+            }
727
+        }
134 728
         true
135 729
     }
136 730
 
@@ -138,14 +732,23 @@ impl ::protobuf::Message for Message {
138 732
         while !is.eof()? {
139 733
             let (field_number, wire_type) = is.read_tag_unpack()?;
140 734
             match field_number {
141
-                1 => {
142
-                    ::protobuf::rt::read_singular_proto3_string_into(wire_type, is, &mut self.addr)?;
143
-                },
144 735
                 6 => {
145 736
                     if wire_type != ::protobuf::wire_format::WireTypeLengthDelimited {
146 737
                         return ::std::result::Result::Err(::protobuf::rt::unexpected_wire_type(wire_type));
147 738
                     }
148
-                    self.union = ::std::option::Option::Some(Message_oneof_union::socket_addr(is.read_bytes()?));
739
+                    self.union = ::std::option::Option::Some(Message_oneof_union::register_peer(is.read_message()?));
740
+                },
741
+                7 => {
742
+                    if wire_type != ::protobuf::wire_format::WireTypeLengthDelimited {
743
+                        return ::std::result::Result::Err(::protobuf::rt::unexpected_wire_type(wire_type));
744
+                    }
745
+                    self.union = ::std::option::Option::Some(Message_oneof_union::peek_peer(is.read_message()?));
746
+                },
747
+                8 => {
748
+                    if wire_type != ::protobuf::wire_format::WireTypeLengthDelimited {
749
+                        return ::std::result::Result::Err(::protobuf::rt::unexpected_wire_type(wire_type));
750
+                    }
751
+                    self.union = ::std::option::Option::Some(Message_oneof_union::peek_peer_response(is.read_message()?));
149 752
                 },
150 753
                 _ => {
151 754
                     ::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?;
@@ -159,13 +762,19 @@ impl ::protobuf::Message for Message {
159 762
     #[allow(unused_variables)]
160 763
     fn compute_size(&self) -> u32 {
161 764
         let mut my_size = 0;
162
-        if !self.addr.is_empty() {
163
-            my_size += ::protobuf::rt::string_size(1, &self.addr);
164
-        }
165 765
         if let ::std::option::Option::Some(ref v) = self.union {
166 766
             match v {
167
-                &Message_oneof_union::socket_addr(ref v) => {
168
-                    my_size += ::protobuf::rt::bytes_size(6, &v);
767
+                &Message_oneof_union::register_peer(ref v) => {
768
+                    let len = v.compute_size();
769
+                    my_size += 1 + ::protobuf::rt::compute_raw_varint32_size(len) + len;
770
+                },
771
+                &Message_oneof_union::peek_peer(ref v) => {
772
+                    let len = v.compute_size();
773
+                    my_size += 1 + ::protobuf::rt::compute_raw_varint32_size(len) + len;
774
+                },
775
+                &Message_oneof_union::peek_peer_response(ref v) => {
776
+                    let len = v.compute_size();
777
+                    my_size += 1 + ::protobuf::rt::compute_raw_varint32_size(len) + len;
169 778
                 },
170 779
             };
171 780
         }
@@ -175,13 +784,22 @@ impl ::protobuf::Message for Message {
175 784
     }
176 785
 
177 786
     fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream<'_>) -> ::protobuf::ProtobufResult<()> {
178
-        if !self.addr.is_empty() {
179
-            os.write_string(1, &self.addr)?;
180
-        }
181 787
         if let ::std::option::Option::Some(ref v) = self.union {
182 788
             match v {
183
-                &Message_oneof_union::socket_addr(ref v) => {
184
-                    os.write_bytes(6, v)?;
789
+                &Message_oneof_union::register_peer(ref v) => {
790
+                    os.write_tag(6, ::protobuf::wire_format::WireTypeLengthDelimited)?;
791
+                    os.write_raw_varint32(v.get_cached_size())?;
792
+                    v.write_to_with_cached_sizes(os)?;
793
+                },
794
+                &Message_oneof_union::peek_peer(ref v) => {
795
+                    os.write_tag(7, ::protobuf::wire_format::WireTypeLengthDelimited)?;
796
+                    os.write_raw_varint32(v.get_cached_size())?;
797
+                    v.write_to_with_cached_sizes(os)?;
798
+                },
799
+                &Message_oneof_union::peek_peer_response(ref v) => {
800
+                    os.write_tag(8, ::protobuf::wire_format::WireTypeLengthDelimited)?;
801
+                    os.write_raw_varint32(v.get_cached_size())?;
802
+                    v.write_to_with_cached_sizes(os)?;
185 803
                 },
186 804
             };
187 805
         }
@@ -227,15 +845,20 @@ impl ::protobuf::Message for Message {
227 845
         unsafe {
228 846
             descriptor.get(|| {
229 847
                 let mut fields = ::std::vec::Vec::new();
230
-                fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeString>(
231
-                    "addr",
232
-                    |m: &Message| { &m.addr },
233
-                    |m: &mut Message| { &mut m.addr },
848
+                fields.push(::protobuf::reflect::accessor::make_singular_message_accessor::<_, RegisterPeer>(
849
+                    "register_peer",
850
+                    Message::has_register_peer,
851
+                    Message::get_register_peer,
234 852
                 ));
235
-                fields.push(::protobuf::reflect::accessor::make_singular_bytes_accessor::<_>(
236
-                    "socket_addr",
237
-                    Message::has_socket_addr,
238
-                    Message::get_socket_addr,
853
+                fields.push(::protobuf::reflect::accessor::make_singular_message_accessor::<_, PeekPeer>(
854
+                    "peek_peer",
855
+                    Message::has_peek_peer,
856
+                    Message::get_peek_peer,
857
+                ));
858
+                fields.push(::protobuf::reflect::accessor::make_singular_message_accessor::<_, PeekPeerResponse>(
859
+                    "peek_peer_response",
860
+                    Message::has_peek_peer_response,
861
+                    Message::get_peek_peer_response,
239 862
                 ));
240 863
                 ::protobuf::reflect::MessageDescriptor::new::<Message>(
241 864
                     "Message",
@@ -259,7 +882,8 @@ impl ::protobuf::Message for Message {
259 882
 
260 883
 impl ::protobuf::Clear for Message {
261 884
     fn clear(&mut self) {
262
-        self.addr.clear();
885
+        self.union = ::std::option::Option::None;
886
+        self.union = ::std::option::Option::None;
263 887
         self.union = ::std::option::Option::None;
264 888
         self.unknown_fields.clear();
265 889
     }
@@ -278,9 +902,14 @@ impl ::protobuf::reflect::ProtobufValue for Message {
278 902
 }
279 903
 
280 904
 static file_descriptor_proto_data: &'static [u8] = b"\
281
-    \n\rmessage.proto\x12\x03hbb\"=\n\x07Message\x12\x0e\n\x04addr\x18\x01\
282
-    \x20\x01(\tB\0\x12\x17\n\x0bsocket_addr\x18\x06\x20\x01(\x0cH\0B\0B\x07\
283
-    \n\x05union:\0B\0b\x06proto3\
905
+    \n\rmessage.proto\x12\x03hbb\"$\n\x0cRegisterPeer\x12\x12\n\x08hbb_addr\
906
+    \x18\x01\x20\x01(\tB\0:\0\"\x20\n\x08PeekPeer\x12\x12\n\x08hbb_addr\x18\
907
+    \x01\x20\x01(\tB\0:\0\"+\n\x10PeekPeerResponse\x12\x15\n\x0bsocket_addr\
908
+    \x18\x01\x20\x01(\x0cB\0:\0\"\x9f\x01\n\x07Message\x12,\n\rregister_peer\
909
+    \x18\x06\x20\x01(\x0b2\x11.hbb.RegisterPeerH\0B\0\x12$\n\tpeek_peer\x18\
910
+    \x07\x20\x01(\x0b2\r.hbb.PeekPeerH\0B\0\x125\n\x12peek_peer_response\x18\
911
+    \x08\x20\x01(\x0b2\x15.hbb.PeekPeerResponseH\0B\0B\x07\n\x05union:\0B\0b\
912
+    \x06proto3\
284 913
 ";
285 914
 
286 915
 static mut file_descriptor_proto_lazy: ::protobuf::lazy::Lazy<::protobuf::descriptor::FileDescriptorProto> = ::protobuf::lazy::Lazy {

+ 77 - 12
src/rendezvous_server.rs

@@ -1,4 +1,4 @@
1
-use super::message_proto::Message;
1
+use super::message_proto::*;
2 2
 use bytes::Bytes;
3 3
 use futures::{FutureExt, SinkExt};
4 4
 use protobuf::{parse_from_bytes, Message as _};
@@ -18,7 +18,7 @@ use tokio_util::{codec::BytesCodec, udp::UdpFramed};
18 18
 pub struct V4AddrMangle(Vec<u8>);
19 19
 
20 20
 impl V4AddrMangle {
21
-    pub fn encode(addr: SocketAddrV4) -> Self {
21
+    pub fn encode(addr: &SocketAddrV4) -> Self {
22 22
         let tm = (SystemTime::now()
23 23
             .duration_since(UNIX_EPOCH)
24 24
             .unwrap()
@@ -50,7 +50,7 @@ impl V4AddrMangle {
50 50
 }
51 51
 
52 52
 pub struct Peer {
53
-    socket_addr: SocketAddr,
53
+    socket_addr: SocketAddrV4,
54 54
 }
55 55
 
56 56
 type PeerMap = HashMap<String, Peer>;
@@ -59,35 +59,100 @@ pub struct RendezvousServer {
59 59
     peer_map: PeerMap,
60 60
 }
61 61
 
62
+type FramedSocket = UdpFramed<BytesCodec>;
63
+type ResultType = Result<(), Box<dyn Error>>;
64
+
62 65
 impl RendezvousServer {
63
-    pub async fn start(addr: &str) -> Result<Self, Box<dyn Error>> {
66
+    pub async fn start(addr: &str) -> ResultType {
64 67
         let socket = UdpSocket::bind(addr).await?;
65 68
         let mut socket = UdpFramed::new(socket, BytesCodec::new());
66 69
 
67
-        let rs = Self {
70
+        let mut rs = Self {
68 71
             peer_map: PeerMap::new(),
69 72
         };
70 73
         while let Some(Ok((bytes, addr))) = socket.next().await {
71 74
             if let SocketAddr::V4(addr_v4) = addr {
72 75
                 if let Ok(msg_in) = parse_from_bytes::<Message>(&bytes) {
73
-                    let msg_out = Message::new();
74
-                    socket
75
-                        .send((Bytes::from(msg_out.write_to_bytes().unwrap()), addr))
76
-                        .await?;
76
+                    match msg_in.union {
77
+                        Some(Message_oneof_union::register_peer(rp)) => {
78
+                            if rp.hbb_addr.len() > 0 {
79
+                                rs.peer_map.insert(
80
+                                    rp.hbb_addr,
81
+                                    Peer {
82
+                                        socket_addr: addr_v4,
83
+                                    },
84
+                                );
85
+                            }
86
+                            tokio_timer::sleep(std::time::Duration::from_secs(60));
87
+                        }
88
+                        Some(Message_oneof_union::peek_peer(pp)) => {
89
+                            rs.handle_peek_peer(&pp, addr, &mut socket).await?;
90
+                            tokio_timer::sleep(std::time::Duration::from_secs(60));
91
+                        }
92
+                        _ => {}
93
+                    }
77 94
                 }
78 95
             }
79 96
         }
80
-        Ok(rs)
97
+        Ok(())
98
+    }
99
+
100
+    pub async fn handle_peek_peer(
101
+        &self,
102
+        pp: &PeekPeer,
103
+        addr: SocketAddr,
104
+        socket: &mut FramedSocket,
105
+    ) -> ResultType {
106
+        if let Some(peer) = self.peer_map.get(&pp.hbb_addr) {
107
+            let mut msg_out = Message::new();
108
+            msg_out.set_peek_peer_response(PeekPeerResponse {
109
+                socket_addr: V4AddrMangle::encode(&peer.socket_addr).0.to_vec(),
110
+                ..Default::default()
111
+            });
112
+            send_to(&msg_out, addr, socket).await?;
113
+        }
114
+        Ok(())
81 115
     }
82 116
 }
83 117
 
118
+#[inline]
119
+pub async fn send_to(msg: &Message, addr: SocketAddr, socket: &mut FramedSocket) -> ResultType {
120
+    socket
121
+        .send((Bytes::from(msg.write_to_bytes().unwrap()), addr))
122
+        .await?;
123
+    Ok(())
124
+}
125
+
84 126
 #[cfg(test)]
85 127
 mod tests {
86 128
     use super::*;
87 129
     #[test]
88 130
     fn test_mangle() {
89 131
         let addr = SocketAddrV4::new(Ipv4Addr::new(192, 168, 16, 32), 21116);
90
-        assert_eq!(addr, V4AddrMangle::encode(addr).decode());
91
-        println!("{:?}", V4AddrMangle::encode(addr).0);
132
+        assert_eq!(addr, V4AddrMangle::encode(&addr).decode());
133
+    }
134
+
135
+    #[allow(unused_must_use)]
136
+    #[tokio::main]
137
+    async fn test_rs_async() {
138
+        let server_addr = "0.0.0.0:21116";
139
+        let f1 = RendezvousServer::start(server_addr);
140
+        let to_addr = server_addr.parse().unwrap();
141
+        let f2 = async {
142
+            let socket = UdpSocket::bind("127.0.0.1:0").await.unwrap();
143
+            let mut socket = UdpFramed::new(socket, BytesCodec::new());
144
+            let mut msg_out = Message::new();
145
+            msg_out.set_peek_peer(PeekPeer {
146
+                hbb_addr: "123".to_string(),
147
+                ..Default::default()
148
+            });
149
+            send_to(&msg_out, to_addr, &mut socket).await;
150
+        };
151
+        tokio::join!(f1, f2);
152
+    }
153
+
154
+    #[test]
155
+    fn test_rs() {
156
+        self::test_rs_async();
92 157
     }
93 158
 }