hedgewars/uFLNet.pas
author unc0rr
Thu, 19 Nov 2015 23:04:53 +0300
branchqmlfrontend
changeset 11415 05cf35103206
parent 11413 ffff8a0d1a76
child 11418 091149424aa4
permissions -rw-r--r--
Lobby page with chat widget(readonly)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10896
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
     1
unit uFLNet;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
     2
interface
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
     3
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
     4
procedure connectOfficialServer;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
     5
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
     6
procedure initModule;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
     7
procedure freeModule;
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
     8
procedure sendNet(s: shortstring);
11415
05cf35103206 Lobby page with chat widget(readonly)
unc0rr
parents: 11413
diff changeset
     9
procedure sendNetLn(s: shortstring);
10896
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    10
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    11
implementation
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    12
uses SDLh, uFLIPC, uFLTypes, uFLUICallback, uFLNetTypes, uFLUtils;
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
    13
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    14
const endCmd: string = #10 + #10;
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
    15
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
    16
function getNextChar: char; forward;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
    17
function getCurrChar: char; forward;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
    18
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    19
type
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
    20
    TNetState = (netDisconnected, netConnecting, netLoggedIn);
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    21
    TParserState = record
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    22
                       cmd: TCmdType;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    23
                       l: LongInt;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    24
                       netState: TNetState;
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
    25
                       buf: shortstring;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
    26
                       bufpos: byte;
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    27
                   end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    28
    PHandler = procedure;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    29
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    30
var state: TParserState;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    31
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    32
procedure handleTail; forward;
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    33
function getShortString: shortstring; forward;
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    34
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    35
// generated stuff here
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    36
const letters: array[0..211] of char = ('A', 'S', 'K', 'P', 'A', 'S', 'S', 'W',
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    37
    'O', 'R', 'D', #10, 'B', 'A', 'N', 'L', 'I', 'S', 'T', #10, 'Y', 'E', #10, 'C',
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    38
    'H', 'A', 'T', #10, 'L', 'I', 'E', 'N', 'T', '_', 'F', 'L', 'A', 'G', 'S', #10,
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    39
    'O', 'N', 'N', 'E', 'C', 'T', 'E', 'D', #10, 'E', 'M', #10, 'R', 'R', 'O', 'R',
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    40
    #10, 'H', 'H', '_', 'N', 'U', 'M', #10, 'I', 'N', 'F', 'O', #10, 'J', 'O', 'I',
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    41
    'N', 'E', 'D', #10, 'I', 'N', 'G', #10, 'K', 'I', 'C', 'K', 'E', 'D', #10, 'L',
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    42
    'E', 'F', 'T', #10, 'O', 'B', 'B', 'Y', ':', 'J', 'O', 'I', 'N', 'E', 'D', #10,
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    43
    'L', 'E', 'F', 'T', #10, 'N', 'I', 'C', 'K', #10, 'O', 'T', 'I', 'C', 'E', #10,
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    44
    'P', 'I', 'N', 'G', #10, 'R', 'O', 'T', 'O', #10, 'R', 'O', 'O', 'M', 'S', #10,
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    45
    'U', 'N', 'D', '_', 'F', 'I', 'N', 'I', 'S', 'H', 'E', 'D', #10, 'U', 'N', '_',
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    46
    'G', 'A', 'M', 'E', #10, 'S', 'E', 'R', 'V', 'E', 'R', '_', 'A', 'U', 'T', 'H',
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    47
    #10, 'M', 'E', 'S', 'S', 'A', 'G', 'E', #10, 'V', 'A', 'R', 'S', #10, 'T', 'E',
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    48
    'A', 'M', '_', 'A', 'C', 'C', 'E', 'P', 'T', 'E', 'D', #10, 'C', 'O', 'L', 'O',
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    49
    'R', #10, 'W', 'A', 'R', 'N', 'I', 'N', 'G', #10, #0, #10);
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    50
const commands: array[0..211] of integer = (12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    51
    -39, 11, 7, 0, 0, 0, 0, 0, -38, 0, 0, -37, 26, 4, 0, 0, -36, 12, 0, 0, 0, 0, 0,
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    52
    0, 0, 0, 0, 0, -35, 0, 0, 0, 0, 0, 0, 0, 0, -34, 8, 2, -33, 0, 0, 0, 0, -32, 7,
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    53
    0, 0, 0, 0, 0, -31, 5, 0, 0, 0, -30, 11, 0, 0, 0, 3, 0, -29, 0, 0, 0, -28, 7, 0,
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    54
    0, 0, 0, 0, -27, 22, 4, 0, 0, -26, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, -25, 0, 0,
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    55
    0, 0, -24, 11, 4, 0, 0, -23, 0, 0, 0, 0, 0, -22, 10, 4, 0, 0, -21, 0, 0, 0, 0,
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    56
    -20, 27, 18, 4, 0, 0, -19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -18, 0, 0, 0, 0,
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    57
    0, 0, 0, -17, 25, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, -16, 8, 0, 0, 0, 0, 0, 0, -15,
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    58
    0, 0, 0, 0, -14, 20, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, -13, 0, 0, 0, 0, 0,
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    59
    -12, 8, 0, 0, 0, 0, 0, 0, -11, 0, -10);
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    60
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    61
procedure handler_;
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    62
begin
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    63
    sendUI(mtNetData, @state.cmd, sizeof(state.cmd));
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    64
    writeln('handler_');
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    65
    handleTail()
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    66
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    67
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    68
procedure handler_L;
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    69
begin
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    70
    writeln('handler_L');
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    71
    handleTail()
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    72
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    73
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    74
procedure handler_ML;
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    75
begin
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    76
    writeln('handler_ML');
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    77
    handleTail()
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    78
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    79
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    80
procedure handler_MS;
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    81
var cmd: TCmdParamS;
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    82
    f: boolean;
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    83
begin
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    84
    sendUI(mtNetData, @state.cmd, sizeof(state.cmd));
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    85
    cmd.cmd:= Succ(state.cmd);
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    86
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    87
    repeat
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    88
        cmd.str1:= getShortString;
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    89
        f:= cmd.str1[0] <> #0;
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    90
        if f then
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    91
            sendUI(mtNetData, @cmd, sizeof(cmd));
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    92
    until not f
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    93
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    94
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    95
procedure handler_S;
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    96
begin
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    97
    writeln('handler_S');
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
    98
    handleTail()
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    99
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   100
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   101
procedure handler_SL;
11415
05cf35103206 Lobby page with chat widget(readonly)
unc0rr
parents: 11413
diff changeset
   102
var cmd: TCmdParamSL;
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   103
begin
11415
05cf35103206 Lobby page with chat widget(readonly)
unc0rr
parents: 11413
diff changeset
   104
    cmd.cmd:= state.cmd;
05cf35103206 Lobby page with chat widget(readonly)
unc0rr
parents: 11413
diff changeset
   105
    cmd.str1:= getShortString;
05cf35103206 Lobby page with chat widget(readonly)
unc0rr
parents: 11413
diff changeset
   106
    cmd.str2:= getShortString; // FIXME should be long string
05cf35103206 Lobby page with chat widget(readonly)
unc0rr
parents: 11413
diff changeset
   107
    sendUI(mtNetData, @cmd, sizeof(cmd));
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   108
    handleTail()
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   109
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   110
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   111
procedure handler_SMS;
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   112
begin
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   113
    writeln('handler_SMS');
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   114
    handleTail()
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   115
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   116
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   117
procedure handler__i;
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   118
var cmd: TCmdParami;
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   119
begin
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   120
    writeln('handler__i');
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   121
    getShortString;
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   122
    cmd.cmd:= state.cmd;
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   123
    cmd.param1:= strToInt(getShortString);
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   124
    sendUI(mtNetData, @cmd, sizeof(cmd));
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   125
    handleTail()
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   126
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   127
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   128
procedure handler_i;
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   129
begin
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   130
    writeln('handler_i');
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   131
    handleTail()
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   132
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   133
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   134
procedure handler__UNKNOWN_;
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   135
begin
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   136
    writeln('[NET] Unknown cmd');
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   137
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   138
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   139
const handlers: array[0..29] of PHandler = (@handler__UNKNOWN_, @handler_L, @handler_MS, @handler_S,
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   140
    @handler_SL, @handler_L, @handler_S, @handler_, @handler_, @handler_MS,
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   141
    @handler_i, @handler_MS, @handler_L, @handler_S, @handler_SL, @handler_MS,
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   142
    @handler_SMS, @handler_, @handler_S, @handler_MS, @handler_MS, @handler_MS,
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   143
    @handler_L, @handler_ML, @handler__i, @handler_SMS, @handler_SL, @handler_SL,
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   144
    @handler_MS, @handler_S);
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   145
const net2cmd: array[0..29] of TCmdType = (cmd_WARNING, cmd_WARNING,
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   146
    cmd_TEAM_COLOR, cmd_TEAM_ACCEPTED, cmd_SERVER_VARS, cmd_SERVER_MESSAGE,
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   147
    cmd_SERVER_AUTH, cmd_RUN_GAME, cmd_ROUND_FINISHED, cmd_ROOMS, cmd_PROTO,
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   148
    cmd_PING, cmd_NOTICE, cmd_NICK, cmd_LOBBY_LEFT, cmd_LOBBY_JOINED, cmd_LEFT,
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   149
    cmd_KICKED, cmd_JOINING, cmd_JOINED, cmd_INFO, cmd_HH_NUM, cmd_ERROR, cmd_EM,
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   150
    cmd_CONNECTED, cmd_CLIENT_FLAGS, cmd_CHAT, cmd_BYE, cmd_BANLIST,
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   151
    cmd_ASKPASSWORD);
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   152
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   153
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   154
// end of generated stuff
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   155
procedure handleTail;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   156
var cnt: Longint;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   157
    c: char;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   158
begin
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   159
    state.l:= 0;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   160
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   161
    c:= getCurrChar;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   162
    repeat
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   163
        if c = #10 then cnt:= 0 else cnt:= 1;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   164
        repeat
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   165
            c:= getNextChar;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   166
            inc(cnt)
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   167
        until (c = #0) or (c = #10);
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   168
    until (c = #0) or (cnt = 1)
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   169
end;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   170
10896
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   171
var sock: PTCPSocket;
10900
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   172
    netReaderThread: PSDL_Thread;
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   173
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   174
function getCurrChar: char;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   175
begin
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   176
    getCurrChar:= state.buf[state.bufpos]
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   177
end;
10900
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   178
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   179
function getNextChar: char;
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   180
var r: byte;
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   181
begin
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   182
    if state.bufpos < byte(state.buf[0]) then
10900
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   183
    begin
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   184
        inc(state.bufpos);
10900
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   185
    end else
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   186
    begin
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   187
        r:= SDLNet_TCP_Recv(sock, @state.buf[1], 255);
10900
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   188
        if r > 0 then
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   189
        begin
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   190
            state.bufpos:= 1;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   191
            state.buf[0]:= char(r);
10900
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   192
        end else
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   193
        begin
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   194
            state.bufpos:= 0;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   195
            state.buf[0]:= #0;
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   196
        end
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   197
    end;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   198
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   199
    getNextChar:= state.buf[state.bufpos];
10900
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   200
end;
10898
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10896
diff changeset
   201
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10896
diff changeset
   202
function netReader(data: pointer): LongInt; cdecl; export;
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   203
var c: char;
10933
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   204
    ipaddr: TIPAddress;
10898
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10896
diff changeset
   205
begin
10933
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   206
    netReader:= 0;
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   207
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   208
    if SDLNet_ResolveHost(ipaddr, PChar('netserver.hedgewars.org'), 46631) = 0 then
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   209
        sock:= SDLNet_TCP_Open(ipaddr);
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   210
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   211
    repeat
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   212
        c:= getNextChar;
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   213
        //writeln('>>>>> ', c, ' [', letters[state.l], '] ', commands[state.l]);
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   214
        if c = #0 then
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   215
            state.netState:= netDisconnected
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   216
        else
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   217
        begin
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   218
            while (letters[state.l] <> c) and (commands[state.l] > 0) do
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   219
                inc(state.l, commands[state.l]);
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   220
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   221
            if c = letters[state.l] then
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   222
                if commands[state.l] < 0 then
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   223
                begin
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   224
                    state.cmd:= net2cmd[-10 - commands[state.l]];
10933
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   225
                    writeln('[NET] ', state.cmd);
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   226
                    handlers[-10 - commands[state.l]]();
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   227
                end
10933
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   228
                else
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   229
                    inc(state.l)
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   230
            else
10933
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   231
            begin
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   232
                handler__UNKNOWN_();
10933
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   233
                handleTail()
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   234
            end
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   235
        end
10933
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   236
    until state.netState = netDisconnected;
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   237
11415
05cf35103206 Lobby page with chat widget(readonly)
unc0rr
parents: 11413
diff changeset
   238
    SDLNet_TCP_Close(sock);
10933
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   239
    sock:= nil;
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   240
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   241
    writeln('[NET] netReader: disconnected');
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   242
end;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   243
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   244
procedure sendNet(s: shortstring);
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   245
begin
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   246
    writeln('[NET] Send: ', s);
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   247
    ipcToNet(s + endCmd);
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   248
end;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   249
11415
05cf35103206 Lobby page with chat widget(readonly)
unc0rr
parents: 11413
diff changeset
   250
procedure sendNetLn(s: shortstring);
05cf35103206 Lobby page with chat widget(readonly)
unc0rr
parents: 11413
diff changeset
   251
begin
05cf35103206 Lobby page with chat widget(readonly)
unc0rr
parents: 11413
diff changeset
   252
    writeln('[NET] Send: ', s);
05cf35103206 Lobby page with chat widget(readonly)
unc0rr
parents: 11413
diff changeset
   253
    ipcToNet(s + #10);
05cf35103206 Lobby page with chat widget(readonly)
unc0rr
parents: 11413
diff changeset
   254
end;
05cf35103206 Lobby page with chat widget(readonly)
unc0rr
parents: 11413
diff changeset
   255
11413
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   256
function getShortString: shortstring;
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   257
var s: shortstring;
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   258
    c: char;
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   259
begin
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   260
    s[0]:= #0;
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   261
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   262
    repeat
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   263
        inc(s[0]);
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   264
        s[byte(s[0])]:= getNextChar
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   265
    until (s[0] = #255) or (s[byte(s[0])] = #10) or (s[byte(s[0])] = #0);
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   266
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   267
    if s[byte(s[0])] = #10 then
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   268
        dec(s[0])
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   269
    else
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   270
        repeat c:= getNextChar until (c = #0) or (c = #10);
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   271
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   272
    getShortString:= s
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   273
end;
ffff8a0d1a76 Implement processing net commands in the main thread
unc0rr
parents: 11403
diff changeset
   274
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   275
procedure netSendCallback(p: pointer; msg: PChar; len: Longword);
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   276
begin
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   277
    // W A R N I N G: totally thread-unsafe due to use of sock variable
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   278
    SDLNet_TCP_Send(sock, msg, len);
10898
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10896
diff changeset
   279
end;
10896
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   280
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   281
procedure connectOfficialServer;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   282
begin
10898
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10896
diff changeset
   283
    if sock <> nil then
10896
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   284
        exit;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   285
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   286
    state.bufpos:= 0;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   287
    state.buf:= '';
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   288
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   289
    state.l:= 0;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   290
    state.netState:= netConnecting;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   291
11403
b894922d58cc Merge default (add a bunch of FIXMEs)
unc0rr
parents: 10953
diff changeset
   292
    netReaderThread:= SDL_CreateThread(@netReader, 'netReader', nil);
11415
05cf35103206 Lobby page with chat widget(readonly)
unc0rr
parents: 11413
diff changeset
   293
    SDL_DetachThread(netReaderThread)
10896
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   294
end;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   295
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   296
procedure initModule;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   297
begin
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   298
    sock:= nil;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   299
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   300
    SDLNet_Init;
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   301
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   302
    registerNetCallback(nil, @netSendCallback);
10896
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   303
end;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   304
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   305
procedure freeModule;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   306
begin
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   307
end;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   308
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   309
end.