asyncio/
socket_base.rs

1use prelude::{Protocol, IoControl, SocketOption, GetSocketOption, SetSocketOption};
2use ffi::*;
3
4use std::mem;
5
6pub const MAX_CONNECTIONS: i32 = 126;
7
8/// Possible values which can be passed to the shutdown method.
9#[repr(i32)]
10pub enum Shutdown {
11    /// Indicates that the reading portion of this socket should be shut down.
12    Read = SHUT_RD,
13
14    /// Indicates that the writing portion of this socket should be shut down.
15    Write = SHUT_WR,
16
17    /// Shut down both the reading and writing portions of this socket.
18    Both = SHUT_RDWR,
19}
20
21impl Into<i32> for Shutdown {
22    fn into(self) -> i32 {
23        self as i32
24    }
25}
26
27#[derive(Default, Clone)]
28pub struct NonBlockingIo(i32);
29
30impl NonBlockingIo {
31    pub fn new(on: bool) -> NonBlockingIo {
32        NonBlockingIo(on as i32)
33    }
34
35    pub fn get(&self) -> bool {
36        self.0 != 0
37    }
38}
39
40impl IoControl for NonBlockingIo {
41    type Data = i32;
42
43    #[cfg(unix)] fn name(&self) -> u64 { FIONBIO }
44    #[cfg(windows)] fn name(&self) -> i32 { FIONBIO as i32 }
45
46    fn data(&mut self) -> &mut Self::Data {
47        &mut self.0
48    }
49}
50
51/// IO control command to get the amount of data that can be read without blocking.
52///
53/// Implements the FIONREAD IO control command.
54///
55/// # Examples
56/// Gettable the IO control:
57///
58/// ```
59/// use asyncio::*;
60/// use asyncio::ip::*;
61/// use asyncio::socket_base::BytesReadable;
62///
63/// let ctx = &IoContext::new().unwrap();
64/// let soc = UdpSocket::new(ctx, Udp::v4()).unwrap();
65///
66/// let mut bytes = BytesReadable::default();
67/// soc.io_control(&mut bytes).unwrap();
68/// let sized = bytes.get();
69/// ```
70#[derive(Default, Clone)]
71pub struct BytesReadable(i32);
72
73impl BytesReadable {
74    pub fn get(&self) -> usize {
75        self.0 as usize
76    }
77}
78
79impl IoControl for BytesReadable {
80    type Data = i32;
81
82    #[cfg(unix)] fn name(&self) -> u64 { FIONREAD }
83    #[cfg(windows)] fn name(&self) -> i32 { FIONREAD }
84
85    fn data(&mut self) -> &mut Self::Data {
86        &mut self.0
87    }
88}
89
90// #[derive(Default, Clone)]
91// pub struct BytesWritten(i32);
92
93// impl BytesWritten {
94//     pub fn get(&self) -> usize {
95//         self.0 as usize
96//     }
97// }
98
99// impl IoControl for BytesWritten {
100//     type Data = i32;
101
102//     fn name(&self) -> i32 {
103//         TIOCOUTQ as i32
104//     }
105
106//     fn data(&mut self) -> &mut Self::Data {
107//         &mut self.0
108//     }
109// }
110
111#[derive(Default, Clone)]
112pub struct AtMark(i32);
113
114impl AtMark {
115    pub fn get(&self) -> bool {
116        self.0 != 0
117    }
118}
119
120impl IoControl for AtMark {
121    type Data = i32;
122
123    #[cfg(unix)] fn name(&self) -> u64 { SIOCATMARK }
124    #[cfg(windows)] fn name(&self) -> i32 { SIOCATMARK }
125
126    fn data(&mut self) -> &mut Self::Data {
127        &mut self.0
128    }
129}
130
131/// socket option to permit sending of broadcast messages.
132///
133/// Implements the SOL_SOCKET/SO_BROADCAST socket option.
134///
135/// # Examples
136/// Setting the option:
137///
138/// ```
139/// use asyncio::*;
140/// use asyncio::ip::*;
141/// use asyncio::socket_base::Broadcast;
142///
143/// let ctx = &IoContext::new().unwrap();
144/// let soc = UdpSocket::new(ctx, Udp::v4()).unwrap();
145///
146/// soc.set_option(Broadcast::new(true)).unwrap();
147/// ```
148///
149/// Getting the option:
150///
151/// ```
152/// use asyncio::*;
153/// use asyncio::ip::*;
154/// use asyncio::socket_base::Broadcast;
155///
156/// let ctx = &IoContext::new().unwrap();
157/// let soc = UdpSocket::new(ctx, Udp::v4()).unwrap();
158///
159/// let opt: Broadcast = soc.get_option().unwrap();
160/// let is_set: bool = opt.get();
161/// ```
162#[derive(Default, Clone)]
163pub struct Broadcast(i32);
164
165impl Broadcast {
166    pub fn new(on: bool) -> Broadcast {
167        Broadcast(on as i32)
168    }
169
170    pub fn get(&self) -> bool {
171        self.0 != 0
172    }
173
174    pub fn set(&mut self, on: bool) {
175        self.0 = on as i32
176    }
177}
178
179impl<P: Protocol> SocketOption<P> for Broadcast {
180    type Data = i32;
181
182    fn level(&self, _: &P) -> i32 {
183        SOL_SOCKET
184    }
185
186    fn name(&self, _: &P) -> i32 {
187        SO_BROADCAST
188    }
189}
190
191impl<P: Protocol> GetSocketOption<P> for Broadcast {
192    fn data_mut(&mut self) -> &mut Self::Data {
193        &mut self.0
194    }
195}
196
197impl<P: Protocol> SetSocketOption<P> for Broadcast {
198    fn data(&self) -> &Self::Data {
199        &self.0
200    }
201}
202
203/// Socket option to enable socket-level debugging.
204///
205/// Implements the SOL_SOCKET/SO_DEBUG socket option.
206///
207/// # Examples
208/// Setting the option:
209///
210/// ```rust,no_run
211/// use asyncio::*;
212/// use asyncio::ip::*;
213/// use asyncio::socket_base::Debug;
214///
215/// let ctx = &IoContext::new().unwrap();
216/// let soc = TcpSocket::new(ctx, Tcp::v4()).unwrap();
217///
218/// soc.set_option(Debug::new(true)).unwrap();
219/// ```
220///
221/// Getting the option:
222///
223/// ```
224/// use asyncio::*;
225/// use asyncio::ip::*;
226/// use asyncio::socket_base::Debug;
227///
228/// let ctx = &IoContext::new().unwrap();
229/// let soc = TcpSocket::new(ctx, Tcp::v4()).unwrap();
230///
231/// let opt: Debug = soc.get_option().unwrap();
232/// let is_set: bool = opt.get();
233/// ```
234#[derive(Default, Clone)]
235pub struct Debug(i32);
236
237impl Debug {
238    pub fn new(on: bool) -> Debug {
239        Debug(on as i32)
240    }
241
242    pub fn get(&self) -> bool {
243        self.0 != 0
244    }
245
246    pub fn set(&mut self, on: bool) {
247        self.0 = on as i32
248    }
249}
250
251impl<P: Protocol> SocketOption<P> for Debug {
252    type Data = i32;
253
254    fn level(&self, _: &P) -> i32 {
255        SOL_SOCKET
256    }
257
258    fn name(&self, _: &P) -> i32 {
259        SO_DEBUG
260    }
261}
262
263impl<P: Protocol> GetSocketOption<P> for Debug {
264
265    fn data_mut(&mut self) -> &mut Self::Data {
266        &mut self.0
267    }
268}
269
270impl<P: Protocol> SetSocketOption<P> for Debug {
271    fn data(&self) -> &Self::Data {
272        &self.0
273    }
274}
275
276/// Socket option to don't use a gateway. send to local network host only.
277///
278/// Implements the SOL_SOCKET/SO_DONTROUTE socket option.
279///
280/// # Examples
281/// Setting the option:
282///
283/// ```
284/// use asyncio::*;
285/// use asyncio::ip::*;
286/// use asyncio::socket_base::DoNotRoute;
287///
288/// let ctx = &IoContext::new().unwrap();
289/// let soc = UdpSocket::new(ctx, Udp::v4()).unwrap();
290///
291/// soc.set_option(DoNotRoute::new(true)).unwrap();
292/// ```
293///
294/// Getting the option:
295///
296/// ```
297/// use asyncio::*;
298/// use asyncio::ip::*;
299/// use asyncio::socket_base::DoNotRoute;
300///
301/// let ctx = &IoContext::new().unwrap();
302/// let soc = UdpSocket::new(ctx, Udp::v4()).unwrap();
303///
304/// let opt: DoNotRoute = soc.get_option().unwrap();
305/// let is_set: bool = opt.get();
306/// ```
307#[derive(Default, Clone)]
308pub struct DoNotRoute(i32);
309
310impl DoNotRoute {
311    pub fn new(on: bool) -> DoNotRoute {
312        DoNotRoute(on as i32)
313    }
314
315    pub fn get(&self) -> bool {
316        self.0 != 0
317    }
318
319    pub fn set(&mut self, on: bool) {
320        self.0 = on as i32
321    }
322}
323
324impl<P: Protocol> SocketOption<P> for DoNotRoute {
325    type Data = i32;
326
327    fn level(&self, _: &P) -> i32 {
328        SOL_SOCKET
329    }
330
331    fn name(&self, _: &P) -> i32 {
332        SO_DONTROUTE
333    }
334}
335
336impl<P: Protocol> GetSocketOption<P> for DoNotRoute {
337    fn data_mut(&mut self) -> &mut Self::Data {
338        &mut self.0
339    }
340}
341
342impl<P: Protocol> SetSocketOption<P> for DoNotRoute {
343    fn data(&self) -> &Self::Data {
344        &self.0
345    }
346}
347
348/// Socket option to send keep-alives.
349///
350/// Implements the SOL_SOKCET/SO_KEEPALIVE socket option.
351///
352/// # Examples
353/// Setting the option:
354///
355/// ```
356/// use asyncio::*;
357/// use asyncio::ip::*;
358/// use asyncio::socket_base::KeepAlive;
359///
360/// let ctx = &IoContext::new().unwrap();
361/// let soc = TcpSocket::new(ctx, Tcp::v4()).unwrap();
362///
363/// soc.set_option(KeepAlive::new(true)).unwrap();
364/// ```
365///
366/// Getting the option:
367///
368/// ```
369/// use asyncio::*;
370/// use asyncio::ip::*;
371/// use asyncio::socket_base::KeepAlive;
372///
373/// let ctx = &IoContext::new().unwrap();
374/// let soc = TcpSocket::new(ctx, Tcp::v4()).unwrap();
375///
376/// let opt: KeepAlive = soc.get_option().unwrap();
377/// let is_set: bool = opt.get();
378/// ```
379#[derive(Default, Clone)]
380pub struct KeepAlive(i32);
381
382impl KeepAlive {
383    pub fn new(on: bool) -> KeepAlive {
384        KeepAlive(on as i32)
385    }
386
387    pub fn get(&self) -> bool {
388        self.0 != 0
389    }
390
391    pub fn set(&mut self, on: bool) {
392        self.0 = on as i32
393    }
394}
395
396impl<P: Protocol> SocketOption<P> for KeepAlive {
397    type Data = i32;
398
399    fn level(&self, _: &P) -> i32 {
400        SOL_SOCKET
401    }
402
403    fn name(&self, _: &P) -> i32 {
404        SO_KEEPALIVE
405    }
406}
407
408impl<P: Protocol> GetSocketOption<P> for KeepAlive {
409    fn data_mut(&mut self) -> &mut Self::Data {
410        &mut self.0
411    }
412}
413
414impl<P: Protocol> SetSocketOption<P> for KeepAlive {
415    fn data(&self) -> &Self::Data {
416        &self.0
417    }
418}
419
420/// Socket option to specify whether the socket lingers on close if unsent data is present.
421///
422/// Implements the SOL_SOCKET/SO_LINGER socket option.
423///
424/// # Examples
425/// Setting the option:
426///
427/// ```
428/// use asyncio::*;
429/// use asyncio::ip::*;
430/// use asyncio::socket_base::Linger;
431///
432/// let ctx = &IoContext::new().unwrap();
433/// let soc = TcpSocket::new(ctx, Tcp::v4()).unwrap();
434///
435/// soc.set_option(Linger::new(Some(30))).unwrap();
436/// ```
437///
438/// Getting the option:
439///
440/// ```
441/// use asyncio::*;
442/// use asyncio::ip::*;
443/// use asyncio::socket_base::Linger;
444///
445/// let ctx = &IoContext::new().unwrap();
446/// let soc = TcpSocket::new(ctx, Tcp::v4()).unwrap();
447///
448/// let opt: Linger = soc.get_option().unwrap();
449/// let is_set: Option<u16> = opt.get();
450/// ```
451#[derive(Clone)]
452pub struct Linger(linger);
453
454impl Linger {
455    pub fn new(timeout: Option<u16>) -> Linger {
456        match timeout {
457            Some(timeout)
458                => Linger(linger { l_onoff: 1, l_linger: timeout.into() }),
459            None
460                => Linger(linger { l_onoff: 0, l_linger: 0 })
461        }
462    }
463
464    pub fn get(&self) -> Option<u16> {
465        if (self.0).l_onoff != 0 {
466            Some((self.0).l_linger as u16)
467        } else {
468            None
469        }
470    }
471}
472
473impl Default for Linger {
474    fn default() -> Linger {
475        unsafe { mem::zeroed() }
476    }
477}
478
479impl<P: Protocol> SocketOption<P> for Linger {
480    type Data = linger;
481
482    fn level(&self, _: &P) -> i32 {
483        SOL_SOCKET
484    }
485
486    fn name(&self, _: &P) -> i32 {
487        SO_LINGER
488    }
489}
490
491impl<P: Protocol> GetSocketOption<P> for Linger {
492    fn data_mut(&mut self) -> &mut Self::Data {
493        unsafe { mem::transmute(&mut self.0) }
494    }
495}
496
497impl<P: Protocol> SetSocketOption<P> for Linger {
498    fn data(&self) -> &Self::Data {
499        unsafe { mem::transmute(&self.0) }
500    }
501
502    fn size(&self) -> usize {
503        mem::size_of_val(&self.0)
504    }
505}
506
507/// Socket option for the receive buffer size of a socket.
508///
509/// Implements the SOL_SOCKET/SO_RCVBUF socket option.
510///
511/// # Examples
512/// Setting the option:
513///
514/// ```
515/// use asyncio::*;
516/// use asyncio::ip::*;
517/// use asyncio::socket_base::RecvBufferSize;
518///
519/// let ctx = &IoContext::new().unwrap();
520/// let soc = TcpSocket::new(ctx, Tcp::v4()).unwrap();
521///
522/// soc.set_option(RecvBufferSize::new(8192)).unwrap();
523/// ```
524///
525/// Getting the option:
526///
527/// ```
528/// use asyncio::*;
529/// use asyncio::ip::*;
530/// use asyncio::socket_base::RecvBufferSize;
531///
532/// let ctx = &IoContext::new().unwrap();
533/// let soc = TcpSocket::new(ctx, Tcp::v4()).unwrap();
534///
535/// let opt: RecvBufferSize = soc.get_option().unwrap();
536/// let size: usize = opt.get();
537/// ```
538#[derive(Default, Clone)]
539pub struct RecvBufferSize(i32);
540
541impl RecvBufferSize {
542    pub fn new(size: usize) -> RecvBufferSize {
543        RecvBufferSize(size as i32)
544    }
545
546    pub fn get(&self) -> usize {
547        self.0 as usize
548    }
549
550    pub fn set(&mut self, size: usize) {
551        self.0 = size as i32
552    }
553}
554
555impl<P: Protocol> SocketOption<P> for RecvBufferSize {
556    type Data = i32;
557
558    fn level(&self, _: &P) -> i32 {
559        SOL_SOCKET
560    }
561
562    fn name(&self, _: &P) -> i32 {
563        SO_RCVBUF
564    }
565}
566
567impl<P: Protocol> GetSocketOption<P> for RecvBufferSize {
568    fn data_mut(&mut self) -> &mut Self::Data {
569        &mut self.0
570    }
571}
572
573impl<P: Protocol> SetSocketOption<P> for RecvBufferSize {
574    fn data(&self) -> &Self::Data {
575        &self.0
576    }
577}
578
579/// Socket option for the receive low watermark.
580///
581/// Implements the SOL_SOCKET/SO_RCVLOWAT socket option.
582///
583/// # Examples
584/// Setting the option:
585///
586/// ```rust,no_run
587/// use asyncio::*;
588/// use asyncio::ip::*;
589/// use asyncio::socket_base::RecvLowWatermark;
590///
591/// let ctx = &IoContext::new().unwrap();
592/// let soc = TcpSocket::new(ctx, Tcp::v4()).unwrap();
593///
594/// soc.set_option(RecvLowWatermark::new(1024)).unwrap();
595/// ```
596///
597/// Getting the option:
598///
599/// ```rust,no_run
600/// use asyncio::*;
601/// use asyncio::ip::*;
602/// use asyncio::socket_base::RecvLowWatermark;
603///
604/// let ctx = &IoContext::new().unwrap();
605/// let soc = TcpSocket::new(ctx, Tcp::v4()).unwrap();
606///
607/// let opt: RecvLowWatermark = soc.get_option().unwrap();
608/// let size: usize = opt.get();
609/// ```
610#[derive(Default, Clone)]
611pub struct RecvLowWatermark(i32);
612
613impl RecvLowWatermark {
614    pub fn new(size: usize) -> RecvLowWatermark {
615        RecvLowWatermark(size as i32)
616    }
617
618    pub fn get(&self) -> usize {
619        self.0 as usize
620    }
621
622    pub fn set(&mut self, size: usize) {
623        self.0 = size as i32
624    }
625}
626
627impl<P: Protocol> SocketOption<P> for RecvLowWatermark {
628    type Data = i32;
629
630    fn level(&self, _: &P) -> i32 {
631        SOL_SOCKET
632    }
633
634    fn name(&self, _: &P) -> i32 {
635        SO_RCVLOWAT
636    }
637}
638
639impl<P: Protocol> GetSocketOption<P> for RecvLowWatermark {
640    fn data_mut(&mut self) -> &mut Self::Data {
641        &mut self.0
642    }
643}
644
645impl<P: Protocol> SetSocketOption<P> for RecvLowWatermark {
646    fn data(&self) -> &Self::Data {
647        &self.0
648    }
649}
650
651/// Socket option to allow the socket to be bound to an address that is already in use.
652///
653/// Implements the SOL_SOCKET/SO_REUSEADDR socket option.
654///
655/// # Examples
656/// Setting the option:
657///
658/// ```
659/// use asyncio::*;
660/// use asyncio::ip::*;
661/// use asyncio::socket_base::ReuseAddr;
662///
663/// let ctx = &IoContext::new().unwrap();
664/// let soc = TcpListener::new(ctx, Tcp::v4()).unwrap();
665///
666/// soc.set_option(ReuseAddr::new(true)).unwrap();
667/// ```
668///
669/// Getting the option:
670///
671/// ```
672/// use asyncio::*;
673/// use asyncio::ip::*;
674/// use asyncio::socket_base::ReuseAddr;
675///
676/// let ctx = &IoContext::new().unwrap();
677/// let soc = TcpListener::new(ctx, Tcp::v4()).unwrap();
678///
679/// let opt: ReuseAddr = soc.get_option().unwrap();
680/// let is_set: bool = opt.get();
681/// ```
682
683#[derive(Default, Clone)]
684pub struct ReuseAddr(i32);
685
686impl<P: Protocol> SocketOption<P> for ReuseAddr {
687    type Data = i32;
688
689    fn level(&self, _: &P) -> i32 {
690        SOL_SOCKET
691    }
692
693    fn name(&self, _: &P) -> i32 {
694        SO_REUSEADDR
695    }
696}
697
698impl<P: Protocol> GetSocketOption<P> for ReuseAddr {
699    fn data_mut(&mut self) -> &mut Self::Data {
700        &mut self.0
701    }
702}
703
704impl<P: Protocol> SetSocketOption<P> for ReuseAddr {
705    fn data(&self) -> &Self::Data {
706        &self.0
707    }
708}
709
710impl ReuseAddr {
711    pub fn new(on: bool) -> ReuseAddr {
712        ReuseAddr(on as i32)
713    }
714
715    pub fn get(&self) -> bool {
716        self.0 != 0
717    }
718
719    pub fn set(&mut self, on: bool) {
720        self.0 = on as i32
721    }
722}
723
724/// Socket option for the send buffer size of a socket.
725///
726/// Implements the SOL_SOCKET/SO_SNDBUF socket option.
727///
728/// # Examples
729/// Setting the option:
730///
731/// ```
732/// use asyncio::*;
733/// use asyncio::ip::*;
734/// use asyncio::socket_base::SendBufferSize;
735///
736/// let ctx = &IoContext::new().unwrap();
737/// let soc = TcpSocket::new(ctx, Tcp::v4()).unwrap();
738///
739/// soc.set_option(SendBufferSize::new(8192)).unwrap();
740/// ```
741///
742/// Getting the option:
743///
744/// ```
745/// use asyncio::*;
746/// use asyncio::ip::*;
747/// use asyncio::socket_base::SendBufferSize;
748///
749/// let ctx = &IoContext::new().unwrap();
750/// let soc = TcpSocket::new(ctx, Tcp::v4()).unwrap();
751///
752/// let opt: SendBufferSize = soc.get_option().unwrap();
753/// let size: usize = opt.get();
754/// ```
755#[derive(Default, Clone)]
756pub struct SendBufferSize(i32);
757
758impl<P: Protocol> SocketOption<P> for SendBufferSize {
759    type Data = i32;
760
761    fn level(&self, _: &P) -> i32 {
762        SOL_SOCKET
763    }
764
765    fn name(&self, _: &P) -> i32 {
766        SO_SNDBUF
767    }
768}
769
770impl<P: Protocol> GetSocketOption<P> for SendBufferSize {
771    fn data_mut(&mut self) -> &mut Self::Data {
772        &mut self.0
773    }
774}
775
776impl<P: Protocol> SetSocketOption<P> for SendBufferSize {
777    fn data(&self) -> &Self::Data {
778        &self.0
779    }
780}
781
782impl SendBufferSize {
783    pub fn new(size: usize) -> SendBufferSize {
784        SendBufferSize(size as i32)
785    }
786
787    pub fn get(&self) -> usize {
788        self.0 as usize
789    }
790
791    pub fn set(&mut self, size: usize) {
792        self.0 = size as i32
793    }
794}
795
796/// Socket option for the send low watermark.
797///
798/// Implements the SOL_SOCKET/SO_SNDLOWAT socket option.
799///
800/// # Examples
801/// Setting the option:
802///
803/// ```rust,no_run
804/// use asyncio::*;
805/// use asyncio::ip::*;
806/// use asyncio::socket_base::SendLowWatermark;
807///
808/// let ctx = &IoContext::new().unwrap();
809/// let soc = TcpSocket::new(ctx, Tcp::v4()).unwrap();
810///
811/// soc.set_option(SendLowWatermark::new(1024)).unwrap();
812/// ```
813///
814/// Getting the option:
815///
816/// ```rust,no_run
817/// use asyncio::*;
818/// use asyncio::ip::*;
819/// use asyncio::socket_base::SendLowWatermark;
820///
821/// let ctx = &IoContext::new().unwrap();
822/// let soc = TcpSocket::new(ctx, Tcp::v4()).unwrap();
823///
824/// let opt: SendLowWatermark = soc.get_option().unwrap();
825/// let size: usize = opt.get();
826/// ```
827#[derive(Default, Clone)]
828pub struct SendLowWatermark(i32);
829
830impl SendLowWatermark {
831    pub fn new(size: usize) -> SendLowWatermark {
832        SendLowWatermark(size as i32)
833    }
834
835    pub fn get(&self) -> usize {
836        self.0 as usize
837    }
838
839    pub fn set(&mut self, size: usize) {
840        self.0 = size as i32
841    }
842}
843
844impl<P: Protocol> SocketOption<P> for SendLowWatermark {
845    type Data = i32;
846
847    fn level(&self, _: &P) -> i32 {
848        SOL_SOCKET
849    }
850
851    fn name(&self, _: &P) -> i32 {
852        SO_SNDLOWAT
853    }
854}
855
856impl<P: Protocol> GetSocketOption<P> for SendLowWatermark {
857    fn data_mut(&mut self) -> &mut Self::Data {
858        &mut self.0
859    }
860}
861
862impl<P: Protocol> SetSocketOption<P> for SendLowWatermark {
863    fn data(&self) -> &Self::Data {
864        &self.0
865    }
866}