inbound_session.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. use async_executor::Executor;
  2. use log::*;
  3. use std::{
  4. net::SocketAddr,
  5. sync::{Arc, Weak},
  6. };
  7. use crate::{
  8. error::{Error, Result},
  9. net::{
  10. protocol::{ProtocolAddress, ProtocolBase, ProtocolPing},
  11. session::{Session, SessionBitflag, SESSION_INBOUND},
  12. Acceptor, AcceptorPtr, ChannelPtr, P2p,
  13. },
  14. system::{StoppableTask, StoppableTaskPtr},
  15. };
  16. /// Defines inbound connections session.
  17. pub struct InboundSession {
  18. p2p: Weak<P2p>,
  19. acceptor: AcceptorPtr,
  20. accept_task: StoppableTaskPtr,
  21. }
  22. impl InboundSession {
  23. /// Create a new inbound session.
  24. pub fn new(p2p: Weak<P2p>) -> Arc<Self> {
  25. let acceptor = Acceptor::new();
  26. Arc::new(Self { p2p, acceptor, accept_task: StoppableTask::new() })
  27. }
  28. /// Starts the inbound session. Begins by accepting connections and fails if
  29. /// the address is not configured. Then runs the channel subscription
  30. /// loop.
  31. pub fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) -> Result<()> {
  32. match self.p2p().settings().inbound {
  33. Some(accept_addr) => {
  34. self.clone().start_accept_session(accept_addr, executor.clone())?;
  35. }
  36. None => {
  37. info!(target: "net", "Not configured for accepting incoming connections.");
  38. return Ok(())
  39. }
  40. }
  41. self.accept_task.clone().start(
  42. self.clone().channel_sub_loop(executor.clone()),
  43. // Ignore stop handler
  44. |_| async {},
  45. Error::ServiceStopped,
  46. executor,
  47. );
  48. Ok(())
  49. }
  50. /// Stops the inbound session.
  51. pub async fn stop(&self) {
  52. self.acceptor.stop().await;
  53. self.accept_task.stop().await;
  54. }
  55. /// Start accepting connections for inbound session.
  56. fn start_accept_session(
  57. self: Arc<Self>,
  58. accept_addr: SocketAddr,
  59. executor: Arc<Executor<'_>>,
  60. ) -> Result<()> {
  61. info!(target: "net", "Starting inbound session on {}", accept_addr);
  62. let result = self.acceptor.clone().start(accept_addr, executor);
  63. if let Err(err) = result.clone() {
  64. error!(target: "net", "Error starting listener: {}", err);
  65. }
  66. result
  67. }
  68. /// Wait for all new channels created by the acceptor and call
  69. /// setup_channel() on them.
  70. async fn channel_sub_loop(self: Arc<Self>, executor: Arc<Executor<'_>>) -> Result<()> {
  71. let channel_sub = self.acceptor.clone().subscribe().await;
  72. loop {
  73. let channel = channel_sub.receive().await?;
  74. // Spawn a detached task to process the channel
  75. // This will just perform the channel setup then exit.
  76. executor.spawn(self.clone().setup_channel(channel, executor.clone())).detach();
  77. }
  78. }
  79. /// Registers the channel. First performs a network handshake and starts the
  80. /// channel. Then starts sending keep-alive and address messages across the
  81. /// channel.
  82. async fn setup_channel(
  83. self: Arc<Self>,
  84. channel: ChannelPtr,
  85. executor: Arc<Executor<'_>>,
  86. ) -> Result<()> {
  87. info!(target: "net", "Connected inbound [{}]", channel.address());
  88. self.clone().register_channel(channel.clone(), executor.clone()).await?;
  89. //self.attach_protocols(channel, executor).await
  90. Ok(())
  91. }
  92. // Starts sending keep-alive and address messages across the channels.
  93. /*async fn attach_protocols(
  94. self: Arc<Self>,
  95. channel: ChannelPtr,
  96. executor: Arc<Executor<'_>>,
  97. ) -> Result<()> {
  98. let hosts = self.p2p().hosts();
  99. let protocol_ping = ProtocolPing::new(channel.clone(), self.p2p());
  100. let protocol_addr = ProtocolAddress::new(channel, hosts).await;
  101. protocol_ping.start(executor.clone()).await;
  102. protocol_addr.start(executor).await;
  103. Ok(())
  104. }*/
  105. }
  106. impl Session for InboundSession {
  107. fn p2p(&self) -> Arc<P2p> {
  108. self.p2p.upgrade().unwrap()
  109. }
  110. fn selector_id(&self) -> SessionBitflag {
  111. SESSION_INBOUND
  112. }
  113. }