gameServer2/src/protocol/messages.rs
author unc0rr
Sat, 14 Jan 2017 22:30:09 +0300
changeset 12137 193dfdcb0620
parent 12136 e25a82ce2374
child 12141 78925eff02c2
permissions -rw-r--r--
- Use logging facilities instead of plain println! - Parse malformed messages, parser doesn't get stuck anymore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
     1
use server::coretypes::{ServerVar, GameCfg, TeamInfo, HedgehogInfo};
12131
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
     2
use std;
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
     3
use std::ops;
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
     4
use std::convert::From;
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
     5
12131
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
     6
#[derive(PartialEq, Debug)]
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
     7
pub enum HWProtocolMessage<'a> {
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
     8
    // core
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
     9
    Ping,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    10
    Pong,
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    11
    Quit(Option<&'a str>),
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    12
    Bye(&'a str),
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    13
    LobbyLeft(&'a str),
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    14
    //Cmd(&'a str, Vec<&'a str>),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    15
    Global(&'a str),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    16
    Watch(&'a str),
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    17
    ToggleServerRegisteredOnly,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    18
    SuperPower,
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    19
    Info(&'a str),
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    20
    // not entered state
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    21
    Nick(&'a str),
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    22
    Proto(u32),
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    23
    Password(&'a str, &'a str),
12135
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12133
diff changeset
    24
    Checker(u32, &'a str, &'a str),
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    25
    // lobby
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    26
    List,
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    27
    Chat(&'a str),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    28
    CreateRoom(&'a str, Option<&'a str>),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    29
    Join(&'a str, Option<&'a str>),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    30
    Follow(&'a str),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    31
    Rnd(Vec<&'a str>),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    32
    Kick(&'a str),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    33
    Ban(&'a str, &'a str, u32),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    34
    BanIP(&'a str, &'a str, u32),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    35
    BanNick(&'a str, &'a str, u32),
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    36
    BanList,
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    37
    Unban(&'a str),
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    38
    SetServerVar(ServerVar),
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    39
    GetServerVar,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    40
    RestartServer,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    41
    Stats,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    42
    // in room
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    43
    Part(Option<&'a str>),
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    44
    Cfg(GameCfg),
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    45
    AddTeam(TeamInfo),
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    46
    RemoveTeam(&'a str),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    47
    SetHedgehogsNumber(&'a str, u8),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    48
    SetTeamColor(&'a str, u8),
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    49
    ToggleReady,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    50
    StartGame,
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    51
    EngineMessage(&'a str),
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    52
    RoundFinished,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    53
    ToggleRestrictJoin,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    54
    ToggleRestrictTeams,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    55
    ToggleRegisteredOnly,
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    56
    RoomName(&'a str),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    57
    Delegate(&'a str),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    58
    TeamChat(&'a str),
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    59
    MaxTeams(u8),
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    60
    Fix,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    61
    Unfix,
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    62
    Greeting(&'a str),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    63
    CallVote(Option<(&'a str, Option<&'a str>)>),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    64
    Vote(&'a str),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    65
    ForceVote(&'a str),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    66
    Save(&'a str, &'a str),
12135
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12133
diff changeset
    67
    Delete(&'a str),
12133
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    68
    SaveRoom(&'a str),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12131
diff changeset
    69
    LoadRoom(&'a str),
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    70
    Connected(u32),
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    71
    Malformed,
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    72
    Empty,
12130
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    73
}
12131
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
    74
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
    75
pub fn number<T: From<u8>
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
    76
                + std::default::Default
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
    77
                + std::ops::MulAssign
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
    78
                + std::ops::AddAssign>
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
    79
    (digits: Vec<u8>) -> T {
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
    80
    let mut value: T = T::default();
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
    81
    for digit in digits {
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
    82
        value *= T::from(10);
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
    83
        value += T::from(digit);
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
    84
    }
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
    85
    value
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12130
diff changeset
    86
}
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    87
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    88
fn construct_message(msg: & [&str]) -> String {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    89
    let mut m = String::with_capacity(64);
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    90
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    91
    for s in msg {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    92
        m.push_str(s);
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    93
        m.push('\n');
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    94
    }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    95
    m.push('\n');
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    96
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    97
    m
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    98
}
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
    99
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   100
impl<'a> HWProtocolMessage<'a> {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   101
    pub fn to_raw_protocol(&self) -> String {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   102
        match self {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   103
            &HWProtocolMessage::Ping
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   104
                => "PING\n\n".to_string(),
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   105
            &HWProtocolMessage::Pong
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   106
                => "PONG\n\n".to_string(),
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   107
            &HWProtocolMessage::Connected(protocol_version)
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   108
                => construct_message(&[
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   109
                    "CONNECTED",
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   110
                    "Hedgewars server http://www.hedgewars.org/",
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   111
                    &protocol_version.to_string()
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
   112
                ]),
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   113
            &HWProtocolMessage::Bye(msg)
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   114
                => construct_message(&["BYE", msg]),
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   115
            &HWProtocolMessage::LobbyLeft(msg)
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   116
                => construct_message(&["LOBBY_LEFT", msg]),
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   117
            _ => String::new()
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   118
        }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   119
    }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12135
diff changeset
   120
}