1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use std::{
    future::Future,
    pin::Pin,
    task::{Context, Poll},
};

use protosocket::{Connection, ConnectionBindings, NetworkStatusEvent};
use tokio::sync::mpsc;

/// To be spawned, this is the task that handles serialization, deserialization, and network readiness events.
pub struct ConnectionDriver<Bindings: ConnectionBindings> {
    connection: Connection<Bindings>,
    network_readiness: mpsc::UnboundedReceiver<NetworkStatusEvent>,
}

impl<Bindings: ConnectionBindings> ConnectionDriver<Bindings> {
    pub(crate) fn new(
        connection: Connection<Bindings>,
        network_readiness: mpsc::UnboundedReceiver<NetworkStatusEvent>,
    ) -> Self {
        Self {
            connection,
            network_readiness,
        }
    }
}

impl<Bindings: ConnectionBindings> ConnectionDriver<Bindings> {
    fn poll_network_status(&mut self, context: &mut Context<'_>) -> Poll<()> {
        loop {
            break match self.network_readiness.poll_recv(context) {
                Poll::Ready(Some(event)) => {
                    self.connection.handle_connection_event(event);
                    continue;
                }
                Poll::Ready(None) => {
                    log::debug!("dropping connection: network readiness sender dropped");
                    Poll::Ready(())
                }
                Poll::Pending => Poll::Pending,
            };
        }
    }
}

impl<Bindings: ConnectionBindings> Future for ConnectionDriver<Bindings> {
    type Output = ();

    fn poll(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Self::Output> {
        if let Poll::Ready(early_out) = self.poll_network_status(context) {
            return Poll::Ready(early_out);
        }

        if let Poll::Ready(early_out) = self.connection.poll_serialize_oubound(context) {
            log::debug!("dropping connection: outbound channel failure");
            return Poll::Ready(early_out);
        }

        match self.connection.poll_write_buffers() {
            Ok(closed) => {
                if closed {
                    log::info!("write connection closed");
                    return Poll::Ready(());
                }
            }
            Err(e) => {
                log::warn!("dropping connection: write failed {e:?}");
                return Poll::Ready(());
            }
        }

        match self.connection.poll_read_inbound() {
            Ok(false) => {
                // log::trace!("checked inbound connection buffer");
            }
            Ok(true) => {
                if self.connection.has_work_in_flight() {
                    log::debug!("read closed connection but work is in flight");
                    return Poll::Ready(());
                } else {
                    log::debug!("read closed connection");
                    return Poll::Ready(());
                }
            }
            Err(e) => {
                log::warn!("dropping connection after read: {e:?}");
                return Poll::Ready(());
            }
        }

        match self.connection.dispatch_messages_from_read_queue(context) {
            Ok(false) => {
                // log::trace!("checked for messages to dispatch");
            }
            Ok(true) => {
                if self.connection.has_work_in_flight() {
                    log::debug!("connection read dispatch is closed but work is in flight");
                    return Poll::Ready(());
                } else {
                    log::debug!("connection read dispatch is closed");
                    return Poll::Ready(());
                }
            }
            Err(e) => {
                log::warn!("dropping connection after read dispatch: {e:?}");
                return Poll::Ready(());
            }
        }

        Poll::Pending
    }
}