pub struct Client { /* private fields */ }
Implementations§
Source§impl Client
impl Client
Sourcepub fn connect(ip: &str, port: u16) -> Result<Client>
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}
Sourcepub fn is_disconnected(&self) -> bool
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}
Sourcepub fn drain_incoming_packets(&mut self) -> Vec<Packet>
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}
Sourcepub fn send(&mut self, packet: impl PacketBody)
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}
Sourcepub fn tick(&mut self) -> Vec<ClientEvent>
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> 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