summaryrefslogtreecommitdiff
path: root/plugins/Chess4Net/ClientQueueUnit.pas
blob: 7dd7d4c5117af89cb107b28d85ab534b5f078eb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
////////////////////////////////////////////////////////////////////////////////
// All code below is exclusively owned by author of Chess4Net - Pavel Perminov
// (packpaul@mail.ru, packpaul1@gmail.com).
// Any changes, modifications, borrowing and adaptation are a subject for
// explicit permition from the owner.

unit ClientQueueUnit;

interface

type
  TClientName = string[25];

  PClientNode = ^TClientNode;

  TClientNode = record
    n: word;
    name: TClientName;
    handler: pointer;
    next: PClientNode;
  end;

  TClientQueue = object
    Number: word;
    procedure Add(const handler: pointer; const name: TClientName = '');
    procedure Remove(const handler: pointer);
    function GetName(const handler: pointer): TClientName;
    function GetNum(const handler: pointer): word;
    function GetHandler(const num: word): pointer;
{
    function Contains(const handler: pointer): boolean;
}
    constructor Create(const max: word);
    destructor Free;
  private
    first: PClientNode;
    MaxClientsNum: word;
  end;

implementation

function TClientQueue.GetHandler(const num: word): pointer;
var
  n: PClientNode;
begin
  n:= first;
  while n <> nil do
    begin
      if num = n^.n then
        begin
          Result:= n^.handler;
          exit;
        end;
      n:= n^.next;
    end;
  Result:= nil;
end;

procedure TClientQueue.Add(const handler: pointer; const name: TClientName);
var
  n: PClientNode;
  p: ^PClientNode;
begin
  if Number = MaxClientsNum then exit;

  n:= first; p:= @first;
  while n <> nil do
    begin
      p:= addr(n^.next);
      n:= n^.next;
    end;
  new(n); inc(Number);
  n^.name:= name;
  n^.handler:= handler;
  n^.next:= nil;
  n^.n:= Number;
  p^:= n;
end;

procedure TClientQueue.Remove(const handler: pointer);
var
  n: PClientNode;
  p: ^PClientNode;
begin
  n:= first; p:= @first;
  while n <> nil do
    begin
      if n^.handler = handler then
        begin
          n:= n^.next;
          dispose(p^);
          p^:= n;
          break;
        end;
      p:= addr(n^.next);
      n:= n^.next;      
    end;

  while n <> nil do
    begin
      dec(n^.n);
      n:= n^.next;
    end;
  dec(Number);
end;


function TClientQueue.GetName(const handler: pointer): TClientName;
var
  n: PClientNode;
begin
  n:= first;
  while n <> nil do
    begin
      if handler = n^.handler then
        begin
          Result:= n^.name;
          exit;
        end;
      n:= n^.next;
    end;
  Result:= '';
end;

function TClientQueue.GetNum(const handler: pointer): word;
var
  n: PClientNode;
begin
  n:= first;
  while n <> nil do
    begin
      if handler = n^.handler then
        begin
          Result:= n^.n;
          exit;
        end;
      n:= n^.next;
    end;
  Result:= 0;
end;

{
function TClientQueue.Contains(const handler: pointer): boolean;
}

constructor TClientQueue.Create(const max: word);
begin
  MaxClientsNum:= max;
end;


destructor TClientQueue.Free;
var
  n: PClientNode;
begin
  while first <> nil do
    begin
      n:= first^.next;
      dispose(first);
      first:= n;
    end;
  Number:= 0;  
end;

end.