Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions open-coroutine/examples/socket_co.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use open_coroutine::task;
use std::io::{Error, ErrorKind, IoSlice, IoSliceMut, Read, Write};
use std::net::{Shutdown, TcpListener, ToSocketAddrs};
#[cfg(unix)]
use std::os::fd::AsRawFd;
use std::sync::{Arc, Condvar, Mutex};
use std::time::Duration;

Expand Down Expand Up @@ -36,6 +38,30 @@ pub fn start_co_server<A: ToSocketAddrs>(addr: A, server_finished: Arc<(Mutex<bo
);
println!("Server Send Multiple");
}
#[cfg(unix)]
for _ in 0..3 {
let mut buffers =
[IoSliceMut::new(&mut buffer1), IoSliceMut::new(&mut buffer2)];
let mut msg = libc::msghdr {
msg_name: std::ptr::null_mut(),
msg_namelen: 0,
msg_iov: buffers.as_mut_ptr().cast::<libc::iovec>(),
msg_iovlen: buffers.len() as _,
msg_control: std::ptr::null_mut(),
msg_controllen: 0,
msg_flags: 0,
};
assert_eq!(26, unsafe {
libc::recvmsg(socket.as_raw_fd(), &mut msg, 0)
});
eprintln!(
"Server Received Message: {} {}",
String::from_utf8_lossy(&buffer1),
String::from_utf8_lossy(&buffer2)
);
assert_eq!(512, unsafe { libc::sendmsg(socket.as_raw_fd(), &msg, 0) });
eprintln!("Server Send Message");
}
println!("Server Shutdown Write");
if socket.shutdown(Shutdown::Write).is_ok() {
println!("Server Closed Connection");
Expand Down Expand Up @@ -87,6 +113,37 @@ pub fn start_co_client<A: ToSocketAddrs>(addr: A) {
String::from_utf8_lossy(&buffer2)
);
}
#[cfg(unix)]
for i in 0..3 {
let mut request1 = format!("MessagePart{i}1").into_bytes();
let mut request2 = format!("MessagePart{i}2").into_bytes();
let mut buffers = [
IoSliceMut::new(request1.as_mut_slice()),
IoSliceMut::new(request2.as_mut_slice()),
];
let mut msg = libc::msghdr {
msg_name: std::ptr::null_mut(),
msg_namelen: 0,
msg_iov: buffers.as_mut_ptr().cast::<libc::iovec>(),
msg_iovlen: buffers.len() as _,
msg_control: std::ptr::null_mut(),
msg_controllen: 0,
msg_flags: 0,
};
assert_eq!(26, unsafe { libc::sendmsg(stream.as_raw_fd(), &msg, 0) });
eprintln!("Client Send Message");
buffers = [IoSliceMut::new(&mut buffer1), IoSliceMut::new(&mut buffer2)];
msg.msg_iov = buffers.as_mut_ptr().cast::<libc::iovec>();
msg.msg_iovlen = buffers.len() as _;
assert_eq!(512, unsafe {
libc::recvmsg(stream.as_raw_fd(), &mut msg, 0)
});
eprintln!(
"Client Received Message: {}{}",
String::from_utf8_lossy(&buffer1),
String::from_utf8_lossy(&buffer2)
);
}
println!("Client Shutdown Write");
stream.shutdown(Shutdown::Write).expect("shutdown failed");
println!("Client Closed");
Expand Down
56 changes: 56 additions & 0 deletions open-coroutine/examples/socket_co_client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use open_coroutine::task;
use std::io::{Error, ErrorKind, IoSlice, IoSliceMut, Read, Write};
use std::net::{Shutdown, TcpListener, ToSocketAddrs};
#[cfg(unix)]
use std::os::fd::AsRawFd;
use std::sync::{Arc, Condvar, Mutex};
use std::time::Duration;

