pub struct Context { /* private fields */ }
Implementations§
Source§impl Context
impl Context
pub fn new(handle: HANDLE, buff: Vec<u8>, io_type: IOType) -> Self
pub fn get_buff(&self) -> &[u8] ⓘ
pub fn io_type(&self) -> &IOType
Sourcepub fn cancel(self) -> Result<()>
pub fn cancel(self) -> Result<()>
Cancel this context with handle and overlapped. There is no guarantee that underlying drivers correctly support cancellation.
use std::net::{TcpStream, TcpListener};
use iocp_rs::{CompletionPort, net::TcpStreamExt, AsHandle};
use std::thread::{spawn, sleep};
use std::os::windows::io::{AsRawSocket, RawSocket};
use std::io::Write;
use std::time::Duration;
use windows_sys::Win32::Foundation::HANDLE;
struct MyTcpStream {
inner: TcpStream
}
impl AsRawSocket for MyTcpStream {
fn as_raw_socket(&self) -> RawSocket {
self.inner.as_raw_socket()
}
}
impl AsHandle for MyTcpStream {
type Handle = HANDLE;
fn as_handle(&self) -> Self::Handle {
self.inner.as_raw_socket() as HANDLE
}
}
impl TcpStreamExt for MyTcpStream {}
fn main() {
let cmp = CompletionPort::new(1).unwrap();
let join = spawn(|| {
let listener = TcpListener::bind("127.0.0.1:999").unwrap();
let (mut stream, _) = listener.accept().unwrap();
sleep(Duration::from_secs(5));
let ret = stream.write(b"123");
dbg!(ret);
});
let stream = TcpStream::connect("127.0.0.1:999").unwrap();
let mut stream1 = MyTcpStream {inner: stream};
cmp.add(1, &stream1).unwrap();
let context = stream1.read(vec![0; 3]).unwrap();
context.cancel();
let result = cmp.get(None).unwrap();
assert_eq!(result.token(), 1);
}
Auto Trait Implementations§
impl Freeze for Context
impl RefUnwindSafe for Context
impl !Send for Context
impl !Sync for Context
impl Unpin for Context
impl UnwindSafe for Context
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more