pub struct ClientSocket { /* private fields */ }
Expand description
Client used to communicate with GameServer
. Must be singleton in your app.
Implementations§
Source§impl ClientSocket
impl ClientSocket
Sourcepub fn new(
port: u16,
server_address: impl ToSocketAddrs,
) -> Result<ClientSocket, Exception>
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}
Sourcepub fn send(&mut self, command: Vec<u8>) -> Result<usize, Exception>
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}
Sourcepub fn recv(&mut self) -> Result<Vec<u8>, Exception>
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§
impl Freeze for ClientSocket
impl RefUnwindSafe for ClientSocket
impl Send for ClientSocket
impl Sync for ClientSocket
impl Unpin for ClientSocket
impl UnwindSafe for ClientSocket
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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