Struct ClientSocket

Source
pub struct ClientSocket { /* private fields */ }
Expand description

Client used to communicate with GameServer. Must be singleton in your app.

Implementations§

Source§

impl ClientSocket

Source

pub fn new( port: u16, server_address: impl ToSocketAddrs, ) -> Result<ClientSocket, Exception>

Create new client and listen on port to recv packets from server_address and send its to them.

Examples found in repository?
examples/ping_pong_client.rs (line 6)
5fn main() {
6    let mut client = victorem::ClientSocket::new(11111, "127.0.0.1:22222").unwrap();
7    let mut id: u32 = 0;
8    let mut timer = Instant::now();
9    let period = Duration::from_millis(100);
10    loop {
11        if timer.elapsed() > period {
12            timer = Instant::now();
13            id += 1;
14            let _ = client.send(format!("Ping {}", id).into_bytes());
15        }
16        let _ = client
17            .recv()
18            .map(|v| String::from_utf8(v).unwrap_or(String::new()))
19            .map(|s| println!("From Server: {}", s));
20    }
21}
Source

pub fn send(&mut self, command: Vec<u8>) -> Result<usize, Exception>

Send data to server Don’t block current thread may wait up to 30 milliseconds if you send commands too often Commands ordered and with some guarantees.

Examples found in repository?
examples/ping_pong_client.rs (line 14)
5fn main() {
6    let mut client = victorem::ClientSocket::new(11111, "127.0.0.1:22222").unwrap();
7    let mut id: u32 = 0;
8    let mut timer = Instant::now();
9    let period = Duration::from_millis(100);
10    loop {
11        if timer.elapsed() > period {
12            timer = Instant::now();
13            id += 1;
14            let _ = client.send(format!("Ping {}", id).into_bytes());
15        }
16        let _ = client
17            .recv()
18            .map(|v| String::from_utf8(v).unwrap_or(String::new()))
19            .map(|s| println!("From Server: {}", s));
20    }
21}
Source

pub fn recv(&mut self) -> Result<Vec<u8>, Exception>

Reads data from server. Don’t block current thread. Return Exception with std::io::ErrorKind::WouldBlock if there is no data available. Data ordered and without some guarantees.

Examples found in repository?
examples/ping_pong_client.rs (line 17)
5fn main() {
6    let mut client = victorem::ClientSocket::new(11111, "127.0.0.1:22222").unwrap();
7    let mut id: u32 = 0;
8    let mut timer = Instant::now();
9    let period = Duration::from_millis(100);
10    loop {
11        if timer.elapsed() > period {
12            timer = Instant::now();
13            id += 1;
14            let _ = client.send(format!("Ping {}", id).into_bytes());
15        }
16        let _ = client
17            .recv()
18            .map(|v| String::from_utf8(v).unwrap_or(String::new()))
19            .map(|s| println!("From Server: {}", s));
20    }
21}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.