@@ -78,28 +78,31 @@ impl UdpSocket {
78
78
bound. close ( ) ;
79
79
inner. take ( ) ;
80
80
}
81
+ // unbound socket just drop (only need to free memory)
81
82
}
82
83
83
84
pub fn try_recv (
84
85
& self ,
85
86
buf : & mut [ u8 ] ,
86
87
) -> Result < ( usize , smoltcp:: wire:: IpEndpoint ) , SystemError > {
87
- let received = match self . inner . read ( ) . as_ref ( ) . expect ( "Udp Inner is None" ) {
88
- UdpInner :: Bound ( bound) => bound. try_recv ( buf) ,
88
+ match self . inner . read ( ) . as_ref ( ) . expect ( "Udp Inner is None" ) {
89
+ UdpInner :: Bound ( bound) => {
90
+ let ret = bound. try_recv ( buf) ;
91
+ poll_ifaces ( ) ;
92
+ ret
93
+ }
89
94
_ => Err ( ENOTCONN ) ,
90
- } ;
91
- poll_ifaces ( ) ;
92
- return received;
95
+ }
93
96
}
94
97
95
98
#[ inline]
96
99
pub fn can_recv ( & self ) -> bool {
97
- self . on_events ( ) . contains ( EP :: EPOLLIN )
100
+ self . event ( ) . contains ( EP :: EPOLLIN )
98
101
}
99
102
100
103
#[ inline]
101
104
pub fn can_send ( & self ) -> bool {
102
- self . on_events ( ) . contains ( EP :: EPOLLOUT )
105
+ self . event ( ) . contains ( EP :: EPOLLOUT )
103
106
}
104
107
105
108
pub fn try_send (
@@ -138,7 +141,7 @@ impl UdpSocket {
138
141
}
139
142
}
140
143
141
- pub fn on_events ( & self ) -> EPollEventType {
144
+ pub fn event ( & self ) -> EPollEventType {
142
145
let mut event = EPollEventType :: empty ( ) ;
143
146
match self . inner . read ( ) . as_ref ( ) . unwrap ( ) {
144
147
UdpInner :: Unbound ( _) => {
@@ -154,8 +157,6 @@ impl UdpSocket {
154
157
155
158
if can_send {
156
159
event. insert ( EP :: EPOLLOUT | EP :: EPOLLWRNORM | EP :: EPOLLWRBAND ) ;
157
- } else {
158
- todo ! ( "缓冲区空间不够,需要使用信号处理" ) ;
159
160
}
160
161
}
161
162
}
@@ -169,7 +170,7 @@ impl Socket for UdpSocket {
169
170
}
170
171
171
172
fn poll ( & self ) -> usize {
172
- self . on_events ( ) . bits ( ) as usize
173
+ self . event ( ) . bits ( ) as usize
173
174
}
174
175
175
176
fn bind ( & self , local_endpoint : Endpoint ) -> Result < ( ) , SystemError > {
@@ -195,7 +196,9 @@ impl Socket for UdpSocket {
195
196
196
197
fn connect ( & self , endpoint : Endpoint ) -> Result < ( ) , SystemError > {
197
198
if let Endpoint :: Ip ( remote) = endpoint {
198
- self . bind_emphemeral ( remote. addr ) ?;
199
+ if !self . is_bound ( ) {
200
+ self . bind_emphemeral ( remote. addr ) ?;
201
+ }
199
202
if let UdpInner :: Bound ( inner) = self . inner . read ( ) . as_ref ( ) . expect ( "UDP Inner disappear" )
200
203
{
201
204
inner. connect ( remote) ;
@@ -272,6 +275,11 @@ impl Socket for UdpSocket {
272
275
}
273
276
. map ( |( len, remote) | ( len, Endpoint :: Ip ( remote) ) ) ;
274
277
}
278
+
279
+ fn close ( & self ) -> Result < ( ) , SystemError > {
280
+ self . close ( ) ;
281
+ Ok ( ( ) )
282
+ }
275
283
}
276
284
277
285
impl InetSocket for UdpSocket {
0 commit comments