author | nemo |
Sun, 21 Nov 2010 01:26:31 -0500 | |
changeset 4392 | 95e27e12d0e9 |
parent 4389 | d1c65b60cd68 |
child 4403 | 0dfe26f48ec1 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
883 | 3 |
* Copyright (c) 2004, 2005, 2007, 2008 Andrey Korotaev <unC0Rr@gmail.com> |
4 | 4 |
* |
183 | 5 |
* This program is free software; you can redistribute it and/or modify |
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
4 | 8 |
* |
183 | 9 |
* This program is distributed in the hope that it will be useful, |
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
4 | 13 |
* |
183 | 14 |
* You should have received a copy of the GNU General Public License |
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
4 | 17 |
*) |
18 |
||
2630 | 19 |
{$INCLUDE "options.inc"} |
20 |
||
4 | 21 |
unit uIO; |
22 |
interface |
|
4376 | 23 |
uses SDLh, uTypes; |
4 | 24 |
|
3611 | 25 |
var ipcPort: Word = 0; |
2716
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
26 |
hiTicks: Word; |
4 | 27 |
|
3038 | 28 |
procedure initModule; |
29 |
procedure freeModule; |
|
2716
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
30 |
|
4377 | 31 |
procedure OutError(Msg: shortstring; isFatalError: boolean); |
32 |
procedure TryDo(Assert: boolean; Msg: shortstring; isFatal: boolean); inline; |
|
33 |
procedure SDLTry(Assert: boolean; isFatal: boolean); |
|
34 |
||
4 | 35 |
procedure SendIPC(s: shortstring); |
154
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
36 |
procedure SendIPCXY(cmd: char; X, Y: SmallInt); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
154
diff
changeset
|
37 |
procedure SendIPCRaw(p: pointer; len: Longword); |
4 | 38 |
procedure SendIPCAndWaitReply(s: shortstring); |
649
26166c87dc75
- Use 2 bytes for timestamp in net protocol, thus decreasing average packet size from 6 to 4
unc0rr
parents:
648
diff
changeset
|
39 |
procedure SendIPCTimeInc; |
2382 | 40 |
procedure SendKeepAliveMessage(Lag: Longword); |
1888
e76274ce7365
Add an ability to run engine without IPC connection.
unc0rr
parents:
1821
diff
changeset
|
41 |
procedure LoadRecordFromFile(fileName: shortstring); |
4376 | 42 |
procedure SendStat(sit: TStatInfoType; s: shortstring); |
159 | 43 |
procedure IPCWaitPongEvent; |
4 | 44 |
procedure IPCCheckSock; |
45 |
procedure InitIPC; |
|
46 |
procedure CloseIPC; |
|
47 |
procedure NetGetNextCmd; |
|
48 |
||
49 |
implementation |
|
4389 | 50 |
uses uConsole, uConsts, uChat, uTeams, uVariables, uCommands, uUtils; |
648
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
51 |
|
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
52 |
type PCmd = ^TCmd; |
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
53 |
TCmd = packed record |
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
54 |
Next: PCmd; |
2574
2f3e5c57359c
Fix (quite rare) spectators queue error when joining game with teams left the game.
unc0rr
parents:
2407
diff
changeset
|
55 |
loTime: Word; |
648
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
56 |
case byte of |
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
57 |
1: (len: byte; |
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
58 |
cmd: Char; |
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
59 |
X, Y: SmallInt); |
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
60 |
2: (str: shortstring); |
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
61 |
end; |
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
62 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
63 |
var IPCSock: PTCPSocket; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
64 |
fds: PSDLNet_SocketSet; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
65 |
isPonged: boolean; |
4 | 66 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
67 |
headcmd: PCmd; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
68 |
lastcmd: PCmd; |
2382 | 69 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
70 |
SendEmptyPacketTicks: LongWord; |
2382 | 71 |
|
648
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
72 |
|
4377 | 73 |
procedure OutError(Msg: shortstring; isFatalError: boolean); |
74 |
begin |
|
75 |
WriteLnToConsole(Msg); |
|
76 |
if isFatalError then |
|
77 |
begin |
|
78 |
SendIPC('E' + GetLastConsoleLine); |
|
79 |
SDL_Quit; |
|
80 |
halt(1) |
|
81 |
end |
|
82 |
end; |
|
83 |
||
84 |
procedure TryDo(Assert: boolean; Msg: shortstring; isFatal: boolean); |
|
85 |
begin |
|
86 |
if not Assert then OutError(Msg, isFatal) |
|
87 |
end; |
|
88 |
||
89 |
procedure SDLTry(Assert: boolean; isFatal: boolean); |
|
90 |
begin |
|
91 |
if not Assert then OutError(SDL_GetError, isFatal) |
|
92 |
end; |
|
93 |
||
2574
2f3e5c57359c
Fix (quite rare) spectators queue error when joining game with teams left the game.
unc0rr
parents:
2407
diff
changeset
|
94 |
function AddCmd(Time: Word; str: shortstring): PCmd; |
2695 | 95 |
var command: PCmd; |
648
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
96 |
begin |
2695 | 97 |
new(command); |
98 |
FillChar(command^, sizeof(TCmd), 0); |
|
99 |
command^.loTime:= Time; |
|
100 |
command^.str:= str; |
|
101 |
if command^.cmd <> 'F' then dec(command^.len, 2); // cut timestamp |
|
648
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
102 |
if headcmd = nil then |
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
103 |
begin |
2695 | 104 |
headcmd:= command; |
105 |
lastcmd:= command |
|
648
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
106 |
end else |
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
107 |
begin |
2695 | 108 |
lastcmd^.Next:= command; |
109 |
lastcmd:= command |
|
648
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
110 |
end; |
2695 | 111 |
AddCmd:= command; |
648
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
112 |
end; |
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
113 |
|
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
114 |
procedure RemoveCmd; |
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
115 |
var tmp: PCmd; |
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
116 |
begin |
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
117 |
TryDo(headcmd <> nil, 'Engine bug: headcmd = nil', true); |
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
118 |
tmp:= headcmd; |
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
119 |
headcmd:= headcmd^.Next; |
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
120 |
if headcmd = nil then lastcmd:= nil; |
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
121 |
dispose(tmp) |
fc5234aa6493
Use list instead of array (no limit for network packets amount)
unc0rr
parents:
526
diff
changeset
|
122 |
end; |
4 | 123 |
|
124 |
procedure InitIPC; |
|
125 |
var ipaddr: TIPAddress; |
|
126 |
begin |
|
127 |
WriteToConsole('Init SDL_Net... '); |
|
128 |
SDLTry(SDLNet_Init = 0, true); |
|
129 |
fds:= SDLNet_AllocSocketSet(1); |
|
130 |
SDLTry(fds <> nil, true); |
|
131 |
WriteLnToConsole(msgOK); |
|
132 |
WriteToConsole('Establishing IPC connection... '); |
|
3407 | 133 |
{$HINTS OFF} |
4 | 134 |
SDLTry(SDLNet_ResolveHost(ipaddr, '127.0.0.1', ipcPort) = 0, true); |
3407 | 135 |
{$HINTS ON} |
4 | 136 |
IPCSock:= SDLNet_TCP_Open(ipaddr); |
137 |
SDLTry(IPCSock <> nil, true); |
|
138 |
WriteLnToConsole(msgOK) |
|
139 |
end; |
|
140 |
||
141 |
procedure CloseIPC; |
|
142 |
begin |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
143 |
SDLNet_FreeSocketSet(fds); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
144 |
SDLNet_TCP_Close(IPCSock); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
145 |
SDLNet_Quit(); |
4 | 146 |
end; |
147 |
||
148 |
procedure ParseIPCCommand(s: shortstring); |
|
649
26166c87dc75
- Use 2 bytes for timestamp in net protocol, thus decreasing average packet size from 6 to 4
unc0rr
parents:
648
diff
changeset
|
149 |
var loTicks: Word; |
4 | 150 |
begin |
151 |
case s[1] of |
|
22 | 152 |
'!': begin {$IFDEF DEBUGFILE}AddFileLog('Ping? Pong!');{$ENDIF}isPonged:= true; end; |
4 | 153 |
'?': SendIPC('!'); |
351 | 154 |
'e': ParseCommand(copy(s, 2, Length(s) - 1), true); |
4 | 155 |
'E': OutError(copy(s, 2, Length(s) - 1), true); |
156 |
'W': OutError(copy(s, 2, Length(s) - 1), false); |
|
4389 | 157 |
'M': ParseCommand('landcheck ' + s, true); |
4 | 158 |
'T': case s[2] of |
159 |
'L': GameType:= gmtLocal; |
|
160 |
'D': GameType:= gmtDemo; |
|
161 |
'N': GameType:= gmtNet; |
|
72 | 162 |
'S': GameType:= gmtSave; |
4 | 163 |
else OutError(errmsgIncorrectUse + ' IPC "T" :' + s[2], true) end; |
164 |
else |
|
649
26166c87dc75
- Use 2 bytes for timestamp in net protocol, thus decreasing average packet size from 6 to 4
unc0rr
parents:
648
diff
changeset
|
165 |
loTicks:= SDLNet_Read16(@s[byte(s[0]) - 1]); |
2574
2f3e5c57359c
Fix (quite rare) spectators queue error when joining game with teams left the game.
unc0rr
parents:
2407
diff
changeset
|
166 |
AddCmd(loTicks, s); |
4374 | 167 |
{$IFDEF DEBUGFILE}AddFileLog('IPC in: '+s[1]+' ticks '+IntToStr(lastcmd^.loTime));{$ENDIF} |
4 | 168 |
end |
169 |
end; |
|
170 |
||
171 |
procedure IPCCheckSock; |
|
2905 | 172 |
const ss: shortstring = ''; |
371 | 173 |
var i: LongInt; |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
174 |
buf: array[0..255] of byte; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
175 |
s: shortstring absolute buf; |
4 | 176 |
begin |
1888
e76274ce7365
Add an ability to run engine without IPC connection.
unc0rr
parents:
1821
diff
changeset
|
177 |
if IPCSock = nil then |
e76274ce7365
Add an ability to run engine without IPC connection.
unc0rr
parents:
1821
diff
changeset
|
178 |
exit; |
e76274ce7365
Add an ability to run engine without IPC connection.
unc0rr
parents:
1821
diff
changeset
|
179 |
|
351 | 180 |
fds^.numsockets:= 0; |
4 | 181 |
SDLNet_AddSocket(fds, IPCSock); |
182 |
||
183 |
while SDLNet_CheckSockets(fds, 0) > 0 do |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
184 |
begin |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
185 |
i:= SDLNet_TCP_Recv(IPCSock, @buf[1], 255 - Length(ss)); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
186 |
if i > 0 then |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
187 |
begin |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
188 |
buf[0]:= i; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
189 |
ss:= ss + s; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
190 |
while (Length(ss) > 1) and (Length(ss) > byte(ss[1])) do |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
191 |
begin |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
192 |
ParseIPCCommand(copy(ss, 2, byte(ss[1]))); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
193 |
Delete(ss, 1, Succ(byte(ss[1]))) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
194 |
end |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
195 |
end else OutError('IPC connection lost', true) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
196 |
end; |
1888
e76274ce7365
Add an ability to run engine without IPC connection.
unc0rr
parents:
1821
diff
changeset
|
197 |
end; |
e76274ce7365
Add an ability to run engine without IPC connection.
unc0rr
parents:
1821
diff
changeset
|
198 |
|
e76274ce7365
Add an ability to run engine without IPC connection.
unc0rr
parents:
1821
diff
changeset
|
199 |
procedure LoadRecordFromFile(fileName: shortstring); |
e76274ce7365
Add an ability to run engine without IPC connection.
unc0rr
parents:
1821
diff
changeset
|
200 |
var f: file; |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
201 |
ss: shortstring = ''; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
202 |
i: LongInt; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
203 |
buf: array[0..255] of byte; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
204 |
s: shortstring absolute buf; |
1888
e76274ce7365
Add an ability to run engine without IPC connection.
unc0rr
parents:
1821
diff
changeset
|
205 |
begin |
2630 | 206 |
|
207 |
// set RDNLY on file open |
|
208 |
filemode:= 0; |
|
2240
7ce9e6b7be3b
-Removal of older WAV files, now useless thanks to OpenAL
koda
parents:
2154
diff
changeset
|
209 |
|
1888
e76274ce7365
Add an ability to run engine without IPC connection.
unc0rr
parents:
1821
diff
changeset
|
210 |
assign(f, fileName); |
e76274ce7365
Add an ability to run engine without IPC connection.
unc0rr
parents:
1821
diff
changeset
|
211 |
reset(f, 1); |
e76274ce7365
Add an ability to run engine without IPC connection.
unc0rr
parents:
1821
diff
changeset
|
212 |
|
3407 | 213 |
i:= 0; // avoid compiler hints |
214 |
buf[0]:= 0; |
|
1888
e76274ce7365
Add an ability to run engine without IPC connection.
unc0rr
parents:
1821
diff
changeset
|
215 |
repeat |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
216 |
BlockRead(f, buf[1], 255 - Length(ss), i); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
217 |
if i > 0 then |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
218 |
begin |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
219 |
buf[0]:= i; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
220 |
ss:= ss + s; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
221 |
while (Length(ss) > 1)and(Length(ss) > byte(ss[1])) do |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
222 |
begin |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
223 |
ParseIPCCommand(copy(ss, 2, byte(ss[1]))); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
224 |
Delete(ss, 1, Succ(byte(ss[1]))) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
225 |
end |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
226 |
end |
1888
e76274ce7365
Add an ability to run engine without IPC connection.
unc0rr
parents:
1821
diff
changeset
|
227 |
until i = 0; |
e76274ce7365
Add an ability to run engine without IPC connection.
unc0rr
parents:
1821
diff
changeset
|
228 |
|
e76274ce7365
Add an ability to run engine without IPC connection.
unc0rr
parents:
1821
diff
changeset
|
229 |
close(f) |
4 | 230 |
end; |
231 |
||
4376 | 232 |
procedure SendStat(sit: TStatInfoType; s: shortstring); |
233 |
const stc: array [TStatInfoType] of char = 'rDkKHTPsSB'; |
|
234 |
var buf: shortstring; |
|
235 |
begin |
|
236 |
buf:= 'i' + stc[sit] + s; |
|
237 |
SendIPCRaw(@buf[0], length(buf) + 1) |
|
238 |
end; |
|
239 |
||
4377 | 240 |
|
4 | 241 |
procedure SendIPC(s: shortstring); |
242 |
begin |
|
49 | 243 |
if IPCSock <> nil then |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
244 |
begin |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
245 |
SendEmptyPacketTicks:= 0; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
246 |
if s[0]>#251 then s[0]:= #251; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
247 |
SDLNet_Write16(GameTicks, @s[Succ(byte(s[0]))]); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
248 |
{$IFDEF DEBUGFILE}AddFileLog('IPC send: '+ s[1]);{$ENDIF} |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
249 |
inc(s[0], 2); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
250 |
SDLNet_TCP_Send(IPCSock, @s, Succ(byte(s[0]))) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
251 |
end |
4 | 252 |
end; |
253 |
||
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
154
diff
changeset
|
254 |
procedure SendIPCRaw(p: pointer; len: Longword); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
154
diff
changeset
|
255 |
begin |
208 | 256 |
if IPCSock <> nil then |
257 |
begin |
|
258 |
SDLNet_TCP_Send(IPCSock, p, len) |
|
259 |
end |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
154
diff
changeset
|
260 |
end; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
154
diff
changeset
|
261 |
|
154
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
262 |
procedure SendIPCXY(cmd: char; X, Y: SmallInt); |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
263 |
var s: shortstring; |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
264 |
begin |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
265 |
s[0]:= #5; |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
266 |
s[1]:= cmd; |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
267 |
SDLNet_Write16(X, @s[2]); |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
268 |
SDLNet_Write16(Y, @s[4]); |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
269 |
SendIPC(s) |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
270 |
end; |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
271 |
|
649
26166c87dc75
- Use 2 bytes for timestamp in net protocol, thus decreasing average packet size from 6 to 4
unc0rr
parents:
648
diff
changeset
|
272 |
procedure SendIPCTimeInc; |
26166c87dc75
- Use 2 bytes for timestamp in net protocol, thus decreasing average packet size from 6 to 4
unc0rr
parents:
648
diff
changeset
|
273 |
const timeinc: shortstring = '#'; |
26166c87dc75
- Use 2 bytes for timestamp in net protocol, thus decreasing average packet size from 6 to 4
unc0rr
parents:
648
diff
changeset
|
274 |
begin |
3971 | 275 |
{$IFDEF DEBUGFILE}AddFileLog('IPC Send #');{$ENDIF} |
649
26166c87dc75
- Use 2 bytes for timestamp in net protocol, thus decreasing average packet size from 6 to 4
unc0rr
parents:
648
diff
changeset
|
276 |
SendIPCRaw(@timeinc, 2) |
26166c87dc75
- Use 2 bytes for timestamp in net protocol, thus decreasing average packet size from 6 to 4
unc0rr
parents:
648
diff
changeset
|
277 |
end; |
26166c87dc75
- Use 2 bytes for timestamp in net protocol, thus decreasing average packet size from 6 to 4
unc0rr
parents:
648
diff
changeset
|
278 |
|
159 | 279 |
procedure IPCWaitPongEvent; |
4 | 280 |
begin |
281 |
isPonged:= false; |
|
282 |
repeat |
|
283 |
IPCCheckSock; |
|
284 |
SDL_Delay(1) |
|
285 |
until isPonged |
|
286 |
end; |
|
287 |
||
159 | 288 |
procedure SendIPCAndWaitReply(s: shortstring); |
289 |
begin |
|
290 |
SendIPC(s); |
|
291 |
SendIPC('?'); |
|
292 |
IPCWaitPongEvent |
|
293 |
end; |
|
294 |
||
2382 | 295 |
procedure SendKeepAliveMessage(Lag: Longword); |
296 |
begin |
|
297 |
inc(SendEmptyPacketTicks, Lag); |
|
298 |
if (SendEmptyPacketTicks >= cSendEmptyPacketTime) then |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
299 |
SendIPC('+') |
2382 | 300 |
end; |
301 |
||
4 | 302 |
procedure NetGetNextCmd; |
303 |
var tmpflag: boolean; |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
304 |
s: shortstring; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
305 |
x16, y16: SmallInt; |
4 | 306 |
begin |
977
fdbf2a5c1ad7
Fix an oops with chat string appearing between two net commands (it's very rare and random condition, but I caught it while playing via net)
unc0rr
parents:
946
diff
changeset
|
307 |
tmpflag:= true; |
fdbf2a5c1ad7
Fix an oops with chat string appearing between two net commands (it's very rare and random condition, but I caught it while playing via net)
unc0rr
parents:
946
diff
changeset
|
308 |
|
1351
aa7aefec5c1b
Add partial implementation of handling disconnects while playing via network
unc0rr
parents:
1066
diff
changeset
|
309 |
while (headcmd <> nil) |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
310 |
and (tmpflag or (headcmd^.cmd = '#')) // '#' is the only cmd which can be sent within same tick after 'N' |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
311 |
and ((GameTicks = hiTicks shl 16 + headcmd^.loTime) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
312 |
or (headcmd^.cmd = 's') // for these commands time is not specified |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
313 |
or (headcmd^.cmd = '#') |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
314 |
or (headcmd^.cmd = 'b') |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
315 |
or (headcmd^.cmd = 'F')) do |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
316 |
begin |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
317 |
case headcmd^.cmd of |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
318 |
'+': ; // do nothing - it is just an empty packet |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
319 |
'#': inc(hiTicks); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
320 |
'L': ParseCommand('+left', true); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
321 |
'l': ParseCommand('-left', true); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
322 |
'R': ParseCommand('+right', true); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
323 |
'r': ParseCommand('-right', true); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
324 |
'U': ParseCommand('+up', true); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
325 |
'u': ParseCommand('-up', true); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
326 |
'D': ParseCommand('+down', true); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
327 |
'd': ParseCommand('-down', true); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
328 |
'Z': ParseCommand('+precise', true); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
329 |
'z': ParseCommand('-precise', true); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
330 |
'A': ParseCommand('+attack', true); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
331 |
'a': ParseCommand('-attack', true); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
332 |
'S': ParseCommand('switch', true); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
333 |
'j': ParseCommand('ljump', true); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
334 |
'J': ParseCommand('hjump', true); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
335 |
',': ParseCommand('skip', true); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
336 |
's': begin |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
337 |
s:= copy(headcmd^.str, 2, Pred(headcmd^.len)); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
338 |
AddChatString(s); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
339 |
WriteLnToConsole(s) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
340 |
end; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
341 |
'b': begin |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
342 |
s:= copy(headcmd^.str, 2, Pred(headcmd^.len)); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
343 |
AddChatString(#4 + s); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
344 |
WriteLnToConsole(s) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
345 |
end; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
346 |
'F': TeamGone(copy(headcmd^.str, 2, Pred(headcmd^.len))); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
347 |
'N': begin |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
348 |
tmpflag:= false; |
4374 | 349 |
{$IFDEF DEBUGFILE}AddFileLog('got cmd "N": time '+IntToStr(hiTicks shl 16 + headcmd^.loTime)){$ENDIF} |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
350 |
end; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
351 |
'p': begin |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
352 |
x16:= SDLNet_Read16(@(headcmd^.X)); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
353 |
y16:= SDLNet_Read16(@(headcmd^.Y)); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
354 |
doPut(x16, y16, false) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
355 |
end; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
356 |
'P': begin |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
357 |
// these are equations solved for CursorPoint |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
358 |
// SDLNet_Read16(@(headcmd^.X)) == CursorPoint.X - WorldDx; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
359 |
// SDLNet_Read16(@(headcmd^.Y)) == cScreenHeight - CursorPoint.Y - WorldDy; |
2969
79024988792e
This *should* solve issue w/ cursor moving around in ammo menu while not your turn NEEDS TESTING, changing uIO makes me nervous
nemo
parents:
2948
diff
changeset
|
360 |
if not (CurrentTeam^.ExtDriven and bShowAmmoMenu) then |
79024988792e
This *should* solve issue w/ cursor moving around in ammo menu while not your turn NEEDS TESTING, changing uIO makes me nervous
nemo
parents:
2948
diff
changeset
|
361 |
begin |
79024988792e
This *should* solve issue w/ cursor moving around in ammo menu while not your turn NEEDS TESTING, changing uIO makes me nervous
nemo
parents:
2948
diff
changeset
|
362 |
CursorPoint.X:= SmallInt(SDLNet_Read16(@(headcmd^.X))) + WorldDx; |
79024988792e
This *should* solve issue w/ cursor moving around in ammo menu while not your turn NEEDS TESTING, changing uIO makes me nervous
nemo
parents:
2948
diff
changeset
|
363 |
CursorPoint.Y:= cScreenHeight - SmallInt(SDLNet_Read16(@(headcmd^.Y))) - WorldDy |
79024988792e
This *should* solve issue w/ cursor moving around in ammo menu while not your turn NEEDS TESTING, changing uIO makes me nervous
nemo
parents:
2948
diff
changeset
|
364 |
end |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
365 |
end; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
366 |
'w': ParseCommand('setweap ' + headcmd^.str[2], true); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
367 |
't': ParseCommand('taunt ' + headcmd^.str[2], true); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
368 |
'h': ParseCommand('hogsay ' + copy(headcmd^.str, 2, Pred(headcmd^.len)), true); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
369 |
'1'..'5': ParseCommand('timer ' + headcmd^.cmd, true); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
370 |
#128..char(128 + cMaxSlotIndex): ParseCommand('slot ' + char(byte(headcmd^.cmd) - 79), true) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
371 |
else |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
372 |
OutError('Unexpected protocol command: ' + headcmd^.cmd, True) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
373 |
end; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
374 |
RemoveCmd |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
375 |
end; |
351 | 376 |
|
2574
2f3e5c57359c
Fix (quite rare) spectators queue error when joining game with teams left the game.
unc0rr
parents:
2407
diff
changeset
|
377 |
if (headcmd <> nil) and tmpflag and (not CurrentTeam^.hasGone) then |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
378 |
TryDo(GameTicks < hiTicks shl 16 + headcmd^.loTime, |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
379 |
'oops, queue error. in buffer: ' + headcmd^.cmd + |
4374 | 380 |
' (' + IntToStr(GameTicks) + ' > ' + |
381 |
IntToStr(hiTicks shl 16 + headcmd^.loTime) + ')', |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
382 |
true); |
4 | 383 |
|
2066 | 384 |
isInLag:= (headcmd = nil) and tmpflag and (not CurrentTeam^.hasGone); |
2043
1f2b91b5e7ef
Don't accept 'team gone' command right after 'next turn' cmd
unc0rr
parents:
2017
diff
changeset
|
385 |
|
1560
e140bc57ff68
Quick replay round to spectators until current move
unc0rr
parents:
1432
diff
changeset
|
386 |
if isInLag then fastUntilLag:= false |
4 | 387 |
end; |
388 |
||
3038 | 389 |
procedure initModule; |
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2695
diff
changeset
|
390 |
begin |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
391 |
IPCSock:= nil; |
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2695
diff
changeset
|
392 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
393 |
headcmd:= nil; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
394 |
lastcmd:= nil; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
395 |
isPonged:= false; // was const |
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2695
diff
changeset
|
396 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
397 |
hiTicks:= 0; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2905
diff
changeset
|
398 |
SendEmptyPacketTicks:= 0; |
2716
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
399 |
end; |
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
400 |
|
3038 | 401 |
procedure freeModule; |
2716
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
402 |
begin |
3611 | 403 |
ipcPort:= 0; |
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2695
diff
changeset
|
404 |
end; |
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2695
diff
changeset
|
405 |
|
4 | 406 |
end. |