overnet_core/proxy/handle/
event_pair.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
141
// Copyright 2021 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.

use super::signals::Collector;
use super::{
    IntoProxied, Message, Proxyable, ProxyableRW, ReadValue, RouterHolder, Serializer, IO,
};
use crate::peer::PeerConnRef;
use anyhow::{format_err, Error};
use fidl::{AsHandleRef, HandleBased, Peered, Signals};
use std::task::{Context, Poll};
use zx_status;

pub(crate) struct EventPair {
    event_pair: fidl::EventPair,
}

impl std::fmt::Debug for EventPair {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.event_pair.fmt(f)
    }
}

impl Proxyable for EventPair {
    type Message = EventPairMessage;

    fn from_fidl_handle(hdl: fidl::Handle) -> Result<Self, Error> {
        Ok(fidl::EventPair::from_handle(hdl).into_proxied()?)
    }

    fn into_fidl_handle(self) -> Result<fidl::Handle, Error> {
        Ok(self.event_pair.into_handle())
    }

    fn signal_peer(&self, clear: Signals, set: Signals) -> Result<(), Error> {
        self.event_pair.signal_peer(clear, set).map_err(Into::into)
    }
}

impl<'a> ProxyableRW<'a> for EventPair {
    type Reader = EventPairReader<'a>;
    type Writer = EventPairWriter;
}

impl IntoProxied for fidl::EventPair {
    type Proxied = EventPair;
    fn into_proxied(self) -> Result<EventPair, Error> {
        Ok(EventPair { event_pair: self })
    }
}

#[derive(Debug, Default, PartialEq)]
pub(crate) struct EventPairMessage;

impl Message for EventPairMessage {
    type Parser = EventPairParser;
    type Serializer = EventPairSerializer;
}

pub(crate) struct EventPairReader<'a> {
    collector: Collector<'a>,
}

impl<'a> IO<'a> for EventPairReader<'a> {
    type Proxyable = EventPair;
    type Output = ReadValue;
    fn new() -> Self {
        EventPairReader { collector: Default::default() }
    }
    fn poll_io(
        &mut self,
        _: &mut EventPairMessage,
        event_pair: &'a EventPair,
        fut_ctx: &mut Context<'_>,
    ) -> Poll<Result<ReadValue, zx_status::Status>> {
        // There's no such thing as an event pair message, so we just pretend to be waiting forever
        let read_result = Poll::Pending;
        self.collector.after_read(fut_ctx, event_pair.event_pair.as_handle_ref(), read_result, true)
    }
}

pub(crate) struct EventPairWriter;

impl IO<'_> for EventPairWriter {
    type Proxyable = EventPair;
    type Output = ();
    fn new() -> Self {
        EventPairWriter
    }
    fn poll_io(
        &mut self,
        _: &mut EventPairMessage,
        _: &EventPair,
        _: &mut Context<'_>,
    ) -> Poll<Result<(), zx_status::Status>> {
        // We always fail to parse event pair messages, therefore we can never write one to an object
        unreachable!()
    }
}

#[derive(Debug)]
pub(crate) struct EventPairSerializer;

impl Serializer for EventPairSerializer {
    type Message = EventPairMessage;
    fn new() -> EventPairSerializer {
        EventPairSerializer
    }
    fn poll_ser(
        &mut self,
        _: &mut Self::Message,
        _: &mut Vec<u8>,
        _: PeerConnRef<'_>,
        _: &mut RouterHolder<'_>,
        _: &mut Context<'_>,
    ) -> Poll<Result<(), Error>> {
        // Reading from the event pair is always pending, therefore we can never serialize a message
        unreachable!()
    }
}

#[derive(Debug)]
pub(crate) struct EventPairParser;

impl Serializer for EventPairParser {
    type Message = EventPairMessage;
    fn new() -> EventPairParser {
        EventPairParser
    }
    fn poll_ser(
        &mut self,
        _: &mut Self::Message,
        _: &mut Vec<u8>,
        _: PeerConnRef<'_>,
        _: &mut RouterHolder<'_>,
        _: &mut Context<'_>,
    ) -> Poll<Result<(), Error>> {
        Poll::Ready(Err(format_err!("Event pairs do not exchange message payloads")))
    }
}