Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate tokio_tcp;
- extern crate tokio_udp;
- extern crate byteorder;
- using std::any::Any;
- struct NetOutput;
- struct NetInput;
- struct NetConnection;
- trait Packet {
- fn encode(&self);
- fn decode(&mut self);
- }
- trait NetHandler {
- fn on_connection(&mut self, c: &NetConnection);
- fn on_disconnect(&mut self, c: &NetConnection);
- fn on_received(&mut self, c: &NetConnection, p: &Packet);
- }
- struct ExamplePacket {
- message: String
- }
- impl Packet for ExamplePacket {
- fn encode(&self, o: &mut NetOutput) {
- o.write_string(self.message);
- }
- fn decode(&mut self, i: &mut NetInput) {
- self.message = i.read_string();
- }
- }
- struct ExampleHandler;
- impl NetHandler for ExampleHandler {
- fn on_connection(&mut self, c: &NetConnection) {
- println!("Connection!");
- }
- fn on_disconnect(&mut self, c: &NetConnection) {
- println!("Disconnect!");
- }
- fn on_received<T: Any>(&mut self, c: &NetConnection, p: &Any) {
- let val = p as &Any;
- match val.downcast_ref::<ExamplePacket>() {
- Some(p) => {
- println!("Received ExamplePacket with {}", p.message);
- },
- None => { }
- }
- }
- }
- fn main() {
- let mut registry = PacketRegistry::new();
- registry.add(0x00, Box::new(ExamplePacket));
- let s = TcpServer::new(SocketAddr::from(([127, 0, 0, 1], 25565)), ®istry, &handler);
- s.begin().unwrap();
- }
Add Comment
Please, Sign In to add comment