123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- #![cfg_attr(not(feature = "std"), no_std)]
- #![allow(unused_mut)]
- #[macro_use]
- extern crate log;
- #[cfg(feature = "std")]
- extern crate env_logger;
- #[cfg(feature = "std")]
- extern crate getopts;
- extern crate smoltcp;
- #[cfg(feature = "std")]
- mod utils;
- use smoltcp::Error;
- use smoltcp::phy::Loopback;
- #[cfg(feature = "std")]
- use smoltcp::phy::Tracer;
- use smoltcp::wire::{EthernetAddress, IpAddress};
- #[cfg(feature = "std")]
- use smoltcp::wire::EthernetFrame;
- use smoltcp::iface::{ArpCache, SliceArpCache, EthernetInterface};
- use smoltcp::socket::{AsSocket, SocketSet};
- use smoltcp::socket::{TcpSocket, TcpSocketBuffer};
- fn main() {
- #[cfg(feature = "std")]
- utils::setup_logging();
- let mut device = Loopback::new();
- #[cfg(feature = "std")]
- let mut device = Tracer::<_, EthernetFrame<&'static [u8]>>::new(device, utils::trace_writer);
- let mut arp_cache_entries: [_; 8] = Default::default();
- let mut arp_cache = SliceArpCache::new(&mut arp_cache_entries[..]);
- let hardware_addr = EthernetAddress::default();
- let mut protocol_addrs = [IpAddress::v4(127, 0, 0, 1)];
- let mut iface = EthernetInterface::new(
- &mut device, &mut arp_cache as &mut ArpCache,
- hardware_addr, &mut protocol_addrs[..]);
- let server_socket = {
- // It is not strictly necessary to use a `static mut` and unsafe code here, but
- // on embedded systems that smoltcp targets it is far better to allocate the data
- // statically to verify that it fits into RAM rather than get undefined behavior
- // when stack overflows.
- static mut TCP_SERVER_RX_DATA: [u8; 1024] = [0; 1024];
- static mut TCP_SERVER_TX_DATA: [u8; 1024] = [0; 1024];
- let tcp_rx_buffer = TcpSocketBuffer::new(unsafe { &mut TCP_SERVER_RX_DATA[..] });
- let tcp_tx_buffer = TcpSocketBuffer::new(unsafe { &mut TCP_SERVER_TX_DATA[..] });
- TcpSocket::new(tcp_rx_buffer, tcp_tx_buffer)
- };
- let client_socket = {
- static mut TCP_CLIENT_RX_DATA: [u8; 1024] = [0; 1024];
- static mut TCP_CLIENT_TX_DATA: [u8; 1024] = [0; 1024];
- let tcp_rx_buffer = TcpSocketBuffer::new(unsafe { &mut TCP_CLIENT_RX_DATA[..] });
- let tcp_tx_buffer = TcpSocketBuffer::new(unsafe { &mut TCP_CLIENT_TX_DATA[..] });
- TcpSocket::new(tcp_rx_buffer, tcp_tx_buffer)
- };
- let mut socket_set_entries: [_; 2] = Default::default();
- let mut socket_set = SocketSet::new(&mut socket_set_entries[..]);
- let server_handle = socket_set.add(server_socket);
- let client_handle = socket_set.add(client_socket);
- let mut did_listen = false;
- let mut did_connect = false;
- let mut done = false;
- let mut timestamp_ms = 0;
- while !done && timestamp_ms < 500 {
- {
- let socket: &mut TcpSocket = socket_set.get_mut(server_handle).as_socket();
- if !socket.is_active() && !socket.is_listening() {
- if !did_listen {
- socket.listen(1234).unwrap();
- did_listen = true;
- }
- }
- if socket.can_recv() {
- debug!("got {:?}", socket.recv(32).unwrap());
- socket.close();
- done = true;
- }
- }
- {
- let socket: &mut TcpSocket = socket_set.get_mut(client_handle).as_socket();
- if !socket.is_open() {
- if !did_connect {
- socket.connect((IpAddress::v4(127, 0, 0, 1), 1234),
- (IpAddress::v4(127, 0, 0, 1), 65000)).unwrap();
- did_connect = true;
- }
- }
- if socket.can_send() {
- socket.send_slice(b"0123456789abcdef").unwrap();
- socket.close();
- }
- }
- match iface.poll(&mut socket_set, timestamp_ms) {
- Ok(()) | Err(Error::Exhausted) => (),
- Err(e) => debug!("poll error: {}", e)
- }
- const DELAY: u64 = 20;
- debug!("{}ms pass", DELAY);
- timestamp_ms += DELAY;
- }
- if !done {
- error!("this is taking too long, bailing out");
- }
- }
|