Skip to content

Commit 6bc6cc7

Browse files
committedJul 30, 2017
Implement fmt::Write for TcpSocket.
1 parent ee0b8b3 commit 6bc6cc7

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed
 

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,9 @@ It responds to:
200200

201201
* pings (`ping 192.168.69.1`);
202202
* UDP packets on port 6969 (`socat stdio udp4-connect:192.168.69.1:6969 <<<"abcdefg"`),
203-
where it will respond "yo dawg" to any incoming packet;
203+
where it will respond "hello" to any incoming packet;
204204
* TCP packets on port 6969 (`socat stdio tcp4-connect:192.168.69.1:6969`),
205-
where it will respond "yo dawg" to any incoming connection and immediately close it;
205+
where it will respond "hello" to any incoming connection and immediately close it;
206206
* TCP packets on port 6970 (`socat stdio tcp4-connect:192.168.69.1:6970 <<<"abcdefg"`),
207207
where it will respond with reversed chunks of the input indefinitely.
208208
* TCP packets on port 6971 (`cat /dev/urandom | socat stdio tcp4-connect:192.168.69.1:6971`),

‎examples/server.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ extern crate smoltcp;
77
mod utils;
88

99
use std::str;
10+
use std::fmt::Write;
1011
use std::time::Instant;
1112
use smoltcp::Error;
1213
use smoltcp::wire::{EthernetAddress, IpAddress};
@@ -60,7 +61,7 @@ fn main() {
6061

6162
let mut tcp_6970_active = false;
6263
loop {
63-
// udp:6969: respond "yo dawg"
64+
// udp:6969: respond "hello"
6465
{
6566
let socket: &mut UdpSocket = sockets.get_mut(udp_handle).as_socket();
6667
if !socket.is_open() {
@@ -76,25 +77,23 @@ fn main() {
7677
Err(_) => None
7778
};
7879
if let Some(endpoint) = client {
79-
let data = b"yo dawg\n";
80+
let data = b"hello\n";
8081
debug!("udp:6969 send data: {:?}",
8182
str::from_utf8(data.as_ref()).unwrap());
8283
socket.send_slice(data, endpoint).unwrap();
8384
}
8485
}
8586

86-
// tcp:6969: respond "yo dawg"
87+
// tcp:6969: respond "hello"
8788
{
8889
let socket: &mut TcpSocket = sockets.get_mut(tcp1_handle).as_socket();
8990
if !socket.is_open() {
9091
socket.listen(6969).unwrap();
9192
}
9293

9394
if socket.can_send() {
94-
let data = b"yo dawg\n";
95-
debug!("tcp:6969 send data: {:?}",
96-
str::from_utf8(data.as_ref()).unwrap());
97-
socket.send_slice(data).unwrap();
95+
debug!("tcp:6969 send greeting");
96+
write!(socket, "hello\n");
9897
debug!("tcp:6969 close");
9998
socket.close();
10099
}

‎src/socket/tcp.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,17 @@ impl<'a> TcpSocket<'a> {
11821182
}
11831183
}
11841184

1185+
impl<'a> fmt::Write for TcpSocket<'a> {
1186+
fn write_str(&mut self, slice: &str) -> fmt::Result {
1187+
let slice = slice.as_bytes();
1188+
if self.send_slice(slice) == Ok(slice.len()) {
1189+
Ok(())
1190+
} else {
1191+
Err(fmt::Error)
1192+
}
1193+
}
1194+
}
1195+
11851196
impl<'a> IpPayload for TcpRepr<'a> {
11861197
fn buffer_len(&self) -> usize {
11871198
self.buffer_len()

0 commit comments

Comments
 (0)
Please sign in to comment.