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
pub mod config;
pub mod types;
use std::{fmt, sync::Arc};
use async_zmq_types::{InnerSocket, IntoInnerSocket, Multipart, SocketBuilder};
use zmq;
use crate::{
async_types::{
MultipartRequest, MultipartResponse, MultipartSink, MultipartSinkStream, MultipartStream,
},
polling::{LocalSession, SockId},
RecvFuture, SendFuture,
};
pub struct Socket {
sock: SockId,
session: LocalSession,
}
impl Socket {
pub fn builder<T>(ctx: Arc<zmq::Context>) -> SocketBuilder<'static, T>
where
T: IntoInnerSocket,
{
SocketBuilder::new(ctx)
}
pub fn inner(self) -> SockId {
self.sock
}
pub fn from_sock_and_session(sock: SockId, session: LocalSession) -> Self {
Socket { sock, session }
}
pub(crate) fn recv_msg(&self) -> RecvFuture {
self.session.recv(&self.sock)
}
pub(crate) fn send_msg(&self, multipart: Multipart) -> SendFuture {
self.session.send(&self.sock, multipart)
}
}
impl<T> InnerSocket<T> for Socket
where
T: IntoInnerSocket + From<Self>,
{
type Request = MultipartRequest<T>;
type Response = MultipartResponse<T>;
type Sink = MultipartSink<T>;
type Stream = MultipartStream<T>;
type SinkStream = MultipartSinkStream<T>;
fn send(self, multipart: Multipart) -> Self::Request {
MultipartRequest::new(self, multipart)
}
fn recv(self) -> Self::Response {
MultipartResponse::new(self)
}
fn stream(self) -> Self::Stream {
MultipartStream::new(self)
}
fn sink(self, buffer_size: usize) -> Self::Sink {
MultipartSink::new(self, buffer_size)
}
fn sink_stream(self, buffer_size: usize) -> Self::SinkStream {
MultipartSinkStream::new(self, buffer_size)
}
}
impl From<(SockId, LocalSession)> for Socket {
fn from((sock, session): (SockId, LocalSession)) -> Self {
Socket { sock, session }
}
}
impl fmt::Debug for Socket {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Socket({:?})", self.sock)
}
}
impl fmt::Display for Socket {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Socket({})", self.sock)
}
}