Struct grubbnet::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)
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
fn main() -> Result<()> {
    // Create a client and connect to localhost
    let mut client = Client::connect("127.0.0.1", 7667)?;

    let mut counter = 0;
    loop {
        // Sleep for a lil bit so we don't hog the CPU
        std::thread::sleep(std::time::Duration::from_millis(100));

        // Send a ping packet every 20 ticks
        counter += 1;
        if (counter % 20) == 0 {
            let ping = PingPacket {
                msg: format!("Ping! Tick {}", counter),
            };
            client.send(ping);
        }

        // Run the network tick and process any events it generates
        for event in client.tick().iter() {
            match event {
                ClientEvent::Disconnected => {
                    println!("Disconnected from server!");
                    break;
                }
                ClientEvent::ReceivedPacket(byte_count) => {
                    println!("Received packet from server ({} bytes)", byte_count);
                }
                ClientEvent::SentPacket(byte_count) => {
                    println!("Sent packet to server ({} bytes)", byte_count);
                }
                _ => eprintln!("Unhandled ClientEvent!"),
            }
        }

        if client.is_disconnected() {
            break;
        }

        // Process incoming packets
        for packet in client.drain_incoming_packets().iter() {
            match packet.header.id {
                0x00 => {
                    let packet = PongPacket::deserialize(&packet.body);
                    println!("Got pong: {}", packet.unwrap().msg);
                }
                _ => eprintln!("Unhandled packet! id: {}", packet.header.id),
            }
        }
    }

    Ok(())
}
source

pub fn is_disconnected(&self) -> bool

Examples found in repository?
examples/simple_client.rs (line 94)
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
fn main() -> Result<()> {
    // Create a client and connect to localhost
    let mut client = Client::connect("127.0.0.1", 7667)?;

    let mut counter = 0;
    loop {
        // Sleep for a lil bit so we don't hog the CPU
        std::thread::sleep(std::time::Duration::from_millis(100));

        // Send a ping packet every 20 ticks
        counter += 1;
        if (counter % 20) == 0 {
            let ping = PingPacket {
                msg: format!("Ping! Tick {}", counter),
            };
            client.send(ping);
        }

        // Run the network tick and process any events it generates
        for event in client.tick().iter() {
            match event {
                ClientEvent::Disconnected => {
                    println!("Disconnected from server!");
                    break;
                }
                ClientEvent::ReceivedPacket(byte_count) => {
                    println!("Received packet from server ({} bytes)", byte_count);
                }
                ClientEvent::SentPacket(byte_count) => {
                    println!("Sent packet to server ({} bytes)", byte_count);
                }
                _ => eprintln!("Unhandled ClientEvent!"),
            }
        }

        if client.is_disconnected() {
            break;
        }

        // Process incoming packets
        for packet in client.drain_incoming_packets().iter() {
            match packet.header.id {
                0x00 => {
                    let packet = PongPacket::deserialize(&packet.body);
                    println!("Got pong: {}", packet.unwrap().msg);
                }
                _ => eprintln!("Unhandled packet! id: {}", packet.header.id),
            }
        }
    }

    Ok(())
}
source

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

