root / Client / UPacketHandlers.pas @ 119:66352054ce4d
History | View | Annotate | Download (4.5 kB)
| 1 | (*
|
|---|---|
| 2 | * CDDL HEADER START |
| 3 | * |
| 4 | * The contents of this file are subject to the terms of the |
| 5 | * Common Development and Distribution License, Version 1.0 only |
| 6 | * (the "License"). You may not use this file except in compliance |
| 7 | * with the License. |
| 8 | * |
| 9 | * You can obtain a copy of the license at |
| 10 | * http://www.opensource.org/licenses/cddl1.php. |
| 11 | * See the License for the specific language governing permissions |
| 12 | * and limitations under the License. |
| 13 | * |
| 14 | * When distributing Covered Code, include this CDDL HEADER in each |
| 15 | * file and include the License file at |
| 16 | * http://www.opensource.org/licenses/cddl1.php. If applicable, |
| 17 | * add the following below this CDDL HEADER, with the fields enclosed |
| 18 | * by brackets "[]" replaced with your own identifying * information: |
| 19 | * Portions Copyright [yyyy] [name of copyright owner] |
| 20 | * |
| 21 | * CDDL HEADER END |
| 22 | * |
| 23 | * |
| 24 | * Portions Copyright 2007 Andreas Schneider |
| 25 | *) |
| 26 | unit UPacketHandlers;
|
| 27 | |
| 28 | interface
|
| 29 | |
| 30 | uses
|
| 31 | SysUtils, dzlib, UEnhancedMemoryStream; |
| 32 | |
| 33 | type
|
| 34 | TPacketProcessor = procedure(ABuffer: TEnhancedMemoryStream);
|
| 35 | TPacketProcessorMethod = procedure(ABuffer: TEnhancedMemoryStream) of object; |
| 36 | |
| 37 | { TPacketHandler }
|
| 38 | |
| 39 | TPacketHandler = class(TObject)
|
| 40 | constructor Create(ALength: Cardinal; APacketProcessor: TPacketProcessor); overload; |
| 41 | constructor Create(ALength: Cardinal; APacketProcessorMethod: TPacketProcessorMethod); overload; |
| 42 | procedure Process(ABuffer: TEnhancedMemoryStream);
|
| 43 | protected
|
| 44 | FLength: Cardinal; |
| 45 | FPacketProcessor: TPacketProcessor; |
| 46 | FPacketProcessorMethod: TPacketProcessorMethod; |
| 47 | published
|
| 48 | property PacketLength: Cardinal read FLength; |
| 49 | end;
|
| 50 | |
| 51 | var
|
| 52 | PacketHandlers: array[0..$FF] of TPacketHandler; |
| 53 | |
| 54 | procedure RegisterPacketHandler(AID: Byte; APacketHandler: TPacketHandler);
|
| 55 | |
| 56 | implementation
|
| 57 | |
| 58 | uses
|
| 59 | UAdminHandling; |
| 60 | |
| 61 | procedure RegisterPacketHandler(AID: Byte; APacketHandler: TPacketHandler);
|
| 62 | begin
|
| 63 | FreeAndNil(PacketHandlers[AID]); |
| 64 | PacketHandlers[AID] := APacketHandler; |
| 65 | end;
|
| 66 | |
| 67 | { TPacketHandler }
|
| 68 | |
| 69 | constructor TPacketHandler.Create(ALength: Cardinal; APacketProcessor: TPacketProcessor);
|
| 70 | begin
|
| 71 | inherited Create;
|
| 72 | FLength := ALength; |
| 73 | FPacketProcessor := APacketProcessor; |
| 74 | FPacketProcessorMethod := nil;
|
| 75 | end;
|
| 76 | |
| 77 | constructor TPacketHandler.Create(ALength: Cardinal;
|
| 78 | APacketProcessorMethod: TPacketProcessorMethod); |
| 79 | begin
|
| 80 | inherited Create;
|
| 81 | FLength := ALength; |
| 82 | FPacketProcessor := nil;
|
| 83 | FPacketProcessorMethod := APacketProcessorMethod; |
| 84 | end;
|
| 85 | |
| 86 | procedure TPacketHandler.Process(ABuffer: TEnhancedMemoryStream);
|
| 87 | begin
|
| 88 | if Assigned(FPacketProcessor) then |
| 89 | FPacketProcessor(ABuffer) |
| 90 | else if Assigned(FPacketProcessorMethod) then |
| 91 | FPacketProcessorMethod(ABuffer); |
| 92 | end;
|
| 93 | |
| 94 | procedure OnCompressedPacket(ABuffer: TEnhancedMemoryStream);
|
| 95 | var
|
| 96 | uncompStream: TEnhancedMemoryStream; |
| 97 | uncompBuffer: TDecompressionStream; |
| 98 | targetSize: Cardinal; |
| 99 | packetID: Byte; |
| 100 | begin
|
| 101 | //writeln('compressed size: ', ABuffer.Size);
|
| 102 | targetSize := ABuffer.ReadCardinal; |
| 103 | //writeln('uncompressed size: ', targetSize);
|
| 104 | uncompBuffer := TDecompressionStream.Create(ABuffer); |
| 105 | uncompStream := TEnhancedMemoryStream.Create; |
| 106 | try
|
| 107 | uncompStream.CopyFrom(uncompBuffer, targetSize); |
| 108 | uncompStream.Position := 0;
|
| 109 | packetID := uncompStream.ReadByte; |
| 110 | if PacketHandlers[packetID] <> nil then |
| 111 | begin
|
| 112 | if PacketHandlers[PacketID].PacketLength = 0 then |
| 113 | uncompStream.Position := uncompStream.Position + 4;
|
| 114 | uncompStream.Lock(uncompStream.Position, uncompStream.Size - uncompStream.Position); |
| 115 | PacketHandlers[PacketID].Process(uncompStream); |
| 116 | uncompStream.Unlock; |
| 117 | end else |
| 118 | begin
|
| 119 | {Writeln('Dropping client due to unknown packet: ', ANetState.Socket.PeerAddress);
|
| 120 | ANetState.Socket.Disconnect; |
| 121 | ANetState.ReceiveQueue.Clear;} |
| 122 | end;
|
| 123 | finally
|
| 124 | if uncompBuffer <> nil then uncompBuffer.Free; |
| 125 | if uncompStream <> nil then uncompStream.Free; |
| 126 | end;
|
| 127 | end;
|
| 128 | |
| 129 | |
| 130 | {$WARNINGS OFF}
|
| 131 | var
|
| 132 | i: Integer; |
| 133 | |
| 134 | initialization
|
| 135 | for i := 0 to $FF do |
| 136 | PacketHandlers[i] := nil;
|
| 137 | PacketHandlers[$01] := TPacketHandler.Create(0, @OnCompressedPacket); |
| 138 | //$02 --> ConnectionHandling, done by TdmNetwork
|
| 139 | PacketHandlers[$03] := TPacketHandler.Create(0, @OnAdminHandlerPacket);; |
| 140 | //$04 --> handled by TLandscape
|
| 141 | //$06-$0B --> handled by TLandscape
|
| 142 | //$0C --> ClientHandling, done by TfrmMain
|
| 143 | //$0D --> RadarMapHandling, done by TfrmRadarMap
|
| 144 | finalization
|
| 145 | for i := 0 to $FF do |
| 146 | if PacketHandlers[i] <> nil then |
| 147 | PacketHandlers[i].Free; |
| 148 | {$WARNINGS ON}
|
| 149 | end.
|
| 150 |