author | unc0rr |
Thu, 21 May 2015 00:19:06 +0300 | |
branch | qmlfrontend |
changeset 10951 | 89a7f617e091 |
parent 10935 | 3a65fcd7c335 |
child 10953 | 360e57620df3 |
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 |
10951
89a7f617e091
- Move protocol handling events to main thread through qt's main loop
unc0rr
parents:
10935
diff
changeset
|
10 |
uses SDLh, uFLIPC, uFLTypes, uFLUICallback; |
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 |
||
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 |
|
10951
89a7f617e091
- Move protocol handling events to main thread through qt's main loop
unc0rr
parents:
10935
diff
changeset
|
60 |
sendUI(mtNetData, nil, 0); |
10929 | 61 |
end; |
62 |
||
63 |
procedure handler_EM; |
|
64 |
begin |
|
65 |
end; |
|
66 |
||
67 |
procedure handler_HH_NUM; |
|
68 |
begin |
|
69 |
end; |
|
70 |
||
71 |
procedure handler_INFO; |
|
72 |
begin |
|
73 |
end; |
|
74 |
||
75 |
procedure handler_JOINED; |
|
76 |
begin |
|
77 |
end; |
|
78 |
||
79 |
procedure handler_JOINING; |
|
80 |
begin |
|
81 |
end; |
|
82 |
||
83 |
procedure handler_KICKED; |
|
84 |
begin |
|
85 |
end; |
|
86 |
||
87 |
procedure handler_LEFT; |
|
88 |
begin |
|
89 |
end; |
|
90 |
||
91 |
procedure handler_LOBBY_JOINED; |
|
92 |
begin |
|
93 |
end; |
|
94 |
||
95 |
procedure handler_LOBBY_LEFT; |
|
96 |
begin |
|
97 |
end; |
|
98 |
||
99 |
procedure handler_NICK; |
|
100 |
begin |
|
101 |
end; |
|
102 |
||
103 |
procedure handler_NOTICE; |
|
104 |
begin |
|
105 |
end; |
|
106 |
||
107 |
procedure handler_PING; |
|
108 |
begin |
|
10933 | 109 |
sendNet('PONG') |
10929 | 110 |
end; |
111 |
||
112 |
procedure handler_PROTO; |
|
113 |
begin |
|
114 |
end; |
|
115 |
||
116 |
procedure handler_ROOMS; |
|
117 |
begin |
|
118 |
end; |
|
119 |
||
120 |
procedure handler_ROUND_FINISHED; |
|
121 |
begin |
|
122 |
end; |
|
123 |
||
124 |
procedure handler_RUN_GAME; |
|
125 |
begin |
|
126 |
end; |
|
127 |
||
128 |
procedure handler_SERVER_AUTH; |
|
129 |
begin |
|
130 |
end; |
|
131 |
||
132 |
procedure handler_SERVER_MESSAGE; |
|
133 |
begin |
|
134 |
end; |
|
135 |
||
136 |
procedure handler_SERVER_VARS; |
|
137 |
begin |
|
138 |
end; |
|
139 |
||
140 |
procedure handler_TEAM_ACCEPTED; |
|
141 |
begin |
|
142 |
end; |
|
143 |
||
144 |
procedure handler_TEAM_COLOR; |
|
145 |
begin |
|
146 |
end; |
|
147 |
||
148 |
procedure handler_WARNING; |
|
149 |
begin |
|
150 |
end; |
|
151 |
||
152 |
procedure handler___UNKNOWN__; |
|
153 |
begin |
|
10931 | 154 |
writeln('[NET] Unknown cmd'); |
10929 | 155 |
end; |
156 |
||
157 |
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); |
|
158 |
||
159 |
||
160 |
// end of generated stuff |
|
10931 | 161 |
procedure handleTail; |
162 |
var cnt: Longint; |
|
163 |
c: char; |
|
164 |
begin |
|
165 |
state.l:= 0; |
|
166 |
||
167 |
c:= getCurrChar; |
|
168 |
repeat |
|
169 |
if c = #10 then cnt:= 0 else cnt:= 1; |
|
170 |
repeat |
|
171 |
c:= getNextChar; |
|
172 |
inc(cnt) |
|
173 |
until (c = #0) or (c = #10); |
|
174 |
until (c = #0) or (cnt = 1) |
|
175 |
end; |
|
176 |
||
10896
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
177 |
var sock: PTCPSocket; |
10900 | 178 |
netReaderThread: PSDL_Thread; |
10931 | 179 |
|
180 |
function getCurrChar: char; |
|
181 |
begin |
|
182 |
getCurrChar:= state.buf[state.bufpos] |
|
183 |
end; |
|
10900 | 184 |
|
185 |
function getNextChar: char; |
|
186 |
var r: byte; |
|
187 |
begin |
|
10931 | 188 |
if state.bufpos < byte(state.buf[0]) then |
10900 | 189 |
begin |
10931 | 190 |
inc(state.bufpos); |
10900 | 191 |
end else |
192 |
begin |
|
10931 | 193 |
r:= SDLNet_TCP_Recv(sock, @state.buf[1], 255); |
10900 | 194 |
if r > 0 then |
195 |
begin |
|
10931 | 196 |
state.bufpos:= 1; |
197 |
state.buf[0]:= char(r); |
|
10900 | 198 |
end else |
199 |
begin |
|
10931 | 200 |
state.bufpos:= 0; |
201 |
state.buf[0]:= #0; |
|
10929 | 202 |
end |
10931 | 203 |
end; |
204 |
||
205 |
getNextChar:= state.buf[state.bufpos]; |
|
10900 | 206 |
end; |
10898 | 207 |
|
10933 | 208 |
function netWriter(sock: PTCPSocket): LongInt; cdecl; export; |
209 |
begin |
|
210 |
netWriter:= 0; |
|
211 |
end; |
|
212 |
||
10898 | 213 |
function netReader(data: pointer): LongInt; cdecl; export; |
10929 | 214 |
var c: char; |
10933 | 215 |
ipaddr: TIPAddress; |
10898 | 216 |
begin |
10933 | 217 |
netReader:= 0; |
218 |
||
219 |
if SDLNet_ResolveHost(ipaddr, PChar('netserver.hedgewars.org'), 46631) = 0 then |
|
220 |
sock:= SDLNet_TCP_Open(ipaddr); |
|
221 |
||
222 |
SDL_CreateThread(@netWriter{$IFDEF SDL2}, 'netWriter'{$ENDIF}, sock); |
|
10931 | 223 |
|
10933 | 224 |
repeat |
225 |
c:= getNextChar; |
|
226 |
//writeln('>>>>> ', c, ' [', letters[state.l], '] ', commands[state.l]); |
|
227 |
if c = #0 then |
|
228 |
state.netState:= netDisconnected |
|
229 |
else |
|
230 |
begin |
|
231 |
while (letters[state.l] <> c) and (commands[state.l] > 0) do |
|
232 |
inc(state.l, commands[state.l]); |
|
233 |
||
234 |
if c = letters[state.l] then |
|
235 |
if commands[state.l] < 0 then |
|
10931 | 236 |
begin |
10933 | 237 |
state.cmd:= TCmdType(-10 - commands[state.l]); |
238 |
writeln('[NET] ', state.cmd); |
|
10931 | 239 |
handlers[-10 - commands[state.l]](); |
240 |
handleTail() |
|
241 |
end |
|
10933 | 242 |
else |
243 |
inc(state.l) |
|
10931 | 244 |
else |
10933 | 245 |
begin |
246 |
handler___UNKNOWN__(); |
|
247 |
handleTail() |
|
248 |
end |
|
10931 | 249 |
end |
10933 | 250 |
until state.netState = netDisconnected; |
10931 | 251 |
|
10933 | 252 |
sock:= nil; |
253 |
||
254 |
writeln('[NET] netReader: disconnected'); |
|
10931 | 255 |
end; |
256 |
||
257 |
procedure sendNet(s: shortstring); |
|
258 |
begin |
|
259 |
writeln('[NET] Send: ', s); |
|
10935 | 260 |
ipcToNet(s + endCmd); |
261 |
end; |
|
262 |
||
263 |
procedure netSendCallback(p: pointer; msg: PChar; len: Longword); |
|
264 |
begin |
|
265 |
// W A R N I N G: totally thread-unsafe due to use of sock variable |
|
266 |
SDLNet_TCP_Send(sock, msg, len); |
|
10898 | 267 |
end; |
10896
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
268 |
|
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
269 |
procedure connectOfficialServer; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
270 |
begin |
10898 | 271 |
if sock <> nil then |
10896
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
272 |
exit; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
273 |
|
10931 | 274 |
state.bufpos:= 0; |
275 |
state.buf:= ''; |
|
276 |
||
277 |
state.l:= 0; |
|
278 |
state.netState:= netConnecting; |
|
279 |
||
10900 | 280 |
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
|
281 |
end; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
282 |
|
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
283 |
procedure initModule; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
284 |
begin |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
285 |
sock:= nil; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
286 |
|
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
287 |
SDLNet_Init; |
10935 | 288 |
|
289 |
registerNetCallback(nil, @netSendCallback); |
|
10896
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
290 |
end; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
291 |
|
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
292 |
procedure freeModule; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
293 |
begin |
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 |
end. |