author | unc0rr |
Thu, 05 Oct 2006 20:13:51 +0000 | |
changeset 190 | 206aabea2229 |
parent 183 | 57c2ef19f719 |
child 196 | 993cf173218b |
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 |
|
37 |
uses uConsole, uConsts, uWorld, uMisc, uRandom, uLand; |
|
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 |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
154
diff
changeset
|
137 |
SDLNet_TCP_Send(IPCSock, p, len) |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
154
diff
changeset
|
138 |
end; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
154
diff
changeset
|
139 |
|
154
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
140 |
procedure SendIPCXY(cmd: char; X, Y: SmallInt); |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
141 |
var s: shortstring; |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
142 |
begin |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
143 |
s[0]:= #5; |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
144 |
s[1]:= cmd; |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
145 |
SDLNet_Write16(X, @s[2]); |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
146 |
SDLNet_Write16(Y, @s[4]); |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
147 |
SendIPC(s) |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
148 |
end; |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
149 |
|
159 | 150 |
procedure IPCWaitPongEvent; |
4 | 151 |
begin |
152 |
isPonged:= false; |
|
153 |
repeat |
|
154 |
IPCCheckSock; |
|
155 |
SDL_Delay(1) |
|
156 |
until isPonged |
|
157 |
end; |
|
158 |
||
159 | 159 |
procedure SendIPCAndWaitReply(s: shortstring); |
160 |
begin |
|
161 |
SendIPC(s); |
|
162 |
SendIPC('?'); |
|
163 |
IPCWaitPongEvent |
|
164 |
end; |
|
165 |
||
4 | 166 |
procedure NetGetNextCmd; |
167 |
var tmpflag: boolean; |
|
168 |
begin |
|
169 |
while (cmdcurpos <= cmdendpos)and(extcmd[cmdcurpos].cmd = 's') do |
|
170 |
begin |
|
171 |
WriteLnToConsole('> ' + copy(extcmd[cmdcurpos].str, 2, Pred(extcmd[cmdcurpos].len))); |
|
172 |
AddCaption('> ' + copy(extcmd[cmdcurpos].str, 2, Pred(extcmd[cmdcurpos].len)), $FFFFFF, capgrpNetSay); |
|
173 |
inc(cmdcurpos) |
|
174 |
end; |
|
175 |
||
176 |
if cmdcurpos <= cmdendpos then |
|
70 | 177 |
TryDo(GameTicks <= extcmd[cmdcurpos].Time, |
178 |
'oops, queue error. in buffer: ' + extcmd[cmdcurpos].cmd + |
|
179 |
' (' + inttostr(GameTicks) + ' > ' + |
|
180 |
inttostr(extcmd[cmdcurpos].Time) + ')', |
|
181 |
true); |
|
4 | 182 |
|
183 |
tmpflag:= true; |
|
184 |
while (cmdcurpos <= cmdendpos)and(GameTicks = extcmd[cmdcurpos].Time) do |
|
185 |
begin |
|
186 |
case extcmd[cmdcurpos].cmd of |
|
37 | 187 |
'L': ParseCommand('+left'); |
188 |
'l': ParseCommand('-left'); |
|
189 |
'R': ParseCommand('+right'); |
|
190 |
'r': ParseCommand('-right'); |
|
191 |
'U': ParseCommand('+up'); |
|
192 |
'u': ParseCommand('-up'); |
|
193 |
'D': ParseCommand('+down'); |
|
194 |
'd': ParseCommand('-down'); |
|
195 |
'A': ParseCommand('+attack'); |
|
196 |
'a': ParseCommand('-attack'); |
|
197 |
'S': ParseCommand('switch'); |
|
198 |
'j': ParseCommand('ljump'); |
|
199 |
'J': ParseCommand('hjump'); |
|
48 | 200 |
',': ParseCommand('skip'); |
4 | 201 |
'N': begin |
202 |
tmpflag:= false; |
|
203 |
{$IFDEF DEBUGFILE}AddFileLog('got cmd "N": time '+inttostr(extcmd[cmdcurpos].Time)){$ENDIF} |
|
204 |
end; |
|
205 |
'p': begin |
|
154
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
206 |
TargetPoint.X:= SDLNet_Read16(@extcmd[cmdcurpos].X); |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
207 |
TargetPoint.Y:= SDLNet_Read16(@extcmd[cmdcurpos].Y); |
37 | 208 |
ParseCommand('put') |
4 | 209 |
end; |
210 |
'P': begin |
|
154
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
211 |
CursorPoint.X:= SDLNet_Read16(@extcmd[cmdcurpos].X) + WorldDx; |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
212 |
CursorPoint.Y:= SDLNet_Read16(@extcmd[cmdcurpos].Y) + WorldDy; |
4 | 213 |
end; |
37 | 214 |
'1'..'5': ParseCommand('timer ' + extcmd[cmdcurpos].cmd); |
112 | 215 |
#128..char(128 + cMaxSlotIndex): ParseCommand('slot ' + char(byte(extcmd[cmdcurpos].cmd) - 79)) |
4 | 216 |
end; |
217 |
inc(cmdcurpos) |
|
218 |
end; |
|
219 |
isInLag:= (cmdcurpos > cmdendpos) and tmpflag |
|
220 |
end; |
|
221 |
||
222 |
end. |