<commit_before>use authoritative::AuthoritativeServer;
use std::io::Result;
use std::io;
use std::io::prelude::*;
use mio::{Token, EventLoop, EventSet, PollOpt, TryRead};
use mio::tcp::{TcpStream};
use byteorder::{ByteOrder, BigEndian, LittleEndian};

pub struct GameClient{
    socket: TcpStream,
    token: Token
}

impl GameClient{
    pub fn new(socket: TcpStream, token: Token) -> GameClient{
        GameClient {
            socket: socket,
            token: token,
        }
    }

    pub fn register(&mut self, event_loop: &mut EventLoop<AuthoritativeServer>) -> Result<()>{
        println!("Registering token {:?}", self.token);

        event_loop.register(
            &self.socket,
            self.token,
            EventSet::readable(),
            PollOpt::edge() | PollOpt::oneshot()
        ).and_then(|(),|{
            Ok(())
        }).or_else(|e|{
            println!("Failed to register {:?}, {:?}", self.token, e);
            Err(e)
        })
    }

    pub fn reregister(&mut self, event_loop: &mut EventLoop<AuthoritativeServer>) -> Result<()>{
        println!("Reregistering token {:?}", self.token);

        event_loop.reregister(
            &self.socket,
            self.token,
            EventSet::readable(),
            PollOpt::edge() | PollOpt::oneshot()
        ).and_then(|(),|{
            Ok(())
        }).or_else(|e|{
            println!("Failed to reregister {:?}, {:?}", self.token, e);
            Err(e)
        })
    }

    pub fn read(&mut self) -> Result<Option<u64>>{
        println!("Fuck yeah!");

        let mut buf = [0u8; 12];

        let bytes_read = match self.socket.try_read(&mut buf){
            Ok(None) => { return Ok(None); },
            Ok(Some(n)) => n,
            Err(e) => { return Err(e); }
        };

        println!("Received: {:?}, {} bytes", buf, bytes_read);

        let some_fucking_value = BigEndian::read_u16(buf[0..2].as_ref());

        println!("Read {}", some_fucking_value);

        let mut recv_buf : Vec<u8> = Vec::with_capacity(8);

        let read_socket = <TcpStream as Read>::by_ref(&mut self.socket);

        match read_socket.take(8).try_read_buf(&mut recv_buf){
            Ok(None) => {},
            Ok(Some(n)) => {},
            Err(e) => {}
        };

        while let Some(b) = read_socket.take(1).try_read_buf(&mut recv_buf).ok(){
            match b{
                None => { break; },
                Some(bytes) => { println!("Read {:?} bytes", bytes); },
            };
        }

        Ok(None)
    }
}
<commit_msg>yey, messages are actually reading!<commit_after>use authoritative::AuthoritativeServer;
use std::io::Result;
use std::io;
use std::io::prelude::*;
use mio::{Token, EventLoop, EventSet, PollOpt, TryRead};
use mio::tcp::{TcpStream};
use byteorder::{ByteOrder, BigEndian, LittleEndian};

pub struct GameClient{
    socket: TcpStream,
    token: Token
}

impl GameClient{
    pub fn new(socket: TcpStream, token: Token) -> GameClient{
        GameClient {
            socket: socket,
            token: token,
        }
    }

    pub fn register(&mut self, event_loop: &mut EventLoop<AuthoritativeServer>) -> Result<()>{
        println!("Registering token {:?}", self.token);

        event_loop.register(
            &self.socket,
            self.token,
            EventSet::readable(),
            PollOpt::edge() | PollOpt::oneshot()
        ).and_then(|(),|{
            Ok(())
        }).or_else(|e|{
            println!("Failed to register {:?}, {:?}", self.token, e);
            Err(e)
        })
    }

    pub fn reregister(&mut self, event_loop: &mut EventLoop<AuthoritativeServer>) -> Result<()>{
        println!("Reregistering token {:?}", self.token);

        event_loop.reregister(
            &self.socket,
            self.token,
            EventSet::readable(),
            PollOpt::edge() | PollOpt::oneshot()
        ).and_then(|(),|{
            Ok(())
        }).or_else(|e|{
            println!("Failed to reregister {:?}, {:?}", self.token, e);
            Err(e)
        })
    }

    pub fn read(&mut self) -> Result<Option<u64>>{
        //println!("Fuck yeah!");

        // let mut buf = [0u8; 12];

        // let bytes_read = match self.socket.try_read(&mut buf){
            // Ok(None) => { return Ok(None); },
            // Ok(Some(n)) => n,
            // Err(e) => { return Err(e); }
        // };

        //let some_fucking_value = BigEndian::read_u16(buf[0..2].as_ref());

        //println!("Read {}", some_fucking_value);

        let mut recv_buf : Vec<u8> = Vec::with_capacity(8);

        let read_socket = <TcpStream as Read>::by_ref(&mut self.socket);

        // match read_socket.take(8).try_read_buf(&mut recv_buf){
        //     Ok(None) => {},
        //     Ok(Some(n)) => {},
        //     Err(e) => {}
        // };

        let mut total_bytes = 0;
        while let Some(b) = read_socket.take(8).try_read_buf(&mut recv_buf).ok(){
            match b{
                None => { break; },
                Some(bytes) => {
                    total_bytes += bytes;
                },
            };
        }

        recv_buf.truncate(total_bytes);

        //println!("Received: {:?}, {} bytes", recv_buf, total_bytes);

        let message : String = String::from_utf8(recv_buf).expect("Failed to read message!");

        println!("Received message: {}", message);

        Ok(None)
    }
}

{'fraction_non_alphanumeric': 0.17271380970011108, 'fraction_numerical': 0.0055534987041836355, 'mean_word_length': 2.0133853876185164, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7435179', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1670, 'n_words': 334}