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