Expand Down Expand Up @@ -34,6 +36,29 @@ pub fn start_server<A: ToSocketAddrs>(addr: A, server_finished: Arc<(Mutex<bool>
);
println!("Server Send Multiple");
}
#[cfg(unix)]
for _ in 0..3 {
let mut buffers = [IoSliceMut::new(&mut buffer1), IoSliceMut::new(&mut buffer2)];
let mut msg = libc::msghdr {
msg_name: std::ptr::null_mut(),
msg_namelen: 0,
msg_iov: buffers.as_mut_ptr().cast::<libc::iovec>(),
msg_iovlen: buffers.len() as _,
msg_control: std::ptr::null_mut(),
msg_controllen: 0,
msg_flags: 0,
};
assert_eq!(26, unsafe {
libc::recvmsg(socket.as_raw_fd(), &mut msg, 0)
});
eprintln!(
"Server Received Message: {} {}",
String::from_utf8_lossy(&buffer1),
String::from_utf8_lossy(&buffer2)
);
assert_eq!(512, unsafe { libc::sendmsg(socket.as_raw_fd(), &msg, 0) });
eprintln!("Server Send Message");
}
println!("Server Shutdown Write");
if socket.shutdown(Shutdown::Write).is_ok() {
println!("Server Closed Connection");
Expand Down Expand Up @@ -83,6 +108,37 @@ pub fn start_co_client<A: ToSocketAddrs>(addr: A) {
String::from_utf8_lossy(&buffer2)
);
}
#[cfg(unix)]
for i in 0..3 {
let mut request1 = format!("MessagePart{i}1").into_bytes();
let mut request2 = format!("MessagePart{i}2").into_bytes();
let mut buffers = [
IoSliceMut::new(request1.as_mut_slice()),
IoSliceMut::new(request2.as_mut_slice()),
];
let mut msg = libc::msghdr {
msg_name: std::ptr::null_mut(),
msg_namelen: 0,
msg_iov: buffers.as_mut_ptr().cast::<libc::iovec>(),
msg_iovlen: buffers.len() as _,
msg_control: std::ptr::null_mut(),
msg_controllen: 0,
msg_flags: 0,
};
assert_eq!(26, unsafe { libc::sendmsg(stream.as_raw_fd(), &msg, 0) });
eprintln!("Client Send Message");
buffers = [IoSliceMut::new(&mut buffer1), IoSliceMut::new(&mut buffer2)];
msg.msg_iov = buffers.as_mut_ptr().cast::<libc::iovec>();
msg.msg_iovlen = buffers.len() as _;
assert_eq!(512, unsafe {
libc::recvmsg(stream.as_raw_fd(), &mut msg, 0)
});
eprintln!(
"Client Received Message: {}{}",
String::from_utf8_lossy(&buffer1),
String::from_utf8_lossy(&buffer2)
);
}
println!("Client Shutdown Write");
stream.shutdown(Shutdown::Write).expect("shutdown failed");
println!("Client Closed");
Expand Down
57 changes: 57 additions & 0 deletions open-coroutine/examples/socket_co_server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use open_coroutine::task;
use std::io::{Error, ErrorKind, IoSlice, IoSliceMut, Read, Write};
use std::net::{Shutdown, TcpListener, ToSocketAddrs};
#[cfg(unix)]
use std::os::fd::AsRawFd;
use std::sync::{Arc, Condvar, Mutex};
use std::time::Duration;

Expand Down Expand Up @@ -36,6 +38,30 @@ pub fn start_co_server<A: ToSocketAddrs>(addr: A, server_finished: Arc<(Mutex<bo
);
println!("Server Send Multiple");
}
#[cfg(unix)]
for _ in 0..3 {
let mut buffers =
[IoSliceMut::new(&mut buffer1), IoSliceMut::new(&mut buffer2)];
let mut msg = libc::msghdr {
msg_name: std::ptr::null_mut(),
msg_namelen: 0,
msg_iov: buffers.as_mut_ptr().cast::<libc::iovec>(),
msg_iovlen: buffers.len() as _,
msg_control: std::ptr::null_mut(),
msg_controllen: 0,
msg_flags: 0,
};
assert_eq!(26, unsafe {
libc::recvmsg(socket.as_raw_fd(), &mut msg, 0)
});
eprintln!(
"Server Received Message: {} {}",
String::from_utf8_lossy(&buffer1),
String::from_utf8_lossy(&buffer2)
);
assert_eq!(512, unsafe { libc::sendmsg(socket.as_raw_fd(), &msg, 0) });
eprintln!("Server Send Message");
}
println!("Server Shutdown Write");
if socket.shutdown(Shutdown::Write).is_ok() {
println!("Server Closed Connection");
Expand Down Expand Up @@ -87,6 +113,37 @@ pub fn start_client<A: ToSocketAddrs>(addr: A) {
String::from_utf8_lossy(&buffer2)
);
}
#[cfg(unix)]
for i in 0..3 {
let mut request1 = format!("MessagePart{i}1").into_bytes();
let mut request2 = format!("MessagePart{i}2").into_bytes();
let mut buffers = [
IoSliceMut::new(request1.as_mut_slice()),
IoSliceMut::new(request2.as_mut_slice()),
];
let mut msg = libc::msghdr {
msg_name: std::ptr::null_mut(),
msg_namelen: 0,
msg_iov: buffers.as_mut_ptr().cast::<libc::iovec>(),
msg_iovlen: buffers.len() as _,
msg_control: std::ptr::null_mut(),
msg_controllen: 0,
msg_flags: 0,
};
assert_eq!(26, unsafe { libc::sendmsg(stream.as_raw_fd(), &msg, 0) });
eprintln!("Client Send Message");
buffers = [IoSliceMut::new(&mut buffer1), IoSliceMut::new(&mut buffer2)];
msg.msg_iov = buffers.as_mut_ptr().cast::<libc::iovec>();
msg.msg_iovlen = buffers.len() as _;
assert_eq!(512, unsafe {
libc::recvmsg(stream.as_raw_fd(), &mut msg, 0)
});
eprintln!(
"Client Received Message: {}{}",
String::from_utf8_lossy(&buffer1),
String::from_utf8_lossy(&buffer2)
);
}
println!("Client Shutdown Write");
stream.shutdown(Shutdown::Write).expect("shutdown failed");
println!("Client Closed");
Expand Down
56 changes: 56 additions & 0 deletions open-coroutine/examples/socket_not_co.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::io::{Error, ErrorKind, IoSlice, IoSliceMut, Read, Write};
use std::net::{Shutdown, TcpListener, ToSocketAddrs};
#[cfg(unix)]
use std::os::fd::AsRawFd;
use std::sync::{Arc, Condvar, Mutex};
use std::time::Duration;

