author | unc0rr |
Tue, 21 Jul 2015 23:46:52 +0300 | |
branch | qmlfrontend |
changeset 10953 | 360e57620df3 |
parent 10951 | 89a7f617e091 |
child 11408 | b894922d58cc |
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 |
10953 | 10 |
uses SDLh, uFLIPC, uFLTypes, uFLUICallback, uFLNetTypes; |
10931 | 11 |
|
10935 | 12 |
const endCmd: string = #10 + #10; |
10931 | 13 |
|
14 |
function getNextChar: char; forward; |
|
15 |
function getCurrChar: char; forward; |
|
16 |
procedure sendNet(s: shortstring); forward; |
|
17 |
||
10929 | 18 |
type |
10931 | 19 |
TNetState = (netDisconnected, netConnecting, netLoggedIn); |
10929 | 20 |
TParserState = record |
21 |
cmd: TCmdType; |
|
22 |
l: LongInt; |
|
23 |
netState: TNetState; |
|
10931 | 24 |
buf: shortstring; |
25 |
bufpos: byte; |
|
10929 | 26 |
end; |
27 |
PHandler = procedure; |
|
28 |
||
29 |
var state: TParserState; |
|
30 |
||
31 |
// generated stuff here |
|
10931 | 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 | 33 |
|
10931 | 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 | 35 |
|
36 |
procedure handler_ASKPASSWORD; |
|
37 |
begin |
|
38 |
end; |
|
39 |
||
40 |
procedure handler_BANLIST; |
|
41 |
begin |
|
42 |
end; |
|
43 |
||
44 |
procedure handler_BYE; |
|
45 |
begin |
|
46 |
end; |
|
47 |
||
48 |
procedure handler_CHAT; |
|
49 |
begin |
|
50 |
end; |
|
51 |
||
52 |
procedure handler_CLIENT_FLAGS; |
|
53 |
begin |
|
54 |
end; |
|
55 |
||
56 |
procedure handler_CONNECTED; |
|
10953 | 57 |
var data: TCmdConnectedData; |
10929 | 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 | 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); |
|
10935 | 259 |
ipcToNet(s + endCmd); |
260 |
end; |
|
261 |
||
262 |
procedure netSendCallback(p: pointer; msg: PChar; len: Longword); |
|
263 |
begin |
|
264 |
// W A R N I N G: totally thread-unsafe due to use of sock variable |
|
265 |
SDLNet_TCP_Send(sock, msg, len); |
|
10898 | 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 | 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 | 273 |
state.bufpos:= 0; |
274 |
state.buf:= ''; |
|
275 |
||
276 |
state.l:= 0; |
|
277 |
state.netState:= netConnecting; |
|
278 |
||
10900 | 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 | 287 |
|
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. |