Revision 13:c78b5eafa10e Client/UAdminHandling.pas

b/Client/UAdminHandling.pas
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 UAdminHandling;
27

  
28
{$mode objfpc}{$H+}
29

  
30
interface
31

  
32
uses
33
  Classes, SysUtils, UPacket, UPacketHandlers, UEnhancedMemoryStream, UEnums;
34
  
35
type
36

  
37
  { TFlushServerPacket }
38

  
39
  TFlushServerPacket = class(TPacket)
40
    constructor Create;
41
  end;
42
  
43
  { TQuitServerPacket }
44

  
45
  TQuitServerPacket = class(TPacket)
46
    constructor Create(AReason: string);
47
  end;
48
  
49
procedure OnAdminHandlerPacket(ABuffer: TEnhancedMemoryStream);
50

  
51
var
52
  AdminPacketHandlers: array[0..$FF] of TPacketHandler;
53

  
54
implementation
55

  
56
procedure OnAdminHandlerPacket(ABuffer: TEnhancedMemoryStream);
57
var
58
  packetHandler: TPacketHandler;
59
begin
60
  packetHandler := AdminPacketHandlers[ABuffer.ReadByte];
61
  if packetHandler <> nil then
62
    packetHandler.Process(ABuffer);
63
end;
64

  
65
{ TFlushServerPacket }
66

  
67
constructor TFlushServerPacket.Create;
68
begin
69
  inherited Create($03, 0);
70
  FStream.WriteByte($01);
71
end;
72

  
73
{ TQuitServerPacket }
74

  
75
constructor TQuitServerPacket.Create(AReason: string);
76
begin
77
  inherited Create($03, 0);
78
  FStream.WriteByte($02);
79
  FStream.WriteStringNull(AReason);
80
end;
81

  
82
{$WARNINGS OFF}
83
var
84
  i: Integer;
85

  
86
initialization
87
  for i := 0 to $FF do
88
    AdminPacketHandlers[i] := nil;
89
finalization
90
  for i := 0 to $FF do
91
    if AdminPacketHandlers[i] <> nil then
92
      AdminPacketHandlers[i].Free;
93
{$WARNINGS ON}
94

  
95
end.
96

  
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 UAdminHandling;
27

  
28
{$mode objfpc}{$H+}
29

  
30
interface
31

  
32
uses
33
  Classes, SysUtils, UPacket, UPacketHandlers, UEnhancedMemoryStream, UEnums;
34
  
35
type
36

  
37
  TAdminHandlerAlreadyAssignedException = class(Exception);
38

  
39
  { TFlushServerPacket }
40

  
41
  TFlushServerPacket = class(TPacket)
42
    constructor Create;
43
  end;
44
  
45
  { TQuitServerPacket }
46

  
47
  TQuitServerPacket = class(TPacket)
48
    constructor Create(AReason: string);
49
  end;
50
  
51
procedure AssignAdminPacketHandler(APacketID: Byte; AHandler: TPacketHandler);
52
procedure OnAdminHandlerPacket(ABuffer: TEnhancedMemoryStream);
53

  
54
var
55
  AdminPacketHandlers: array[0..$FF] of TPacketHandler;
56

  
57
implementation
58

  
59
procedure AssignAdminPacketHandler(APacketID: Byte; AHandler: TPacketHandler);
60
begin
61
  if AdminPacketHandlers[APacketID] <> nil then
62
    raise TAdminHandlerAlreadyAssignedException.CreateFmt(
63
      'The AdminPacketHandler $%.2x is already assigned!', [APacketID]);
64

  
65
  AdminPacketHandlers[APacketID] := AHandler;
66
end;
67

  
68
procedure OnAdminHandlerPacket(ABuffer: TEnhancedMemoryStream);
69
var
70
  packetHandler: TPacketHandler;
71
begin
72
  packetHandler := AdminPacketHandlers[ABuffer.ReadByte];
73
  if packetHandler <> nil then
74
    packetHandler.Process(ABuffer);
75
end;
76

  
77
{ TFlushServerPacket }
78

  
79
constructor TFlushServerPacket.Create;
80
begin
81
  inherited Create($03, 0);
82
  FStream.WriteByte($01);
83
end;
84

  
85
{ TQuitServerPacket }
86

  
87
constructor TQuitServerPacket.Create(AReason: string);
88
begin
89
  inherited Create($03, 0);
90
  FStream.WriteByte($02);
91
  FStream.WriteStringNull(AReason);
92
end;
93

  
94
{$WARNINGS OFF}
95
var
96
  i: Integer;
97

  
98
initialization
99
  for i := 0 to $FF do
100
    AdminPacketHandlers[i] := nil;
101
finalization
102
  for i := 0 to $FF do
103
    if AdminPacketHandlers[i] <> nil then
104
      AdminPacketHandlers[i].Free;
105
{$WARNINGS ON}
106

  
107
end.
108

  

Also available in: Unified diff