author | unc0rr |
Mon, 14 May 2007 18:58:54 +0000 | |
changeset 513 | 69e06d710d46 |
parent 392 | 25be13ae29da |
child 526 | e3689572bb15 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
3 |
* Copyright (c) 2004, 2005 Andrey Korotaev <unC0Rr@gmail.com> |
|
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 |
||
19 |
unit uIO; |
|
20 |
interface |
|
21 |
uses SDLh; |
|
22 |
{$INCLUDE options.inc} |
|
23 |
||
24 |
const ipcPort: Word = 0; |
|
25 |
||
26 |
procedure SendIPC(s: shortstring); |
|
154
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
27 |
procedure SendIPCXY(cmd: char; X, Y: SmallInt); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
154
diff
changeset
|
28 |
procedure SendIPCRaw(p: pointer; len: Longword); |
4 | 29 |
procedure SendIPCAndWaitReply(s: shortstring); |
159 | 30 |
procedure IPCWaitPongEvent; |
4 | 31 |
procedure IPCCheckSock; |
32 |
procedure InitIPC; |
|
33 |
procedure CloseIPC; |
|
34 |
procedure NetGetNextCmd; |
|
35 |
||
36 |
implementation |
|
196 | 37 |
uses uConsole, uConsts, uWorld, uMisc, uLand; |
4 | 38 |
const isPonged: boolean = false; |
49 | 39 |
var IPCSock: PTCPSocket = nil; |
4 | 40 |
fds: PSDLNet_SocketSet; |
41 |
||
42 |
extcmd: array[word] of packed record |
|
43 |
Time: LongWord; |
|
44 |
case byte of |
|
45 |
1: (len: byte; |
|
46 |
cmd: Char; |
|
95 | 47 |
X, Y: SmallInt); |
4 | 48 |
2: (str: shortstring); |
49 |
end; |
|
371 | 50 |
cmdcurpos: LongInt = 0; |
51 |
cmdendpos: LongInt = -1; |
|
4 | 52 |
|
53 |
procedure InitIPC; |
|
54 |
var ipaddr: TIPAddress; |
|
55 |
begin |
|
56 |
WriteToConsole('Init SDL_Net... '); |
|
57 |
SDLTry(SDLNet_Init = 0, true); |
|
58 |
fds:= SDLNet_AllocSocketSet(1); |
|
59 |
SDLTry(fds <> nil, true); |
|
60 |
WriteLnToConsole(msgOK); |
|
61 |
WriteToConsole('Establishing IPC connection... '); |
|
62 |
SDLTry(SDLNet_ResolveHost(ipaddr, '127.0.0.1', ipcPort) = 0, true); |
|
63 |
IPCSock:= SDLNet_TCP_Open(ipaddr); |
|
64 |
SDLTry(IPCSock <> nil, true); |
|
65 |
WriteLnToConsole(msgOK) |
|
66 |
end; |
|
67 |
||
68 |
procedure CloseIPC; |
|
69 |
begin |
|
70 |
SDLNet_FreeSocketSet(fds); |
|
71 |
SDLNet_TCP_Close(IPCSock); |
|
72 |
SDLNet_Quit |
|
73 |
end; |
|
74 |
||
75 |
procedure ParseIPCCommand(s: shortstring); |
|
76 |
begin |
|
77 |
case s[1] of |
|
22 | 78 |
'!': begin {$IFDEF DEBUGFILE}AddFileLog('Ping? Pong!');{$ENDIF}isPonged:= true; end; |
4 | 79 |
'?': SendIPC('!'); |
351 | 80 |
'e': ParseCommand(copy(s, 2, Length(s) - 1), true); |
4 | 81 |
'E': OutError(copy(s, 2, Length(s) - 1), true); |
82 |
'W': OutError(copy(s, 2, Length(s) - 1), false); |
|
367 | 83 |
'M': CheckLandDigest(s); |
4 | 84 |
'T': case s[2] of |
85 |
'L': GameType:= gmtLocal; |
|
86 |
'D': GameType:= gmtDemo; |
|
87 |
'N': GameType:= gmtNet; |
|
72 | 88 |
'S': GameType:= gmtSave; |
4 | 89 |
else OutError(errmsgIncorrectUse + ' IPC "T" :' + s[2], true) end; |
90 |
else |
|
91 |
inc(cmdendpos); |
|
154
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
92 |
extcmd[cmdendpos].Time := SDLNet_Read32(@s[byte(s[0]) - 3]); |
4 | 93 |
extcmd[cmdendpos].str := s; |
94 |
{$IFDEF DEBUGFILE}AddFileLog('IPC in: '+s[1]+' ticks '+inttostr(extcmd[cmdendpos].Time)+' at '+inttostr(cmdendpos));{$ENDIF} |
|
95 |
dec(extcmd[cmdendpos].len, 4) |
|
96 |
end |
|
97 |
end; |
|
98 |
||
99 |
procedure IPCCheckSock; |
|
100 |
const ss: string = ''; |
|
371 | 101 |
var i: LongInt; |
4 | 102 |
buf: array[0..255] of byte; |
103 |
s: shortstring absolute buf; |
|
104 |
begin |
|
351 | 105 |
fds^.numsockets:= 0; |
4 | 106 |
SDLNet_AddSocket(fds, IPCSock); |
107 |
||
108 |
while SDLNet_CheckSockets(fds, 0) > 0 do |
|
109 |
begin |
|
351 | 110 |
i:= SDLNet_TCP_Recv(IPCSock, @buf[1], 255 - Length(ss)); |
4 | 111 |
if i > 0 then |
112 |
begin |
|
113 |
buf[0]:= i; |
|
114 |
ss:= ss + s; |
|
115 |
while (Length(ss) > 1)and(Length(ss) > byte(ss[1])) do |
|
116 |
begin |
|
117 |
ParseIPCCommand(copy(ss, 2, byte(ss[1]))); |
|
118 |
Delete(ss, 1, Succ(byte(ss[1]))) |
|
119 |
end |
|
120 |
end else OutError('IPC connection lost', true) |
|
121 |
end; |
|
122 |
end; |
|
123 |
||
124 |
procedure SendIPC(s: shortstring); |
|
125 |
begin |
|
49 | 126 |
if IPCSock <> nil then |
127 |
begin |
|
128 |
if s[0]>#251 then s[0]:= #251; |
|
154
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
129 |
SDLNet_Write32(GameTicks, @s[Succ(byte(s[0]))]); |
49 | 130 |
{$IFDEF DEBUGFILE}AddFileLog('IPC send: '+s);{$ENDIF} |
131 |
inc(s[0],4); |
|
132 |
SDLNet_TCP_Send(IPCSock, @s, Succ(byte(s[0]))) |
|
133 |
end |
|
4 | 134 |
end; |
135 |
||
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
154
diff
changeset
|
136 |
procedure SendIPCRaw(p: pointer; len: Longword); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
154
diff
changeset
|
137 |
begin |
208 | 138 |
if IPCSock <> nil then |
139 |
begin |
|
140 |
SDLNet_TCP_Send(IPCSock, p, len) |
|
141 |
end |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
154
diff
changeset
|
142 |
end; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
154
diff
changeset
|
143 |
|
154
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
144 |
procedure SendIPCXY(cmd: char; X, Y: SmallInt); |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
145 |
var s: shortstring; |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
146 |
begin |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
147 |
s[0]:= #5; |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
148 |
s[1]:= cmd; |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
149 |
SDLNet_Write16(X, @s[2]); |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
150 |
SDLNet_Write16(Y, @s[4]); |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
151 |
SendIPC(s) |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
152 |
end; |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
153 |
|
159 | 154 |
procedure IPCWaitPongEvent; |
4 | 155 |
begin |
156 |
isPonged:= false; |
|
157 |
repeat |
|
158 |
IPCCheckSock; |
|
159 |
SDL_Delay(1) |
|
160 |
until isPonged |
|
161 |
end; |
|
162 |
||
159 | 163 |
procedure SendIPCAndWaitReply(s: shortstring); |
164 |
begin |
|
165 |
SendIPC(s); |
|
166 |
SendIPC('?'); |
|
167 |
IPCWaitPongEvent |
|
168 |
end; |
|
169 |
||
4 | 170 |
procedure NetGetNextCmd; |
171 |
var tmpflag: boolean; |
|
172 |
begin |
|
173 |
while (cmdcurpos <= cmdendpos)and(extcmd[cmdcurpos].cmd = 's') do |
|
174 |
begin |
|
175 |
WriteLnToConsole('> ' + copy(extcmd[cmdcurpos].str, 2, Pred(extcmd[cmdcurpos].len))); |
|
176 |
AddCaption('> ' + copy(extcmd[cmdcurpos].str, 2, Pred(extcmd[cmdcurpos].len)), $FFFFFF, capgrpNetSay); |
|
177 |
inc(cmdcurpos) |
|
178 |
end; |
|
351 | 179 |
|
4 | 180 |
if cmdcurpos <= cmdendpos then |
70 | 181 |
TryDo(GameTicks <= extcmd[cmdcurpos].Time, |
182 |
'oops, queue error. in buffer: ' + extcmd[cmdcurpos].cmd + |
|
183 |
' (' + inttostr(GameTicks) + ' > ' + |
|
184 |
inttostr(extcmd[cmdcurpos].Time) + ')', |
|
185 |
true); |
|
4 | 186 |
|
187 |
tmpflag:= true; |
|
188 |
while (cmdcurpos <= cmdendpos)and(GameTicks = extcmd[cmdcurpos].Time) do |
|
189 |
begin |
|
190 |
case extcmd[cmdcurpos].cmd of |
|
351 | 191 |
'L': ParseCommand('+left', true); |
192 |
'l': ParseCommand('-left', true); |
|
193 |
'R': ParseCommand('+right', true); |
|
194 |
'r': ParseCommand('-right', true); |
|
195 |
'U': ParseCommand('+up', true); |
|
196 |
'u': ParseCommand('-up', true); |
|
197 |
'D': ParseCommand('+down', true); |
|
198 |
'd': ParseCommand('-down', true); |
|
199 |
'A': ParseCommand('+attack', true); |
|
200 |
'a': ParseCommand('-attack', true); |
|
201 |
'S': ParseCommand('switch', true); |
|
202 |
'j': ParseCommand('ljump', true); |
|
203 |
'J': ParseCommand('hjump', true); |
|
204 |
',': ParseCommand('skip', true); |
|
4 | 205 |
'N': begin |
206 |
tmpflag:= false; |
|
207 |
{$IFDEF DEBUGFILE}AddFileLog('got cmd "N": time '+inttostr(extcmd[cmdcurpos].Time)){$ENDIF} |
|
208 |
end; |
|
209 |
'p': begin |
|
392 | 210 |
TargetPoint.X:= SmallInt(SDLNet_Read16(@extcmd[cmdcurpos].X)); |
211 |
TargetPoint.Y:= SmallInt(SDLNet_Read16(@extcmd[cmdcurpos].Y)); |
|
351 | 212 |
ParseCommand('put', true) |
4 | 213 |
end; |
214 |
'P': begin |
|
392 | 215 |
CursorPoint.X:= SmallInt(SDLNet_Read16(@extcmd[cmdcurpos].X) + WorldDx); |
216 |
CursorPoint.Y:= SmallInt(SDLNet_Read16(@extcmd[cmdcurpos].Y) + WorldDy); |
|
4 | 217 |
end; |
351 | 218 |
'1'..'5': ParseCommand('timer ' + extcmd[cmdcurpos].cmd, true); |
219 |
#128..char(128 + cMaxSlotIndex): ParseCommand('slot ' + char(byte(extcmd[cmdcurpos].cmd) - 79), true) |
|
4 | 220 |
end; |
221 |
inc(cmdcurpos) |
|
222 |
end; |
|
223 |
isInLag:= (cmdcurpos > cmdendpos) and tmpflag |
|
224 |
end; |
|
225 |
||
226 |
end. |