Examples found in repository?
examples/simple_client.rs (line 99)
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
fn main() -> Result<()> {
    // Create a client and connect to localhost
    let mut client = Client::connect("127.0.0.1", 7667)?;

    let mut counter = 0;
    loop {
        // Sleep for a lil bit so we don't hog the CPU
        std::thread::sleep(std::time::Duration::from_millis(100));

        // Send a ping packet every 20 ticks
        counter += 1;
        if (counter % 20) == 0 {
            let ping = PingPacket {
                msg: format!("Ping! Tick {}", counter),
            };
            client.send(ping);
        }

        // Run the network tick and process any events it generates
        for event in client.tick().iter() {
            match event {
                ClientEvent::Disconnected => {
                    println!("Disconnected from server!");
                    break;
                }
                ClientEvent::ReceivedPacket(byte_count) => {
                    println!("Received packet from server ({} bytes)", byte_count);
                }
                ClientEvent::SentPacket(byte_count) => {
                    println!("Sent packet to server ({} bytes)", byte_count);
                }
                _ => eprintln!("Unhandled ClientEvent!"),
            }
        }

        if client.is_disconnected() {
            break;
        }

        // Process incoming packets
        for packet in client.drain_incoming_packets().iter() {
            match packet.header.id {
                0x00 => {
                    let packet = PongPacket::deserialize(&packet.body);
                    println!("Got pong: {}", packet.unwrap().msg);
                }
                _ => eprintln!("Unhandled packet! id: {}", packet.header.id),
            }
        }
    }

    Ok(())
}
source

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

Examples found in repository?
examples/simple_client.rs (line 74)
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
fn main() -> Result<()> {
    // Create a client and connect to localhost
    let mut client = Client::connect("127.0.0.1", 7667)?;

    let mut counter = 0;
    loop {
        // Sleep for a lil bit so we don't hog the CPU
        std::thread::sleep(std::time::Duration::from_millis(100));

        // Send a ping packet every 20 ticks
        counter += 1;
        if (counter % 20) == 0 {
            let ping = PingPacket {
                msg: format!("Ping! Tick {}", counter),
            };
            client.send(ping);
        }

        // Run the network tick and process any events it generates
        for event in client.tick().iter() {
            match event {
                ClientEvent::Disconnected => {
                    println!("Disconnected from server!");
                    break;
                }
                ClientEvent::ReceivedPacket(byte_count) => {
                    println!("Received packet from server ({} bytes)", byte_count);
                }
                ClientEvent::SentPacket(byte_count) => {
                    println!("Sent packet to server ({} bytes)", byte_count);
                }
                _ => eprintln!("Unhandled ClientEvent!"),
            }
        }

        if client.is_disconnected() {
            break;
        }

        // Process incoming packets
        for packet in client.drain_incoming_packets().iter() {
            match packet.header.id {
                0x00 => {
                    let packet = PongPacket::deserialize(&packet.body);
                    println!("Got pong: {}", packet.unwrap().msg);
                }
                _ => eprintln!("Unhandled packet! id: {}", packet.header.id),
            }
        }
    }

    Ok(())
}
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)
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
fn main() -> Result<()> {
    // Create a client and connect to localhost
    let mut client = Client::connect("127.0.0.1", 7667)?;

    let mut counter = 0;
    loop {
        // Sleep for a lil bit so we don't hog the CPU
        std::thread::sleep(std::time::Duration::from_millis(100));

        // Send a ping packet every 20 ticks
        counter += 1;
        if (counter % 20) == 0 {
            let ping = PingPacket {
                msg: format!("Ping! Tick {}", counter),
            };
            client.send(ping);
        }

        // Run the network tick and process any events it generates
        for event in client.tick().iter() {
            match event {
                ClientEvent::Disconnected => {
                    println!("Disconnected from server!");
                    break;
                }
                ClientEvent::ReceivedPacket(byte_count) => {
                    println!("Received packet from server ({} bytes)", byte_count);
                }
                ClientEvent::SentPacket(byte_count) => {
                    println!("Sent packet to server ({} bytes)", byte_count);
                }
                _ => eprintln!("Unhandled ClientEvent!"),
            }
        }

        if client.is_disconnected() {
            break;
        }

        // Process incoming packets
        for packet in client.drain_incoming_packets().iter() {
            match packet.header.id {
                0x00 => {
                    let packet = PongPacket::deserialize(&packet.body);
                    println!("Got pong: {}", packet.unwrap().msg);
                }
                _ => eprintln!("Unhandled packet! id: {}", packet.header.id),
            }
        }
    }

    Ok(())
}

Auto Trait Implementations§

§

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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.