author | unc0rr |
Sun, 22 Nov 2015 18:06:32 +0300 | |
branch | qmlfrontend |
changeset 11425 | 2947f06e8533 |
parent 11424 | 86c13e5662f1 |
child 11429 | d96a37de1076 |
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; |
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
8 |
procedure sendNet(s: shortstring); |
11415 | 9 |
procedure sendNetLn(s: shortstring); |
10896
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
10 |
|
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
11 |
implementation |
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
12 |
uses SDLh, uFLIPC, uFLTypes, uFLUICallback, uFLNetTypes, uFLUtils; |
10931 | 13 |
|
10935 | 14 |
const endCmd: string = #10 + #10; |
10931 | 15 |
|
16 |
function getNextChar: char; forward; |
|
17 |
function getCurrChar: char; forward; |
|
18 |
||
10929 | 19 |
type |
11424 | 20 |
TNetState = (netDisconnected, netConnecting); |
10929 | 21 |
TParserState = record |
22 |
cmd: TCmdType; |
|
23 |
l: LongInt; |
|
24 |
netState: TNetState; |
|
10931 | 25 |
buf: shortstring; |
26 |
bufpos: byte; |
|
10929 | 27 |
end; |
28 |
PHandler = procedure; |
|
29 |
||
30 |
var state: TParserState; |
|
31 |
||
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
32 |
procedure handleTail; forward; |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
33 |
function getShortString: shortstring; forward; |
10929 | 34 |
|
11425
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
35 |
const letters: array[0..224] of char = ('A', 'S', 'K', 'P', 'A', 'S', 'S', 'W', |
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
36 |
'O', 'R', 'D', #10, 'B', 'A', 'N', 'L', 'I', 'S', 'T', #10, 'Y', 'E', #10, 'C', |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
37 |
'H', 'A', 'T', #10, 'L', 'I', 'E', 'N', 'T', '_', 'F', 'L', 'A', 'G', 'S', #10, |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
38 |
'O', 'N', 'N', 'E', 'C', 'T', 'E', 'D', #10, 'E', 'M', #10, 'R', 'R', 'O', 'R', |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
39 |
#10, 'H', 'H', '_', 'N', 'U', 'M', #10, 'I', 'N', 'F', 'O', #10, 'J', 'O', 'I', |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
40 |
'N', 'E', 'D', #10, 'I', 'N', 'G', #10, 'K', 'I', 'C', 'K', 'E', 'D', #10, 'L', |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
41 |
'E', 'F', 'T', #10, 'O', 'B', 'B', 'Y', ':', 'J', 'O', 'I', 'N', 'E', 'D', #10, |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
42 |
'L', 'E', 'F', 'T', #10, 'N', 'I', 'C', 'K', #10, 'O', 'T', 'I', 'C', 'E', #10, |
11419 | 43 |
'P', 'I', 'N', 'G', #10, 'R', 'O', 'T', 'O', #10, 'R', 'O', 'O', 'M', 'S', #10, |
11425
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
44 |
#10, 'A', 'D', 'D', #10, 'D', 'E', 'L', #10, 'U', 'P', 'D', #10, 'U', 'N', 'D', |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
45 |
'_', 'F', 'I', 'N', 'I', 'S', 'H', 'E', 'D', #10, 'U', 'N', '_', 'G', 'A', 'M', |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
46 |
'E', #10, 'S', 'E', 'R', 'V', 'E', 'R', '_', 'A', 'U', 'T', 'H', #10, 'M', 'E', |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
47 |
'S', 'S', 'A', 'G', 'E', #10, 'V', 'A', 'R', 'S', #10, 'T', 'E', 'A', 'M', '_', |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
48 |
'A', 'C', 'C', 'E', 'P', 'T', 'E', 'D', #10, 'C', 'O', 'L', 'O', 'R', #10, 'W', |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
49 |
'A', 'R', 'N', 'I', 'N', 'G', #10, #0, #10); |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
50 |
const commands: array[0..224] of integer = (12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
51 |
-42, 11, 7, 0, 0, 0, 0, 0, -41, 0, 0, -40, 26, 4, 0, 0, -39, 12, 0, 0, 0, 0, 0, |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
52 |
0, 0, 0, 0, 0, -38, 0, 0, 0, 0, 0, 0, 0, 0, -37, 8, 2, -36, 0, 0, 0, 0, -35, 7, |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
53 |
0, 0, 0, 0, 0, -34, 5, 0, 0, 0, -33, 11, 0, 0, 0, 3, 0, -32, 0, 0, 0, -31, 7, 0, |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
54 |
0, 0, 0, 0, -30, 22, 4, 0, 0, -29, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, -28, 0, 0, |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
55 |
0, 0, -27, 11, 4, 0, 0, -26, 0, 0, 0, 0, 0, -25, 10, 4, 0, 0, -24, 0, 0, 0, 0, |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
56 |
-23, 40, 31, 17, 0, 2, -22, 0, 4, 0, 0, -21, 4, 0, 0, -20, 0, 0, 0, -19, 0, 0, |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
57 |
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, |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
58 |
0, 5, 0, 0, 0, -16, 8, 0, 0, 0, 0, 0, 0, -15, 0, 0, 0, 0, -14, 20, 0, 0, 0, 0, |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
59 |
9, 0, 0, 0, 0, 0, 0, 0, -13, 0, 0, 0, 0, 0, -12, 8, 0, 0, 0, 0, 0, 0, -11, 0, |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
60 |
-10); |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
61 |
|
10929 | 62 |
|
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
63 |
procedure handler_; |
10929 | 64 |
begin |
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
65 |
sendUI(mtNetData, @state.cmd, sizeof(state.cmd)); |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
66 |
handleTail() |
10929 | 67 |
end; |
68 |
||
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
69 |
procedure handler_L; |
11423 | 70 |
var cmd: TCmdParamL; |
10929 | 71 |
begin |
11423 | 72 |
cmd.cmd:= state.cmd; |
73 |
cmd.str1:= getShortString; // FIXME long line |
|
74 |
if cmd.str1[0] = #0 then exit; |
|
75 |
sendUI(mtNetData, @cmd, sizeof(cmd)); |
|
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
76 |
handleTail() |
10929 | 77 |
end; |
78 |
||
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
79 |
procedure handler_ML; |
11423 | 80 |
var cmd: TCmdParamL; |
81 |
f: boolean; |
|
10929 | 82 |
begin |
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
83 |
writeln('handler_ML'); |
11423 | 84 |
sendUI(mtNetData, @state.cmd, sizeof(state.cmd)); |
85 |
cmd.cmd:= Succ(state.cmd); |
|
86 |
||
87 |
repeat |
|
88 |
cmd.str1:= getShortString; // FIXME long line |
|
89 |
f:= cmd.str1[0] <> #0; |
|
90 |
if f then |
|
91 |
sendUI(mtNetData, @cmd, sizeof(cmd)); |
|
92 |
until not f |
|
10929 | 93 |
end; |
94 |
||
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
95 |
procedure handler_MS; |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
96 |
var cmd: TCmdParamS; |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
97 |
f: boolean; |
10929 | 98 |
begin |
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
99 |
sendUI(mtNetData, @state.cmd, sizeof(state.cmd)); |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
100 |
cmd.cmd:= Succ(state.cmd); |
10929 | 101 |
|
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
102 |
repeat |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
103 |
cmd.str1:= getShortString; |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
104 |
f:= cmd.str1[0] <> #0; |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
105 |
if f then |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
106 |
sendUI(mtNetData, @cmd, sizeof(cmd)); |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
107 |
until not f |
10929 | 108 |
end; |
109 |
||
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
110 |
procedure handler_S; |
11423 | 111 |
var cmd: TCmdParamS; |
10929 | 112 |
begin |
11423 | 113 |
cmd.cmd:= state.cmd; |
114 |
cmd.str1:= getShortString; |
|
115 |
if cmd.str1[0] = #0 then exit; |
|
116 |
sendUI(mtNetData, @cmd, sizeof(cmd)); |
|
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
117 |
handleTail() |
10929 | 118 |
end; |
119 |
||
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
120 |
procedure handler_SL; |
11415 | 121 |
var cmd: TCmdParamSL; |
10929 | 122 |
begin |
11415 | 123 |
cmd.cmd:= state.cmd; |
124 |
cmd.str1:= getShortString; |
|
11420 | 125 |
if cmd.str1[0] = #0 then exit; |
11415 | 126 |
cmd.str2:= getShortString; // FIXME should be long string |
11420 | 127 |
if cmd.str2[0] = #0 then exit; |
11415 | 128 |
sendUI(mtNetData, @cmd, sizeof(cmd)); |
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
129 |
handleTail() |
10929 | 130 |
end; |
131 |
||
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
132 |
procedure handler_SMS; |
11424 | 133 |
var cmd: TCmdParamS; |
134 |
f: boolean; |
|
10929 | 135 |
begin |
11424 | 136 |
cmd.cmd:= state.cmd; |
137 |
cmd.str1:= getShortString; |
|
138 |
if cmd.str1[0] = #0 then exit; |
|
139 |
sendUI(mtNetData, @cmd, sizeof(cmd)); |
|
140 |
||
141 |
cmd.cmd:= Succ(state.cmd); |
|
142 |
repeat |
|
143 |
cmd.str1:= getShortString; |
|
144 |
f:= cmd.str1[0] <> #0; |
|
145 |
if f then |
|
146 |
sendUI(mtNetData, @cmd, sizeof(cmd)); |
|
147 |
until not f |
|
10929 | 148 |
end; |
149 |
||
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
150 |
procedure handler__i; |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
151 |
var cmd: TCmdParami; |
11420 | 152 |
s: shortstring; |
10929 | 153 |
begin |
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
154 |
writeln('handler__i'); |
11420 | 155 |
s:= getShortString(); |
156 |
if s[0] = #0 then exit; |
|
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
157 |
cmd.cmd:= state.cmd; |
11420 | 158 |
s:= getShortString(); |
159 |
if s[0] = #0 then exit; |
|
160 |
cmd.param1:= strToInt(s); |
|
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
161 |
sendUI(mtNetData, @cmd, sizeof(cmd)); |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
162 |
handleTail() |
10929 | 163 |
end; |
164 |
||
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
165 |
procedure handler_i; |
10929 | 166 |
begin |
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
167 |
writeln('handler_i'); |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
168 |
handleTail() |
10929 | 169 |
end; |
170 |
||
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
171 |
procedure handler__UNKNOWN_; |
10929 | 172 |
begin |
10931 | 173 |
writeln('[NET] Unknown cmd'); |
11419 | 174 |
handleTail() |
10929 | 175 |
end; |
176 |
||
11425
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
177 |
const handlers: array[0..32] of PHandler = (@handler__UNKNOWN_, @handler_L, |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
178 |
@handler_MS, @handler_S, @handler_SL, @handler_L, @handler_S, @handler_, |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
179 |
@handler_, @handler_MS, @handler_S, @handler_MS, @handler_MS, @handler_i, |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
180 |
@handler_MS, @handler_L, @handler_S, @handler_SL, @handler_MS, @handler_SMS, |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
181 |
@handler_, @handler_S, @handler_MS, @handler_MS, @handler_MS, @handler_L, |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
182 |
@handler_ML, @handler__i, @handler_SMS, @handler_SL, @handler_SL, @handler_MS, |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
183 |
@handler_S); |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
184 |
const net2cmd: array[0..32] of TCmdType = (cmd_WARNING, cmd_WARNING, |
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
185 |
cmd_TEAM_COLOR, cmd_TEAM_ACCEPTED, cmd_SERVER_VARS, cmd_SERVER_MESSAGE, |
11425
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
186 |
cmd_SERVER_AUTH, cmd_RUN_GAME, cmd_ROUND_FINISHED, cmd_ROOM_UPD, cmd_ROOM_DEL, |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
187 |
cmd_ROOM_ADD, cmd_ROOMS, cmd_PROTO, cmd_PING, cmd_NOTICE, cmd_NICK, |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
188 |
cmd_LOBBY_LEFT, cmd_LOBBY_JOINED, cmd_LEFT, cmd_KICKED, cmd_JOINING, cmd_JOINED, |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
189 |
cmd_INFO, cmd_HH_NUM, cmd_ERROR, cmd_EM, cmd_CONNECTED, cmd_CLIENT_FLAGS, |
2947f06e8533
Another approach to parsing two-lines protocol commands
unc0rr
parents:
11424
diff
changeset
|
190 |
cmd_CHAT, cmd_BYE, cmd_BANLIST, cmd_ASKPASSWORD); |
11418 | 191 |
|
10929 | 192 |
// end of generated stuff |
10931 | 193 |
procedure handleTail; |
194 |
var cnt: Longint; |
|
195 |
c: char; |
|
196 |
begin |
|
197 |
c:= getCurrChar; |
|
198 |
repeat |
|
199 |
if c = #10 then cnt:= 0 else cnt:= 1; |
|
200 |
repeat |
|
201 |
c:= getNextChar; |
|
202 |
inc(cnt) |
|
203 |
until (c = #0) or (c = #10); |
|
204 |
until (c = #0) or (cnt = 1) |
|
205 |
end; |
|
206 |
||
10896
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
207 |
var sock: PTCPSocket; |
10900 | 208 |
netReaderThread: PSDL_Thread; |
10931 | 209 |
|
210 |
function getCurrChar: char; |
|
211 |
begin |
|
212 |
getCurrChar:= state.buf[state.bufpos] |
|
213 |
end; |
|
10900 | 214 |
|
215 |
function getNextChar: char; |
|
216 |
var r: byte; |
|
217 |
begin |
|
10931 | 218 |
if state.bufpos < byte(state.buf[0]) then |
10900 | 219 |
begin |
10931 | 220 |
inc(state.bufpos); |
10900 | 221 |
end else |
222 |
begin |
|
10931 | 223 |
r:= SDLNet_TCP_Recv(sock, @state.buf[1], 255); |
10900 | 224 |
if r > 0 then |
225 |
begin |
|
10931 | 226 |
state.bufpos:= 1; |
227 |
state.buf[0]:= char(r); |
|
10900 | 228 |
end else |
229 |
begin |
|
10931 | 230 |
state.bufpos:= 0; |
231 |
state.buf[0]:= #0; |
|
10929 | 232 |
end |
10931 | 233 |
end; |
234 |
||
235 |
getNextChar:= state.buf[state.bufpos]; |
|
10900 | 236 |
end; |
10898 | 237 |
|
238 |
function netReader(data: pointer): LongInt; cdecl; export; |
|
10929 | 239 |
var c: char; |
10933 | 240 |
ipaddr: TIPAddress; |
10898 | 241 |
begin |
10933 | 242 |
netReader:= 0; |
243 |
||
244 |
if SDLNet_ResolveHost(ipaddr, PChar('netserver.hedgewars.org'), 46631) = 0 then |
|
245 |
sock:= SDLNet_TCP_Open(ipaddr); |
|
246 |
||
247 |
repeat |
|
248 |
c:= getNextChar; |
|
249 |
//writeln('>>>>> ', c, ' [', letters[state.l], '] ', commands[state.l]); |
|
250 |
if c = #0 then |
|
251 |
state.netState:= netDisconnected |
|
252 |
else |
|
253 |
begin |
|
254 |
while (letters[state.l] <> c) and (commands[state.l] > 0) do |
|
255 |
inc(state.l, commands[state.l]); |
|
256 |
||
257 |
if c = letters[state.l] then |
|
258 |
if commands[state.l] < 0 then |
|
10931 | 259 |
begin |
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
260 |
state.cmd:= net2cmd[-10 - commands[state.l]]; |
10933 | 261 |
writeln('[NET] ', state.cmd); |
10931 | 262 |
handlers[-10 - commands[state.l]](); |
11420 | 263 |
state.l:= 0 |
10931 | 264 |
end |
10933 | 265 |
else |
266 |
inc(state.l) |
|
10931 | 267 |
else |
10933 | 268 |
begin |
11419 | 269 |
handler__UNKNOWN_() |
10933 | 270 |
end |
10931 | 271 |
end |
10933 | 272 |
until state.netState = netDisconnected; |
10931 | 273 |
|
11415 | 274 |
SDLNet_TCP_Close(sock); |
10933 | 275 |
sock:= nil; |
276 |
||
277 |
writeln('[NET] netReader: disconnected'); |
|
10931 | 278 |
end; |
279 |
||
280 |
procedure sendNet(s: shortstring); |
|
281 |
begin |
|
282 |
writeln('[NET] Send: ', s); |
|
10935 | 283 |
ipcToNet(s + endCmd); |
284 |
end; |
|
285 |
||
11415 | 286 |
procedure sendNetLn(s: shortstring); |
287 |
begin |
|
288 |
writeln('[NET] Send: ', s); |
|
289 |
ipcToNet(s + #10); |
|
290 |
end; |
|
291 |
||
11413
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
292 |
function getShortString: shortstring; |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
293 |
var s: shortstring; |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
294 |
c: char; |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
295 |
begin |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
296 |
s[0]:= #0; |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
297 |
|
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
298 |
repeat |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
299 |
inc(s[0]); |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
300 |
s[byte(s[0])]:= getNextChar |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
301 |
until (s[0] = #255) or (s[byte(s[0])] = #10) or (s[byte(s[0])] = #0); |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
302 |
|
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
303 |
if s[byte(s[0])] = #10 then |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
304 |
dec(s[0]) |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
305 |
else |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
306 |
repeat c:= getNextChar until (c = #0) or (c = #10); |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
307 |
|
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
308 |
getShortString:= s |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
309 |
end; |
ffff8a0d1a76
Implement processing net commands in the main thread
unc0rr
parents:
11403
diff
changeset
|
310 |
|
10935 | 311 |
procedure netSendCallback(p: pointer; msg: PChar; len: Longword); |
312 |
begin |
|
313 |
// W A R N I N G: totally thread-unsafe due to use of sock variable |
|
314 |
SDLNet_TCP_Send(sock, msg, len); |
|
10898 | 315 |
end; |
10896
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
316 |
|
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
317 |
procedure connectOfficialServer; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
318 |
begin |
10898 | 319 |
if sock <> nil then |
10896
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
320 |
exit; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
321 |
|
10931 | 322 |
state.bufpos:= 0; |
323 |
state.buf:= ''; |
|
324 |
||
325 |
state.l:= 0; |
|
326 |
state.netState:= netConnecting; |
|
327 |
||
11403 | 328 |
netReaderThread:= SDL_CreateThread(@netReader, 'netReader', nil); |
11415 | 329 |
SDL_DetachThread(netReaderThread) |
10896
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
330 |
end; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
331 |
|
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
332 |
procedure initModule; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
333 |
begin |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
334 |
sock:= nil; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
335 |
|
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
336 |
SDLNet_Init; |
10935 | 337 |
|
338 |
registerNetCallback(nil, @netSendCallback); |
|
10896
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
339 |
end; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
340 |
|
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
341 |
procedure freeModule; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
342 |
begin |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
343 |
end; |
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
344 |
|
5a74923120d5
Start network support: only setting up a connection for now
unc0rr
parents:
diff
changeset
|
345 |
end. |