author | displacer |
Sun, 01 Oct 2006 20:14:30 +0000 | |
changeset 177 | c67c15e6fae3 |
parent 159 | 63909aecb0ed |
child 183 | 57c2ef19f719 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
3 |
* Copyright (c) 2004, 2005 Andrey Korotaev <unC0Rr@gmail.com> |
|
4 |
* |
|
5 |
* Distributed under the terms of the BSD-modified licence: |
|
6 |
* |
|
7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 |
* of this software and associated documentation files (the "Software"), to deal |
|
9 |
* with the Software without restriction, including without limitation the |
|
10 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
|
11 |
* sell copies of the Software, and to permit persons to whom the Software is |
|
12 |
* furnished to do so, subject to the following conditions: |
|
13 |
* |
|
14 |
* 1. Redistributions of source code must retain the above copyright notice, |
|
15 |
* this list of conditions and the following disclaimer. |
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
|
17 |
* this list of conditions and the following disclaimer in the documentation |
|
18 |
* and/or other materials provided with the distribution. |
|
19 |
* 3. The name of the author may not be used to endorse or promote products |
|
20 |
* derived from this software without specific prior written permission. |
|
21 |
* |
|
22 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
|
23 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
|
24 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
|
25 |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
27 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
|
28 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
29 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|
30 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|
31 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
32 |
*) |
|
33 |
||
34 |
unit uIO; |
|
35 |
interface |
|
36 |
uses SDLh; |
|
37 |
{$INCLUDE options.inc} |
|
38 |
||
39 |
const ipcPort: Word = 0; |
|
40 |
||
41 |
procedure SendIPC(s: shortstring); |
|
154
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
42 |
procedure SendIPCXY(cmd: char; X, Y: SmallInt); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
154
diff
changeset
|
43 |
procedure SendIPCRaw(p: pointer; len: Longword); |
4 | 44 |
procedure SendIPCAndWaitReply(s: shortstring); |
159 | 45 |
procedure IPCWaitPongEvent; |
4 | 46 |
procedure IPCCheckSock; |
47 |
procedure InitIPC; |
|
48 |
procedure CloseIPC; |
|
49 |
procedure NetGetNextCmd; |
|
50 |
||
51 |
implementation |
|
52 |
uses uConsole, uConsts, uWorld, uMisc, uRandom, uLand; |
|
53 |
const isPonged: boolean = false; |
|
49 | 54 |
var IPCSock: PTCPSocket = nil; |
4 | 55 |
fds: PSDLNet_SocketSet; |
56 |
||
57 |
extcmd: array[word] of packed record |
|
58 |
Time: LongWord; |
|
59 |
case byte of |
|
60 |
1: (len: byte; |
|
61 |
cmd: Char; |
|
95 | 62 |
X, Y: SmallInt); |
4 | 63 |
2: (str: shortstring); |
64 |
end; |
|
65 |
cmdcurpos: integer = 0; |
|
66 |
cmdendpos: integer = -1; |
|
67 |
||
68 |
procedure InitIPC; |
|
69 |
var ipaddr: TIPAddress; |
|
70 |
begin |
|
71 |
WriteToConsole('Init SDL_Net... '); |
|
72 |
SDLTry(SDLNet_Init = 0, true); |
|
73 |
fds:= SDLNet_AllocSocketSet(1); |
|
74 |
SDLTry(fds <> nil, true); |
|
75 |
WriteLnToConsole(msgOK); |
|
76 |
WriteToConsole('Establishing IPC connection... '); |
|
77 |
SDLTry(SDLNet_ResolveHost(ipaddr, '127.0.0.1', ipcPort) = 0, true); |
|
78 |
IPCSock:= SDLNet_TCP_Open(ipaddr); |
|
79 |
SDLTry(IPCSock <> nil, true); |
|
80 |
WriteLnToConsole(msgOK) |
|
81 |
end; |
|
82 |
||
83 |
procedure CloseIPC; |
|
84 |
begin |
|
85 |
SDLNet_FreeSocketSet(fds); |
|
86 |
SDLNet_TCP_Close(IPCSock); |
|
87 |
SDLNet_Quit |
|
88 |
end; |
|
89 |
||
90 |
procedure ParseIPCCommand(s: shortstring); |
|
91 |
begin |
|
92 |
case s[1] of |
|
22 | 93 |
'!': begin {$IFDEF DEBUGFILE}AddFileLog('Ping? Pong!');{$ENDIF}isPonged:= true; end; |
4 | 94 |
'?': SendIPC('!'); |
95 |
'e': ParseCommand(copy(s, 2, Length(s) - 1)); |
|
96 |
'E': OutError(copy(s, 2, Length(s) - 1), true); |
|
97 |
'W': OutError(copy(s, 2, Length(s) - 1), false); |
|
98 |
'T': case s[2] of |
|
99 |
'L': GameType:= gmtLocal; |
|
100 |
'D': GameType:= gmtDemo; |
|
101 |
'N': GameType:= gmtNet; |
|
72 | 102 |
'S': GameType:= gmtSave; |
4 | 103 |
else OutError(errmsgIncorrectUse + ' IPC "T" :' + s[2], true) end; |
104 |
else |
|
105 |
inc(cmdendpos); |
|
154
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
106 |
extcmd[cmdendpos].Time := SDLNet_Read32(@s[byte(s[0]) - 3]); |
4 | 107 |
extcmd[cmdendpos].str := s; |
108 |
{$IFDEF DEBUGFILE}AddFileLog('IPC in: '+s[1]+' ticks '+inttostr(extcmd[cmdendpos].Time)+' at '+inttostr(cmdendpos));{$ENDIF} |
|
109 |
dec(extcmd[cmdendpos].len, 4) |
|
110 |
end |
|
111 |
end; |
|
112 |
||
113 |
procedure IPCCheckSock; |
|
114 |
const ss: string = ''; |
|
115 |
var i: integer; |
|
116 |
buf: array[0..255] of byte; |
|
117 |
s: shortstring absolute buf; |
|
118 |
begin |
|
119 |
fds.numsockets:= 0; |
|
120 |
SDLNet_AddSocket(fds, IPCSock); |
|
121 |
||
122 |
while SDLNet_CheckSockets(fds, 0) > 0 do |
|
123 |
begin |
|
124 |
i:= SDLNet_TCP_Recv(IPCSock, @buf[1], 255); |
|
125 |
if i > 0 then |
|
126 |
begin |
|
127 |
buf[0]:= i; |
|
128 |
ss:= ss + s; |
|
129 |
while (Length(ss) > 1)and(Length(ss) > byte(ss[1])) do |
|
130 |
begin |
|
131 |
ParseIPCCommand(copy(ss, 2, byte(ss[1]))); |
|
132 |
Delete(ss, 1, Succ(byte(ss[1]))) |
|
133 |
end |
|
134 |
end else OutError('IPC connection lost', true) |
|
135 |
end; |
|
136 |
end; |
|
137 |
||
138 |
procedure SendIPC(s: shortstring); |
|
139 |
begin |
|
49 | 140 |
if IPCSock <> nil then |
141 |
begin |
|
142 |
if s[0]>#251 then s[0]:= #251; |
|
154
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
143 |
SDLNet_Write32(GameTicks, @s[Succ(byte(s[0]))]); |
49 | 144 |
{$IFDEF DEBUGFILE}AddFileLog('IPC send: '+s);{$ENDIF} |
145 |
inc(s[0],4); |
|
146 |
SDLNet_TCP_Send(IPCSock, @s, Succ(byte(s[0]))) |
|
147 |
end |
|
4 | 148 |
end; |
149 |
||
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
154
diff
changeset
|
150 |
procedure SendIPCRaw(p: pointer; len: Longword); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
154
diff
changeset
|
151 |
begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
154
diff
changeset
|
152 |
SDLNet_TCP_Send(IPCSock, p, len) |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
154
diff
changeset
|
153 |
end; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
154
diff
changeset
|
154 |
|
154
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
155 |
procedure SendIPCXY(cmd: char; X, Y: SmallInt); |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
156 |
var s: shortstring; |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
157 |
begin |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
158 |
s[0]:= #5; |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
159 |
s[1]:= cmd; |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
160 |
SDLNet_Write16(X, @s[2]); |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
161 |
SDLNet_Write16(Y, @s[4]); |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
162 |
SendIPC(s) |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
163 |
end; |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
164 |
|
159 | 165 |
procedure IPCWaitPongEvent; |
4 | 166 |
begin |
167 |
isPonged:= false; |
|
168 |
repeat |
|
169 |
IPCCheckSock; |
|
170 |
SDL_Delay(1) |
|
171 |
until isPonged |
|
172 |
end; |
|
173 |
||
159 | 174 |
procedure SendIPCAndWaitReply(s: shortstring); |
175 |
begin |
|
176 |
SendIPC(s); |
|
177 |
SendIPC('?'); |
|
178 |
IPCWaitPongEvent |
|
179 |
end; |
|
180 |
||
4 | 181 |
procedure NetGetNextCmd; |
182 |
var tmpflag: boolean; |
|
183 |
begin |
|
184 |
while (cmdcurpos <= cmdendpos)and(extcmd[cmdcurpos].cmd = 's') do |
|
185 |
begin |
|
186 |
WriteLnToConsole('> ' + copy(extcmd[cmdcurpos].str, 2, Pred(extcmd[cmdcurpos].len))); |
|
187 |
AddCaption('> ' + copy(extcmd[cmdcurpos].str, 2, Pred(extcmd[cmdcurpos].len)), $FFFFFF, capgrpNetSay); |
|
188 |
inc(cmdcurpos) |
|
189 |
end; |
|
190 |
||
191 |
if cmdcurpos <= cmdendpos then |
|
70 | 192 |
TryDo(GameTicks <= extcmd[cmdcurpos].Time, |
193 |
'oops, queue error. in buffer: ' + extcmd[cmdcurpos].cmd + |
|
194 |
' (' + inttostr(GameTicks) + ' > ' + |
|
195 |
inttostr(extcmd[cmdcurpos].Time) + ')', |
|
196 |
true); |
|
4 | 197 |
|
198 |
tmpflag:= true; |
|
199 |
while (cmdcurpos <= cmdendpos)and(GameTicks = extcmd[cmdcurpos].Time) do |
|
200 |
begin |
|
201 |
case extcmd[cmdcurpos].cmd of |
|
37 | 202 |
'L': ParseCommand('+left'); |
203 |
'l': ParseCommand('-left'); |
|
204 |
'R': ParseCommand('+right'); |
|
205 |
'r': ParseCommand('-right'); |
|
206 |
'U': ParseCommand('+up'); |
|
207 |
'u': ParseCommand('-up'); |
|
208 |
'D': ParseCommand('+down'); |
|
209 |
'd': ParseCommand('-down'); |
|
210 |
'A': ParseCommand('+attack'); |
|
211 |
'a': ParseCommand('-attack'); |
|
212 |
'S': ParseCommand('switch'); |
|
213 |
'j': ParseCommand('ljump'); |
|
214 |
'J': ParseCommand('hjump'); |
|
48 | 215 |
',': ParseCommand('skip'); |
4 | 216 |
'N': begin |
217 |
tmpflag:= false; |
|
218 |
{$IFDEF DEBUGFILE}AddFileLog('got cmd "N": time '+inttostr(extcmd[cmdcurpos].Time)){$ENDIF} |
|
219 |
end; |
|
220 |
'p': begin |
|
154
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
221 |
TargetPoint.X:= SDLNet_Read16(@extcmd[cmdcurpos].X); |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
222 |
TargetPoint.Y:= SDLNet_Read16(@extcmd[cmdcurpos].Y); |
37 | 223 |
ParseCommand('put') |
4 | 224 |
end; |
225 |
'P': begin |
|
154
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
226 |
CursorPoint.X:= SDLNet_Read16(@extcmd[cmdcurpos].X) + WorldDx; |
5667e6f38704
Network protocol uses integers in network byte order
unc0rr
parents:
112
diff
changeset
|
227 |
CursorPoint.Y:= SDLNet_Read16(@extcmd[cmdcurpos].Y) + WorldDy; |
4 | 228 |
end; |
37 | 229 |
'1'..'5': ParseCommand('timer ' + extcmd[cmdcurpos].cmd); |
112 | 230 |
#128..char(128 + cMaxSlotIndex): ParseCommand('slot ' + char(byte(extcmd[cmdcurpos].cmd) - 79)) |
4 | 231 |
end; |
232 |
inc(cmdcurpos) |
|
233 |
end; |
|
234 |
isInLag:= (cmdcurpos > cmdendpos) and tmpflag |
|
235 |
end; |
|
236 |
||
237 |
end. |