overnet_core/proxy/run/
spawn.rs

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Factory functions for proxies - one each for sending a handle and receiving a handle.

use super::super::handle::{IntoProxied, ProxyableHandle, ProxyableRW};
use super::super::{Proxy, ProxyTransferInitiationReceiver};
use crate::handle_info::WithRights;
use crate::peer::{FramedStreamReader, FramedStreamWriter, PeerConnRef};
use crate::router::{FoundTransfer, OpenedTransfer, Router};
use anyhow::{format_err, Error};
use fidl_fuchsia_overnet_protocol::{StreamId, StreamRef, TransferInitiator, TransferWaiter};
use std::future::Future;
use std::sync::Weak;

pub(crate) async fn send<Hdl: 'static + for<'a> ProxyableRW<'a>>(
    hdl: Hdl,
    initiate_transfer: ProxyTransferInitiationReceiver,
    stream_writer: FramedStreamWriter,
    stream_reader: FramedStreamReader,
    router: Weak<Router>,
) -> Result<(), Error> {
    super::main::run_main_loop(
        Proxy::new(hdl, router),
        initiate_transfer,
        stream_writer,
        None,
        stream_reader,
    )
    .await
}

// Start receiving from some stream for a channel.
// Returns a handle, and an optional future that runs the proxying activity (or none if no proxying is occurring).
pub(crate) async fn recv<Hdl, CreateType>(
    create_handles: impl FnOnce() -> Result<(CreateType, CreateType), Error>,
    rights: CreateType::Rights,
    initiate_transfer: ProxyTransferInitiationReceiver,
    stream_ref: StreamRef,
    conn: PeerConnRef<'_>,
    router: Weak<Router>,
) -> Result<(fidl::Handle, Option<impl Send + Future<Output = Result<(), Error>>>), Error>
where
    Hdl: 'static + for<'a> ProxyableRW<'a>,
    CreateType: fidl::HandleBased + IntoProxied<Proxied = Hdl> + std::fmt::Debug + WithRights,
{
    Ok(match stream_ref {
        StreamRef::Creating(StreamId { id: stream_id }) => {
            let (app_chan, overnet_chan) = create_handles()?;
            let app_chan = app_chan.with_rights(rights)?;
            let (stream_writer, stream_reader) = conn.bind_id(stream_id).await?;
            let overnet_chan = overnet_chan.into_proxied()?;
            (
                app_chan.into_handle(),
                Some(super::main::run_main_loop(
                    Proxy::new(overnet_chan, router),
                    initiate_transfer,
                    stream_writer,
                    None,
                    stream_reader,
                )),
            )
        }
        StreamRef::TransferInitiator(TransferInitiator {
            stream_id: StreamId { id: stream_id },
            new_destination_node,
            transfer_key,
        }) => {
            let (app_chan, overnet_chan) = create_handles()?;
            let app_chan = app_chan.with_rights(rights)?;
            let initial_stream_reader: FramedStreamReader =
                conn.bind_uni_id(stream_id).await?.into();
            let opened_transfer = Weak::upgrade(&router)
                .ok_or_else(|| format_err!("No router to handle draining stream ref"))?
                .open_transfer(
                    new_destination_node.into(),
                    transfer_key,
                    overnet_chan.into_handle(),
                )
                .await?;
            match opened_transfer {
                OpenedTransfer::Fused => {
                    let app_chan = app_chan.into_proxied()?;
                    (
                        ProxyableHandle::new(app_chan, router)
                            .drain_stream_to_handle(initial_stream_reader)
                            .await?,
                        None,
                    )
                }
                OpenedTransfer::Remote(stream_writer, stream_reader, overnet_chan) => (
                    app_chan.into_handle(),
                    Some(super::main::run_main_loop(
                        Proxy::new(Hdl::from_fidl_handle(overnet_chan)?, router),
                        initiate_transfer,
                        stream_writer,
                        Some(initial_stream_reader),
                        stream_reader,
                    )),
                ),
            }
        }
        StreamRef::TransferWaiter(TransferWaiter {
            stream_id: StreamId { id: stream_id },
            transfer_key,
        }) => {
            let initial_stream_reader: FramedStreamReader =
                conn.bind_uni_id(stream_id).await?.into();
            let found_transfer = Weak::upgrade(&router)
                .ok_or_else(|| format_err!("No router to handle draining stream ref"))?
                .find_transfer(transfer_key)
                .await?;
            match found_transfer {
                FoundTransfer::Fused(handle) => {
                    let handle = Hdl::from_fidl_handle(handle)?;
                    (
                        ProxyableHandle::new(handle, router)
                            .drain_stream_to_handle(initial_stream_reader)
                            .await?,
                        None,
                    )
                }
                FoundTransfer::Remote(stream_writer, stream_reader) => {
                    let (app_chan, overnet_chan) = create_handles()?;
                    (
                        app_chan.with_rights(rights)?.into_handle(),
                        Some(super::main::run_main_loop(
                            Proxy::new(overnet_chan.into_proxied()?, router),
                            initiate_transfer,
                            stream_writer,
                            Some(initial_stream_reader),
                            stream_reader,
                        )),
                    )
                }
            }
        }
    })
}