use std::io::{Read, Write, Result};
use std::net::{ToSocketAddrs, SocketAddr};
use std::ops::Deref;
use socket::UtpSocket;
pub struct UtpStream {
socket: UtpSocket,
}
impl UtpStream {
pub fn bind<A: ToSocketAddrs>(addr: A) -> Result<UtpStream> {
UtpSocket::bind(addr).map(|s| UtpStream { socket: s })
}
pub fn connect<A: ToSocketAddrs>(dst: A) -> Result<UtpStream> {
UtpSocket::connect(dst).map(|s| UtpStream { socket: s })
}
pub fn close(&mut self) -> Result<()> {
self.socket.close()
}
pub fn local_addr(&self) -> Result<SocketAddr> {
self.socket.local_addr()
}
pub fn set_max_retransmission_retries(&mut self, n: u32) {
self.socket.max_retransmission_retries = n;
}
pub fn send_keepalive(&mut self) {
self.socket.send_keepalive();
}
}
impl Read for UtpStream {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
self.socket.recv_from(buf).map(|(read, _src)| read)
}
}
impl Write for UtpStream {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
self.socket.send_to(buf)
}
fn flush(&mut self) -> Result<()> {
self.socket.flush()
}
}
impl Into<UtpStream> for UtpSocket {
fn into(self) -> UtpStream {
UtpStream { socket: self }
}
}
impl Deref for UtpStream {
type Target = UtpSocket;
fn deref(&self) -> &UtpSocket {
&self.socket
}
}