use std::net::TcpStream;
use std::io::{ErrorKind,Error};
use crate::ic_types::{IcConnection,IcPacket};
pub struct IcClient {
pub con: IcConnection
}
impl IcClient {
pub fn connect(ip: &str,testing: bool) -> Result<IcClient,Error> {
if ! testing {
let con = TcpStream::connect(ip.to_owned()+":64209");
if let Ok(c) = con {
return Ok(IcClient { con: IcConnection::new(c,testing) });
} else {
return Err(Error::new(ErrorKind::Other,"Failed to connect."));
}
} else {
let con = TcpStream::connect(ip.to_owned()+":46290");
if let Ok(c) = con {
return Ok(IcClient { con: IcConnection::new(c,testing) });
} else {
return Err(Error::new(ErrorKind::Other,"Failed to connect."));
}
}
}
pub fn send_cmd(&mut self,c: &mut IcPacket) -> IcPacket {
self.con.send_packet(c).unwrap();
let retp = self.con.get_packet().unwrap_or(IcPacket::new_empty());
return retp;
}
}