@@ -4,7 +4,7 @@ use gethostname::gethostname;
4
4
use nix:: unistd:: sethostname;
5
5
use socket2:: { Domain , Protocol , Socket , Type as SocketType } ;
6
6
use std:: convert:: TryFrom ;
7
- use std:: io:: { self , prelude :: * } ;
7
+ use std:: io;
8
8
use std:: net:: { Ipv4Addr , Ipv6Addr , Shutdown , SocketAddr , ToSocketAddrs } ;
9
9
use std:: time:: Duration ;
10
10
@@ -20,7 +20,7 @@ use crate::pyobject::{
20
20
BorrowValue , Either , IntoPyObject , PyClassImpl , PyObjectRef , PyRef , PyResult , PyValue ,
21
21
StaticType , TryFromObject ,
22
22
} ;
23
- use crate :: vm :: VirtualMachine ;
23
+ use crate :: { py_io , VirtualMachine } ;
24
24
25
25
#[ cfg( unix) ]
26
26
type RawSocket = std:: os:: unix:: io:: RawFd ;
@@ -48,7 +48,8 @@ mod c {
48
48
pub use winapi:: shared:: ws2def:: * ;
49
49
pub use winapi:: um:: winsock2:: {
50
50
SD_BOTH as SHUT_RDWR , SD_RECEIVE as SHUT_RD , SD_SEND as SHUT_WR , SOCK_DGRAM , SOCK_RAW ,
51
- SOCK_RDM , SOCK_STREAM , SOL_SOCKET , SO_BROADCAST , SO_REUSEADDR , SO_TYPE , * ,
51
+ SOCK_RDM , SOCK_STREAM , SOL_SOCKET , SO_BROADCAST , SO_ERROR , SO_OOBINLINE , SO_REUSEADDR ,
52
+ SO_TYPE , * ,
52
53
} ;
53
54
}
54
55
@@ -71,7 +72,7 @@ pub type PySocketRef = PyRef<PySocket>;
71
72
72
73
#[ pyimpl( flags( BASETYPE ) ) ]
73
74
impl PySocket {
74
- fn sock ( & self ) -> PyRwLockReadGuard < ' _ , Socket > {
75
+ pub fn sock ( & self ) -> PyRwLockReadGuard < ' _ , Socket > {
75
76
self . sock . read ( )
76
77
}
77
78
@@ -167,52 +168,90 @@ impl PySocket {
167
168
}
168
169
169
170
#[ pymethod]
170
- fn recv ( & self , bufsize : usize , vm : & VirtualMachine ) -> PyResult < Vec < u8 > > {
171
+ fn recv (
172
+ & self ,
173
+ bufsize : usize ,
174
+ flags : OptionalArg < i32 > ,
175
+ vm : & VirtualMachine ,
176
+ ) -> PyResult < Vec < u8 > > {
177
+ let flags = flags. unwrap_or ( 0 ) ;
171
178
let mut buffer = vec ! [ 0u8 ; bufsize] ;
172
179
let n = self
173
180
. sock ( )
174
- . recv ( & mut buffer)
181
+ . recv_with_flags ( & mut buffer, flags )
175
182
. map_err ( |err| convert_sock_error ( vm, err) ) ?;
176
183
buffer. truncate ( n) ;
177
184
Ok ( buffer)
178
185
}
179
186
180
187
#[ pymethod]
181
- fn recv_into ( & self , buf : PyRwBytesLike , vm : & VirtualMachine ) -> PyResult < usize > {
182
- buf. with_ref ( |buf| self . sock ( ) . recv ( buf) )
188
+ fn recv_into (
189
+ & self ,
190
+ buf : PyRwBytesLike ,
191
+ flags : OptionalArg < i32 > ,
192
+ vm : & VirtualMachine ,
193
+ ) -> PyResult < usize > {
194
+ let flags = flags. unwrap_or ( 0 ) ;
195
+ buf. with_ref ( |buf| self . sock ( ) . recv_with_flags ( buf, flags) )
183
196
. map_err ( |err| convert_sock_error ( vm, err) )
184
197
}
185
198
186
199
#[ pymethod]
187
- fn recvfrom ( & self , bufsize : usize , vm : & VirtualMachine ) -> PyResult < ( Vec < u8 > , AddrTuple ) > {
200
+ fn recvfrom (
201
+ & self ,
202
+ bufsize : usize ,
203
+ flags : OptionalArg < i32 > ,
204
+ vm : & VirtualMachine ,
205
+ ) -> PyResult < ( Vec < u8 > , AddrTuple ) > {
206
+ let flags = flags. unwrap_or ( 0 ) ;
188
207
let mut buffer = vec ! [ 0u8 ; bufsize] ;
189
208
let ( n, addr) = self
190
209
. sock ( )
191
- . recv_from ( & mut buffer)
210
+ . recv_from_with_flags ( & mut buffer, flags )
192
211
. map_err ( |err| convert_sock_error ( vm, err) ) ?;
193
212
buffer. truncate ( n) ;
194
213
Ok ( ( buffer, get_addr_tuple ( addr) ) )
195
214
}
196
215
197
216
#[ pymethod]
198
- fn send ( & self , bytes : PyBytesLike , vm : & VirtualMachine ) -> PyResult < usize > {
217
+ fn send (
218
+ & self ,
219
+ bytes : PyBytesLike ,
220
+ flags : OptionalArg < i32 > ,
221
+ vm : & VirtualMachine ,
222
+ ) -> PyResult < usize > {
223
+ let flags = flags. unwrap_or ( 0 ) ;
199
224
bytes
200
- . with_ref ( |b| self . sock ( ) . send ( b ) )
225
+ . with_ref ( |b| self . sock ( ) . send_with_flags ( b , flags ) )
201
226
. map_err ( |err| convert_sock_error ( vm, err) )
202
227
}
203
228
204
229
#[ pymethod]
205
- fn sendall ( & self , bytes : PyBytesLike , vm : & VirtualMachine ) -> PyResult < ( ) > {
230
+ fn sendall (
231
+ & self ,
232
+ bytes : PyBytesLike ,
233
+ flags : OptionalArg < i32 > ,
234
+ vm : & VirtualMachine ,
235
+ ) -> PyResult < ( ) > {
236
+ let flags = flags. unwrap_or ( 0 ) ;
237
+ let sock = self . sock ( ) ;
206
238
bytes
207
- . with_ref ( |b| self . sock_mut ( ) . write_all ( b ) )
239
+ . with_ref ( |buf| py_io :: write_all ( buf , | b| sock . send_with_flags ( b , flags ) ) )
208
240
. map_err ( |err| convert_sock_error ( vm, err) )
209
241
}
210
242
211
243
#[ pymethod]
212
- fn sendto ( & self , bytes : PyBytesLike , address : Address , vm : & VirtualMachine ) -> PyResult < ( ) > {
244
+ fn sendto (
245
+ & self ,
246
+ bytes : PyBytesLike ,
247
+ address : Address ,
248
+ flags : OptionalArg < i32 > ,
249
+ vm : & VirtualMachine ,
250
+ ) -> PyResult < ( ) > {
251
+ let flags = flags. unwrap_or ( 0 ) ;
213
252
let addr = get_addr ( vm, address, Some ( self . family . load ( ) ) ) ?;
214
253
bytes
215
- . with_ref ( |b| self . sock ( ) . send_to ( b, & addr) )
254
+ . with_ref ( |b| self . sock ( ) . send_to_with_flags ( b, & addr, flags ) )
216
255
. map_err ( |err| convert_sock_error ( vm, err) ) ?;
217
256
Ok ( ( ) )
218
257
}
@@ -411,15 +450,15 @@ impl PySocket {
411
450
412
451
impl io:: Read for PySocketRef {
413
452
fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
414
- <Socket as io:: Read >:: read ( & mut self . sock_mut ( ) , buf)
453
+ <& Socket as io:: Read >:: read ( & mut & * self . sock ( ) , buf)
415
454
}
416
455
}
417
456
impl io:: Write for PySocketRef {
418
457
fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
419
- <Socket as io:: Write >:: write ( & mut self . sock_mut ( ) , buf)
458
+ <& Socket as io:: Write >:: write ( & mut & * self . sock ( ) , buf)
420
459
}
421
460
fn flush ( & mut self ) -> io:: Result < ( ) > {
422
- <Socket as io:: Write >:: flush ( & mut self . sock_mut ( ) )
461
+ <& Socket as io:: Write >:: flush ( & mut & * self . sock ( ) )
423
462
}
424
463
}
425
464
@@ -850,7 +889,8 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
850
889
"SO_REUSEADDR" => ctx. new_int( c:: SO_REUSEADDR ) ,
851
890
"SO_TYPE" => ctx. new_int( c:: SO_TYPE ) ,
852
891
"SO_BROADCAST" => ctx. new_int( c:: SO_BROADCAST ) ,
853
- // "SO_EXCLUSIVEADDRUSE" => ctx.new_int(c::SO_EXCLUSIVEADDRUSE),
892
+ "SO_OOBINLINE" => ctx. new_int( c:: SO_OOBINLINE ) ,
893
+ "SO_ERROR" => ctx. new_int( c:: SO_ERROR ) ,
854
894
"TCP_NODELAY" => ctx. new_int( c:: TCP_NODELAY ) ,
855
895
"AI_ALL" => ctx. new_int( c:: AI_ALL ) ,
856
896
"AI_PASSIVE" => ctx. new_int( c:: AI_PASSIVE ) ,
0 commit comments