Expand Down Expand Up @@ -33,6 +35,29 @@ fn start_server<A: ToSocketAddrs>(addr: A, server_finished: Arc<(Mutex<bool>, Co
);
eprintln!("Server Send Multiple");
}
#[cfg(unix)]
for _ in 0..3 {
let mut buffers = [IoSliceMut::new(&mut buffer1), IoSliceMut::new(&mut buffer2)];
let mut msg = libc::msghdr {
msg_name: std::ptr::null_mut(),
msg_namelen: 0,
msg_iov: buffers.as_mut_ptr().cast::<libc::iovec>(),
msg_iovlen: buffers.len() as _,
msg_control: std::ptr::null_mut(),
msg_controllen: 0,
msg_flags: 0,
};
assert_eq!(26, unsafe {
libc::recvmsg(socket.as_raw_fd(), &mut msg, 0)
});
eprintln!(
"Server Received Message: {} {}",
String::from_utf8_lossy(&buffer1),
String::from_utf8_lossy(&buffer2)
);
assert_eq!(512, unsafe { libc::sendmsg(socket.as_raw_fd(), &msg, 0) });
eprintln!("Server Send Message");
}
eprintln!("Server Shutdown Write");
if socket.shutdown(Shutdown::Write).is_ok() {
eprintln!("Server Closed Connection");
Expand Down Expand Up @@ -82,6 +107,37 @@ fn start_client<A: ToSocketAddrs>(addr: A) {
String::from_utf8_lossy(&buffer2)
);
}
#[cfg(unix)]
for i in 0..3 {
let mut request1 = format!("MessagePart{i}1").into_bytes();
let mut request2 = format!("MessagePart{i}2").into_bytes();
let mut buffers = [
IoSliceMut::new(request1.as_mut_slice()),
IoSliceMut::new(request2.as_mut_slice()),
];
let mut msg = libc::msghdr {
msg_name: std::ptr::null_mut(),
msg_namelen: 0,
msg_iov: buffers.as_mut_ptr().cast::<libc::iovec>(),
msg_iovlen: buffers.len() as _,
msg_control: std::ptr::null_mut(),
msg_controllen: 0,
msg_flags: 0,
};
assert_eq!(26, unsafe { libc::sendmsg(stream.as_raw_fd(), &msg, 0) });
eprintln!("Client Send Message");
buffers = [IoSliceMut::new(&mut buffer1), IoSliceMut::new(&mut buffer2)];
msg.msg_iov = buffers.as_mut_ptr().cast::<libc::iovec>();
msg.msg_iovlen = buffers.len() as _;
assert_eq!(512, unsafe {
libc::recvmsg(stream.as_raw_fd(), &mut msg, 0)
});
eprintln!(
"Client Received Message: {}{}",
String::from_utf8_lossy(&buffer1),
String::from_utf8_lossy(&buffer2)
);
}
eprintln!("Client Shutdown Write");
stream.shutdown(Shutdown::Write).expect("shutdown failed");
eprintln!("Client Closed");
Expand Down
Loading