hedgewars/uFLNet.pas
author unc0rr
Tue, 21 Jul 2015 23:46:52 +0300
branchqmlfrontend
changeset 10953 360e57620df3
parent 10951 89a7f617e091
child 11408 b894922d58cc
permissions -rw-r--r--
Put types into a separate file
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;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
     8
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
     9
implementation
10953
360e57620df3 Put types into a separate file
unc0rr
parents: 10951
diff changeset
    10
uses SDLh, uFLIPC, uFLTypes, uFLUICallback, uFLNetTypes;
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
    11
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    12
const endCmd: string = #10 + #10;
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
    13
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
    14
function getNextChar: char; forward;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
    15
function getCurrChar: char; forward;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
    16
procedure sendNet(s: shortstring); forward;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
    17
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    18
type
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
    19
    TNetState = (netDisconnected, netConnecting, netLoggedIn);
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    20
    TParserState = record
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    21
                       cmd: TCmdType;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    22
                       l: LongInt;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    23
                       netState: TNetState;
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
    24
                       buf: shortstring;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
    25
                       bufpos: byte;
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    26
                   end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    27
    PHandler = procedure;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    28
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    29
var state: TParserState;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    30
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    31
// generated stuff here
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
    32
const letters: array[0..206] of char = ('A', 'S', 'K', 'P', 'A', 'S', 'S', 'W', 'O', 'R', 'D', #10, 'B', 'A', 'N', 'L', 'I', 'S', 'T', #10, 'Y', 'E', #10, 'C', 'H', 'A', 'T', #10, 'L', 'I', 'E', 'N', 'T', '_', 'F', 'L', 'A', 'G', 'S', #10, 'O', 'N', 'N', 'E', 'C', 'T', 'E', 'D', #10, 'E', 'M', #10, 'H', 'H', '_', 'N', 'U', 'M', #10, 'I', 'N', 'F', 'O', #10, 'J', 'O', 'I', 'N', 'E', 'D', #10, 'I', 'N', 'G', #10, 'K', 'I', 'C', 'K', 'E', 'D', #10, 'L', 'E', 'F', 'T', #10, 'O', 'B', 'B', 'Y', ':', 'J', 'O', 'I', 'N', 'E', 'D', #10, 'L', 'E', 'F', 'T', #10, 'N', 'I', 'C', 'K', #10, 'O', 'T', 'I', 'C', 'E', #10, 'P', 'I', 'N', 'G', #10, 'R', 'O', 'T', 'O', #10, 'R', 'O', 'O', 'M', 'S', #10, 'U', 'N', 'D', '_', 'F', 'I', 'N', 'I', 'S', 'H', 'E', 'D', #10, 'U', 'N', '_', 'G', 'A', 'M', 'E', #10, 'S', 'E', 'R', 'V', 'E', 'R', '_', 'A', 'U', 'T', 'H', #10, 'M', 'E', 'S', 'S', 'A', 'G', 'E', #10, 'V', 'A', 'R', 'S', #10, 'T', 'E', 'A', 'M', '_', 'A', 'C', 'C', 'E', 'P', 'T', 'E', 'D', #10, 'C', 'O', 'L', 'O', 'R', #10, 'W', 'A', 'R', 'N', 'I', 'N', 'G', #10, #0, #10);
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    33
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
    34
const commands: array[0..206] of integer = (12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -38, 11, 7, 0, 0, 0, 0, 0, -37, 0, 0, -36, 26, 4, 0, 0, -35, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -34, 0, 0, 0, 0, 0, 0, 0, 0, -33, 3, 0, -32, 7, 0, 0, 0, 0, 0, -31, 5, 0, 0, 0, -30, 11, 0, 0, 0, 3, 0, -29, 0, 0, 0, -28, 7, 0, 0, 0, 0, 0, -27, 22, 4, 0, 0, -26, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, -25, 0, 0, 0, 0, -24, 11, 4, 0, 0, -23, 0, 0, 0, 0, 0, -22, 10, 4, 0, 0, -21, 0, 0, 0, 0, -20, 27, 18, 4, 0, 0, -19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -18, 0, 0, 0, 0, 0, 0, 0, -17, 25, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, -16, 8, 0, 0, 0, 0, 0, 0, -15, 0, 0, 0, 0, -14, 20, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, -13, 0, 0, 0, 0, 0, -12, 8, 0, 0, 0, 0, 0, 0, -11, 0, -10);
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    35
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    36
procedure handler_ASKPASSWORD;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    37
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    38
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    39
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    40
procedure handler_BANLIST;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    41
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    42
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    43
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    44
procedure handler_BYE;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    45
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    46
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    47
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    48
procedure handler_CHAT;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    49
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    50
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    51
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    52
procedure handler_CLIENT_FLAGS;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    53
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    54
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    55
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    56
procedure handler_CONNECTED;
10953
360e57620df3 Put types into a separate file
unc0rr
parents: 10951
diff changeset
    57
var data: TCmdConnectedData;
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    58
begin
10951
89a7f617e091 - Move protocol handling events to main thread through qt's main loop
unc0rr
parents: 10935
diff changeset
    59
    sendUI(mtNetData, nil, 0);
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    60
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    61
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    62
procedure handler_EM;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    63
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    64
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    65
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    66
procedure handler_HH_NUM;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    67
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    68
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    69
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    70
procedure handler_INFO;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    71
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    72
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    73
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    74
procedure handler_JOINED;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    75
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    76
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    77
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    78
procedure handler_JOINING;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    79
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    80
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    81
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    82
procedure handler_KICKED;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    83
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    84
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    85
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    86
procedure handler_LEFT;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    87
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    88
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    89
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    90
procedure handler_LOBBY_JOINED;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    91
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    92
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    93
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    94
procedure handler_LOBBY_LEFT;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    95
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    96
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    97
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    98
procedure handler_NICK;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
    99
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   100
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   101
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   102
procedure handler_NOTICE;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   103
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   104
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   105
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   106
procedure handler_PING;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   107
begin
10933
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   108
    sendNet('PONG')
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   109
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   110
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   111
procedure handler_PROTO;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   112
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   113
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   114
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   115
procedure handler_ROOMS;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   116
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   117
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   118
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   119
procedure handler_ROUND_FINISHED;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   120
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   121
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   122
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   123
procedure handler_RUN_GAME;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   124
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   125
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   126
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   127
procedure handler_SERVER_AUTH;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   128
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   129
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   130
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   131
procedure handler_SERVER_MESSAGE;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   132
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   133
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   134
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   135
procedure handler_SERVER_VARS;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   136
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   137
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   138
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   139
procedure handler_TEAM_ACCEPTED;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   140
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   141
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   142
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   143
procedure handler_TEAM_COLOR;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   144
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   145
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   146
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   147
procedure handler_WARNING;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   148
begin
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   149
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   150
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   151
procedure handler___UNKNOWN__;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   152
begin
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   153
    writeln('[NET] Unknown cmd');
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   154
end;
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   155
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   156
const handlers: array[0..28] of PHandler = (@handler___UNKNOWN__, @handler_WARNING, @handler_TEAM_COLOR, @handler_TEAM_ACCEPTED, @handler_SERVER_VARS, @handler_SERVER_MESSAGE, @handler_SERVER_AUTH, @handler_RUN_GAME, @handler_ROUND_FINISHED, @handler_ROOMS, @handler_PROTO, @handler_PING, @handler_NOTICE, @handler_NICK, @handler_LOBBY_LEFT, @handler_LOBBY_JOINED, @handler_LEFT, @handler_KICKED, @handler_JOINING, @handler_JOINED, @handler_INFO, @handler_HH_NUM, @handler_EM, @handler_CONNECTED, @handler_CLIENT_FLAGS, @handler_CHAT, @handler_BYE, @handler_BANLIST, @handler_ASKPASSWORD);
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   157
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   158
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   159
// end of generated stuff
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   160
procedure handleTail;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   161
var cnt: Longint;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   162
    c: char;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   163
begin
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   164
    state.l:= 0;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   165
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   166
    c:= getCurrChar;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   167
    repeat
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   168
        if c = #10 then cnt:= 0 else cnt:= 1;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   169
        repeat
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   170
            c:= getNextChar;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   171
            inc(cnt)
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   172
        until (c = #0) or (c = #10);
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   173
    until (c = #0) or (cnt = 1)
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   174
end;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   175
10896
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   176
var sock: PTCPSocket;
10900
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   177
    netReaderThread: PSDL_Thread;
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   178
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   179
function getCurrChar: char;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   180
begin
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   181
    getCurrChar:= state.buf[state.bufpos]
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   182
end;
10900
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   183
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   184
function getNextChar: char;
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   185
var r: byte;
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
    if state.bufpos < byte(state.buf[0]) then
10900
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   188
    begin
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   189
        inc(state.bufpos);
10900
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   190
    end else
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   191
    begin
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   192
        r:= SDLNet_TCP_Recv(sock, @state.buf[1], 255);
10900
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   193
        if r > 0 then
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   194
        begin
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   195
            state.bufpos:= 1;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   196
            state.buf[0]:= char(r);
10900
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   197
        end else
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   198
        begin
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   199
            state.bufpos:= 0;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   200
            state.buf[0]:= #0;
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   201
        end
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   202
    end;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   203
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   204
    getNextChar:= state.buf[state.bufpos];
10900
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   205
end;
10898
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10896
diff changeset
   206
10933
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   207
function netWriter(sock: PTCPSocket): LongInt; cdecl; export;
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   208
begin
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   209
    netWriter:= 0;
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   210
end;
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   211
10898
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10896
diff changeset
   212
function netReader(data: pointer): LongInt; cdecl; export;
10929
8ebf01f75d9f Mockup of protocol parser
unc0rr
parents: 10900
diff changeset
   213
var c: char;
10933
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   214
    ipaddr: TIPAddress;
10898
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10896
diff changeset
   215
begin
10933
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   216
    netReader:= 0;
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   217
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   218
    if SDLNet_ResolveHost(ipaddr, PChar('netserver.hedgewars.org'), 46631) = 0 then
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   219
        sock:= SDLNet_TCP_Open(ipaddr);
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
    SDL_CreateThread(@netWriter{$IFDEF SDL2}, 'netWriter'{$ENDIF}, sock);
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   222
10933
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   223
    repeat
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   224
        c:= getNextChar;
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   225
        //writeln('>>>>> ', c, ' [', letters[state.l], '] ', commands[state.l]);
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   226
        if c = #0 then
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   227
            state.netState:= netDisconnected
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
        begin
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   230
            while (letters[state.l] <> c) and (commands[state.l] > 0) do
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   231
                inc(state.l, commands[state.l]);
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   232
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   233
            if c = letters[state.l] then
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   234
                if commands[state.l] < 0 then
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   235
                begin
10933
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   236
                    state.cmd:= TCmdType(-10 - commands[state.l]);
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   237
                    writeln('[NET] ', state.cmd);
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   238
                    handlers[-10 - commands[state.l]]();
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   239
                    handleTail()
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   240
                end
10933
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   241
                else
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   242
                    inc(state.l)
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   243
            else
10933
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   244
            begin
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   245
                handler___UNKNOWN__();
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   246
                handleTail()
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   247
            end
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   248
        end
10933
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   249
    until state.netState = netDisconnected;
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   250
10933
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   251
    sock:= nil;
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   252
f1da4126a61c Some more work on flib network
unc0rr
parents: 10931
diff changeset
   253
    writeln('[NET] netReader: disconnected');
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   254
end;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   255
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   256
procedure sendNet(s: shortstring);
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   257
begin
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   258
    writeln('[NET] Send: ', s);
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   259
    ipcToNet(s + endCmd);
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   260
end;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   261
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   262
procedure netSendCallback(p: pointer; msg: PChar; len: Longword);
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   263
begin
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   264
    // 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
   265
    SDLNet_TCP_Send(sock, msg, len);
10898
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10896
diff changeset
   266
end;
10896
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   267
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   268
procedure connectOfficialServer;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   269
begin
10898
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10896
diff changeset
   270
    if sock <> nil then
10896
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   271
        exit;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   272
10931
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   273
    state.bufpos:= 0;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   274
    state.buf:= '';
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   275
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   276
    state.l:= 0;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   277
    state.netState:= netConnecting;
384765cd0caf Parse net commands, answer to pings
unc0rr
parents: 10929
diff changeset
   278
10900
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
   279
    netReaderThread:= SDL_CreateThread(@netReader{$IFDEF SDL2}, 'netReader'{$ENDIF}, nil);
10896
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   280
end;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   281
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   282
procedure initModule;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   283
begin
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   284
    sock:= nil;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   285
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   286
    SDLNet_Init;
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   287
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   288
    registerNetCallback(nil, @netSendCallback);
10896
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   289
end;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   290
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   291
procedure freeModule;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   292
begin
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   293
end;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   294
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
   295
end.