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
// Copyright (c) 2015-2017 Contributors as noted in the AUTHORS file.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>
// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// This file may not be copied, modified, or distributed except according to those terms.

use std::io;

use super::*;
use reactor;
use core::{SocketId, EndpointId};
use core::endpoint::Request;
use io_error::*;

#[doc(hidden)]
pub struct RequestSender {
    req_tx: EventLoopRequestSender,
    socket_id: SocketId,
    id: EndpointId
}

impl RequestSender {
    pub fn new(tx: EventLoopRequestSender, sid: SocketId, eid: EndpointId) -> RequestSender {
        RequestSender {
            req_tx: tx,
            socket_id: sid,
            id: eid,
        }
    }
    fn send(&self, req: Request) -> io::Result<()> {
        self.req_tx.send(reactor::Request::Endpoint(self.socket_id, self.id, req)).map_err(from_send_error)
    }
}

/// Endpoint of a socket.
///   
/// Obtained via the socket [bind](struct.Socket.html#method.bind) or 
/// [connect](struct.Socket.html#method.connect) methods.  
/// Can only be used to close an endpoint.  
/// Note that `drop(Endpoint)` will **NOT** close it.
pub struct Endpoint {
    request_sender: RequestSender,
    remote: bool
}

impl Endpoint {
    #[doc(hidden)]
    pub fn new(request_tx: RequestSender, remote: bool) -> Endpoint {
        Endpoint {
            request_sender: request_tx,
            remote: remote
        }
    }

    pub fn close(self) -> io::Result<()> {
        self.request_sender.send(Request::Close(self.remote))
    }
}