author | unc0rr |
Sat, 18 Nov 2006 18:17:03 +0000 | |
changeset 248 | aab204fe5061 |
parent 208 | a049157d673a |
child 351 | 29bc9c36ad5f |
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; |
|
50 |
cmdcurpos: integer = 0; |
|
51 |
cmdendpos: integer = -1; |
|
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('!'); |
80 |
'e': ParseCommand(copy(s, 2, Length(s) - 1)); |
|
81 |
'E': OutError(copy(s, 2, Length(s) - 1), true); |
|
82 |
'W': OutError(copy(s, 2, Length(s) - 1), false); |
|
83 |
'T': case s[2] of |
|
84 |
'L': GameType:= gmtLocal; |
|
85 |
'D': GameType:= gmtDemo; |
|
86 |
'N': GameType:= gmtNet; |
|
72 | 87 |
'S': GameType:= gmtSave; |
4 | 88 |
else OutError(errmsgIncorrectUse + ' IPC "T" :' + s[2], true) end; |
89 |
else |
|
90 |
inc(cmdendpos); |
|
154
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
91 |
extcmd[cmdendpos].Time := SDLNet_Read32(@s[byte(s[0]) - 3]); |
4 | 92 |
extcmd[cmdendpos].str := s; |
93 |
{$IFDEF DEBUGFILE}AddFileLog('IPC in: '+s[1]+' ticks '+inttostr(extcmd[cmdendpos].Time)+' at '+inttostr(cmdendpos));{$ENDIF} |
|
94 |
dec(extcmd[cmdendpos].len, 4) |
|
95 |
end |
|
96 |
end; |
|
97 |
||
98 |
procedure IPCCheckSock; |
|
99 |
const ss: string = ''; |
|
100 |
var i: integer; |
|
101 |
buf: array[0..255] of byte; |
|
102 |
s: shortstring absolute buf; |
|
103 |
begin |
|
104 |
fds.numsockets:= 0; |
|
105 |
SDLNet_AddSocket(fds, IPCSock); |
|
106 |
||
107 |
while SDLNet_CheckSockets(fds, 0) > 0 do |
|
108 |
begin |
|
109 |
i:= SDLNet_TCP_Recv(IPCSock, @buf[1], 255); |
|
110 |
if i > 0 then |
|
111 |
begin |
|
112 |
buf[0]:= i; |
|
113 |
ss:= ss + s; |
|
114 |
while (Length(ss) > 1)and(Length(ss) > byte(ss[1])) do |
|
115 |
begin |
|
116 |
ParseIPCCommand(copy(ss, 2, byte(ss[1]))); |
|
117 |
Delete(ss, 1, Succ(byte(ss[1]))) |
|
118 |
end |
|
119 |
end else OutError('IPC connection lost', true) |
|
120 |
end; |
|
121 |
end; |
|
122 |
||
123 |
procedure SendIPC(s: shortstring); |
|
124 |
begin |
|
49 | 125 |
if IPCSock <> nil then |
126 |
begin |
|
127 |
if s[0]>#251 then s[0]:= #251; |
|
154
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
128 |
SDLNet_Write32(GameTicks, @s[Succ(byte(s[0]))]); |
49 | 129 |
{$IFDEF DEBUGFILE}AddFileLog('IPC send: '+s);{$ENDIF} |
130 |
inc(s[0],4); |
|
131 |
SDLNet_TCP_Send(IPCSock, @s, Succ(byte(s[0]))) |
|
132 |
end |
|
4 | 133 |
end; |
134 |
||
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
154
diff
changeset
|
135 |
procedure SendIPCRaw(p: pointer; len: Longword); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
154
diff
changeset
|
136 |
begin |
208 | 137 |
if IPCSock <> nil then |
138 |
begin |
|
139 |
SDLNet_TCP_Send(IPCSock, p, len) |
|
140 |
end |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
154
diff
changeset
|
141 |
end; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
154
diff
changeset
|
142 |
|
154
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
143 |
procedure SendIPCXY(cmd: char; X, Y: SmallInt); |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
144 |
var s: shortstring; |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
145 |
begin |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
146 |
s[0]:= #5; |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
147 |
s[1]:= cmd; |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
148 |
SDLNet_Write16(X, @s[2]); |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
149 |
SDLNet_Write16(Y, @s[4]); |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
150 |
SendIPC(s) |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
151 |
end; |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
152 |
|
159 | 153 |
procedure IPCWaitPongEvent; |
4 | 154 |
begin |
155 |
isPonged:= false; |
|
156 |
repeat |
|
157 |
IPCCheckSock; |
|
158 |
SDL_Delay(1) |
|
159 |
until isPonged |
|
160 |
end; |
|
161 |
||
159 | 162 |
procedure SendIPCAndWaitReply(s: shortstring); |
163 |
begin |
|
164 |
SendIPC(s); |
|
165 |
SendIPC('?'); |
|
166 |
IPCWaitPongEvent |
|
167 |
end; |
|
168 |
||
4 | 169 |
procedure NetGetNextCmd; |
170 |
var tmpflag: boolean; |
|
171 |
begin |
|
172 |
while (cmdcurpos <= cmdendpos)and(extcmd[cmdcurpos].cmd = 's') do |
|
173 |
begin |
|
174 |
WriteLnToConsole('> ' + copy(extcmd[cmdcurpos].str, 2, Pred(extcmd[cmdcurpos].len))); |
|
175 |
AddCaption('> ' + copy(extcmd[cmdcurpos].str, 2, Pred(extcmd[cmdcurpos].len)), $FFFFFF, capgrpNetSay); |
|
176 |
inc(cmdcurpos) |
|
177 |
end; |
|
178 |
||
179 |
if cmdcurpos <= cmdendpos then |
|
70 | 180 |
TryDo(GameTicks <= extcmd[cmdcurpos].Time, |
181 |
'oops, queue error. in buffer: ' + extcmd[cmdcurpos].cmd + |
|
182 |
' (' + inttostr(GameTicks) + ' > ' + |
|
183 |
inttostr(extcmd[cmdcurpos].Time) + ')', |
|
184 |
true); |
|
4 | 185 |
|
186 |
tmpflag:= true; |
|
187 |
while (cmdcurpos <= cmdendpos)and(GameTicks = extcmd[cmdcurpos].Time) do |
|
188 |
begin |
|
189 |
case extcmd[cmdcurpos].cmd of |
|
37 | 190 |
'L': ParseCommand('+left'); |
191 |
'l': ParseCommand('-left'); |
|
192 |
'R': ParseCommand('+right'); |
|
193 |
'r': ParseCommand('-right'); |
|
194 |
'U': ParseCommand('+up'); |
|
195 |
'u': ParseCommand('-up'); |
|
196 |
'D': ParseCommand('+down'); |
|
197 |
'd': ParseCommand('-down'); |
|
198 |
'A': ParseCommand('+attack'); |
|
199 |
'a': ParseCommand('-attack'); |
|
200 |
'S': ParseCommand('switch'); |
|
201 |
'j': ParseCommand('ljump'); |
|
202 |
'J': ParseCommand('hjump'); |
|
48 | 203 |
',': ParseCommand('skip'); |
4 | 204 |
'N': begin |
205 |
tmpflag:= false; |
|
206 |
{$IFDEF DEBUGFILE}AddFileLog('got cmd "N": time '+inttostr(extcmd[cmdcurpos].Time)){$ENDIF} |
|
207 |
end; |
|
208 |
'p': begin |
|
154
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
209 |
TargetPoint.X:= SDLNet_Read16(@extcmd[cmdcurpos].X); |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
210 |
TargetPoint.Y:= SDLNet_Read16(@extcmd[cmdcurpos].Y); |
37 | 211 |
ParseCommand('put') |
4 | 212 |
end; |
213 |
'P': begin |
|
154
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
214 |
CursorPoint.X:= SDLNet_Read16(@extcmd[cmdcurpos].X) + WorldDx; |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
215 |
CursorPoint.Y:= SDLNet_Read16(@extcmd[cmdcurpos].Y) + WorldDy; |
4 | 216 |
end; |
37 | 217 |
'1'..'5': ParseCommand('timer ' + extcmd[cmdcurpos].cmd); |
112 | 218 |
#128..char(128 + cMaxSlotIndex): ParseCommand('slot ' + char(byte(extcmd[cmdcurpos].cmd) - 79)) |
4 | 219 |
end; |
220 |
inc(cmdcurpos) |
|
221 |
end; |
|
222 |
isInLag:= (cmdcurpos > cmdendpos) and tmpflag |
|
223 |
end; |
|
224 |
||
225 |
end. |