shutdown.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #![allow(dead_code)]
  2. use core::sync::atomic::AtomicU8;
  3. use system_error::SystemError;
  4. bitflags! {
  5. /// @brief 用于指定socket的关闭类型
  6. /// 参考:https://code.dragonos.org.cn/xref/linux-6.1.9/include/net/sock.h?fi=SHUTDOWN_MASK#1573
  7. pub struct ShutdownBit: u8 {
  8. const SHUT_RD = 0;
  9. const SHUT_WR = 1;
  10. const SHUT_RDWR = 2;
  11. }
  12. }
  13. const RCV_SHUTDOWN: u8 = 0x01;
  14. const SEND_SHUTDOWN: u8 = 0x02;
  15. const SHUTDOWN_MASK: u8 = 0x03;
  16. #[derive(Debug, Default)]
  17. pub struct Shutdown {
  18. bit: AtomicU8,
  19. }
  20. impl From<ShutdownBit> for Shutdown {
  21. fn from(shutdown_bit: ShutdownBit) -> Self {
  22. match shutdown_bit {
  23. ShutdownBit::SHUT_RD => Shutdown {
  24. bit: AtomicU8::new(RCV_SHUTDOWN),
  25. },
  26. ShutdownBit::SHUT_WR => Shutdown {
  27. bit: AtomicU8::new(SEND_SHUTDOWN),
  28. },
  29. ShutdownBit::SHUT_RDWR => Shutdown {
  30. bit: AtomicU8::new(SHUTDOWN_MASK),
  31. },
  32. _ => Shutdown::default(),
  33. }
  34. }
  35. }
  36. impl Shutdown {
  37. pub fn new() -> Self {
  38. Self {
  39. bit: AtomicU8::new(0),
  40. }
  41. }
  42. pub fn recv_shutdown(&self) {
  43. self.bit
  44. .fetch_or(RCV_SHUTDOWN, core::sync::atomic::Ordering::SeqCst);
  45. }
  46. pub fn send_shutdown(&self) {
  47. self.bit
  48. .fetch_or(SEND_SHUTDOWN, core::sync::atomic::Ordering::SeqCst);
  49. }
  50. pub fn is_recv_shutdown(&self) -> bool {
  51. self.bit.load(core::sync::atomic::Ordering::SeqCst) & RCV_SHUTDOWN != 0
  52. }
  53. pub fn is_send_shutdown(&self) -> bool {
  54. self.bit.load(core::sync::atomic::Ordering::SeqCst) & SEND_SHUTDOWN != 0
  55. }
  56. pub fn is_both_shutdown(&self) -> bool {
  57. self.bit.load(core::sync::atomic::Ordering::SeqCst) & SHUTDOWN_MASK == SHUTDOWN_MASK
  58. }
  59. pub fn is_empty(&self) -> bool {
  60. self.bit.load(core::sync::atomic::Ordering::SeqCst) == 0
  61. }
  62. pub fn from_how(how: usize) -> Self {
  63. Self::from(ShutdownBit::from_bits_truncate(how as u8))
  64. }
  65. pub fn get(&self) -> ShutdownTemp {
  66. ShutdownTemp {
  67. bit: self.bit.load(core::sync::atomic::Ordering::SeqCst),
  68. }
  69. }
  70. }
  71. pub struct ShutdownTemp {
  72. bit: u8,
  73. }
  74. impl ShutdownTemp {
  75. pub fn is_recv_shutdown(&self) -> bool {
  76. self.bit & RCV_SHUTDOWN != 0
  77. }
  78. pub fn is_send_shutdown(&self) -> bool {
  79. self.bit & SEND_SHUTDOWN != 0
  80. }
  81. pub fn is_both_shutdown(&self) -> bool {
  82. self.bit & SHUTDOWN_MASK == SHUTDOWN_MASK
  83. }
  84. pub fn is_empty(&self) -> bool {
  85. self.bit == 0
  86. }
  87. pub fn bits(&self) -> ShutdownBit {
  88. ShutdownBit { bits: self.bit }
  89. }
  90. }
  91. impl From<ShutdownBit> for ShutdownTemp {
  92. fn from(shutdown_bit: ShutdownBit) -> Self {
  93. match shutdown_bit {
  94. ShutdownBit::SHUT_RD => Self { bit: RCV_SHUTDOWN },
  95. ShutdownBit::SHUT_WR => Self { bit: SEND_SHUTDOWN },
  96. ShutdownBit::SHUT_RDWR => Self { bit: SHUTDOWN_MASK },
  97. _ => Self { bit: 0 },
  98. }
  99. }
  100. }
  101. impl TryFrom<usize> for ShutdownTemp {
  102. type Error = SystemError;
  103. fn try_from(value: usize) -> Result<Self, Self::Error> {
  104. match value {
  105. 0..2 => Ok(ShutdownTemp {
  106. bit: value as u8 + 1,
  107. }),
  108. _ => Err(SystemError::EINVAL),
  109. }
  110. }
  111. }