author | unc0rr |
Wed, 13 May 2015 23:21:40 +0300 | |
branch | qmlfrontend |
changeset 10929 | 8ebf01f75d9f |
parent 10900 | 6a805e822074 |
child 10931 | 384765cd0caf |
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; |
10929 | 11 |
type TCmdType = (cmd_ASKPASSWORD, cmd_BANLIST, cmd_BYE, cmd_CHAT, cmd_CLIENT_FLAGS, cmd_CONNECTED, cmd_EM, cmd_HH_NUM, cmd_INFO, cmd_JOINED, cmd_JOINING, cmd_KICKED, cmd_LEFT, cmd_LOBBY_JOINED, cmd_LOBBY_LEFT, cmd_NICK, cmd_NOTICE, cmd_PING, cmd_PROTO, cmd_ROOMS, cmd_ROUND_FINISHED, cmd_RUN_GAME, cmd_SERVER_AUTH, cmd_SERVER_MESSAGE, cmd_SERVER_VARS, cmd_TEAM_ACCEPTED, cmd_TEAM_COLOR, cmd_WARNING, cmd___UNKNOWN__); |
10896
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
12 |
|
10929 | 13 |
type |
14 |
TNetState = (netDisconnected, netLoggedIn); |
|
15 |
TParserState = record |
|
16 |
cmd: TCmdType; |
|
17 |
l: LongInt; |
|
18 |
netState: TNetState; |
|
19 |
end; |
|
20 |
PHandler = procedure; |
|
21 |
||
22 |
var state: TParserState; |
|
23 |
||
24 |
// generated stuff here |
|
25 |
const letters: array[0..235] of char = ('A', 'S', 'K', 'P', 'A', 'S', 'S', 'W', 'O', 'R', 'D', #10, #0, 'B', 'A', 'N', 'L', 'I', 'S', 'T', #10, #0, 'Y', 'E', #10, #0, 'C', 'H', 'A', 'T', #10, #0, 'L', 'I', 'E', 'N', 'T', '_', 'F', 'L', 'A', 'G', 'S', #10, #0, 'O', 'N', 'N', 'E', 'C', 'T', 'E', 'D', #10, #0, 'E', 'M', #10, #0, 'H', 'H', '_', 'N', 'U', 'M', #10, #0, 'I', 'N', 'F', 'O', #10, #0, 'J', 'O', 'I', 'N', 'E', 'D', #10, #0, 'I', 'N', 'G', #10, #0, 'K', 'I', 'C', 'K', 'E', 'D', #10, #0, 'L', 'E', 'F', 'T', #10, #0, 'O', 'B', 'B', 'Y', ':', 'J', 'O', 'I', 'N', 'E', 'D', #10, #0, 'L', 'E', 'F', 'T', #10, #0, 'N', 'I', 'C', 'K', #10, #0, 'O', 'T', 'I', 'C', 'E', #10, #0, 'P', 'I', 'N', 'G', #10, #0, 'R', 'O', 'T', 'O', #10, #0, 'R', 'O', 'O', 'M', 'S', #10, #0, 'U', 'N', 'D', '_', 'F', 'I', 'N', 'I', 'S', 'H', 'E', 'D', #10, #0, 'U', 'N', '_', 'G', 'A', 'M', 'E', #10, #0, 'S', 'E', 'R', 'V', 'E', 'R', '_', 'A', 'U', 'T', 'H', #10, #0, 'M', 'E', 'S', 'S', 'A', 'G', 'E', #10, #0, 'V', 'A', 'R', 'S', #10, #0, 'T', 'E', 'A', 'M', '_', 'A', 'C', 'C', 'E', 'P', 'T', 'E', 'D', #10, #0, 'C', 'O', 'L', 'O', 'R', #10, #0, 'W', 'A', 'R', 'N', 'I', 'N', 'G', #10, #0, '$', #10, #0); |
|
26 |
||
27 |
const commands: array[0..235] of integer = (13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -38, 13, 8, 0, 0, 0, 0, 0, 0, -37, 0, 0, 0, -36, 29, 5, 0, 0, 0, -35, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -34, 0, 0, 0, 0, 0, 0, 0, 0, 0, -33, 4, 0, 0, -32, 8, 0, 0, 0, 0, 0, 0, -31, 6, 0, 0, 0, 0, -30, 13, 0, 0, 0, 4, 0, 0, -29, 0, 0, 0, 0, -28, 8, 0, 0, 0, 0, 0, 0, -27, 25, 5, 0, 0, 0, -26, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, -25, 0, 0, 0, 0, 0, -24, 13, 5, 0, 0, 0, -23, 0, 0, 0, 0, 0, 0, -22, 12, 5, 0, 0, 0, -21, 0, 0, 0, 0, 0, -20, 30, 20, 5, 0, 0, 0, -19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -18, 0, 0, 0, 0, 0, 0, 0, 0, -17, 28, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, -16, 9, 0, 0, 0, 0, 0, 0, 0, -15, 0, 0, 0, 0, 0, -14, 22, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, -13, 0, 0, 0, 0, 0, 0, -12, 9, 0, 0, 0, 0, 0, 0, 0, -11, 0, 0, -10); |
|
28 |
||
29 |
procedure handler_ASKPASSWORD; |
|
30 |
begin |
|
31 |
state.cmd:= cmd_ASKPASSWORD; |
|
32 |
end; |
|
33 |
||
34 |
procedure handler_BANLIST; |
|
35 |
begin |
|
36 |
state.cmd:= cmd_BANLIST; |
|
37 |
end; |
|
38 |
||
39 |
procedure handler_BYE; |
|
40 |
begin |
|
41 |
state.cmd:= cmd_BYE; |
|
42 |
end; |
|
43 |
||
44 |
procedure handler_CHAT; |
|
45 |
begin |
|
46 |
state.cmd:= cmd_CHAT; |
|
47 |
end; |
|
48 |
||
49 |
procedure handler_CLIENT_FLAGS; |
|
50 |
begin |
|
51 |
state.cmd:= cmd_CLIENT_FLAGS; |
|
52 |
end; |
|
53 |
||
54 |
procedure handler_CONNECTED; |
|
55 |
begin |
|
56 |
state.cmd:= cmd_CONNECTED; |
|
57 |
end; |
|
58 |
||
59 |
procedure handler_EM; |
|
60 |
begin |
|
61 |
state.cmd:= cmd_EM; |
|
62 |
end; |
|
63 |
||
64 |
procedure handler_HH_NUM; |
|
65 |
begin |
|
66 |
state.cmd:= cmd_HH_NUM; |
|
67 |
end; |
|
68 |
||
69 |
procedure handler_INFO; |
|
70 |
begin |
|
71 |
state.cmd:= cmd_INFO; |
|
72 |
end; |
|
73 |
||
74 |
procedure handler_JOINED; |
|
75 |
begin |
|
76 |
state.cmd:= cmd_JOINED; |
|
77 |
end; |
|
78 |
||
79 |
procedure handler_JOINING; |
|
80 |
begin |
|
81 |
state.cmd:= cmd_JOINING; |
|
82 |
end; |
|
83 |
||
84 |
procedure handler_KICKED; |
|
85 |
begin |
|
86 |
state.cmd:= cmd_KICKED; |
|
87 |
end; |
|
88 |
||
89 |
procedure handler_LEFT; |
|
90 |
begin |
|
91 |
state.cmd:= cmd_LEFT; |
|
92 |
end; |
|
93 |
||
94 |
procedure handler_LOBBY_JOINED; |
|
95 |
begin |
|
96 |
state.cmd:= cmd_LOBBY_JOINED; |
|
97 |
end; |
|
98 |
||
99 |
procedure handler_LOBBY_LEFT; |
|
100 |
begin |
|
101 |
state.cmd:= cmd_LOBBY_LEFT; |
|
102 |
end; |
|
103 |
||
104 |
procedure handler_NICK; |
|
105 |
begin |
|
106 |
state.cmd:= cmd_NICK; |
|
107 |
end; |
|
108 |
||
109 |
procedure handler_NOTICE; |
|
110 |
begin |
|
111 |
state.cmd:= cmd_NOTICE; |
|
112 |
end; |
|
113 |
||
114 |
procedure handler_PING; |
|
115 |
begin |
|
116 |
state.cmd:= cmd_PING; |
|
117 |
end; |
|
118 |
||
119 |
procedure handler_PROTO; |
|
120 |
begin |
|
121 |
state.cmd:= cmd_PROTO; |
|
122 |
end; |
|
123 |
||
124 |
procedure handler_ROOMS; |
|
125 |
begin |
|
126 |
state.cmd:= cmd_ROOMS; |
|
127 |
end; |
|
128 |
||
129 |
procedure handler_ROUND_FINISHED; |
|
130 |
begin |
|
131 |
state.cmd:= cmd_ROUND_FINISHED; |
|
132 |
end; |
|
133 |
||
134 |
procedure handler_RUN_GAME; |
|
135 |
begin |
|
136 |
state.cmd:= cmd_RUN_GAME; |
|
137 |
end; |
|
138 |
||
139 |
procedure handler_SERVER_AUTH; |
|
140 |
begin |
|
141 |
state.cmd:= cmd_SERVER_AUTH; |
|
142 |
end; |
|
143 |
||
144 |
procedure handler_SERVER_MESSAGE; |
|
145 |
begin |
|
146 |
state.cmd:= cmd_SERVER_MESSAGE; |
|
147 |
end; |
|
148 |
||
149 |
procedure handler_SERVER_VARS; |
|
150 |
begin |
|
151 |
state.cmd:= cmd_SERVER_VARS; |
|
152 |
end; |
|
153 |
||
154 |
procedure handler_TEAM_ACCEPTED; |
|
155 |
begin |
|
156 |
state.cmd:= cmd_TEAM_ACCEPTED; |
|
157 |
end; |
|
158 |
||
159 |
procedure handler_TEAM_COLOR; |
|
160 |
begin |
|
161 |
state.cmd:= cmd_TEAM_COLOR; |
|
162 |
end; |
|
163 |
||
164 |
procedure handler_WARNING; |
|
165 |
begin |
|
166 |
state.cmd:= cmd_WARNING; |
|
167 |
end; |
|
168 |
||
169 |
procedure handler___UNKNOWN__; |
|
170 |
begin |
|
171 |
state.cmd:= cmd___UNKNOWN__; |
|
172 |
end; |
|
173 |
||
174 |
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); |
|
175 |
||
176 |
||
177 |
// end of generated stuff |
|
10896
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
178 |
var sock: PTCPSocket; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
179 |
fds: PSDLNet_SocketSet; |
10900 | 180 |
netReaderThread: PSDL_Thread; |
181 |
sockbuf: shortstring; |
|
182 |
sockbufpos: byte; |
|
183 |
||
184 |
function getNextChar: char; |
|
185 |
var r: byte; |
|
186 |
begin |
|
187 |
if sockbufpos < byte(sockbuf[0]) then |
|
188 |
begin |
|
189 |
inc(sockbufpos); |
|
190 |
getNextChar:= sockbuf[sockbufpos]; |
|
191 |
end else |
|
192 |
begin |
|
193 |
r:= SDLNet_TCP_Recv(sock, @sockbuf[1], 255); |
|
194 |
if r > 0 then |
|
195 |
begin |
|
196 |
sockbufpos:= 1; |
|
10929 | 197 |
sockbuf[0]:= char(r); |
10900 | 198 |
getNextChar:= sockbuf[1]; |
199 |
end else |
|
200 |
begin |
|
201 |
sockbufpos:= 0; |
|
202 |
sockbuf[0]:= #0; |
|
203 |
getNextChar:= #0 |
|
10929 | 204 |
end |
205 |
end |
|
10900 | 206 |
end; |
10898 | 207 |
|
208 |
function netReader(data: pointer): LongInt; cdecl; export; |
|
10929 | 209 |
var c: char; |
10898 | 210 |
begin |
10929 | 211 |
repeat |
212 |
c:= getNextChar; |
|
213 |
if c = #0 then |
|
214 |
state.netState:= netDisconnected; |
|
215 |
if c = letters[state.l] then |
|
216 |
if commands[state.l] < 0 then |
|
217 |
handlers[-10 - commands[state.l]]() |
|
218 |
else |
|
219 |
inc(state.l) |
|
220 |
else |
|
221 |
if commands[state.l] = 0 then |
|
222 |
// unknown cmd |
|
223 |
else |
|
224 |
repeat |
|
225 |
inc(state.l, commands[state.l]) |
|
226 |
until (letters[state.l] = c) or (commands[state.l] = 0) |
|
227 |
until state.netState = netDisconnected |
|
10898 | 228 |
end; |
10896
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
229 |
|
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
230 |
procedure connectOfficialServer; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
231 |
var ipaddr: TIPAddress; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
232 |
begin |
10898 | 233 |
if sock <> nil then |
10896
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
234 |
exit; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
235 |
|
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
236 |
if SDLNet_ResolveHost(ipaddr, PChar('netserver.hedgewars.org'), 46631) = 0 then |
10898 | 237 |
sock:= SDLNet_TCP_Open(ipaddr); |
238 |
||
10900 | 239 |
sockbufpos:= 0; |
240 |
sockbuf:= ''; |
|
241 |
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
|
242 |
end; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
243 |
|
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
244 |
procedure initModule; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
245 |
begin |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
246 |
sock:= nil; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
247 |
|
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
248 |
SDLNet_Init; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
249 |
fds:= SDLNet_AllocSocketSet(1); |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
250 |
end; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
251 |
|
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
252 |
procedure freeModule; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
253 |
begin |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
254 |
end; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
255 |
|
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
256 |
end. |