author | unc0rr |
Sun, 17 May 2015 00:14:30 +0300 | |
branch | qmlfrontend |
changeset 10933 | f1da4126a61c |
parent 10931 | 384765cd0caf |
child 10935 | 3a65fcd7c335 |
permissions | -rw-r--r-- |
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 |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
10 |
uses SDLh; |
10931 | 11 |
|
12 |
const endCmd: array[0..1] of char = (#10, #10); |
|
13 |
||
14 |
function getNextChar: char; forward; |
|
15 |
function getCurrChar: char; forward; |
|
16 |
procedure sendNet(s: shortstring); forward; |
|
17 |
||
10933 | 18 |
type TCmdType = (cmd___UNKNOWN__, cmd_WARNING, cmd_TEAM_COLOR, cmd_TEAM_ACCEPTED, cmd_SERVER_VARS, cmd_SERVER_MESSAGE, cmd_SERVER_AUTH, cmd_RUN_GAME, cmd_ROUND_FINISHED, cmd_ROOMS, cmd_PROTO, cmd_PING, cmd_NOTICE, cmd_NICK, cmd_LOBBY_LEFT, cmd_LOBBY_JOINED, cmd_LEFT, cmd_KICKED, cmd_JOINING, cmd_JOINED, cmd_INFO, cmd_HH_NUM, cmd_EM, cmd_CONNECTED, cmd_CLIENT_FLAGS, cmd_CHAT, cmd_BYE, cmd_BANLIST, cmd_ASKPASSWORD); |
10896
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
19 |
|
10929 | 20 |
type |
10931 | 21 |
TNetState = (netDisconnected, netConnecting, netLoggedIn); |
10929 | 22 |
TParserState = record |
23 |
cmd: TCmdType; |
|
24 |
l: LongInt; |
|
25 |
netState: TNetState; |
|
10931 | 26 |
buf: shortstring; |
27 |
bufpos: byte; |
|
10929 | 28 |
end; |
29 |
PHandler = procedure; |
|
30 |
||
31 |
var state: TParserState; |
|
32 |
||
33 |
// generated stuff here |
|
10931 | 34 |
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 | 35 |
|
10931 | 36 |
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 | 37 |
|
38 |
procedure handler_ASKPASSWORD; |
|
39 |
begin |
|
40 |
end; |
|
41 |
||
42 |
procedure handler_BANLIST; |
|
43 |
begin |
|
44 |
end; |
|
45 |
||
46 |
procedure handler_BYE; |
|
47 |
begin |
|
48 |
end; |
|
49 |
||
50 |
procedure handler_CHAT; |
|
51 |
begin |
|
52 |
end; |
|
53 |
||
54 |
procedure handler_CLIENT_FLAGS; |
|
55 |
begin |
|
56 |
end; |
|
57 |
||
58 |
procedure handler_CONNECTED; |
|
59 |
begin |
|
60 |
end; |
|
61 |
||
62 |
procedure handler_EM; |
|
63 |
begin |
|
64 |
end; |
|
65 |
||
66 |
procedure handler_HH_NUM; |
|
67 |
begin |
|
68 |
end; |
|
69 |
||
70 |
procedure handler_INFO; |
|
71 |
begin |
|
72 |
end; |
|
73 |
||
74 |
procedure handler_JOINED; |
|
75 |
begin |
|
76 |
end; |
|
77 |
||
78 |
procedure handler_JOINING; |
|
79 |
begin |
|
80 |
end; |
|
81 |
||
82 |
procedure handler_KICKED; |
|
83 |
begin |
|
84 |
end; |
|
85 |
||
86 |
procedure handler_LEFT; |
|
87 |
begin |
|
88 |
end; |
|
89 |
||
90 |
procedure handler_LOBBY_JOINED; |
|
91 |
begin |
|
92 |
end; |
|
93 |
||
94 |
procedure handler_LOBBY_LEFT; |
|
95 |
begin |
|
96 |
end; |
|
97 |
||
98 |
procedure handler_NICK; |
|
99 |
begin |
|
100 |
end; |
|
101 |
||
102 |
procedure handler_NOTICE; |
|
103 |
begin |
|
104 |
end; |
|
105 |
||
106 |
procedure handler_PING; |
|
107 |
begin |
|
10933 | 108 |
sendNet('PONG') |
10929 | 109 |
end; |
110 |
||
111 |
procedure handler_PROTO; |
|
112 |
begin |
|
113 |
end; |
|
114 |
||
115 |
procedure handler_ROOMS; |
|
116 |
begin |
|
117 |
end; |
|
118 |
||
119 |
procedure handler_ROUND_FINISHED; |
|
120 |
begin |
|
121 |
end; |
|
122 |
||
123 |
procedure handler_RUN_GAME; |
|
124 |
begin |
|
125 |
end; |
|
126 |
||
127 |
procedure handler_SERVER_AUTH; |
|
128 |
begin |
|
129 |
end; |
|
130 |
||
131 |
procedure handler_SERVER_MESSAGE; |
|
132 |
begin |
|
133 |
end; |
|
134 |
||
135 |
procedure handler_SERVER_VARS; |
|
136 |
begin |
|
137 |
end; |
|
138 |
||
139 |
procedure handler_TEAM_ACCEPTED; |
|
140 |
begin |
|
141 |
end; |
|
142 |
||
143 |
procedure handler_TEAM_COLOR; |
|
144 |
begin |
|
145 |
end; |
|
146 |
||
147 |
procedure handler_WARNING; |
|
148 |
begin |
|
149 |
end; |
|
150 |
||
151 |
procedure handler___UNKNOWN__; |
|
152 |
begin |
|
10931 | 153 |
writeln('[NET] Unknown cmd'); |
10929 | 154 |
end; |
155 |
||
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); |
|
157 |
||
158 |
||
159 |
// end of generated stuff |
|
10931 | 160 |
procedure handleTail; |
161 |
var cnt: Longint; |
|
162 |
c: char; |
|
163 |
begin |
|
164 |
state.l:= 0; |
|
165 |
||
166 |
c:= getCurrChar; |
|
167 |
repeat |
|
168 |
if c = #10 then cnt:= 0 else cnt:= 1; |
|
169 |
repeat |
|
170 |
c:= getNextChar; |
|
171 |
inc(cnt) |
|
172 |
until (c = #0) or (c = #10); |
|
173 |
until (c = #0) or (cnt = 1) |
|
174 |
end; |
|
175 |
||
10896
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
176 |
var sock: PTCPSocket; |
10900 | 177 |
netReaderThread: PSDL_Thread; |
10931 | 178 |
|
179 |
function getCurrChar: char; |
|
180 |
begin |
|
181 |
getCurrChar:= state.buf[state.bufpos] |
|
182 |
end; |
|
10900 | 183 |
|
184 |
function getNextChar: char; |
|
185 |
var r: byte; |
|
186 |
begin |
|
10931 | 187 |
if state.bufpos < byte(state.buf[0]) then |
10900 | 188 |
begin |
10931 | 189 |
inc(state.bufpos); |
10900 | 190 |
end else |
191 |
begin |
|
10931 | 192 |
r:= SDLNet_TCP_Recv(sock, @state.buf[1], 255); |
10900 | 193 |
if r > 0 then |
194 |
begin |
|
10931 | 195 |
state.bufpos:= 1; |
196 |
state.buf[0]:= char(r); |
|
10900 | 197 |
end else |
198 |
begin |
|
10931 | 199 |
state.bufpos:= 0; |
200 |
state.buf[0]:= #0; |
|
10929 | 201 |
end |
10931 | 202 |
end; |
203 |
||
204 |
getNextChar:= state.buf[state.bufpos]; |
|
10900 | 205 |
end; |
10898 | 206 |
|
10933 | 207 |
function netWriter(sock: PTCPSocket): LongInt; cdecl; export; |
208 |
begin |
|
209 |
netWriter:= 0; |
|
210 |
end; |
|
211 |
||
10898 | 212 |
function netReader(data: pointer): LongInt; cdecl; export; |
10929 | 213 |
var c: char; |
10933 | 214 |
ipaddr: TIPAddress; |
10898 | 215 |
begin |
10933 | 216 |
netReader:= 0; |
217 |
||
218 |
if SDLNet_ResolveHost(ipaddr, PChar('netserver.hedgewars.org'), 46631) = 0 then |
|
219 |
sock:= SDLNet_TCP_Open(ipaddr); |
|
220 |
||
221 |
SDL_CreateThread(@netWriter{$IFDEF SDL2}, 'netWriter'{$ENDIF}, sock); |
|
10931 | 222 |
|
10933 | 223 |
repeat |
224 |
c:= getNextChar; |
|
225 |
//writeln('>>>>> ', c, ' [', letters[state.l], '] ', commands[state.l]); |
|
226 |
if c = #0 then |
|
227 |
state.netState:= netDisconnected |
|
228 |
else |
|
229 |
begin |
|
230 |
while (letters[state.l] <> c) and (commands[state.l] > 0) do |
|
231 |
inc(state.l, commands[state.l]); |
|
232 |
||
233 |
if c = letters[state.l] then |
|
234 |
if commands[state.l] < 0 then |
|
10931 | 235 |
begin |
10933 | 236 |
state.cmd:= TCmdType(-10 - commands[state.l]); |
237 |
writeln('[NET] ', state.cmd); |
|
10931 | 238 |
handlers[-10 - commands[state.l]](); |
239 |
handleTail() |
|
240 |
end |
|
10933 | 241 |
else |
242 |
inc(state.l) |
|
10931 | 243 |
else |
10933 | 244 |
begin |
245 |
handler___UNKNOWN__(); |
|
246 |
handleTail() |
|
247 |
end |
|
10931 | 248 |
end |
10933 | 249 |
until state.netState = netDisconnected; |
10931 | 250 |
|
10933 | 251 |
sock:= nil; |
252 |
||
253 |
writeln('[NET] netReader: disconnected'); |
|
10931 | 254 |
end; |
255 |
||
256 |
procedure sendNet(s: shortstring); |
|
257 |
begin |
|
258 |
writeln('[NET] Send: ', s); |
|
259 |
SDLNet_TCP_Send(sock, @s[1], byte(s[0])); |
|
260 |
SDLNet_TCP_Send(sock, @endCmd, 2); |
|
10898 | 261 |
end; |
10896
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
262 |
|
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
263 |
procedure connectOfficialServer; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
264 |
begin |
10898 | 265 |
if sock <> nil then |
10896
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
266 |
exit; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
267 |
|
10931 | 268 |
state.bufpos:= 0; |
269 |
state.buf:= ''; |
|
270 |
||
271 |
state.l:= 0; |
|
272 |
state.netState:= netConnecting; |
|
273 |
||
10900 | 274 |
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
|
275 |
end; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
276 |
|
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
277 |
procedure initModule; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
278 |
begin |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
279 |
sock:= nil; |
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 |
SDLNet_Init; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
282 |
end; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
283 |
|
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
284 |
procedure freeModule; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
285 |
begin |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
286 |
end; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
287 |
|
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
288 |
end. |