diff options
author | open-trade <[email protected]> | 2020-03-07 00:37:23 +0800 |
---|---|---|
committer | open-trade <[email protected]> | 2020-03-07 00:37:23 +0800 |
commit | f7cb0cfde666f26c66a5f82fd677b9162c2902ee (patch) | |
tree | 3bd9c3d9c8ff400a482fe491587318636facf3d2 /src | |
parent | 1695b5e3574b578aae0d75189e4395c631ca3b8a (diff) | |
download | rustdesk-server-f7cb0cfde666f26c66a5f82fd677b9162c2902ee.tar.gz rustdesk-server-f7cb0cfde666f26c66a5f82fd677b9162c2902ee.zip |
how to await tokio_timer::sleep?
Diffstat (limited to 'src')
-rw-r--r-- | src/protos/message.rs | 745 | ||||
-rw-r--r-- | src/rendezvous_server.rs | 89 |
2 files changed, 764 insertions, 70 deletions
diff --git a/src/protos/message.rs b/src/protos/message.rs index a40385f..58e186b 100644 --- a/src/protos/message.rs +++ b/src/protos/message.rs @@ -27,9 +27,514 @@ use protobuf::ProtobufEnum as ProtobufEnum_imported_for_functions; // const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_10_2; #[derive(PartialEq,Clone,Default)] -pub struct Message { +pub struct RegisterPeer { + // message fields + pub hbb_addr: ::std::string::String, + // special fields + pub unknown_fields: ::protobuf::UnknownFields, + pub cached_size: ::protobuf::CachedSize, +} + +impl<'a> ::std::default::Default for &'a RegisterPeer { + fn default() -> &'a RegisterPeer { + <RegisterPeer as ::protobuf::Message>::default_instance() + } +} + +impl RegisterPeer { + pub fn new() -> RegisterPeer { + ::std::default::Default::default() + } + + // string hbb_addr = 1; + + + pub fn get_hbb_addr(&self) -> &str { + &self.hbb_addr + } + pub fn clear_hbb_addr(&mut self) { + self.hbb_addr.clear(); + } + + // Param is passed by value, moved + pub fn set_hbb_addr(&mut self, v: ::std::string::String) { + self.hbb_addr = v; + } + + // Mutable pointer to the field. + // If field is not initialized, it is initialized with default value first. + pub fn mut_hbb_addr(&mut self) -> &mut ::std::string::String { + &mut self.hbb_addr + } + + // Take field + pub fn take_hbb_addr(&mut self) -> ::std::string::String { + ::std::mem::replace(&mut self.hbb_addr, ::std::string::String::new()) + } +} + +impl ::protobuf::Message for RegisterPeer { + fn is_initialized(&self) -> bool { + true + } + + fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream<'_>) -> ::protobuf::ProtobufResult<()> { + while !is.eof()? { + let (field_number, wire_type) = is.read_tag_unpack()?; + match field_number { + 1 => { + ::protobuf::rt::read_singular_proto3_string_into(wire_type, is, &mut self.hbb_addr)?; + }, + _ => { + ::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?; + }, + }; + } + ::std::result::Result::Ok(()) + } + + // Compute sizes of nested messages + #[allow(unused_variables)] + fn compute_size(&self) -> u32 { + let mut my_size = 0; + if !self.hbb_addr.is_empty() { + my_size += ::protobuf::rt::string_size(1, &self.hbb_addr); + } + my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields()); + self.cached_size.set(my_size); + my_size + } + + fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream<'_>) -> ::protobuf::ProtobufResult<()> { + if !self.hbb_addr.is_empty() { + os.write_string(1, &self.hbb_addr)?; + } + os.write_unknown_fields(self.get_unknown_fields())?; + ::std::result::Result::Ok(()) + } + + fn get_cached_size(&self) -> u32 { + self.cached_size.get() + } + + fn get_unknown_fields(&self) -> &::protobuf::UnknownFields { + &self.unknown_fields + } + + fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields { + &mut self.unknown_fields + } + + fn as_any(&self) -> &dyn (::std::any::Any) { + self as &dyn (::std::any::Any) + } + fn as_any_mut(&mut self) -> &mut dyn (::std::any::Any) { + self as &mut dyn (::std::any::Any) + } + fn into_any(self: Box<Self>) -> ::std::boxed::Box<dyn (::std::any::Any)> { + self + } + + fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor { + Self::descriptor_static() + } + + fn new() -> RegisterPeer { + RegisterPeer::new() + } + + fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor { + static mut descriptor: ::protobuf::lazy::Lazy<::protobuf::reflect::MessageDescriptor> = ::protobuf::lazy::Lazy { + lock: ::protobuf::lazy::ONCE_INIT, + ptr: 0 as *const ::protobuf::reflect::MessageDescriptor, + }; + unsafe { + descriptor.get(|| { + let mut fields = ::std::vec::Vec::new(); + fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeString>( + "hbb_addr", + |m: &RegisterPeer| { &m.hbb_addr }, + |m: &mut RegisterPeer| { &mut m.hbb_addr }, + )); + ::protobuf::reflect::MessageDescriptor::new::<RegisterPeer>( + "RegisterPeer", + fields, + file_descriptor_proto() + ) + }) + } + } + + fn default_instance() -> &'static RegisterPeer { + static mut instance: ::protobuf::lazy::Lazy<RegisterPeer> = ::protobuf::lazy::Lazy { + lock: ::protobuf::lazy::ONCE_INIT, + ptr: 0 as *const RegisterPeer, + }; + unsafe { + instance.get(RegisterPeer::new) + } + } +} + +impl ::protobuf::Clear for RegisterPeer { + fn clear(&mut self) { + self.hbb_addr.clear(); + self.unknown_fields.clear(); + } +} + +impl ::std::fmt::Debug for RegisterPeer { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + ::protobuf::text_format::fmt(self, f) + } +} + +impl ::protobuf::reflect::ProtobufValue for RegisterPeer { + fn as_ref(&self) -> ::protobuf::reflect::ProtobufValueRef { + ::protobuf::reflect::ProtobufValueRef::Message(self) + } +} + +#[derive(PartialEq,Clone,Default)] +pub struct PeekPeer { // message fields - pub addr: ::std::string::String, + pub hbb_addr: ::std::string::String, + // special fields + pub unknown_fields: ::protobuf::UnknownFields, + pub cached_size: ::protobuf::CachedSize, +} + +impl<'a> ::std::default::Default for &'a PeekPeer { + fn default() -> &'a PeekPeer { + <PeekPeer as ::protobuf::Message>::default_instance() + } +} + +impl PeekPeer { + pub fn new() -> PeekPeer { + ::std::default::Default::default() + } + + // string hbb_addr = 1; + + + pub fn get_hbb_addr(&self) -> &str { + &self.hbb_addr + } + pub fn clear_hbb_addr(&mut self) { + self.hbb_addr.clear(); + } + + // Param is passed by value, moved + pub fn set_hbb_addr(&mut self, v: ::std::string::String) { + self.hbb_addr = v; + } + + // Mutable pointer to the field. + // If field is not initialized, it is initialized with default value first. + pub fn mut_hbb_addr(&mut self) -> &mut ::std::string::String { + &mut self.hbb_addr + } + + // Take field + pub fn take_hbb_addr(&mut self) -> ::std::string::String { + ::std::mem::replace(&mut self.hbb_addr, ::std::string::String::new()) + } +} + +impl ::protobuf::Message for PeekPeer { + fn is_initialized(&self) -> bool { + true + } + + fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream<'_>) -> ::protobuf::ProtobufResult<()> { + while !is.eof()? { + let (field_number, wire_type) = is.read_tag_unpack()?; + match field_number { + 1 => { + ::protobuf::rt::read_singular_proto3_string_into(wire_type, is, &mut self.hbb_addr)?; + }, + _ => { + ::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?; + }, + }; + } + ::std::result::Result::Ok(()) + } + + // Compute sizes of nested messages + #[allow(unused_variables)] + fn compute_size(&self) -> u32 { + let mut my_size = 0; + if !self.hbb_addr.is_empty() { + my_size += ::protobuf::rt::string_size(1, &self.hbb_addr); + } + my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields()); + self.cached_size.set(my_size); + my_size + } + + fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream<'_>) -> ::protobuf::ProtobufResult<()> { + if !self.hbb_addr.is_empty() { + os.write_string(1, &self.hbb_addr)?; + } + os.write_unknown_fields(self.get_unknown_fields())?; + ::std::result::Result::Ok(()) + } + + fn get_cached_size(&self) -> u32 { + self.cached_size.get() + } + + fn get_unknown_fields(&self) -> &::protobuf::UnknownFields { + &self.unknown_fields + } + + fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields { + &mut self.unknown_fields + } + + fn as_any(&self) -> &dyn (::std::any::Any) { + self as &dyn (::std::any::Any) + } + fn as_any_mut(&mut self) -> &mut dyn (::std::any::Any) { + self as &mut dyn (::std::any::Any) + } + fn into_any(self: Box<Self>) -> ::std::boxed::Box<dyn (::std::any::Any)> { + self + } + + fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor { + Self::descriptor_static() + } + + fn new() -> PeekPeer { + PeekPeer::new() + } + + fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor { + static mut descriptor: ::protobuf::lazy::Lazy<::protobuf::reflect::MessageDescriptor> = ::protobuf::lazy::Lazy { + lock: ::protobuf::lazy::ONCE_INIT, + ptr: 0 as *const ::protobuf::reflect::MessageDescriptor, + }; + unsafe { + descriptor.get(|| { + let mut fields = ::std::vec::Vec::new(); + fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeString>( + "hbb_addr", + |m: &PeekPeer| { &m.hbb_addr }, + |m: &mut PeekPeer| { &mut m.hbb_addr }, + )); + ::protobuf::reflect::MessageDescriptor::new::<PeekPeer>( + "PeekPeer", + fields, + file_descriptor_proto() + ) + }) + } + } + + fn default_instance() -> &'static PeekPeer { + static mut instance: ::protobuf::lazy::Lazy<PeekPeer> = ::protobuf::lazy::Lazy { + lock: ::protobuf::lazy::ONCE_INIT, + ptr: 0 as *const PeekPeer, + }; + unsafe { + instance.get(PeekPeer::new) + } + } +} + +impl ::protobuf::Clear for PeekPeer { + fn clear(&mut self) { + self.hbb_addr.clear(); + self.unknown_fields.clear(); + } +} + +impl ::std::fmt::Debug for PeekPeer { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + ::protobuf::text_format::fmt(self, f) + } +} + +impl ::protobuf::reflect::ProtobufValue for PeekPeer { + fn as_ref(&self) -> ::protobuf::reflect::ProtobufValueRef { + ::protobuf::reflect::ProtobufValueRef::Message(self) + } +} + +#[derive(PartialEq,Clone,Default)] +pub struct PeekPeerResponse { + // message fields + pub socket_addr: ::std::vec::Vec<u8>, + // special fields + pub unknown_fields: ::protobuf::UnknownFields, + pub cached_size: ::protobuf::CachedSize, +} + +impl<'a> ::std::default::Default for &'a PeekPeerResponse { + fn default() -> &'a PeekPeerResponse { + <PeekPeerResponse as ::protobuf::Message>::default_instance() + } +} + +impl PeekPeerResponse { + pub fn new() -> PeekPeerResponse { + ::std::default::Default::default() + } + + // bytes socket_addr = 1; + + + pub fn get_socket_addr(&self) -> &[u8] { + &self.socket_addr + } + pub fn clear_socket_addr(&mut self) { + self.socket_addr.clear(); + } + + // Param is passed by value, moved + pub fn set_socket_addr(&mut self, v: ::std::vec::Vec<u8>) { + self.socket_addr = v; + } + + // Mutable pointer to the field. + // If field is not initialized, it is initialized with default value first. + pub fn mut_socket_addr(&mut self) -> &mut ::std::vec::Vec<u8> { + &mut self.socket_addr + } + + // Take field + pub fn take_socket_addr(&mut self) -> ::std::vec::Vec<u8> { + ::std::mem::replace(&mut self.socket_addr, ::std::vec::Vec::new()) + } +} + +impl ::protobuf::Message for PeekPeerResponse { + fn is_initialized(&self) -> bool { + true + } + + fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream<'_>) -> ::protobuf::ProtobufResult<()> { + while !is.eof()? { + let (field_number, wire_type) = is.read_tag_unpack()?; + match field_number { + 1 => { + ::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.socket_addr)?; + }, + _ => { + ::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?; + }, + }; + } + ::std::result::Result::Ok(()) + } + + // Compute sizes of nested messages + #[allow(unused_variables)] + fn compute_size(&self) -> u32 { + let mut my_size = 0; + if !self.socket_addr.is_empty() { + my_size += ::protobuf::rt::bytes_size(1, &self.socket_addr); + } + my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields()); + self.cached_size.set(my_size); + my_size + } + + fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream<'_>) -> ::protobuf::ProtobufResult<()> { + if !self.socket_addr.is_empty() { + os.write_bytes(1, &self.socket_addr)?; + } + os.write_unknown_fields(self.get_unknown_fields())?; + ::std::result::Result::Ok(()) + } + + fn get_cached_size(&self) -> u32 { + self.cached_size.get() + } + + fn get_unknown_fields(&self) -> &::protobuf::UnknownFields { + &self.unknown_fields + } + + fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields { + &mut self.unknown_fields + } + + fn as_any(&self) -> &dyn (::std::any::Any) { + self as &dyn (::std::any::Any) + } + fn as_any_mut(&mut self) -> &mut dyn (::std::any::Any) { + self as &mut dyn (::std::any::Any) + } + fn into_any(self: Box<Self>) -> ::std::boxed::Box<dyn (::std::any::Any)> { + self + } + + fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor { + Self::descriptor_static() + } + + fn new() -> PeekPeerResponse { + PeekPeerResponse::new() + } + + fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor { + static mut descriptor: ::protobuf::lazy::Lazy<::protobuf::reflect::MessageDescriptor> = ::protobuf::lazy::Lazy { + lock: ::protobuf::lazy::ONCE_INIT, + ptr: 0 as *const ::protobuf::reflect::MessageDescriptor, + }; + unsafe { + descriptor.get(|| { + let mut fields = ::std::vec::Vec::new(); + fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>( + "socket_addr", + |m: &PeekPeerResponse| { &m.socket_addr }, + |m: &mut PeekPeerResponse| { &mut m.socket_addr }, + )); + ::protobuf::reflect::MessageDescriptor::new::<PeekPeerResponse>( + "PeekPeerResponse", + fields, + file_descriptor_proto() + ) + }) + } + } + + fn default_instance() -> &'static PeekPeerResponse { + static mut instance: ::protobuf::lazy::Lazy<PeekPeerResponse> = ::protobuf::lazy::Lazy { + lock: ::protobuf::lazy::ONCE_INIT, + ptr: 0 as *const PeekPeerResponse, + }; + unsafe { + instance.get(PeekPeerResponse::new) + } + } +} + +impl ::protobuf::Clear for PeekPeerResponse { + fn clear(&mut self) { + self.socket_addr.clear(); + self.unknown_fields.clear(); + } +} + +impl ::std::fmt::Debug for PeekPeerResponse { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + ::protobuf::text_format::fmt(self, f) + } +} + +impl ::protobuf::reflect::ProtobufValue for PeekPeerResponse { + fn as_ref(&self) -> ::protobuf::reflect::ProtobufValueRef { + ::protobuf::reflect::ProtobufValueRef::Message(self) + } +} + +#[derive(PartialEq,Clone,Default)] +pub struct Message { // message oneof groups pub union: ::std::option::Option<Message_oneof_union>, // special fields @@ -45,7 +550,9 @@ impl<'a> ::std::default::Default for &'a Message { #[derive(Clone,PartialEq,Debug)] pub enum Message_oneof_union { - socket_addr(::std::vec::Vec<u8>), + register_peer(RegisterPeer), + peek_peer(PeekPeer), + peek_peer_response(PeekPeerResponse), } impl Message { @@ -53,84 +560,171 @@ impl Message { ::std::default::Default::default() } - // string addr = 1; + // .hbb.RegisterPeer register_peer = 6; - pub fn get_addr(&self) -> &str { - &self.addr + pub fn get_register_peer(&self) -> &RegisterPeer { + match self.union { + ::std::option::Option::Some(Message_oneof_union::register_peer(ref v)) => v, + _ => RegisterPeer::default_instance(), + } } - pub fn clear_addr(&mut self) { - self.addr.clear(); + pub fn clear_register_peer(&mut self) { + self.union = ::std::option::Option::None; + } + + pub fn has_register_peer(&self) -> bool { + match self.union { + ::std::option::Option::Some(Message_oneof_union::register_peer(..)) => true, + _ => false, + } } // Param is passed by value, moved - pub fn set_addr(&mut self, v: ::std::string::String) { - self.addr = v; + pub fn set_register_peer(&mut self, v: RegisterPeer) { + self.union = ::std::option::Option::Some(Message_oneof_union::register_peer(v)) } // Mutable pointer to the field. - // If field is not initialized, it is initialized with default value first. - pub fn mut_addr(&mut self) -> &mut ::std::string::String { - &mut self.addr + pub fn mut_register_peer(&mut self) -> &mut RegisterPeer { + if let ::std::option::Option::Some(Message_oneof_union::register_peer(_)) = self.union { + } else { + self.union = ::std::option::Option::Some(Message_oneof_union::register_peer(RegisterPeer::new())); + } + match self.union { + ::std::option::Option::Some(Message_oneof_union::register_peer(ref mut v)) => v, + _ => panic!(), + } } // Take field - pub fn take_addr(&mut self) -> ::std::string::String { - ::std::mem::replace(&mut self.addr, ::std::string::String::new()) + pub fn take_register_peer(&mut self) -> RegisterPeer { + if self.has_register_peer() { + match self.union.take() { + ::std::option::Option::Some(Message_oneof_union::register_peer(v)) => v, + _ => panic!(), + } + } else { + RegisterPeer::new() + } } - // bytes socket_addr = 6; + // .hbb.PeekPeer peek_peer = 7; - pub fn get_socket_addr(&self) -> &[u8] { + pub fn get_peek_peer(&self) -> &PeekPeer { match self.union { - ::std::option::Option::Some(Message_oneof_union::socket_addr(ref v)) => v, - _ => &[], + ::std::option::Option::Some(Message_oneof_union::peek_peer(ref v)) => v, + _ => PeekPeer::default_instance(), } } - pub fn clear_socket_addr(&mut self) { + pub fn clear_peek_peer(&mut self) { self.union = ::std::option::Option::None; } - pub fn has_socket_addr(&self) -> bool { + pub fn has_peek_peer(&self) -> bool { match self.union { - ::std::option::Option::Some(Message_oneof_union::socket_addr(..)) => true, + ::std::option::Option::Some(Message_oneof_union::peek_peer(..)) => true, _ => false, } } // Param is passed by value, moved - pub fn set_socket_addr(&mut self, v: ::std::vec::Vec<u8>) { - self.union = ::std::option::Option::Some(Message_oneof_union::socket_addr(v)) + pub fn set_peek_peer(&mut self, v: PeekPeer) { + self.union = ::std::option::Option::Some(Message_oneof_union::peek_peer(v)) } // Mutable pointer to the field. - pub fn mut_socket_addr(&mut self) -> &mut ::std::vec::Vec<u8> { - if let ::std::option::Option::Some(Message_oneof_union::socket_addr(_)) = self.union { + pub fn mut_peek_peer(&mut self) -> &mut PeekPeer { + if let ::std::option::Option::Some(Message_oneof_union::peek_peer(_)) = self.union { } else { - self.union = ::std::option::Option::Some(Message_oneof_union::socket_addr(::std::vec::Vec::new())); + self.union = ::std::option::Option::Some(Message_oneof_union::peek_peer(PeekPeer::new())); } match self.union { - ::std::option::Option::Some(Message_oneof_union::socket_addr(ref mut v)) => v, + ::std::option::Option::Some(Message_oneof_union::peek_peer(ref mut v)) => v, _ => panic!(), } } // Take field - pub fn take_socket_addr(&mut self) -> ::std::vec::Vec<u8> { - if self.has_socket_addr() { + pub fn take_peek_peer(&mut self) -> PeekPeer { + if self.has_peek_peer() { match self.union.take() { - ::std::option::Option::Some(Message_oneof_union::socket_addr(v)) => v, + ::std::option::Option::Some(Message_oneof_union::peek_peer(v)) => v, _ => panic!(), } } else { - ::std::vec::Vec::new() + PeekPeer::new() + } + } + + // .hbb.PeekPeerResponse peek_peer_response = 8; + + + pub fn get_peek_peer_response(&self) -> &PeekPeerResponse { + match self.union { + ::std::option::Option::Some(Message_oneof_union::peek_peer_response(ref v)) => v, + _ => PeekPeerResponse::default_instance(), + } + } + pub fn clear_peek_peer_response(&mut self) { + self.union = ::std::option::Option::None; + } + + pub fn has_peek_peer_response(&self) -> bool { + match self.union { + ::std::option::Option::Some(Message_oneof_union::peek_peer_response(..)) => true, + _ => false, + } + } + + // Param is passed by value, moved + pub fn set_peek_peer_response(&mut self, v: PeekPeerResponse) { + self.union = ::std::option::Option::Some(Message_oneof_union::peek_peer_response(v)) + } + + // Mutable pointer to the field. + pub fn mut_peek_peer_response(&mut self) -> &mut PeekPeerResponse { + if let ::std::option::Option::Some(Message_oneof_union::peek_peer_response(_)) = self.union { + } else { + self.union = ::std::option::Option::Some(Message_oneof_union::peek_peer_response(PeekPeerResponse::new())); + } + match self.union { + ::std::option::Option::Some(Message_oneof_union::peek_peer_response(ref mut v)) => v, + _ => panic!(), + } + } + + // Take field + pub fn take_peek_peer_response(&mut self) -> PeekPeerResponse { + if self.has_peek_peer_response() { + match self.union.take() { + ::std::option::Option::Some(Message_oneof_union::peek_peer_response(v)) => v, + _ => panic!(), + } + } else { + PeekPeerResponse::new() } } } impl ::protobuf::Message for Message { fn is_initialized(&self) -> bool { + if let Some(Message_oneof_union::register_peer(ref v)) = self.union { + if !v.is_initialized() { + return false; + } + } + if let Some(Message_oneof_union::peek_peer(ref v)) = self.union { + if !v.is_initialized() { + return false; + } + } + if let Some(Message_oneof_union::peek_peer_response(ref v)) = self.union { + if !v.is_initialized() { + return false; + } + } true } @@ -138,14 +732,23 @@ impl ::protobuf::Message for Message { while !is.eof()? { let (field_number, wire_type) = is.read_tag_unpack()?; match field_number { - 1 => { - ::protobuf::rt::read_singular_proto3_string_into(wire_type, is, &mut self.addr)?; - }, 6 => { if wire_type != ::protobuf::wire_format::WireTypeLengthDelimited { return ::std::result::Result::Err(::protobuf::rt::unexpected_wire_type(wire_type)); } - self.union = ::std::option::Option::Some(Message_oneof_union::socket_addr(is.read_bytes()?)); + self.union = ::std::option::Option::Some(Message_oneof_union::register_peer(is.read_message()?)); + }, + 7 => { + if wire_type != ::protobuf::wire_format::WireTypeLengthDelimited { + return ::std::result::Result::Err(::protobuf::rt::unexpected_wire_type(wire_type)); + } + self.union = ::std::option::Option::Some(Message_oneof_union::peek_peer(is.read_message()?)); + }, + 8 => { + if wire_type != ::protobuf::wire_format::WireTypeLengthDelimited { + return ::std::result::Result::Err(::protobuf::rt::unexpected_wire_type(wire_type)); + } + self.union = ::std::option::Option::Some(Message_oneof_union::peek_peer_response(is.read_message()?)); }, _ => { ::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 { #[allow(unused_variables)] fn compute_size(&self) -> u32 { let mut my_size = 0; - if !self.addr.is_empty() { - my_size += ::protobuf::rt::string_size(1, &self.addr); - } if let ::std::option::Option::Some(ref v) = self.union { match v { - &Message_oneof_union::socket_addr(ref v) => { - my_size += ::protobuf::rt::bytes_size(6, &v); + &Message_oneof_union::register_peer(ref v) => { + let len = v.compute_size(); + my_size += 1 + ::protobuf::rt::compute_raw_varint32_size(len) + len; + }, + &Message_oneof_union::peek_peer(ref v) => { + let len = v.compute_size(); + my_size += 1 + ::protobuf::rt::compute_raw_varint32_size(len) + len; + }, + &Message_oneof_union::peek_peer_response(ref v) => { + let len = v.compute_size(); + my_size += 1 + ::protobuf::rt::compute_raw_varint32_size(len) + len; }, }; } @@ -175,13 +784,22 @@ impl ::protobuf::Message for Message { } fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream<'_>) -> ::protobuf::ProtobufResult<()> { - if !self.addr.is_empty() { - os.write_string(1, &self.addr)?; - } if let ::std::option::Option::Some(ref v) = self.union { match v { - &Message_oneof_union::socket_addr(ref v) => { - os.write_bytes(6, v)?; + &Message_oneof_union::register_peer(ref v) => { + os.write_tag(6, ::protobuf::wire_format::WireTypeLengthDelimited)?; + os.write_raw_varint32(v.get_cached_size())?; + v.write_to_with_cached_sizes(os)?; + }, + &Message_oneof_union::peek_peer(ref v) => { + os.write_tag(7, ::protobuf::wire_format::WireTypeLengthDelimited)?; + os.write_raw_varint32(v.get_cached_size())?; + v.write_to_with_cached_sizes(os)?; + }, + &Message_oneof_union::peek_peer_response(ref v) => { + os.write_tag(8, ::protobuf::wire_format::WireTypeLengthDelimited)?; + os.write_raw_varint32(v.get_cached_size())?; + v.write_to_with_cached_sizes(os)?; }, }; } @@ -227,15 +845,20 @@ impl ::protobuf::Message for Message { unsafe { descriptor.get(|| { let mut fields = ::std::vec::Vec::new(); - fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeString>( - "addr", - |m: &Message| { &m.addr }, - |m: &mut Message| { &mut m.addr }, + fields.push(::protobuf::reflect::accessor::make_singular_message_accessor::<_, RegisterPeer>( + "register_peer", + Message::has_register_peer, + Message::get_register_peer, )); - fields.push(::protobuf::reflect::accessor::make_singular_bytes_accessor::<_>( - "socket_addr", - Message::has_socket_addr, - Message::get_socket_addr, + fields.push(::protobuf::reflect::accessor::make_singular_message_accessor::<_, PeekPeer>( + "peek_peer", + Message::has_peek_peer, + Message::get_peek_peer, + )); + fields.push(::protobuf::reflect::accessor::make_singular_message_accessor::<_, PeekPeerResponse>( + "peek_peer_response", + Message::has_peek_peer_response, + Message::get_peek_peer_response, )); ::protobuf::reflect::MessageDescriptor::new::<Message>( "Message", @@ -259,7 +882,8 @@ impl ::protobuf::Message for Message { impl ::protobuf::Clear for Message { fn clear(&mut self) { - self.addr.clear(); + self.union = ::std::option::Option::None; + self.union = ::std::option::Option::None; self.union = ::std::option::Option::None; self.unknown_fields.clear(); } @@ -278,9 +902,14 @@ impl ::protobuf::reflect::ProtobufValue for Message { } static file_descriptor_proto_data: &'static [u8] = b"\ - \n\rmessage.proto\x12\x03hbb\"=\n\x07Message\x12\x0e\n\x04addr\x18\x01\ - \x20\x01(\tB\0\x12\x17\n\x0bsocket_addr\x18\x06\x20\x01(\x0cH\0B\0B\x07\ - \n\x05union:\0B\0b\x06proto3\ + \n\rmessage.proto\x12\x03hbb\"$\n\x0cRegisterPeer\x12\x12\n\x08hbb_addr\ + \x18\x01\x20\x01(\tB\0:\0\"\x20\n\x08PeekPeer\x12\x12\n\x08hbb_addr\x18\ + \x01\x20\x01(\tB\0:\0\"+\n\x10PeekPeerResponse\x12\x15\n\x0bsocket_addr\ + \x18\x01\x20\x01(\x0cB\0:\0\"\x9f\x01\n\x07Message\x12,\n\rregister_peer\ + \x18\x06\x20\x01(\x0b2\x11.hbb.RegisterPeerH\0B\0\x12$\n\tpeek_peer\x18\ + \x07\x20\x01(\x0b2\r.hbb.PeekPeerH\0B\0\x125\n\x12peek_peer_response\x18\ + \x08\x20\x01(\x0b2\x15.hbb.PeekPeerResponseH\0B\0B\x07\n\x05union:\0B\0b\ + \x06proto3\ "; static mut file_descriptor_proto_lazy: ::protobuf::lazy::Lazy<::protobuf::descriptor::FileDescriptorProto> = ::protobuf::lazy::Lazy { diff --git a/src/rendezvous_server.rs b/src/rendezvous_server.rs index 1b5d3d8..72ad23b 100644 --- a/src/rendezvous_server.rs +++ b/src/rendezvous_server.rs @@ -1,4 +1,4 @@ -use super::message_proto::Message; +use super::message_proto::*; use bytes::Bytes; use futures::{FutureExt, SinkExt}; use protobuf::{parse_from_bytes, Message as _}; @@ -18,7 +18,7 @@ use tokio_util::{codec::BytesCodec, udp::UdpFramed}; pub struct V4AddrMangle(Vec<u8>); impl V4AddrMangle { - pub fn encode(addr: SocketAddrV4) -> Self { + pub fn encode(addr: &SocketAddrV4) -> Self { let tm = (SystemTime::now() .duration_since(UNIX_EPOCH) .unwrap() @@ -50,7 +50,7 @@ impl V4AddrMangle { } pub struct Peer { - socket_addr: SocketAddr, + socket_addr: SocketAddrV4, } type PeerMap = HashMap<String, Peer>; @@ -59,35 +59,100 @@ pub struct RendezvousServer { peer_map: PeerMap, } +type FramedSocket = UdpFramed<BytesCodec>; +type ResultType = Result<(), Box<dyn Error>>; + impl RendezvousServer { - pub async fn start(addr: &str) -> Result<Self, Box<dyn Error>> { + pub async fn start(addr: &str) -> ResultType { let socket = UdpSocket::bind(addr).await?; let mut socket = UdpFramed::new(socket, BytesCodec::new()); - let rs = Self { + let mut rs = Self { peer_map: PeerMap::new(), }; while let Some(Ok((bytes, addr))) = socket.next().await { if let SocketAddr::V4(addr_v4) = addr { if let Ok(msg_in) = parse_from_bytes::<Message>(&bytes) { - let msg_out = Message::new(); - socket - .send((Bytes::from(msg_out.write_to_bytes().unwrap()), addr)) - .await?; + match msg_in.union { + Some(Message_oneof_union::register_peer(rp)) => { + if rp.hbb_addr.len() > 0 { + rs.peer_map.insert( + rp.hbb_addr, + Peer { + socket_addr: addr_v4, + }, + ); + } + tokio_timer::sleep(std::time::Duration::from_secs(60)); + } + Some(Message_oneof_union::peek_peer(pp)) => { + rs.handle_peek_peer(&pp, addr, &mut socket).await?; + tokio_timer::sleep(std::time::Duration::from_secs(60)); + } + _ => {} + } } } } - Ok(rs) + Ok(()) + } + + pub async fn handle_peek_peer( + &self, + pp: &PeekPeer, + addr: SocketAddr, + socket: &mut FramedSocket, + ) -> ResultType { + if let Some(peer) = self.peer_map.get(&pp.hbb_addr) { + let mut msg_out = Message::new(); + msg_out.set_peek_peer_response(PeekPeerResponse { + socket_addr: V4AddrMangle::encode(&peer.socket_addr).0.to_vec(), + ..Default::default() + }); + send_to(&msg_out, addr, socket).await?; + } + Ok(()) } } +#[inline] +pub async fn send_to(msg: &Message, addr: SocketAddr, socket: &mut FramedSocket) -> ResultType { + socket + .send((Bytes::from(msg.write_to_bytes().unwrap()), addr)) + .await?; + Ok(()) +} + #[cfg(test)] mod tests { use super::*; #[test] fn test_mangle() { let addr = SocketAddrV4::new(Ipv4Addr::new(192, 168, 16, 32), 21116); - assert_eq!(addr, V4AddrMangle::encode(addr).decode()); - println!("{:?}", V4AddrMangle::encode(addr).0); + assert_eq!(addr, V4AddrMangle::encode(&addr).decode()); + } + + #[allow(unused_must_use)] + #[tokio::main] + async fn test_rs_async() { + let server_addr = "0.0.0.0:21116"; + let f1 = RendezvousServer::start(server_addr); + let to_addr = server_addr.parse().unwrap(); + let f2 = async { + let socket = UdpSocket::bind("127.0.0.1:0").await.unwrap(); + let mut socket = UdpFramed::new(socket, BytesCodec::new()); + let mut msg_out = Message::new(); + msg_out.set_peek_peer(PeekPeer { + hbb_addr: "123".to_string(), + ..Default::default() + }); + send_to(&msg_out, to_addr, &mut socket).await; + }; + tokio::join!(f1, f2); + } + + #[test] + fn test_rs() { + self::test_rs_async(); } } |