use std::ffi::{CString, CStr};
use std::str;
use traits::Wrappable;
use system::Time;
use csfml_network_sys as ffi;
#[derive(Clone, Copy)]
pub struct IpAddress{
ip: ffi::sfIpAddress
}
impl IpAddress {
pub fn new_from_string(address: &str) -> IpAddress {
let c_address = CString::new(address.as_bytes()).unwrap().as_ptr();
IpAddress {
ip: unsafe { ffi::sfIpAddress_fromString(c_address) }
}
}
pub fn new_from_bytes(byte0: u8, byte1: u8, byte2: u8, byte3: u8) -> IpAddress {
IpAddress {
ip: unsafe { ffi::sfIpAddress_fromBytes(byte0, byte1, byte2, byte3) }
}
}
pub fn new_from_integer(address: u32) -> IpAddress {
IpAddress {
ip: unsafe { ffi::sfIpAddress_fromInteger(address) }
}
}
pub fn to_string(&self) -> String {
unsafe {
let ptr: *const _ = ::std::mem::transmute(&self.ip);
str::from_utf8(CStr::from_ptr(ptr).to_bytes()).unwrap().into()
}
}
pub fn to_integer(&self) -> u32 {
unsafe {
ffi::sfIpAddress_toInteger(self.ip)
}
}
pub fn get_local_address() -> IpAddress {
IpAddress {
ip: unsafe { ffi::sfIpAddress_getLocalAddress() }
}
}
pub fn get_public_address(timeout: &Time) -> IpAddress {
IpAddress {
ip: unsafe { ffi::sfIpAddress_getPublicAddress(timeout.unwrap()) }
}
}
}
impl Wrappable<ffi::sfIpAddress> for IpAddress {
fn wrap(ip: ffi::sfIpAddress) -> IpAddress {
IpAddress {
ip: ip
}
}
fn unwrap(&self) -> ffi::sfIpAddress {
self.ip
}
}