Struct Client

Source
pub struct Client { /* private fields */ }

Implementations§

Source§

impl Client

Source

pub fn connect(ip: &str, port: u16) -> Result<Client>

Examples found in repository?
examples/simple_client.rs (line 61)
59fn main() -> Result<()> {
60    // Create a client and connect to localhost
61    let mut client = Client::connect("127.0.0.1", 7667)?;
62
63    let mut counter = 0;
64    loop {
65        // Sleep for a lil bit so we don't hog the CPU
66        std::thread::sleep(std::time::Duration::from_millis(100));
67
68        // Send a ping packet every 20 ticks
69        counter += 1;
70        if (counter % 20) == 0 {
71            let ping = PingPacket {
72                msg: format!("Ping! Tick {}", counter),
73            };
74            client.send(ping);
75        }
76
77        // Run the network tick and process any events it generates
78        for event in client.tick().iter() {
79            match event {
80                ClientEvent::Disconnected => {
81                    println!("Disconnected from server!");
82                    break;
83                }
84                ClientEvent::ReceivedPacket(byte_count) => {
85                    println!("Received packet from server ({} bytes)", byte_count);
86                }
87                ClientEvent::SentPacket(byte_count) => {
88                    println!("Sent packet to server ({} bytes)", byte_count);
89                }
90                _ => eprintln!("Unhandled ClientEvent!"),
91            }
92        }
93
94        if client.is_disconnected() {
95            break;
96        }
97
98        // Process incoming packets
99        for packet in client.drain_incoming_packets().iter() {
100            match packet.header.id {
101                0x00 => {
102                    let packet = PongPacket::deserialize(&packet.body);
103                    println!("Got pong: {}", packet.unwrap().msg);
104                }
105                _ => eprintln!("Unhandled packet! id: {}", packet.header.id),
106            }
107        }
108    }
109
110    Ok(())
111}
Source

pub fn is_disconnected(&self) -> bool

Examples found in repository?
examples/simple_client.rs (line 94)
59fn main() -> Result<()> {
60    // Create a client and connect to localhost
61    let mut client = Client::connect("127.0.0.1", 7667)?;
62
63    let mut counter = 0;
64    loop {
65        // Sleep for a lil bit so we don't hog the CPU
66        std::thread::sleep(std::time::Duration::from_millis(100));
67
68        // Send a ping packet every 20 ticks
69        counter += 1;
70        if (counter % 20) == 0 {
71            let ping = PingPacket {
72                msg: format!("Ping! Tick {}", counter),
73            };
74            client.send(ping);
75        }
76
77        // Run the network tick and process any events it generates
78        for event in client.tick().iter() {
79            match event {
80                ClientEvent::Disconnected => {
81                    println!("Disconnected from server!");
82                    break;
83                }
84                ClientEvent::ReceivedPacket(byte_count) => {
85                    println!("Received packet from server ({} bytes)", byte_count);
86                }
87                ClientEvent::SentPacket(byte_count) => {
88                    println!("Sent packet to server ({} bytes)", byte_count);
89                }
90                _ => eprintln!("Unhandled ClientEvent!"),
91            }
92        }
93
94        if client.is_disconnected() {
95            break;
96        }
97
98        // Process incoming packets
99        for packet in client.drain_incoming_packets().iter() {
100            match packet.header.id {
101                0x00 => {
102                    let packet = PongPacket::deserialize(&packet.body);
103                    println!("Got pong: {}", packet.unwrap().msg);
104                }
105                _ => eprintln!("Unhandled packet! id: {}", packet.header.id),
106            }
107        }
108    }
109
110    Ok(())
111}
Source

pub fn drain_incoming_packets(&mut self) -> Vec<Packet>

