1use std::{
2 future::Future,
3 io,
4 net::SocketAddr,
5 pin::Pin,
6 task::{Context, Poll},
7 time::Duration,
8};
9
10use compio_buf::{BufResult, IoBuf, IoBufMut, IoVectoredBuf, IoVectoredBufMut};
11use compio_driver::{
12 BufferRef, SharedFd, impl_raw_fd,
13 op::{RecvFlags, RecvMsgMultiResult, SendFlags, SendMsgZc, SendVectoredZc, SendZc},
14};
15use compio_io::{
16 AsyncRead, AsyncReadManaged, AsyncReadMulti, AsyncWrite, AsyncWriteZerocopy,
17 ancillary::{
18 AsyncReadAncillary, AsyncReadAncillaryManaged, AsyncReadAncillaryMulti,
19 AsyncWriteAncillary, AsyncWriteAncillaryZerocopy,
20 },
21 util::Splittable,
22};
23use compio_runtime::fd::PollFd;
24use futures_util::{Stream, StreamExt, stream::FusedStream};
25use socket2::{Protocol, SockAddr, Socket as Socket2, Type};
26
27use crate::{
28 Extract, Incoming, MSG_NOSIGNAL, ReadHalf, Socket, ToSocketAddrsAsync, WriteHalf, Zerocopy,
29};
30
31#[derive(Debug, Clone)]
64pub struct TcpListener {
65 inner: Socket,
66}
67
68impl TcpListener {
69 pub async fn bind(addr: impl ToSocketAddrsAsync) -> io::Result<Self> {
82 super::each_addr(addr, |addr| async move {
83 let sa = SockAddr::from(addr);
84 let socket = Socket::new(sa.domain(), Type::STREAM, Some(Protocol::TCP)).await?;
85 socket.socket.set_reuse_address(true)?;
86 socket.bind(&sa).await?;
87 socket.listen(128).await?;
88 Ok(Self { inner: socket })
89 })
90 .await
91 }
92
93 pub fn from_std(stream: std::net::TcpListener) -> io::Result<Self> {
95 Ok(Self {
96 inner: Socket::from_socket2(Socket2::from(stream))?,
97 })
98 }
99
100 pub fn close(self) -> impl Future<Output = io::Result<()>> {
107 self.inner.close()
108 }
109
110 pub async fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
116 let (socket, addr) = self.inner.accept().await?;
117 let stream = TcpStream { inner: socket };
118 Ok((stream, addr.as_socket().expect("should be SocketAddr")))
119 }
120
121 #[cfg(windows)]
124 pub async fn accept_with(&self, sock: TcpSocket) -> io::Result<(TcpStream, SocketAddr)> {
125 let (socket, addr) = self.inner.accept_with(sock.inner).await?;
126 let stream = TcpStream { inner: socket };
127 Ok((stream, addr.as_socket().expect("should be SocketAddr")))
128 }
129
130 pub fn incoming(&self) -> TcpIncoming<'_> {
132 TcpIncoming {
133 inner: self.inner.incoming(),
134 }
135 }
136
137 pub fn local_addr(&self) -> io::Result<SocketAddr> {
161 self.inner
162 .local_addr()
163 .map(|addr| addr.as_socket().expect("should be SocketAddr"))
164 }
165
166 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
168 self.inner.socket.take_error()
169 }
170
171 pub fn ttl_v4(&self) -> io::Result<u32> {
177 self.inner.socket.ttl_v4()
178 }
179
180 pub fn set_ttl_v4(&self, ttl: u32) -> io::Result<()> {
185 self.inner.socket.set_ttl_v4(ttl)
186 }
187}
188
189impl_raw_fd!(TcpListener, socket2::Socket, inner, socket);
190
191pub struct TcpIncoming<'a> {
193 inner: Incoming<'a>,
194}
195
196impl Stream for TcpIncoming<'_> {
197 type Item = io::Result<TcpStream>;
198
199 fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
200 let this = self.get_mut();
201 this.inner.poll_next_unpin(cx).map(|res| {
202 res.map(|res| {
203 let socket = res?;
204 Ok(TcpStream { inner: socket })
205 })
206 })
207 }
208}
209
210impl FusedStream for TcpIncoming<'_> {
211 fn is_terminated(&self) -> bool {
212 self.inner.is_terminated()
213 }
214}
215
216#[derive(Debug, Clone)]
238pub struct TcpStream {
239 inner: Socket,
240}
241
242impl TcpStream {
243 pub async fn connect(addr: impl ToSocketAddrsAsync) -> io::Result<Self> {
248 use std::net::{Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6};
249
250 super::each_addr(addr, |addr| async move {
251 let addr2 = SockAddr::from(addr);
252 let socket = Socket::new(addr2.domain(), Type::STREAM, Some(Protocol::TCP)).await?;
253 if cfg!(windows) {
254 let bind_addr = if addr.is_ipv4() {
255 SockAddr::from(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0))
256 } else if addr.is_ipv6() {
257 SockAddr::from(SocketAddrV6::new(Ipv6Addr::UNSPECIFIED, 0, 0, 0))
258 } else {
259 return Err(io::Error::new(
260 io::ErrorKind::AddrNotAvailable,
261 "Unsupported address domain.",
262 ));
263 };
264 socket.bind(&bind_addr).await?;
265 };
266 socket.connect_async(&addr2).await?;
267 Ok(Self { inner: socket })
268 })
269 .await
270 }
271
272 pub fn from_std(stream: std::net::TcpStream) -> io::Result<Self> {
274 Ok(Self {
275 inner: Socket::from_socket2(Socket2::from(stream))?,
276 })
277 }
278
279 pub fn close(self) -> impl Future<Output = io::Result<()>> {
290 self.inner.close()
291 }
292
293 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
295 self.inner
296 .peer_addr()
297 .map(|addr| addr.as_socket().expect("should be SocketAddr"))
298 }
299
300 pub fn local_addr(&self) -> io::Result<SocketAddr> {
302 self.inner
303 .local_addr()
304 .map(|addr| addr.as_socket().expect("should be SocketAddr"))
305 }
306
307 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
309 self.inner.socket.take_error()
310 }
311
312 pub fn split(&self) -> (ReadHalf<'_, Self>, WriteHalf<'_, Self>) {
319 crate::split(self)
320 }
321
322 pub fn into_split(self) -> (Self, Self) {
328 (self.clone(), self)
329 }
330
331 pub fn to_poll_fd(&self) -> io::Result<PollFd<Socket2>> {
333 self.inner.to_poll_fd()
334 }
335
336 pub fn into_poll_fd(self) -> io::Result<PollFd<Socket2>> {
338 self.inner.into_poll_fd()
339 }
340
341 #[cfg(windows)]
346 pub async fn disconnect(self) -> io::Result<TcpSocket> {
347 self.inner.disconnect().await?;
348 Ok(TcpSocket { inner: self.inner })
349 }
350
351 pub fn nodelay(&self) -> io::Result<bool> {
356 self.inner.socket.tcp_nodelay()
357 }
358
359 pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
367 self.inner.socket.set_tcp_nodelay(nodelay)
368 }
369
370 #[cfg(any(
374 target_os = "linux",
375 target_os = "android",
376 target_os = "fuchsia",
377 target_os = "cygwin",
378 ))]
379 pub fn quickack(&self) -> io::Result<bool> {
380 self.inner.socket.tcp_quickack()
381 }
382
383 #[cfg(any(
388 target_os = "linux",
389 target_os = "android",
390 target_os = "fuchsia",
391 target_os = "cygwin",
392 ))]
393 pub fn set_quickack(&self, quickack: bool) -> io::Result<()> {
394 self.inner.socket.set_tcp_quickack(quickack)
395 }
396
397 pub fn linger(&self) -> io::Result<Option<Duration>> {
400 self.inner.socket.linger()
401 }
402
403 pub fn set_zero_linger(&self) -> io::Result<()> {
406 self.inner.socket.set_linger(Some(Duration::ZERO))
407 }
408
409 pub fn ttl_v4(&self) -> io::Result<u32> {
415 self.inner.socket.ttl_v4()
416 }
417
418 pub fn set_ttl_v4(&self, ttl: u32) -> io::Result<()> {
423 self.inner.socket.set_ttl_v4(ttl)
424 }
425
426 pub async fn send_out_of_band<T: IoBuf>(&self, buf: T) -> BufResult<usize, T> {
430 #[cfg(unix)]
431 use libc::MSG_OOB;
432 #[cfg(windows)]
433 use windows_sys::Win32::Networking::WinSock::MSG_OOB;
434
435 self.inner
436 .send(
437 buf,
438 SendFlags::from_bits_retain(MSG_OOB as _) | MSG_NOSIGNAL,
439 )
440 .await
441 }
442}
443
444impl AsyncRead for TcpStream {
445 #[inline]
446 async fn read<B: IoBufMut>(&mut self, buf: B) -> BufResult<usize, B> {
447 (&*self).read(buf).await
448 }
449
450 #[inline]
451 async fn read_vectored<V: IoVectoredBufMut>(&mut self, buf: V) -> BufResult<usize, V> {
452 (&*self).read_vectored(buf).await
453 }
454}
455
456impl AsyncRead for &TcpStream {
457 #[inline]
458 async fn read<B: IoBufMut>(&mut self, buf: B) -> BufResult<usize, B> {
459 self.inner.recv(buf, RecvFlags::empty()).await
460 }
461
462 #[inline]
463 async fn read_vectored<V: IoVectoredBufMut>(&mut self, buf: V) -> BufResult<usize, V> {
464 self.inner.recv_vectored(buf, RecvFlags::empty()).await
465 }
466}
467
468impl AsyncReadManaged for TcpStream {
469 type Buffer = BufferRef;
470
471 async fn read_managed(&mut self, len: usize) -> io::Result<Option<Self::Buffer>> {
472 (&*self).read_managed(len).await
473 }
474}
475
476impl AsyncReadManaged for &TcpStream {
477 type Buffer = BufferRef;
478
479 async fn read_managed(&mut self, len: usize) -> io::Result<Option<Self::Buffer>> {
480 self.inner.recv_managed(len, RecvFlags::empty()).await
481 }
482}
483
484impl AsyncReadMulti for TcpStream {
485 fn read_multi(&mut self, len: usize) -> impl Stream<Item = io::Result<Self::Buffer>> {
486 self.inner.recv_multi(len, RecvFlags::empty())
487 }
488}
489
490impl AsyncReadMulti for &TcpStream {
491 fn read_multi(&mut self, len: usize) -> impl Stream<Item = io::Result<Self::Buffer>> {
492 self.inner.recv_multi(len, RecvFlags::empty())
493 }
494}
495
496impl AsyncReadAncillary for TcpStream {
497 #[inline]
498 async fn read_with_ancillary<T: IoBufMut, C: IoBufMut>(
499 &mut self,
500 buffer: T,
501 control: C,
502 ) -> BufResult<(usize, usize), (T, C)> {
503 (&*self).read_with_ancillary(buffer, control).await
504 }
505
506 #[inline]
507 async fn read_vectored_with_ancillary<T: IoVectoredBufMut, C: IoBufMut>(
508 &mut self,
509 buffer: T,
510 control: C,
511 ) -> BufResult<(usize, usize), (T, C)> {
512 (&*self).read_vectored_with_ancillary(buffer, control).await
513 }
514}
515
516impl AsyncReadAncillary for &TcpStream {
517 #[inline]
518 async fn read_with_ancillary<T: IoBufMut, C: IoBufMut>(
519 &mut self,
520 buffer: T,
521 control: C,
522 ) -> BufResult<(usize, usize), (T, C)> {
523 self.inner
524 .recv_msg(buffer, control, RecvFlags::empty())
525 .await
526 .map_res(|(res, len, _addr)| (res, len))
527 }
528
529 #[inline]
530 async fn read_vectored_with_ancillary<T: IoVectoredBufMut, C: IoBufMut>(
531 &mut self,
532 buffer: T,
533 control: C,
534 ) -> BufResult<(usize, usize), (T, C)> {
535 self.inner
536 .recv_msg_vectored(buffer, control, RecvFlags::empty())
537 .await
538 .map_res(|(res, len, _addr)| (res, len))
539 }
540}
541
542impl AsyncReadAncillaryManaged for TcpStream {
543 #[inline]
544 async fn read_managed_with_ancillary<C: IoBufMut>(
545 &mut self,
546 len: usize,
547 control: C,
548 ) -> io::Result<Option<(Self::Buffer, C)>> {
549 (&*self).read_managed_with_ancillary(len, control).await
550 }
551}
552
553impl AsyncReadAncillaryManaged for &TcpStream {
554 #[inline]
555 async fn read_managed_with_ancillary<C: IoBufMut>(
556 &mut self,
557 len: usize,
558 control: C,
559 ) -> io::Result<Option<(Self::Buffer, C)>> {
560 self.inner
561 .recv_msg_managed(len, control, RecvFlags::empty())
562 .await
563 .map(|res| res.map(|(res, len, _addr)| (res, len)))
564 }
565}
566
567impl AsyncReadAncillaryMulti for TcpStream {
568 type Return = RecvMsgMultiResult;
569
570 #[inline]
571 fn read_multi_with_ancillary(
572 &mut self,
573 control_len: usize,
574 ) -> impl Stream<Item = io::Result<Self::Return>> {
575 self.inner.recv_msg_multi(control_len, RecvFlags::empty())
576 }
577}
578
579impl AsyncReadAncillaryMulti for &TcpStream {
580 type Return = RecvMsgMultiResult;
581
582 #[inline]
583 fn read_multi_with_ancillary(
584 &mut self,
585 control_len: usize,
586 ) -> impl Stream<Item = io::Result<Self::Return>> {
587 self.inner.recv_msg_multi(control_len, RecvFlags::empty())
588 }
589}
590
591impl AsyncWrite for TcpStream {
592 #[inline]
593 async fn write<T: IoBuf>(&mut self, buf: T) -> BufResult<usize, T> {
594 (&*self).write(buf).await
595 }
596
597 #[inline]
598 async fn write_vectored<T: IoVectoredBuf>(&mut self, buf: T) -> BufResult<usize, T> {
599 (&*self).write_vectored(buf).await
600 }
601
602 #[inline]
603 async fn flush(&mut self) -> io::Result<()> {
604 (&*self).flush().await
605 }
606
607 #[inline]
608 async fn shutdown(&mut self) -> io::Result<()> {
609 (&*self).shutdown().await
610 }
611}
612
613impl AsyncWrite for &TcpStream {
614 #[inline]
615 async fn write<T: IoBuf>(&mut self, buf: T) -> BufResult<usize, T> {
616 self.inner.send(buf, MSG_NOSIGNAL).await
617 }
618
619 #[inline]
620 async fn write_vectored<T: IoVectoredBuf>(&mut self, buf: T) -> BufResult<usize, T> {
621 self.inner.send_vectored(buf, MSG_NOSIGNAL).await
622 }
623
624 #[inline]
625 async fn flush(&mut self) -> io::Result<()> {
626 Ok(())
627 }
628
629 #[inline]
630 async fn shutdown(&mut self) -> io::Result<()> {
631 self.inner.shutdown().await
632 }
633}
634
635impl AsyncWriteAncillary for TcpStream {
636 #[inline]
637 async fn write_with_ancillary<T: IoBuf, C: IoBuf>(
638 &mut self,
639 buffer: T,
640 control: C,
641 ) -> BufResult<usize, (T, C)> {
642 (&*self).write_with_ancillary(buffer, control).await
643 }
644
645 #[inline]
646 async fn write_vectored_with_ancillary<T: IoVectoredBuf, C: IoBuf>(
647 &mut self,
648 buffer: T,
649 control: C,
650 ) -> BufResult<usize, (T, C)> {
651 (&*self)
652 .write_vectored_with_ancillary(buffer, control)
653 .await
654 }
655}
656
657impl AsyncWriteAncillary for &TcpStream {
658 #[inline]
659 async fn write_with_ancillary<T: IoBuf, C: IoBuf>(
660 &mut self,
661 buffer: T,
662 control: C,
663 ) -> BufResult<usize, (T, C)> {
664 self.inner
665 .send_msg(buffer, control, None, MSG_NOSIGNAL)
666 .await
667 }
668
669 #[inline]
670 async fn write_vectored_with_ancillary<T: IoVectoredBuf, C: IoBuf>(
671 &mut self,
672 buffer: T,
673 control: C,
674 ) -> BufResult<usize, (T, C)> {
675 self.inner
676 .send_msg_vectored(buffer, control, None, MSG_NOSIGNAL)
677 .await
678 }
679}
680
681impl AsyncWriteZerocopy for TcpStream {
682 type BufferReadyFuture<T: IoBuf> = Zerocopy<SendZc<T, SharedFd<Socket2>>>;
683 type VectoredBufferReadyFuture<T: IoVectoredBuf> =
684 Zerocopy<SendVectoredZc<T, SharedFd<Socket2>>>;
685
686 async fn write_zerocopy<T: IoBuf>(
687 &mut self,
688 buf: T,
689 ) -> BufResult<usize, Self::BufferReadyFuture<T>> {
690 self.inner.send_zerocopy(buf, MSG_NOSIGNAL).await
691 }
692
693 async fn write_zerocopy_vectored<T: IoVectoredBuf>(
694 &mut self,
695 buf: T,
696 ) -> BufResult<usize, Self::VectoredBufferReadyFuture<T>> {
697 self.inner.send_zerocopy_vectored(buf, MSG_NOSIGNAL).await
698 }
699}
700
701impl AsyncWriteZerocopy for &TcpStream {
702 type BufferReadyFuture<T: IoBuf> = Zerocopy<SendZc<T, SharedFd<Socket2>>>;
703 type VectoredBufferReadyFuture<T: IoVectoredBuf> =
704 Zerocopy<SendVectoredZc<T, SharedFd<Socket2>>>;
705
706 async fn write_zerocopy<T: IoBuf>(
707 &mut self,
708 buf: T,
709 ) -> BufResult<usize, Self::BufferReadyFuture<T>> {
710 self.inner.send_zerocopy(buf, MSG_NOSIGNAL).await
711 }
712
713 async fn write_zerocopy_vectored<T: IoVectoredBuf>(
714 &mut self,
715 buf: T,
716 ) -> BufResult<usize, Self::VectoredBufferReadyFuture<T>> {
717 self.inner.send_zerocopy_vectored(buf, MSG_NOSIGNAL).await
718 }
719}
720
721impl AsyncWriteAncillaryZerocopy for TcpStream {
722 type BufferReadyFuture<T: IoBuf, C: IoBuf> =
723 Extract<Zerocopy<SendMsgZc<[T; 1], C, SharedFd<Socket2>>>, T, C>;
724 type VectoredBufferReadyFuture<T: IoVectoredBuf, C: IoBuf> =
725 Zerocopy<SendMsgZc<T, C, SharedFd<Socket2>>>;
726
727 async fn write_zerocopy_with_ancillary<T: IoBuf, C: IoBuf>(
728 &mut self,
729 buf: T,
730 control: C,
731 ) -> BufResult<usize, Self::BufferReadyFuture<T, C>> {
732 self.inner
733 .send_msg_zerocopy(buf, control, None, MSG_NOSIGNAL)
734 .await
735 }
736
737 async fn write_zerocopy_vectored_with_ancillary<T: IoVectoredBuf, C: IoBuf>(
738 &mut self,
739 buf: T,
740 control: C,
741 ) -> BufResult<usize, Self::VectoredBufferReadyFuture<T, C>> {
742 self.inner
743 .send_msg_zerocopy_vectored(buf, control, None, MSG_NOSIGNAL)
744 .await
745 }
746}
747
748impl AsyncWriteAncillaryZerocopy for &TcpStream {
749 type BufferReadyFuture<T: IoBuf, C: IoBuf> =
750 Extract<Zerocopy<SendMsgZc<[T; 1], C, SharedFd<Socket2>>>, T, C>;
751 type VectoredBufferReadyFuture<T: IoVectoredBuf, C: IoBuf> =
752 Zerocopy<SendMsgZc<T, C, SharedFd<Socket2>>>;
753
754 async fn write_zerocopy_with_ancillary<T: IoBuf, C: IoBuf>(
755 &mut self,
756 buf: T,
757 control: C,
758 ) -> BufResult<usize, Self::BufferReadyFuture<T, C>> {
759 self.inner
760 .send_msg_zerocopy(buf, control, None, MSG_NOSIGNAL)
761 .await
762 }
763
764 async fn write_zerocopy_vectored_with_ancillary<T: IoVectoredBuf, C: IoBuf>(
765 &mut self,
766 buf: T,
767 control: C,
768 ) -> BufResult<usize, Self::VectoredBufferReadyFuture<T, C>> {
769 self.inner
770 .send_msg_zerocopy_vectored(buf, control, None, MSG_NOSIGNAL)
771 .await
772 }
773}
774
775impl Splittable for TcpStream {
776 type ReadHalf = Self;
777 type WriteHalf = Self;
778
779 fn split(self) -> (Self::ReadHalf, Self::WriteHalf) {
780 self.into_split()
781 }
782}
783
784impl<'a> Splittable for &'a TcpStream {
785 type ReadHalf = ReadHalf<'a, TcpStream>;
786 type WriteHalf = WriteHalf<'a, TcpStream>;
787
788 fn split(self) -> (Self::ReadHalf, Self::WriteHalf) {
789 crate::split(self)
790 }
791}
792
793impl<'a> Splittable for &'a mut TcpStream {
794 type ReadHalf = ReadHalf<'a, TcpStream>;
795 type WriteHalf = WriteHalf<'a, TcpStream>;
796
797 fn split(self) -> (Self::ReadHalf, Self::WriteHalf) {
798 crate::split(self)
799 }
800}
801
802impl_raw_fd!(TcpStream, socket2::Socket, inner, socket);
803
804#[derive(Debug)]
807pub struct TcpSocket {
808 inner: Socket,
809}
810
811impl TcpSocket {
812 pub async fn new_v4() -> io::Result<TcpSocket> {
814 TcpSocket::new(socket2::Domain::IPV4).await
815 }
816
817 pub async fn new_v6() -> io::Result<TcpSocket> {
819 TcpSocket::new(socket2::Domain::IPV6).await
820 }
821
822 async fn new(domain: socket2::Domain) -> io::Result<TcpSocket> {
823 let inner =
824 Socket::new(domain, socket2::Type::STREAM, Some(socket2::Protocol::TCP)).await?;
825 Ok(TcpSocket { inner })
826 }
827
828 pub fn set_keepalive(&self, keepalive: bool) -> io::Result<()> {
830 self.inner.socket.set_keepalive(keepalive)
831 }
832
833 pub fn keepalive(&self) -> io::Result<bool> {
835 self.inner.socket.keepalive()
836 }
837
838 pub fn set_reuseaddr(&self, reuseaddr: bool) -> io::Result<()> {
840 self.inner.socket.set_reuse_address(reuseaddr)
841 }
842
843 pub fn reuseaddr(&self) -> io::Result<bool> {
845 self.inner.socket.reuse_address()
846 }
847
848 #[cfg(all(
851 unix,
852 not(target_os = "solaris"),
853 not(target_os = "illumos"),
854 not(target_os = "cygwin"),
855 ))]
856 pub fn set_reuseport(&self, reuseport: bool) -> io::Result<()> {
857 self.inner.socket.set_reuse_port(reuseport)
858 }
859
860 #[cfg(all(
863 unix,
864 not(target_os = "solaris"),
865 not(target_os = "illumos"),
866 not(target_os = "cygwin"),
867 ))]
868 pub fn reuseport(&self) -> io::Result<bool> {
869 self.inner.socket.reuse_port()
870 }
871
872 pub fn set_send_buffer_size(&self, size: u32) -> io::Result<()> {
876 self.inner.socket.set_send_buffer_size(size as usize)
877 }
878
879 pub fn send_buffer_size(&self) -> io::Result<u32> {
884 self.inner.socket.send_buffer_size().map(|n| n as u32)
885 }
886
887 pub fn set_recv_buffer_size(&self, size: u32) -> io::Result<()> {
891 self.inner.socket.set_recv_buffer_size(size as usize)
892 }
893
894 pub fn recv_buffer_size(&self) -> io::Result<u32> {
899 self.inner.socket.recv_buffer_size().map(|n| n as u32)
900 }
901
902 pub fn set_zero_linger(&self) -> io::Result<()> {
905 self.inner.socket.set_linger(Some(Duration::ZERO))
906 }
907
908 pub fn linger(&self) -> io::Result<Option<Duration>> {
911 self.inner.socket.linger()
912 }
913
914 pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
922 self.inner.socket.set_tcp_nodelay(nodelay)
923 }
924
925 pub fn nodelay(&self) -> io::Result<bool> {
931 self.inner.socket.tcp_nodelay()
932 }
933
934 #[cfg(any(
940 target_os = "android",
941 target_os = "dragonfly",
942 target_os = "freebsd",
943 target_os = "fuchsia",
944 target_os = "linux",
945 target_os = "macos",
946 target_os = "netbsd",
947 target_os = "openbsd",
948 target_os = "cygwin",
949 ))]
950 pub fn tclass_v6(&self) -> io::Result<u32> {
951 self.inner.socket.tclass_v6()
952 }
953
954 #[cfg(any(
963 target_os = "android",
964 target_os = "dragonfly",
965 target_os = "freebsd",
966 target_os = "fuchsia",
967 target_os = "linux",
968 target_os = "macos",
969 target_os = "netbsd",
970 target_os = "openbsd",
971 target_os = "cygwin",
972 ))]
973 pub fn set_tclass_v6(&self, tclass: u32) -> io::Result<()> {
974 self.inner.socket.set_tclass_v6(tclass)
975 }
976
977 #[cfg(not(any(
983 target_os = "fuchsia",
984 target_os = "redox",
985 target_os = "solaris",
986 target_os = "illumos",
987 target_os = "haiku"
988 )))]
989 pub fn tos_v4(&self) -> io::Result<u32> {
990 self.inner.socket.tos_v4()
991 }
992
993 #[cfg(not(any(
1004 target_os = "fuchsia",
1005 target_os = "redox",
1006 target_os = "solaris",
1007 target_os = "illumos",
1008 target_os = "haiku"
1009 )))]
1010 pub fn set_tos_v4(&self, tos: u32) -> io::Result<()> {
1011 self.inner.socket.set_tos_v4(tos)
1012 }
1013
1014 #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux",))]
1018 pub fn device(&self) -> io::Result<Option<Vec<u8>>> {
1019 self.inner.socket.device()
1020 }
1021
1022 #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
1030 pub fn bind_device(&self, interface: Option<&[u8]>) -> io::Result<()> {
1031 self.inner.socket.bind_device(interface)
1032 }
1033
1034 pub fn local_addr(&self) -> io::Result<SocketAddr> {
1036 Ok(self
1037 .inner
1038 .local_addr()?
1039 .as_socket()
1040 .expect("should be SocketAddr"))
1041 }
1042
1043 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
1045 self.inner.socket.take_error()
1046 }
1047
1048 pub async fn bind(&self, addr: SocketAddr) -> io::Result<()> {
1050 self.inner.bind(&addr.into()).await
1051 }
1052
1053 pub async fn connect(self, addr: SocketAddr) -> io::Result<TcpStream> {
1062 self.inner.connect_async(&addr.into()).await?;
1063 Ok(TcpStream { inner: self.inner })
1064 }
1065
1066 pub async fn listen(self, backlog: i32) -> io::Result<TcpListener> {
1073 self.inner.listen(backlog).await?;
1074 Ok(TcpListener { inner: self.inner })
1075 }
1076
1077 pub fn from_std_stream(stream: std::net::TcpStream) -> io::Result<TcpSocket> {
1082 Ok(Self {
1083 inner: Socket::from_socket2(Socket2::from(stream))?,
1084 })
1085 }
1086}
1087
1088impl_raw_fd!(TcpSocket, socket2::Socket, inner, socket);