Implement fmt::Write for TcpSocket.
parent
a1f865f6d3
commit
7825bc6070
|
@ -200,9 +200,9 @@ It responds to:
|
|||
|
||||
* pings (`ping 192.168.69.1`);
|
||||
* UDP packets on port 6969 (`socat stdio udp4-connect:192.168.69.1:6969 <<<"abcdefg"`),
|
||||
where it will respond "yo dawg" to any incoming packet;
|
||||
where it will respond "hello" to any incoming packet;
|
||||
* TCP packets on port 6969 (`socat stdio tcp4-connect:192.168.69.1:6969`),
|
||||
where it will respond "yo dawg" to any incoming connection and immediately close it;
|
||||
where it will respond "hello" to any incoming connection and immediately close it;
|
||||
* TCP packets on port 6970 (`socat stdio tcp4-connect:192.168.69.1:6970 <<<"abcdefg"`),
|
||||
where it will respond with reversed chunks of the input indefinitely.
|
||||
* TCP packets on port 6971 (`cat /dev/urandom | socat stdio tcp4-connect:192.168.69.1:6971`),
|
||||
|
|
|
@ -7,6 +7,7 @@ extern crate smoltcp;
|
|||
mod utils;
|
||||
|
||||
use std::str;
|
||||
use std::fmt::Write;
|
||||
use std::time::Instant;
|
||||
use smoltcp::Error;
|
||||
use smoltcp::wire::{EthernetAddress, IpAddress};
|
||||
|
@ -60,7 +61,7 @@ fn main() {
|
|||
|
||||
let mut tcp_6970_active = false;
|
||||
loop {
|
||||
// udp:6969: respond "yo dawg"
|
||||
// udp:6969: respond "hello"
|
||||
{
|
||||
let socket: &mut UdpSocket = sockets.get_mut(udp_handle).as_socket();
|
||||
if !socket.is_open() {
|
||||
|
@ -76,14 +77,14 @@ fn main() {
|
|||
Err(_) => None
|
||||
};
|
||||
if let Some(endpoint) = client {
|
||||
let data = b"yo dawg\n";
|
||||
let data = b"hello\n";
|
||||
debug!("udp:6969 send data: {:?}",
|
||||
str::from_utf8(data.as_ref()).unwrap());
|
||||
socket.send_slice(data, endpoint).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
// tcp:6969: respond "yo dawg"
|
||||
// tcp:6969: respond "hello"
|
||||
{
|
||||
let socket: &mut TcpSocket = sockets.get_mut(tcp1_handle).as_socket();
|
||||
if !socket.is_open() {
|
||||
|
@ -91,10 +92,8 @@ fn main() {
|
|||
}
|
||||
|
||||
if socket.can_send() {
|
||||
let data = b"yo dawg\n";
|
||||
debug!("tcp:6969 send data: {:?}",
|
||||
str::from_utf8(data.as_ref()).unwrap());
|
||||
socket.send_slice(data).unwrap();
|
||||
debug!("tcp:6969 send greeting");
|
||||
write!(socket, "hello\n");
|
||||
debug!("tcp:6969 close");
|
||||
socket.close();
|
||||
}
|
||||
|
|
|
@ -1182,6 +1182,17 @@ impl<'a> TcpSocket<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> fmt::Write for TcpSocket<'a> {
|
||||
fn write_str(&mut self, slice: &str) -> fmt::Result {
|
||||
let slice = slice.as_bytes();
|
||||
if self.send_slice(slice) == Ok(slice.len()) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(fmt::Error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IpPayload for TcpRepr<'a> {
|
||||
fn buffer_len(&self) -> usize {
|
||||
self.buffer_len()
|
||||
|
|
Loading…
Reference in New Issue