Examples found in repository?
examples/simple_client.rs (line 99)
59fn main() -> Result<()> {
60    // Create a client and connect to localhost
61    let mut client = Client::connect("127.0.0.1", 7667)?;
62
63    let mut counter = 0;
64    loop {
65        // Sleep for a lil bit so we don't hog the CPU
66        std::thread::sleep(std::time::Duration::from_millis(100));
67
68        // Send a ping packet every 20 ticks
69        counter += 1;
70        if (counter % 20) == 0 {
71            let ping = PingPacket {
72                msg: format!("Ping! Tick {}", counter),
73            };
74            client.send(ping);
75        }
76
77        // Run the network tick and process any events it generates
78        for event in client.tick().iter() {
79            match event {
80                ClientEvent::Disconnected => {
81                    println!("Disconnected from server!");
82                    break;
83                }
84                ClientEvent::ReceivedPacket(byte_count) => {
85                    println!("Received packet from server ({} bytes)", byte_count);
86                }
87                ClientEvent::SentPacket(byte_count) => {
88                    println!("Sent packet to server ({} bytes)", byte_count);
89                }
90                _ => eprintln!("Unhandled ClientEvent!"),
91            }
92        }
93
94        if client.is_disconnected() {
95            break;
96        }
97
98        // Process incoming packets
99        for packet in client.drain_incoming_packets().iter() {
100            match packet.header.id {
101                0x00 => {
102                    let packet = PongPacket::deserialize(&packet.body);
103                    println!("Got pong: {}", packet.unwrap().msg);
104                }
105                _ => eprintln!("Unhandled packet! id: {}", packet.header.id),
106            }
107        }
108    }
109
110    Ok(())
111}
Source

pub fn send(&mut self, packet: impl PacketBody)

Examples found in repository?
examples/simple_client.rs (line 74)
59fn main() -> Result<()> {
60    // Create a client and connect to localhost
61    let mut client = Client::connect("127.0.0.1", 7667)?;
62
63    let mut counter = 0;
64    loop {
65        // Sleep for a lil bit so we don't hog the CPU
66        std::thread::sleep(std::time::Duration::from_millis(100));
67
68        // Send a ping packet every 20 ticks
69        counter += 1;
70        if (counter % 20) == 0 {
71            let ping = PingPacket {
72                msg: format!("Ping! Tick {}", counter),
73            };
74            client.send(ping);
75        }
76
77        // Run the network tick and process any events it generates
78        for event in client.tick().iter() {
79            match event {
80                ClientEvent::Disconnected => {
81                    println!("Disconnected from server!");
82                    break;
83                }
84                ClientEvent::ReceivedPacket(byte_count) => {
85                    println!("Received packet from server ({} bytes)", byte_count);
86                }
87                ClientEvent::SentPacket(byte_count) => {
88                    println!("Sent packet to server ({} bytes)", byte_count);
89                }
90                _ => eprintln!("Unhandled ClientEvent!"),
91            }
92        }
93
94        if client.is_disconnected() {
95            break;
96        }
97
98        // Process incoming packets
99        for packet in client.drain_incoming_packets().iter() {
100            match packet.header.id {
101                0x00 => {
102                    let packet = PongPacket::deserialize(&packet.body);
103                    println!("Got pong: {}", packet.unwrap().msg);
104                }
105                _ => eprintln!("Unhandled packet! id: {}", packet.header.id),
106            }
107        }
108    }
109
110    Ok(())
111}
Source

pub fn tick(&mut self) -> Vec<ClientEvent>

Runs a network tick, which sends/receives packets based on socket readiness

Examples found in repository?
examples/simple_client.rs (line 78)
59fn main() -> Result<()> {
60    // Create a client and connect to localhost
61    let mut client = Client::connect("127.0.0.1", 7667)?;
62
63    let mut counter = 0;
64    loop {
65        // Sleep for a lil bit so we don't hog the CPU
66        std::thread::sleep(std::time::Duration::from_millis(100));
67
68        // Send a ping packet every 20 ticks
69        counter += 1;
70        if (counter % 20) == 0 {
71            let ping = PingPacket {
72                msg: format!("Ping! Tick {}", counter),
73            };
74            client.send(ping);
75        }
76
77        // Run the network tick and process any events it generates
78        for event in client.tick().iter() {
79            match event {
80                ClientEvent::Disconnected => {
81                    println!("Disconnected from server!");
82                    break;
83                }
84                ClientEvent::ReceivedPacket(byte_count) => {
85                    println!("Received packet from server ({} bytes)", byte_count);
86                }
87                ClientEvent::SentPacket(byte_count) => {
88                    println!("Sent packet to server ({} bytes)", byte_count);
89                }
90                _ => eprintln!("Unhandled ClientEvent!"),
91            }
92        }
93
94        if client.is_disconnected() {
95            break;
96        }
97
98        // Process incoming packets
99        for packet in client.drain_incoming_packets().iter() {
100            match packet.header.id {
101                0x00 => {
102                    let packet = PongPacket::deserialize(&packet.body);
103                    println!("Got pong: {}", packet.unwrap().msg);
104                }
105                _ => eprintln!("Unhandled packet! id: {}", packet.header.id),
106            }
107        }
108    }
109
110    Ok(())
111}

Auto Trait Implementations§

§

impl !Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

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, 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.