- Added hgeol
- Fixed repository side eol to be LF
This commit is contained in:
@@ -1,89 +1,89 @@
|
||||
(*
|
||||
* CDDL HEADER START
|
||||
*
|
||||
* The contents of this file are subject to the terms of the
|
||||
* Common Development and Distribution License, Version 1.0 only
|
||||
* (the "License"). You may not use this file except in compliance
|
||||
* with the License.
|
||||
*
|
||||
* You can obtain a copy of the license at
|
||||
* http://www.opensource.org/licenses/cddl1.php.
|
||||
* See the License for the specific language governing permissions
|
||||
* and limitations under the License.
|
||||
*
|
||||
* When distributing Covered Code, include this CDDL HEADER in each
|
||||
* file and include the License file at
|
||||
* http://www.opensource.org/licenses/cddl1.php. If applicable,
|
||||
* add the following below this CDDL HEADER, with the fields enclosed
|
||||
* by brackets "[]" replaced with your own identifying * information:
|
||||
* Portions Copyright [yyyy] [name of copyright owner]
|
||||
*
|
||||
* CDDL HEADER END
|
||||
*
|
||||
*
|
||||
* Portions Copyright 2007 Andreas Schneider
|
||||
*)
|
||||
unit UNetState;
|
||||
|
||||
{$mode objfpc}{$H+}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, SysUtils, lNet, UEnhancedMemoryStream, UAccount, ULinkedList;
|
||||
|
||||
type
|
||||
|
||||
{ TNetState }
|
||||
|
||||
TNetState = class(TObject)
|
||||
constructor Create(ASocket: TLSocket);
|
||||
destructor Destroy; override;
|
||||
protected
|
||||
FSocket: TLSocket;
|
||||
FSendQueue: TEnhancedMemoryStream;
|
||||
FReceiveQueue: TEnhancedMemoryStream;
|
||||
FAccount: TAccount;
|
||||
FSubscriptions: TList;
|
||||
FLastAction: TDateTime;
|
||||
public
|
||||
property Socket: TLSocket read FSocket;
|
||||
property SendQueue: TEnhancedMemoryStream read FSendQueue;
|
||||
property ReceiveQueue: TEnhancedMemoryStream read FReceiveQueue;
|
||||
property Account: TAccount read FAccount write FAccount;
|
||||
property Subscriptions: TList read FSubscriptions;
|
||||
property LastAction: TDateTime read FLastAction write FLastAction;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
{ TNetState }
|
||||
|
||||
constructor TNetState.Create(ASocket: TLSocket);
|
||||
begin
|
||||
inherited Create;
|
||||
FSocket := ASocket;
|
||||
FSendQueue := TEnhancedMemoryStream.Create;
|
||||
FReceiveQueue := TEnhancedMemoryStream.Create;
|
||||
FAccount := nil;
|
||||
FSubscriptions := TList.Create;
|
||||
FLastAction := Now;
|
||||
end;
|
||||
|
||||
destructor TNetState.Destroy;
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
if FSendQueue <> nil then FreeAndNil(FSendQueue);
|
||||
if FReceiveQueue <> nil then FreeAndNil(FReceiveQueue);
|
||||
if FSubscriptions <> nil then
|
||||
begin
|
||||
for i := 0 to FSubscriptions.Count - 1 do
|
||||
TLinkedList(FSubscriptions.Items[i]).Delete(Self);
|
||||
FreeAndNil(FSubscriptions);
|
||||
end;
|
||||
inherited Destroy;
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
(*
|
||||
* CDDL HEADER START
|
||||
*
|
||||
* The contents of this file are subject to the terms of the
|
||||
* Common Development and Distribution License, Version 1.0 only
|
||||
* (the "License"). You may not use this file except in compliance
|
||||
* with the License.
|
||||
*
|
||||
* You can obtain a copy of the license at
|
||||
* http://www.opensource.org/licenses/cddl1.php.
|
||||
* See the License for the specific language governing permissions
|
||||
* and limitations under the License.
|
||||
*
|
||||
* When distributing Covered Code, include this CDDL HEADER in each
|
||||
* file and include the License file at
|
||||
* http://www.opensource.org/licenses/cddl1.php. If applicable,
|
||||
* add the following below this CDDL HEADER, with the fields enclosed
|
||||
* by brackets "[]" replaced with your own identifying * information:
|
||||
* Portions Copyright [yyyy] [name of copyright owner]
|
||||
*
|
||||
* CDDL HEADER END
|
||||
*
|
||||
*
|
||||
* Portions Copyright 2007 Andreas Schneider
|
||||
*)
|
||||
unit UNetState;
|
||||
|
||||
{$mode objfpc}{$H+}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, SysUtils, lNet, UEnhancedMemoryStream, UAccount, ULinkedList;
|
||||
|
||||
type
|
||||
|
||||
{ TNetState }
|
||||
|
||||
TNetState = class(TObject)
|
||||
constructor Create(ASocket: TLSocket);
|
||||
destructor Destroy; override;
|
||||
protected
|
||||
FSocket: TLSocket;
|
||||
FSendQueue: TEnhancedMemoryStream;
|
||||
FReceiveQueue: TEnhancedMemoryStream;
|
||||
FAccount: TAccount;
|
||||
FSubscriptions: TList;
|
||||
FLastAction: TDateTime;
|
||||
public
|
||||
property Socket: TLSocket read FSocket;
|
||||
property SendQueue: TEnhancedMemoryStream read FSendQueue;
|
||||
property ReceiveQueue: TEnhancedMemoryStream read FReceiveQueue;
|
||||
property Account: TAccount read FAccount write FAccount;
|
||||
property Subscriptions: TList read FSubscriptions;
|
||||
property LastAction: TDateTime read FLastAction write FLastAction;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
{ TNetState }
|
||||
|
||||
constructor TNetState.Create(ASocket: TLSocket);
|
||||
begin
|
||||
inherited Create;
|
||||
FSocket := ASocket;
|
||||
FSendQueue := TEnhancedMemoryStream.Create;
|
||||
FReceiveQueue := TEnhancedMemoryStream.Create;
|
||||
FAccount := nil;
|
||||
FSubscriptions := TList.Create;
|
||||
FLastAction := Now;
|
||||
end;
|
||||
|
||||
destructor TNetState.Destroy;
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
if FSendQueue <> nil then FreeAndNil(FSendQueue);
|
||||
if FReceiveQueue <> nil then FreeAndNil(FReceiveQueue);
|
||||
if FSubscriptions <> nil then
|
||||
begin
|
||||
for i := 0 to FSubscriptions.Count - 1 do
|
||||
TLinkedList(FSubscriptions.Items[i]).Delete(Self);
|
||||
FreeAndNil(FSubscriptions);
|
||||
end;
|
||||
inherited Destroy;
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
|
||||
@@ -1,218 +1,218 @@
|
||||
(*
|
||||
* CDDL HEADER START
|
||||
*
|
||||
* The contents of this file are subject to the terms of the
|
||||
* Common Development and Distribution License, Version 1.0 only
|
||||
* (the "License"). You may not use this file except in compliance
|
||||
* with the License.
|
||||
*
|
||||
* You can obtain a copy of the license at
|
||||
* http://www.opensource.org/licenses/cddl1.php.
|
||||
* See the License for the specific language governing permissions
|
||||
* and limitations under the License.
|
||||
*
|
||||
* When distributing Covered Code, include this CDDL HEADER in each
|
||||
* file and include the License file at
|
||||
* http://www.opensource.org/licenses/cddl1.php. If applicable,
|
||||
* add the following below this CDDL HEADER, with the fields enclosed
|
||||
* by brackets "[]" replaced with your own identifying * information:
|
||||
* Portions Copyright [yyyy] [name of copyright owner]
|
||||
*
|
||||
* CDDL HEADER END
|
||||
*
|
||||
*
|
||||
* Portions Copyright 2007 Andreas Schneider
|
||||
*)
|
||||
unit UPacketHandlers;
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, SysUtils, dzlib, UConfig, UNetState, UEnhancedMemoryStream, UEnums,
|
||||
ULinkedList, URegions;
|
||||
|
||||
type
|
||||
TPacketProcessor = procedure(ABuffer: TEnhancedMemoryStream; ANetState: TNetState);
|
||||
TPacketProcessorMethod = procedure(ABuffer: TEnhancedMemoryStream; ANetState: TNetState) of object;
|
||||
|
||||
{ TPacketHandler }
|
||||
|
||||
TPacketHandler = class(TObject)
|
||||
constructor Create(ALength: Cardinal; APacketProcessor: TPacketProcessor); overload;
|
||||
constructor Create(ALength: Cardinal; APacketProcessorMethod: TPacketProcessorMethod); overload;
|
||||
procedure Process(ABuffer: TEnhancedMemoryStream; ANetState: TNetState);
|
||||
protected
|
||||
FLength: Cardinal;
|
||||
FPacketProcessor: TPacketProcessor;
|
||||
FPacketProcessorMethod: TPacketProcessorMethod;
|
||||
published
|
||||
property PacketLength: Cardinal read FLength;
|
||||
end;
|
||||
|
||||
var
|
||||
PacketHandlers: array[0..$FF] of TPacketHandler;
|
||||
|
||||
function ValidateAccess(ANetState: TNetState; ALevel: TAccessLevel): Boolean; overload;
|
||||
function ValidateAccess(ANetState: TNetState; ALevel: TAccessLevel; AX, AY: Cardinal): Boolean; overload;
|
||||
procedure RegisterPacketHandler(AID: Byte; APacketHandler: TPacketHandler);
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
UCEDServer, UPackets, UConnectionHandling, UAdminHandling, UClientHandling;
|
||||
|
||||
function ValidateAccess(ANetState: TNetState; ALevel: TAccessLevel): Boolean;
|
||||
begin
|
||||
Result := (ANetState.Account <> nil) and (ANetState.Account.AccessLevel >= ALevel);
|
||||
end;
|
||||
|
||||
function ValidateAccess(ANetState: TNetState; ALevel: TAccessLevel; AX, AY: Cardinal): Boolean;
|
||||
var
|
||||
i,j: Word;
|
||||
region: TRegion;
|
||||
rect: TRect;
|
||||
begin
|
||||
if not ValidateAccess(ANetState, ALevel) then Exit(False);
|
||||
if (ANetState.Account.Regions.Count = 0) or
|
||||
(ANetState.Account.AccessLevel >= alAdministrator) then Exit(True); //no restrictions
|
||||
|
||||
Result := False;
|
||||
for i := 0 to ANetState.Account.Regions.Count - 1 do
|
||||
begin
|
||||
region := Config.Regions.Find(ANetState.Account.Regions[i]);
|
||||
if region <> nil then
|
||||
begin
|
||||
for j := 0 to region.Areas.Count - 1 do
|
||||
begin
|
||||
rect := region.Areas.Rects[j];
|
||||
if (AX >= rect.Left) and
|
||||
(AX < rect.Right) and
|
||||
(AY >= rect.Top) and
|
||||
(AY < rect.Bottom) then
|
||||
Exit(True);
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure RegisterPacketHandler(AID: Byte; APacketHandler: TPacketHandler);
|
||||
begin
|
||||
if Assigned(PacketHandlers[AID]) then FreeAndNil(PacketHandlers[AID]);
|
||||
PacketHandlers[AID] := APacketHandler;
|
||||
end;
|
||||
|
||||
{ TPacketHandler }
|
||||
|
||||
constructor TPacketHandler.Create(ALength: Cardinal; APacketProcessor: TPacketProcessor);
|
||||
begin
|
||||
inherited Create;
|
||||
FLength := ALength;
|
||||
FPacketProcessor := APacketProcessor;
|
||||
FPacketProcessorMethod := nil;
|
||||
end;
|
||||
|
||||
constructor TPacketHandler.Create(ALength: Cardinal;
|
||||
APacketProcessorMethod: TPacketProcessorMethod);
|
||||
begin
|
||||
inherited Create;
|
||||
FLength := ALength;
|
||||
FPacketProcessor := nil;
|
||||
FPacketProcessorMethod := APacketProcessorMethod;
|
||||
end;
|
||||
|
||||
procedure TPacketHandler.Process(ABuffer: TEnhancedMemoryStream; ANetState: TNetState);
|
||||
begin
|
||||
if Assigned(FPacketProcessor) then
|
||||
FPacketProcessor(ABuffer, ANetState)
|
||||
else if Assigned(FPacketProcessorMethod) then
|
||||
FPacketProcessorMethod(ABuffer, ANetState);
|
||||
end;
|
||||
|
||||
procedure OnCompressedPacket(ABuffer: TEnhancedMemoryStream; ANetState: TNetState);
|
||||
var
|
||||
uncompStream: TEnhancedMemoryStream;
|
||||
uncompBuffer: TDecompressionStream;
|
||||
targetSize: Cardinal;
|
||||
packetID: Byte;
|
||||
begin
|
||||
targetSize := ABuffer.ReadCardinal;
|
||||
uncompBuffer := TDecompressionStream.Create(ABuffer);
|
||||
uncompStream := TEnhancedMemoryStream.Create;
|
||||
try
|
||||
uncompStream.CopyFrom(uncompBuffer, targetSize);
|
||||
uncompStream.Position := 0;
|
||||
packetID := uncompStream.ReadByte;
|
||||
if PacketHandlers[packetID] <> nil then
|
||||
begin
|
||||
if PacketHandlers[PacketID].PacketLength = 0 then
|
||||
uncompStream.Position := uncompStream.Position + 4;
|
||||
uncompStream.Lock(uncompStream.Position, uncompStream.Size - uncompStream.Position);
|
||||
PacketHandlers[PacketID].Process(uncompStream, ANetState);
|
||||
uncompStream.Unlock;
|
||||
end else
|
||||
begin
|
||||
Writeln(TimeStamp, 'Dropping client due to unknown packet: ', ANetState.Socket.PeerAddress);
|
||||
ANetState.ReceiveQueue.Clear;
|
||||
CEDServerInstance.Disconnect(ANetState.Socket);
|
||||
end;
|
||||
finally
|
||||
if uncompBuffer <> nil then uncompBuffer.Free;
|
||||
if uncompStream <> nil then uncompStream.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure OnRequestBlocksPacket(ABuffer: TEnhancedMemoryStream; ANetState: TNetState);
|
||||
var
|
||||
coords: TBlockCoordsArray;
|
||||
begin
|
||||
if not ValidateAccess(ANetState, alView) then Exit;
|
||||
SetLength(coords, (ABuffer.Size - ABuffer.Position) div SizeOf(TBlockCoords));
|
||||
ABuffer.Read(coords[0], Length(coords) * SizeOf(TBlockCoords));
|
||||
CEDServerInstance.SendPacket(ANetState, TCompressedPacket.Create(TBlockPacket.Create(coords, ANetState)));
|
||||
end;
|
||||
|
||||
procedure OnFreeBlockPacket(ABuffer: TEnhancedMemoryStream; ANetState: TNetState);
|
||||
var
|
||||
x, y: Word;
|
||||
blockSubscriptions: TLinkedList;
|
||||
begin
|
||||
if not ValidateAccess(ANetState, alView) then Exit;
|
||||
x := ABuffer.ReadWord;
|
||||
y := ABuffer.ReadWord;
|
||||
blockSubscriptions := CEDServerInstance.Landscape.BlockSubscriptions[X, Y];
|
||||
if blockSubscriptions <> nil then
|
||||
begin
|
||||
blockSubscriptions.Delete(ANetState);
|
||||
ANetState.Subscriptions.Remove(blockSubscriptions);
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure OnNoOpPacket(ABuffer: TEnhancedMemoryStream; ANetState: TNetState);
|
||||
begin
|
||||
//no operation
|
||||
end;
|
||||
|
||||
{$WARNINGS OFF}
|
||||
var
|
||||
i: Integer;
|
||||
|
||||
initialization
|
||||
for i := 0 to $FF do
|
||||
PacketHandlers[i] := nil;
|
||||
PacketHandlers[$01] := TPacketHandler.Create(0, @OnCompressedPacket);
|
||||
PacketHandlers[$02] := TPacketHandler.Create(0, @OnConnectionHandlerPacket);
|
||||
PacketHandlers[$03] := TPacketHandler.Create(0, @OnAdminHandlerPacket);
|
||||
PacketHandlers[$04] := TPacketHandler.Create(0, @OnRequestBlocksPacket);
|
||||
PacketHandlers[$05] := TPacketHandler.Create(5, @OnFreeBlockPacket);
|
||||
//$06-$0B handled by landscape
|
||||
PacketHandlers[$0C] := TPacketHandler.Create(0, @OnClientHandlerPacket);
|
||||
//$0D handled by radarmap
|
||||
//$0E handled by landscape
|
||||
PacketHandlers[$FF] := TPacketHandler.Create(1, @OnNoOpPacket);
|
||||
finalization
|
||||
for i := 0 to $FF do
|
||||
if PacketHandlers[i] <> nil then
|
||||
PacketHandlers[i].Free;
|
||||
{$WARNINGS ON}
|
||||
end.
|
||||
|
||||
(*
|
||||
* CDDL HEADER START
|
||||
*
|
||||
* The contents of this file are subject to the terms of the
|
||||
* Common Development and Distribution License, Version 1.0 only
|
||||
* (the "License"). You may not use this file except in compliance
|
||||
* with the License.
|
||||
*
|
||||
* You can obtain a copy of the license at
|
||||
* http://www.opensource.org/licenses/cddl1.php.
|
||||
* See the License for the specific language governing permissions
|
||||
* and limitations under the License.
|
||||
*
|
||||
* When distributing Covered Code, include this CDDL HEADER in each
|
||||
* file and include the License file at
|
||||
* http://www.opensource.org/licenses/cddl1.php. If applicable,
|
||||
* add the following below this CDDL HEADER, with the fields enclosed
|
||||
* by brackets "[]" replaced with your own identifying * information:
|
||||
* Portions Copyright [yyyy] [name of copyright owner]
|
||||
*
|
||||
* CDDL HEADER END
|
||||
*
|
||||
*
|
||||
* Portions Copyright 2007 Andreas Schneider
|
||||
*)
|
||||
unit UPacketHandlers;
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, SysUtils, dzlib, UConfig, UNetState, UEnhancedMemoryStream, UEnums,
|
||||
ULinkedList, URegions;
|
||||
|
||||
type
|
||||
TPacketProcessor = procedure(ABuffer: TEnhancedMemoryStream; ANetState: TNetState);
|
||||
TPacketProcessorMethod = procedure(ABuffer: TEnhancedMemoryStream; ANetState: TNetState) of object;
|
||||
|
||||
{ TPacketHandler }
|
||||
|
||||
TPacketHandler = class(TObject)
|
||||
constructor Create(ALength: Cardinal; APacketProcessor: TPacketProcessor); overload;
|
||||
constructor Create(ALength: Cardinal; APacketProcessorMethod: TPacketProcessorMethod); overload;
|
||||
procedure Process(ABuffer: TEnhancedMemoryStream; ANetState: TNetState);
|
||||
protected
|
||||
FLength: Cardinal;
|
||||
FPacketProcessor: TPacketProcessor;
|
||||
FPacketProcessorMethod: TPacketProcessorMethod;
|
||||
published
|
||||
property PacketLength: Cardinal read FLength;
|
||||
end;
|
||||
|
||||
var
|
||||
PacketHandlers: array[0..$FF] of TPacketHandler;
|
||||
|
||||
function ValidateAccess(ANetState: TNetState; ALevel: TAccessLevel): Boolean; overload;
|
||||
function ValidateAccess(ANetState: TNetState; ALevel: TAccessLevel; AX, AY: Cardinal): Boolean; overload;
|
||||
procedure RegisterPacketHandler(AID: Byte; APacketHandler: TPacketHandler);
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
UCEDServer, UPackets, UConnectionHandling, UAdminHandling, UClientHandling;
|
||||
|
||||
function ValidateAccess(ANetState: TNetState; ALevel: TAccessLevel): Boolean;
|
||||
begin
|
||||
Result := (ANetState.Account <> nil) and (ANetState.Account.AccessLevel >= ALevel);
|
||||
end;
|
||||
|
||||
function ValidateAccess(ANetState: TNetState; ALevel: TAccessLevel; AX, AY: Cardinal): Boolean;
|
||||
var
|
||||
i,j: Word;
|
||||
region: TRegion;
|
||||
rect: TRect;
|
||||
begin
|
||||
if not ValidateAccess(ANetState, ALevel) then Exit(False);
|
||||
if (ANetState.Account.Regions.Count = 0) or
|
||||
(ANetState.Account.AccessLevel >= alAdministrator) then Exit(True); //no restrictions
|
||||
|
||||
Result := False;
|
||||
for i := 0 to ANetState.Account.Regions.Count - 1 do
|
||||
begin
|
||||
region := Config.Regions.Find(ANetState.Account.Regions[i]);
|
||||
if region <> nil then
|
||||
begin
|
||||
for j := 0 to region.Areas.Count - 1 do
|
||||
begin
|
||||
rect := region.Areas.Rects[j];
|
||||
if (AX >= rect.Left) and
|
||||
(AX < rect.Right) and
|
||||
(AY >= rect.Top) and
|
||||
(AY < rect.Bottom) then
|
||||
Exit(True);
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure RegisterPacketHandler(AID: Byte; APacketHandler: TPacketHandler);
|
||||
begin
|
||||
if Assigned(PacketHandlers[AID]) then FreeAndNil(PacketHandlers[AID]);
|
||||
PacketHandlers[AID] := APacketHandler;
|
||||
end;
|
||||
|
||||
{ TPacketHandler }
|
||||
|
||||
constructor TPacketHandler.Create(ALength: Cardinal; APacketProcessor: TPacketProcessor);
|
||||
begin
|
||||
inherited Create;
|
||||
FLength := ALength;
|
||||
FPacketProcessor := APacketProcessor;
|
||||
FPacketProcessorMethod := nil;
|
||||
end;
|
||||
|
||||
constructor TPacketHandler.Create(ALength: Cardinal;
|
||||
APacketProcessorMethod: TPacketProcessorMethod);
|
||||
begin
|
||||
inherited Create;
|
||||
FLength := ALength;
|
||||
FPacketProcessor := nil;
|
||||
FPacketProcessorMethod := APacketProcessorMethod;
|
||||
end;
|
||||
|
||||
procedure TPacketHandler.Process(ABuffer: TEnhancedMemoryStream; ANetState: TNetState);
|
||||
begin
|
||||
if Assigned(FPacketProcessor) then
|
||||
FPacketProcessor(ABuffer, ANetState)
|
||||
else if Assigned(FPacketProcessorMethod) then
|
||||
FPacketProcessorMethod(ABuffer, ANetState);
|
||||
end;
|
||||
|
||||
procedure OnCompressedPacket(ABuffer: TEnhancedMemoryStream; ANetState: TNetState);
|
||||
var
|
||||
uncompStream: TEnhancedMemoryStream;
|
||||
uncompBuffer: TDecompressionStream;
|
||||
targetSize: Cardinal;
|
||||
packetID: Byte;
|
||||
begin
|
||||
targetSize := ABuffer.ReadCardinal;
|
||||
uncompBuffer := TDecompressionStream.Create(ABuffer);
|
||||
uncompStream := TEnhancedMemoryStream.Create;
|
||||
try
|
||||
uncompStream.CopyFrom(uncompBuffer, targetSize);
|
||||
uncompStream.Position := 0;
|
||||
packetID := uncompStream.ReadByte;
|
||||
if PacketHandlers[packetID] <> nil then
|
||||
begin
|
||||
if PacketHandlers[PacketID].PacketLength = 0 then
|
||||
uncompStream.Position := uncompStream.Position + 4;
|
||||
uncompStream.Lock(uncompStream.Position, uncompStream.Size - uncompStream.Position);
|
||||
PacketHandlers[PacketID].Process(uncompStream, ANetState);
|
||||
uncompStream.Unlock;
|
||||
end else
|
||||
begin
|
||||
Writeln(TimeStamp, 'Dropping client due to unknown packet: ', ANetState.Socket.PeerAddress);
|
||||
ANetState.ReceiveQueue.Clear;
|
||||
CEDServerInstance.Disconnect(ANetState.Socket);
|
||||
end;
|
||||
finally
|
||||
if uncompBuffer <> nil then uncompBuffer.Free;
|
||||
if uncompStream <> nil then uncompStream.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure OnRequestBlocksPacket(ABuffer: TEnhancedMemoryStream; ANetState: TNetState);
|
||||
var
|
||||
coords: TBlockCoordsArray;
|
||||
begin
|
||||
if not ValidateAccess(ANetState, alView) then Exit;
|
||||
SetLength(coords, (ABuffer.Size - ABuffer.Position) div SizeOf(TBlockCoords));
|
||||
ABuffer.Read(coords[0], Length(coords) * SizeOf(TBlockCoords));
|
||||
CEDServerInstance.SendPacket(ANetState, TCompressedPacket.Create(TBlockPacket.Create(coords, ANetState)));
|
||||
end;
|
||||
|
||||
procedure OnFreeBlockPacket(ABuffer: TEnhancedMemoryStream; ANetState: TNetState);
|
||||
var
|
||||
x, y: Word;
|
||||
blockSubscriptions: TLinkedList;
|
||||
begin
|
||||
if not ValidateAccess(ANetState, alView) then Exit;
|
||||
x := ABuffer.ReadWord;
|
||||
y := ABuffer.ReadWord;
|
||||
blockSubscriptions := CEDServerInstance.Landscape.BlockSubscriptions[X, Y];
|
||||
if blockSubscriptions <> nil then
|
||||
begin
|
||||
blockSubscriptions.Delete(ANetState);
|
||||
ANetState.Subscriptions.Remove(blockSubscriptions);
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure OnNoOpPacket(ABuffer: TEnhancedMemoryStream; ANetState: TNetState);
|
||||
begin
|
||||
//no operation
|
||||
end;
|
||||
|
||||
{$WARNINGS OFF}
|
||||
var
|
||||
i: Integer;
|
||||
|
||||
initialization
|
||||
for i := 0 to $FF do
|
||||
PacketHandlers[i] := nil;
|
||||
PacketHandlers[$01] := TPacketHandler.Create(0, @OnCompressedPacket);
|
||||
PacketHandlers[$02] := TPacketHandler.Create(0, @OnConnectionHandlerPacket);
|
||||
PacketHandlers[$03] := TPacketHandler.Create(0, @OnAdminHandlerPacket);
|
||||
PacketHandlers[$04] := TPacketHandler.Create(0, @OnRequestBlocksPacket);
|
||||
PacketHandlers[$05] := TPacketHandler.Create(5, @OnFreeBlockPacket);
|
||||
//$06-$0B handled by landscape
|
||||
PacketHandlers[$0C] := TPacketHandler.Create(0, @OnClientHandlerPacket);
|
||||
//$0D handled by radarmap
|
||||
//$0E handled by landscape
|
||||
PacketHandlers[$FF] := TPacketHandler.Create(1, @OnNoOpPacket);
|
||||
finalization
|
||||
for i := 0 to $FF do
|
||||
if PacketHandlers[i] <> nil then
|
||||
PacketHandlers[i].Free;
|
||||
{$WARNINGS ON}
|
||||
end.
|
||||
|
||||
|
||||
@@ -1,226 +1,226 @@
|
||||
(*
|
||||
* CDDL HEADER START
|
||||
*
|
||||
* The contents of this file are subject to the terms of the
|
||||
* Common Development and Distribution License, Version 1.0 only
|
||||
* (the "License"). You may not use this file except in compliance
|
||||
* with the License.
|
||||
*
|
||||
* You can obtain a copy of the license at
|
||||
* http://www.opensource.org/licenses/cddl1.php.
|
||||
* See the License for the specific language governing permissions
|
||||
* and limitations under the License.
|
||||
*
|
||||
* When distributing Covered Code, include this CDDL HEADER in each
|
||||
* file and include the License file at
|
||||
* http://www.opensource.org/licenses/cddl1.php. If applicable,
|
||||
* add the following below this CDDL HEADER, with the fields enclosed
|
||||
* by brackets "[]" replaced with your own identifying * information:
|
||||
* Portions Copyright [yyyy] [name of copyright owner]
|
||||
*
|
||||
* CDDL HEADER END
|
||||
*
|
||||
*
|
||||
* Portions Copyright 2007 Andreas Schneider
|
||||
*)
|
||||
unit UPackets;
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, dzlib, UEnhancedMemoryStream, UPacket, UMap, UStatics, ULinkedList,
|
||||
UNetState;
|
||||
|
||||
type
|
||||
TBlockCoords = packed record
|
||||
X: Word;
|
||||
Y: Word;
|
||||
end;
|
||||
TBlockCoordsArray = array of TBlockCoords;
|
||||
|
||||
{ TCompressedPacket }
|
||||
|
||||
TCompressedPacket = class(TPacket)
|
||||
constructor Create(APacket: TPacket);
|
||||
end;
|
||||
|
||||
{ TSendBlocksPacket }
|
||||
|
||||
TBlockPacket = class(TPacket)
|
||||
constructor Create(ACoords: TBlockCoordsArray; ANetState: TNetState);
|
||||
end;
|
||||
|
||||
{ TDrawMapPacket }
|
||||
|
||||
TDrawMapPacket = class(TPacket)
|
||||
constructor Create(AMapCell: TMapCell);
|
||||
end;
|
||||
|
||||
{ TInsertStaticPacket }
|
||||
|
||||
TInsertStaticPacket = class(TPacket)
|
||||
constructor Create(AStaticItem: TStaticItem);
|
||||
end;
|
||||
|
||||
{ TDeleteStaticPacket }
|
||||
|
||||
TDeleteStaticPacket = class(TPacket)
|
||||
constructor Create(AStaticItem: TStaticItem);
|
||||
end;
|
||||
|
||||
{ TElevateStaticPacket }
|
||||
|
||||
TElevateStaticPacket = class(TPacket)
|
||||
constructor Create(AStaticItem: TStaticItem; ANewZ: ShortInt);
|
||||
end;
|
||||
|
||||
{ TMoveStaticPacket }
|
||||
|
||||
TMoveStaticPacket = class(TPacket)
|
||||
constructor Create(AStaticItem: TStaticItem; ANewX, ANewY: Word);
|
||||
end;
|
||||
|
||||
{ THueStaticPacket }
|
||||
|
||||
THueStaticPacket = class(TPacket)
|
||||
constructor Create(AStaticItem: TStaticItem; ANewHue: Word);
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
UCEDServer;
|
||||
|
||||
{ TCompressedPacket }
|
||||
|
||||
constructor TCompressedPacket.Create(APacket: TPacket);
|
||||
var
|
||||
compBuffer: TEnhancedMemoryStream;
|
||||
compStream: TCompressionStream;
|
||||
sourceStream: TEnhancedMemoryStream;
|
||||
begin
|
||||
inherited Create($01, 0);
|
||||
sourceStream := APacket.Stream;
|
||||
compBuffer := TEnhancedMemoryStream.Create;
|
||||
compStream := TCompressionStream.Create(clMax, compBuffer);
|
||||
compStream.CopyFrom(sourceStream, 0);
|
||||
compStream.Free;
|
||||
FStream.WriteCardinal(sourceStream.Size);
|
||||
FStream.CopyFrom(compBuffer, 0);
|
||||
compBuffer.Free;
|
||||
APacket.Free;
|
||||
end;
|
||||
|
||||
{ TBlockPacket }
|
||||
|
||||
constructor TBlockPacket.Create(ACoords: TBlockCoordsArray; ANetState: TNetState);
|
||||
var
|
||||
i: Integer;
|
||||
mapBlock: TMapBlock;
|
||||
staticsBlock: TStaticBlock;
|
||||
subscriptions: TLinkedList;
|
||||
begin
|
||||
inherited Create($04, 0);
|
||||
for i := Low(ACoords) to High(ACoords) do
|
||||
begin
|
||||
mapBlock := CEDServerInstance.Landscape.GetMapBlock(ACoords[i].X, ACoords[i].Y);
|
||||
if mapBlock = nil then Continue;
|
||||
mapBlock.GetSize;
|
||||
staticsBlock := CEDServerInstance.Landscape.GetStaticBlock(ACoords[i].X, ACoords[i].Y);
|
||||
if staticsBlock = nil then Continue;
|
||||
staticsBlock.GetSize;
|
||||
|
||||
FStream.Write(ACoords[i], SizeOf(TBlockCoords));
|
||||
mapBlock.Write(FStream);
|
||||
FStream.WriteWord(staticsBlock.Items.Count);
|
||||
staticsBlock.Write(FStream);
|
||||
|
||||
if ANetState <> nil then
|
||||
begin
|
||||
subscriptions := CEDServerInstance.Landscape.BlockSubscriptions[ACoords[i].X, ACoords[i].Y];
|
||||
subscriptions.Delete(ANetState);
|
||||
subscriptions.Add(Integer(ANetState), ANetState);
|
||||
if ANetState.Subscriptions.IndexOf(subscriptions) = -1 then
|
||||
ANetState.Subscriptions.Add(subscriptions);
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
{ TDrawMapPacket }
|
||||
|
||||
constructor TDrawMapPacket.Create(AMapCell: TMapCell);
|
||||
begin
|
||||
inherited Create($06, 8);
|
||||
FStream.WriteWord(AMapCell.X);
|
||||
FStream.WriteWord(AMapCell.Y);
|
||||
FStream.WriteShortInt(AMapCell.Altitude);
|
||||
FStream.WriteWord(AMapCell.TileID);
|
||||
end;
|
||||
|
||||
{ TInsertStaticPacket }
|
||||
|
||||
constructor TInsertStaticPacket.Create(AStaticItem: TStaticItem);
|
||||
begin
|
||||
inherited Create($07, 10);
|
||||
FStream.WriteWord(AStaticItem.X);
|
||||
FStream.WriteWord(AStaticItem.Y);
|
||||
FStream.WriteShortInt(AStaticItem.Z);
|
||||
FStream.WriteWord(AStaticItem.TileID);
|
||||
FStream.WriteWord(AStaticItem.Hue);
|
||||
end;
|
||||
|
||||
{ TDeleteStaticPacket }
|
||||
|
||||
constructor TDeleteStaticPacket.Create(AStaticItem: TStaticItem);
|
||||
begin
|
||||
inherited Create($08, 10);
|
||||
FStream.WriteWord(AStaticItem.X);
|
||||
FStream.WriteWord(AStaticItem.Y);
|
||||
FStream.WriteShortInt(AStaticItem.Z);
|
||||
FStream.WriteWord(AStaticItem.TileID);
|
||||
FStream.WriteWord(AStaticItem.Hue);
|
||||
end;
|
||||
|
||||
{ TElevateStaticPacket }
|
||||
|
||||
constructor TElevateStaticPacket.Create(AStaticItem: TStaticItem; ANewZ: ShortInt);
|
||||
begin
|
||||
inherited Create($09, 11);
|
||||
FStream.WriteWord(AStaticItem.X);
|
||||
FStream.WriteWord(AStaticItem.Y);
|
||||
FStream.WriteShortInt(AStaticItem.Z);
|
||||
FStream.WriteWord(AStaticItem.TileID);
|
||||
FStream.WriteWord(AStaticItem.Hue);
|
||||
FStream.WriteShortInt(ANewZ);
|
||||
end;
|
||||
|
||||
{ TMoveStaticPacket }
|
||||
|
||||
constructor TMoveStaticPacket.Create(AStaticItem: TStaticItem; ANewX,
|
||||
ANewY: Word);
|
||||
begin
|
||||
inherited Create($0A, 14);
|
||||
FStream.WriteWord(AStaticItem.X);
|
||||
FStream.WriteWord(AStaticItem.Y);
|
||||
FStream.WriteShortInt(AStaticItem.Z);
|
||||
FStream.WriteWord(AStaticItem.TileID);
|
||||
FStream.WriteWord(AStaticItem.Hue);
|
||||
FStream.WriteWord(ANewX);
|
||||
FStream.WriteWord(ANewY);
|
||||
end;
|
||||
|
||||
{ THueStaticPacket }
|
||||
|
||||
constructor THueStaticPacket.Create(AStaticItem: TStaticItem; ANewHue: Word);
|
||||
begin
|
||||
inherited Create($0B, 12);
|
||||
FStream.WriteWord(AStaticItem.X);
|
||||
FStream.WriteWord(AStaticItem.Y);
|
||||
FStream.WriteShortInt(AStaticItem.Z);
|
||||
FStream.WriteWord(AStaticItem.TileID);
|
||||
FStream.WriteWord(AStaticItem.Hue);
|
||||
FStream.WriteWord(ANewHue);
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
(*
|
||||
* CDDL HEADER START
|
||||
*
|
||||
* The contents of this file are subject to the terms of the
|
||||
* Common Development and Distribution License, Version 1.0 only
|
||||
* (the "License"). You may not use this file except in compliance
|
||||
* with the License.
|
||||
*
|
||||
* You can obtain a copy of the license at
|
||||
* http://www.opensource.org/licenses/cddl1.php.
|
||||
* See the License for the specific language governing permissions
|
||||
* and limitations under the License.
|
||||
*
|
||||
* When distributing Covered Code, include this CDDL HEADER in each
|
||||
* file and include the License file at
|
||||
* http://www.opensource.org/licenses/cddl1.php. If applicable,
|
||||
* add the following below this CDDL HEADER, with the fields enclosed
|
||||
* by brackets "[]" replaced with your own identifying * information:
|
||||
* Portions Copyright [yyyy] [name of copyright owner]
|
||||
*
|
||||
* CDDL HEADER END
|
||||
*
|
||||
*
|
||||
* Portions Copyright 2007 Andreas Schneider
|
||||
*)
|
||||
unit UPackets;
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, dzlib, UEnhancedMemoryStream, UPacket, UMap, UStatics, ULinkedList,
|
||||
UNetState;
|
||||
|
||||
type
|
||||
TBlockCoords = packed record
|
||||
X: Word;
|
||||
Y: Word;
|
||||
end;
|
||||
TBlockCoordsArray = array of TBlockCoords;
|
||||
|
||||
{ TCompressedPacket }
|
||||
|
||||
TCompressedPacket = class(TPacket)
|
||||
constructor Create(APacket: TPacket);
|
||||
end;
|
||||
|
||||
{ TSendBlocksPacket }
|
||||
|
||||
TBlockPacket = class(TPacket)
|
||||
constructor Create(ACoords: TBlockCoordsArray; ANetState: TNetState);
|
||||
end;
|
||||
|
||||
{ TDrawMapPacket }
|
||||
|
||||
TDrawMapPacket = class(TPacket)
|
||||
constructor Create(AMapCell: TMapCell);
|
||||
end;
|
||||
|
||||
{ TInsertStaticPacket }
|
||||
|
||||
TInsertStaticPacket = class(TPacket)
|
||||
constructor Create(AStaticItem: TStaticItem);
|
||||
end;
|
||||
|
||||
{ TDeleteStaticPacket }
|
||||
|
||||
TDeleteStaticPacket = class(TPacket)
|
||||
constructor Create(AStaticItem: TStaticItem);
|
||||
end;
|
||||
|
||||
{ TElevateStaticPacket }
|
||||
|
||||
TElevateStaticPacket = class(TPacket)
|
||||
constructor Create(AStaticItem: TStaticItem; ANewZ: ShortInt);
|
||||
end;
|
||||
|
||||
{ TMoveStaticPacket }
|
||||
|
||||
TMoveStaticPacket = class(TPacket)
|
||||
constructor Create(AStaticItem: TStaticItem; ANewX, ANewY: Word);
|
||||
end;
|
||||
|
||||
{ THueStaticPacket }
|
||||
|
||||
THueStaticPacket = class(TPacket)
|
||||
constructor Create(AStaticItem: TStaticItem; ANewHue: Word);
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
UCEDServer;
|
||||
|
||||
{ TCompressedPacket }
|
||||
|
||||
constructor TCompressedPacket.Create(APacket: TPacket);
|
||||
var
|
||||
compBuffer: TEnhancedMemoryStream;
|
||||
compStream: TCompressionStream;
|
||||
sourceStream: TEnhancedMemoryStream;
|
||||
begin
|
||||
inherited Create($01, 0);
|
||||
sourceStream := APacket.Stream;
|
||||
compBuffer := TEnhancedMemoryStream.Create;
|
||||
compStream := TCompressionStream.Create(clMax, compBuffer);
|
||||
compStream.CopyFrom(sourceStream, 0);
|
||||
compStream.Free;
|
||||
FStream.WriteCardinal(sourceStream.Size);
|
||||
FStream.CopyFrom(compBuffer, 0);
|
||||
compBuffer.Free;
|
||||
APacket.Free;
|
||||
end;
|
||||
|
||||
{ TBlockPacket }
|
||||
|
||||
constructor TBlockPacket.Create(ACoords: TBlockCoordsArray; ANetState: TNetState);
|
||||
var
|
||||
i: Integer;
|
||||
mapBlock: TMapBlock;
|
||||
staticsBlock: TStaticBlock;
|
||||
subscriptions: TLinkedList;
|
||||
begin
|
||||
inherited Create($04, 0);
|
||||
for i := Low(ACoords) to High(ACoords) do
|
||||
begin
|
||||
mapBlock := CEDServerInstance.Landscape.GetMapBlock(ACoords[i].X, ACoords[i].Y);
|
||||
if mapBlock = nil then Continue;
|
||||
mapBlock.GetSize;
|
||||
staticsBlock := CEDServerInstance.Landscape.GetStaticBlock(ACoords[i].X, ACoords[i].Y);
|
||||
if staticsBlock = nil then Continue;
|
||||
staticsBlock.GetSize;
|
||||
|
||||
FStream.Write(ACoords[i], SizeOf(TBlockCoords));
|
||||
mapBlock.Write(FStream);
|
||||
FStream.WriteWord(staticsBlock.Items.Count);
|
||||
staticsBlock.Write(FStream);
|
||||
|
||||
if ANetState <> nil then
|
||||
begin
|
||||
subscriptions := CEDServerInstance.Landscape.BlockSubscriptions[ACoords[i].X, ACoords[i].Y];
|
||||
subscriptions.Delete(ANetState);
|
||||
subscriptions.Add(Integer(ANetState), ANetState);
|
||||
if ANetState.Subscriptions.IndexOf(subscriptions) = -1 then
|
||||
ANetState.Subscriptions.Add(subscriptions);
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
{ TDrawMapPacket }
|
||||
|
||||
constructor TDrawMapPacket.Create(AMapCell: TMapCell);
|
||||
begin
|
||||
inherited Create($06, 8);
|
||||
FStream.WriteWord(AMapCell.X);
|
||||
FStream.WriteWord(AMapCell.Y);
|
||||
FStream.WriteShortInt(AMapCell.Altitude);
|
||||
FStream.WriteWord(AMapCell.TileID);
|
||||
end;
|
||||
|
||||
{ TInsertStaticPacket }
|
||||
|
||||
constructor TInsertStaticPacket.Create(AStaticItem: TStaticItem);
|
||||
begin
|
||||
inherited Create($07, 10);
|
||||
FStream.WriteWord(AStaticItem.X);
|
||||
FStream.WriteWord(AStaticItem.Y);
|
||||
FStream.WriteShortInt(AStaticItem.Z);
|
||||
FStream.WriteWord(AStaticItem.TileID);
|
||||
FStream.WriteWord(AStaticItem.Hue);
|
||||
end;
|
||||
|
||||
{ TDeleteStaticPacket }
|
||||
|
||||
constructor TDeleteStaticPacket.Create(AStaticItem: TStaticItem);
|
||||
begin
|
||||
inherited Create($08, 10);
|
||||
FStream.WriteWord(AStaticItem.X);
|
||||
FStream.WriteWord(AStaticItem.Y);
|
||||
FStream.WriteShortInt(AStaticItem.Z);
|
||||
FStream.WriteWord(AStaticItem.TileID);
|
||||
FStream.WriteWord(AStaticItem.Hue);
|
||||
end;
|
||||
|
||||
{ TElevateStaticPacket }
|
||||
|
||||
constructor TElevateStaticPacket.Create(AStaticItem: TStaticItem; ANewZ: ShortInt);
|
||||
begin
|
||||
inherited Create($09, 11);
|
||||
FStream.WriteWord(AStaticItem.X);
|
||||
FStream.WriteWord(AStaticItem.Y);
|
||||
FStream.WriteShortInt(AStaticItem.Z);
|
||||
FStream.WriteWord(AStaticItem.TileID);
|
||||
FStream.WriteWord(AStaticItem.Hue);
|
||||
FStream.WriteShortInt(ANewZ);
|
||||
end;
|
||||
|
||||
{ TMoveStaticPacket }
|
||||
|
||||
constructor TMoveStaticPacket.Create(AStaticItem: TStaticItem; ANewX,
|
||||
ANewY: Word);
|
||||
begin
|
||||
inherited Create($0A, 14);
|
||||
FStream.WriteWord(AStaticItem.X);
|
||||
FStream.WriteWord(AStaticItem.Y);
|
||||
FStream.WriteShortInt(AStaticItem.Z);
|
||||
FStream.WriteWord(AStaticItem.TileID);
|
||||
FStream.WriteWord(AStaticItem.Hue);
|
||||
FStream.WriteWord(ANewX);
|
||||
FStream.WriteWord(ANewY);
|
||||
end;
|
||||
|
||||
{ THueStaticPacket }
|
||||
|
||||
constructor THueStaticPacket.Create(AStaticItem: TStaticItem; ANewHue: Word);
|
||||
begin
|
||||
inherited Create($0B, 12);
|
||||
FStream.WriteWord(AStaticItem.X);
|
||||
FStream.WriteWord(AStaticItem.Y);
|
||||
FStream.WriteShortInt(AStaticItem.Z);
|
||||
FStream.WriteWord(AStaticItem.TileID);
|
||||
FStream.WriteWord(AStaticItem.Hue);
|
||||
FStream.WriteWord(ANewHue);
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user