|
|
|
@ -7,6 +7,7 @@ use prost::Message;
|
|
|
|
|
use std::io::Cursor;
|
|
|
|
|
use serde::{Serialize, Deserialize};
|
|
|
|
|
use rand_core::RngCore;
|
|
|
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
|
|
|
|
|
|
use std::io::prelude::*;
|
|
|
|
|
|
|
|
|
@ -187,7 +188,7 @@ impl BackendMessage {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn as_bytes(self: BackendMessage) -> Vec<u8> {
|
|
|
|
|
fn as_bytes(self: &BackendMessage) -> Vec<u8> {
|
|
|
|
|
let mut bytes = Vec::new();
|
|
|
|
|
|
|
|
|
|
bytes.push(self.status);
|
|
|
|
@ -358,20 +359,21 @@ impl NoticeMsg {
|
|
|
|
|
|
|
|
|
|
struct TMClient {
|
|
|
|
|
stream: openssl::ssl::SslStream<TcpStream>,
|
|
|
|
|
notices: mpsc::Sender<tm::Notice>,
|
|
|
|
|
responses: mpsc::Sender<BackendMessage>,
|
|
|
|
|
requests: mpsc::Receiver<BackendMessage>,
|
|
|
|
|
notices: mpsc::Sender<Box<tm::Notice>>,
|
|
|
|
|
responses: mpsc::Sender<Box<BackendMessage>>,
|
|
|
|
|
requests: mpsc::Receiver<Box<BackendMessage>>,
|
|
|
|
|
uuid: [u8; 16],
|
|
|
|
|
client_name: [u8; 32],
|
|
|
|
|
password: String,
|
|
|
|
|
last_seq_num: u64,
|
|
|
|
|
username: [u8; 16],
|
|
|
|
|
connected: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct TMConnection {
|
|
|
|
|
notices: mpsc::Receiver<tm::Notice>,
|
|
|
|
|
responses: mpsc::Receiver<BackendMessage>,
|
|
|
|
|
requests: mpsc::Sender<BackendMessage>,
|
|
|
|
|
notices: mpsc::Receiver<Box<tm::Notice>>,
|
|
|
|
|
responses: mpsc::Receiver<Box<BackendMessage>>,
|
|
|
|
|
requests: mpsc::Sender<Box<BackendMessage>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TMClient {
|
|
|
|
@ -407,6 +409,7 @@ impl TMClient {
|
|
|
|
|
password,
|
|
|
|
|
last_seq_num: 0xFFFFFFFFFFFFFFFF,
|
|
|
|
|
username,
|
|
|
|
|
connected: false,
|
|
|
|
|
},
|
|
|
|
|
TMConnection{
|
|
|
|
|
requests: request_tx,
|
|
|
|
@ -416,33 +419,42 @@ impl TMClient {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn process(self: &mut TMClient) {
|
|
|
|
|
if self.connected == true {
|
|
|
|
|
for request in self.requests.try_iter() {
|
|
|
|
|
let packet = BackendPacket::new(TM_HEADER, 0.00, 3, self.last_seq_num + 1, request.as_bytes());
|
|
|
|
|
let time = SystemTime::now();
|
|
|
|
|
let millis = time.duration_since(UNIX_EPOCH).unwrap();
|
|
|
|
|
let packet = BackendPacket::new(TM_HEADER, (millis.as_millis() as f64)/1000.0, 2, self.last_seq_num + 1, request.as_bytes());
|
|
|
|
|
match self.stream.write(&packet.as_bytes()) {
|
|
|
|
|
Ok(_) => self.last_seq_num += 1,
|
|
|
|
|
Ok(_) => {
|
|
|
|
|
log::debug!("Sent: {:?}", packet);
|
|
|
|
|
self.last_seq_num += 1;
|
|
|
|
|
},
|
|
|
|
|
Err(error) => println!("Request send error: {:?}", error),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut incoming = [0; 2048];
|
|
|
|
|
match self.stream.read(&mut incoming) {
|
|
|
|
|
Ok(read) => {
|
|
|
|
|
let data = incoming[0..read].to_vec();
|
|
|
|
|
match BackendPacket::from_bytes(data) {
|
|
|
|
|
Some(packet) => {
|
|
|
|
|
log::debug!("Recevied: {:?}", packet);
|
|
|
|
|
self.last_seq_num = packet.seq_num;
|
|
|
|
|
match packet.msg_type {
|
|
|
|
|
// Notice Message
|
|
|
|
|
4 => {
|
|
|
|
|
match NoticeMsg::from_bytes(packet.data.clone()) {
|
|
|
|
|
Some(notice) => {
|
|
|
|
|
println!("Received notice: {:?}", notice);
|
|
|
|
|
log::debug!("Received notice: {:?}", notice);
|
|
|
|
|
let ack = BackendPacket::new(packet.header, packet.timestamp, 5, self.last_seq_num+1, notice.notice_id.to_le_bytes().to_vec());
|
|
|
|
|
self.last_seq_num += 1;
|
|
|
|
|
match self.stream.write(&ack.as_bytes()) {
|
|
|
|
|
Ok(_) => println!("Sent ACK for notice {}", notice.notice_id),
|
|
|
|
|
Err(error) => println!("ACK error: {:?}", error),
|
|
|
|
|
}
|
|
|
|
|
match self.notices.send(notice.notice) {
|
|
|
|
|
match self.notices.send(Box::new(notice.notice)) {
|
|
|
|
|
Ok(_) => println!("Forwarded notice to callback engine"),
|
|
|
|
|
Err(error) => println!("Notice forward error {:?}", error),
|
|
|
|
|
}
|
|
|
|
@ -454,9 +466,14 @@ impl TMClient {
|
|
|
|
|
3 => {
|
|
|
|
|
match BackendMessage::from_bytes(packet.data.clone()) {
|
|
|
|
|
Some(message) => {
|
|
|
|
|
match self.responses.send(message) {
|
|
|
|
|
Ok(_) => println!("Forwarded response to callback engine"),
|
|
|
|
|
Err(error) => println!("Response forward error {:?}", error),
|
|
|
|
|
println!("About to send");
|
|
|
|
|
match self.responses.send(Box::new(message)) {
|
|
|
|
|
Ok(_) => {
|
|
|
|
|
println!("Forwarded response to callback engine");
|
|
|
|
|
}
|
|
|
|
|
Err(error) => {
|
|
|
|
|
println!("Response forward error {:?}", error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
None => println!("BackendMessage parse error: {:?}", packet),
|
|
|
|
@ -476,6 +493,7 @@ impl TMClient {
|
|
|
|
|
} else if welcome_msg.state_valid == 0 {
|
|
|
|
|
println!("pw_valid but not state_valid");
|
|
|
|
|
} else {
|
|
|
|
|
self.connected = true;
|
|
|
|
|
println!("Connected to TM backend!");
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
@ -487,6 +505,7 @@ impl TMClient {
|
|
|
|
|
},
|
|
|
|
|
None => {
|
|
|
|
|
println!("Failed to parse BackendPacket({}): {}", read, String::from_utf8_lossy(&incoming));
|
|
|
|
|
// Sleep to prevent busy loop when TM is spamming 0 length packets
|
|
|
|
|
thread::sleep(Duration::from_millis(100));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -654,7 +673,21 @@ fn main() {
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// TODO: send "get_schedule" message and wait for it's response to know we're connected
|
|
|
|
|
let mut get_match_list_tuple = tm::MatchTuple::default();
|
|
|
|
|
get_match_list_tuple.division = Some(1);
|
|
|
|
|
get_match_list_tuple.round = None;
|
|
|
|
|
get_match_list_tuple.instance = Some(0);
|
|
|
|
|
get_match_list_tuple.r#match = Some(0);
|
|
|
|
|
get_match_list_tuple.session = Some(0);
|
|
|
|
|
|
|
|
|
|
let mut get_match_list_data = tm::BackendMessageData::default();
|
|
|
|
|
get_match_list_data.match_tuple = Some(get_match_list_tuple);
|
|
|
|
|
|
|
|
|
|
let get_match_list_req = BackendMessage::new(1002, get_match_list_data.clone());
|
|
|
|
|
tm_connection.requests.send(Box::new(get_match_list_req)).unwrap();
|
|
|
|
|
|
|
|
|
|
let get_match_list_resp = tm_connection.responses.recv().unwrap();
|
|
|
|
|
println!("Get Match List Response: {:?}", get_match_list_resp);
|
|
|
|
|
|
|
|
|
|
while running {
|
|
|
|
|
thread::sleep(Duration::from_millis(1000));
|
|
|
|
@ -669,7 +702,7 @@ fn main() {
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Some(callback) => {
|
|
|
|
|
let (messages, next_event) = callback(notice, event);
|
|
|
|
|
let (messages, next_event) = callback(*notice, event);
|
|
|
|
|
event = next_event;
|
|
|
|
|
for message in messages {
|
|
|
|
|
let result = client.publish(message.topic, QoS::AtMostOnce, true, message.payload);
|
|
|
|
|