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