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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// 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.

#[doc(hidden)] pub mod network;
#[doc(hidden)] pub mod context;
#[doc(hidden)] pub mod config;
#[doc(hidden)] pub mod socket;
#[doc(hidden)] pub mod session;
#[doc(hidden)] pub mod endpoint;
#[doc(hidden)] pub mod device;
#[doc(hidden)] pub mod probe;

#[cfg(test)]
pub mod tests;

use std::fmt;
use std::hash::{BuildHasher, Hasher};

#[doc(hidden)]
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct Scheduled(usize);

/*****************************************************************************/
/*                                                                           */
/* EndpointId                                                                */
/*                                                                           */
/*****************************************************************************/

#[doc(hidden)]
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct EndpointId(usize);

impl fmt::Debug for EndpointId {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<usize> for EndpointId {
    fn from(value: usize) -> EndpointId {
        EndpointId(value)
    }
}

impl Into<usize> for EndpointId {
    fn into(self) -> usize {
        self.0
    }
}

impl<'x> Into<usize> for &'x EndpointId {
    fn into(self) -> usize {
        self.0
    }
}

/*****************************************************************************/
/*                                                                           */
/* Endpoint                                                                  */
/*                                                                           */
/*****************************************************************************/

#[doc(hidden)]
pub struct EndpointTmpl {
    pub pids: (u16, u16),
    pub spec: EndpointSpec
}

#[doc(hidden)]
pub struct EndpointSpec {
    pub url: String,
    pub desc: EndpointDesc
}

#[doc(hidden)]
pub struct EndpointDesc {
    pub send_priority: u8,
    pub recv_priority: u8,
    pub tcp_no_delay: bool,
    pub recv_max_size: u64
}

/*****************************************************************************/
/*                                                                           */
/* SocketId                                                                  */
/*                                                                           */
/*****************************************************************************/

#[doc(hidden)]
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct SocketId(usize);

impl fmt::Debug for SocketId {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<usize> for SocketId {
    fn from(value: usize) -> SocketId {
        SocketId(value)
    }
}

/*****************************************************************************/
/*                                                                           */
/* DeviceId                                                                  */
/*                                                                           */
/*****************************************************************************/

#[doc(hidden)]
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct DeviceId(usize);

impl fmt::Debug for DeviceId {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<usize> for DeviceId {
    fn from(value: usize) -> DeviceId {
        DeviceId(value)
    }
}

/*****************************************************************************/
/*                                                                           */
/* ProbeId                                                                   */
/*                                                                           */
/*****************************************************************************/

#[doc(hidden)]
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct ProbeId(usize);

impl fmt::Debug for ProbeId {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<usize> for ProbeId {
    fn from(value: usize) -> ProbeId {
        ProbeId(value)
    }
}

/// Request for socket polling, tells whether the poll should wait for the socket to become readable or writable.
pub struct PollReq {
    pub sid: SocketId,
    pub recv: bool,
    pub send: bool
}

/// Result of a socket polling, tells whether the socket is readable or writable.
pub struct PollRes {
    pub recv: bool,
    pub send: bool
}

/*****************************************************************************/
/*                                                                           */
/* Message                                                                   */
/*                                                                           */
/*****************************************************************************/

#[derive(Default, Debug)]
pub struct Message {
    pub header: Vec<u8>,
    pub body: Vec<u8>
}

impl Message {
    pub fn new() -> Message {
        Message {
            header: Vec::new(),
            body: Vec::new()
        }
    }

    pub fn from_body(body: Vec<u8>) -> Message {
        Message {
            header: Vec::new(),
            body: body
        }
    }

    pub fn from_header_and_body(header: Vec<u8>, body: Vec<u8>) -> Message {
        Message {
            header: header,
            body: body
        }
    }

    pub fn len(&self) -> usize {
        self.header.len() + self.body.len()
    }

    pub fn get_header(&self) -> &[u8] {
        &self.header
    }

    pub fn get_body(&self) -> &[u8] {
        &self.body
    }

    pub fn split(self) -> (Vec<u8>, Vec<u8>) {
        (self.header, self.body)
    }

    pub fn without_header(self) -> Message {
        Message::from_body(self.body)
    }
}

impl Into<Vec<u8>> for Message {
    fn into(self) -> Vec<u8> {
        self.body
    }
}

impl From<Vec<u8>> for Message {
    fn from(value: Vec<u8>) -> Message {
        Message::from_body(value)
    }
}

/*****************************************************************************/
/*                                                                           */
/* Hash                                                                      */
/*                                                                           */
/*****************************************************************************/

pub struct IdHasher(u64);

impl Default for IdHasher {
    fn default() -> IdHasher {
        IdHasher(0)
    }
}

impl Hasher for IdHasher {

    #[inline]
    fn finish(&self) -> u64 {
        self.0
    }

    #[inline]
    fn write_u64(&mut self, i: u64) {
        self.0 = self.0.wrapping_add(i);
    }

    #[inline]
    fn write(&mut self, bytes: &[u8]) {
        for x in bytes {
            self.write_u64(*x as u64);
        }
    }
    #[inline]
    fn write_u8(&mut self, i: u8) {
        self.write_u64(i as u64)
    }
    #[inline]
    fn write_u16(&mut self, i: u16) {
        self.write_u64(i as u64)
    }
    #[inline]
    fn write_u32(&mut self, i: u32) {
        self.write_u64(i as u64)
    }
    #[inline]
    fn write_usize(&mut self, i: usize) {
        self.write_u64(i as u64)
    }
    #[inline]
    fn write_i8(&mut self, i: i8) {
        self.write_u64(i as u64)
    }
    #[inline]
    fn write_i16(&mut self, i: i16) {
        self.write_u64(i as u64)
    }
    #[inline]
    fn write_i32(&mut self, i: i32) {
        self.write_u64(i as u64)
    }
    #[inline]
    fn write_i64(&mut self, i: i64) {
        self.write_u64(i as u64)
    }
    #[inline]
    fn write_isize(&mut self, i: isize) {
        self.write_u64(i as u64)
    }  
}

pub struct BuildIdHasher;

impl Default for BuildIdHasher {
    fn default() -> BuildIdHasher {
        BuildIdHasher
    }
}

impl BuildHasher for BuildIdHasher {
    type Hasher = IdHasher;

    #[inline]
    fn build_hasher(&self) -> IdHasher {
        IdHasher(0)
    }     
}