-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgnat-sockets-server.adb
2859 lines (2738 loc) · 95.6 KB
/
gnat-sockets-server.adb
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- --
-- package GNAT.Sockets.Server Copyright (c) Dmitry A. Kazakov --
-- Implementation Luebeck --
-- Winter, 2012 --
-- --
-- Last revision : 14:53 29 Feb 2020 --
-- --
-- This library is free software; you can redistribute it and/or --
-- modify it under the terms of the GNU General Public License as --
-- published by the Free Software Foundation; either version 2 of --
-- the License, or (at your option) any later version. This library --
-- is distributed in the hope that it will be useful, but WITHOUT --
-- ANY WARRANTY; without even the implied warranty of --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --
-- General Public License for more details. You should have --
-- received a copy of the GNU General Public License along with --
-- this library; if not, write to the Free Software Foundation, --
-- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. --
-- --
-- As a special exception, if other files instantiate generics from --
-- this unit, or you link this unit with other files to produce an --
-- executable, this unit does not by itself cause the resulting --
-- executable to be covered by the GNU General Public License. This --
-- exception does not however invalidate any other reasons why the --
-- executable file might be covered by the GNU Public License. --
--____________________________________________________________________--
with Ada.Calendar; use Ada.Calendar;
with Ada.Characters.Handling; use Ada.Characters.Handling;
with Ada.IO_Exceptions; use Ada.IO_Exceptions;
with Strings_Edit; use Strings_Edit;
with Strings_Edit.Integers; use Strings_Edit.Integers;
with Ada.Unchecked_Deallocation;
package body GNAT.Sockets.Server is
Receive_Masks : constant array (IO_Tracing_Mode) of Factory_Flags :=
( Trace_None => 0,
Trace_Encoded => Trace_Encoded_Received,
Trace_Decoded => Trace_Decoded_Received,
Trace_Any => Trace_Encoded_Received
or Trace_Decoded_Received
);
Sent_Masks : constant array (IO_Tracing_Mode) of Factory_Flags :=
( Trace_None => 0,
Trace_Encoded => Trace_Encoded_Sent,
Trace_Decoded => Trace_Decoded_Sent,
Trace_Any => Trace_Encoded_Sent
or Trace_Decoded_Sent
);
procedure Free is
new Ada.Unchecked_Deallocation (Encoder'Class, Encoder_Ptr);
procedure Activated (Client : in out Connection) is
begin
null;
end Activated;
procedure Append
( List : in out Connection_Ptr;
Item : Connection_Ptr;
Count : in out Integer
) is
begin
if Item.Successor = null then
if List = null then
List := Item;
Item.Successor := Item;
Item.Predecessor := Item;
else
Item.Successor := List;
Item.Predecessor := List.Predecessor;
List.Predecessor := Item;
Item.Predecessor.Successor := Item;
end if;
Count := Count + 1;
end if;
end Append;
function Available_To_Process (Client : Connection)
return Stream_Element_Count is
begin
return Used (Client.Read);
end Available_To_Process;
function Available_To_Send (Client : Connection)
return Stream_Element_Count is
begin
return Free (Client.Written);
end Available_To_Send;
procedure Clear (Client : in out Connection'Class) is
begin
Client.Failed := False; -- Clean I/O state
Client.External_Action := False;
Client.Data_Sent := False;
Client.Dont_Block := False;
Client.Read.Expected := 0;
Client.Read.First_Read := 0;
Client.Read.Free_To_Read := 0;
Client.Written.First_Written := 0;
Client.Written.Free_To_Write := 0;
Client.Written.Send_Blocked := False;
Free (Client.Transport);
end Clear;
procedure Close (Socket : in out Socket_Type) is
begin
if Socket /= No_Socket then
begin
Shutdown_Socket (Socket);
exception
when others =>
null;
end;
begin
Close_Socket (Socket);
exception
when others =>
null;
end;
Socket := No_Socket;
end if;
end Close;
procedure Connect
( Listener : in out Connections_Server;
Client : Connection_Ptr;
Host : String;
Port : Port_Type;
Max_Connect_No : Positive := Positive'Last;
Overlapped : Stream_Element_Count :=
Stream_Element_Count'Last
) is
Address : Sock_Addr_Type renames Client.Client_Address;
Option : Request_Type := (Non_Blocking_IO, True);
begin
if Client.Socket /= No_Socket then
Raise_Exception
( Use_Error'Identity,
"Connection " & Image (Address) & " is already in use"
);
end if;
Address.Addr := To_Addr (Host);
Address.Port := Port;
Create_Socket (Client.Socket);
Set_Socket_Option
( Client.Socket,
Socket_Level,
(Reuse_Address, True)
);
Control_Socket (Client.Socket, Option);
Connect_Parameters_Set
( Client.all,
Host,
Address,
Max_Connect_No
);
Client.Session := Session_Disconnected;
Client.Client := True;
Client.Connect_No := 0;
Client.Max_Connect_No := Max_Connect_No;
Client.Socket_Listener := Listener'Unchecked_Access;
Client.Overlapped_Read := Stream_Element_Count'Min
( Overlapped,
Client.Output_Size
);
Increment_Count (Client.all);
Listener.Request.Connect (Client);
end Connect;
procedure Connect_Error
( Client : in out Connection;
Error : Error_Type
) is
begin
null;
end Connect_Error;
procedure Connect_Parameters_Set
( Client : in out Connection;
Host : String;
Address : Sock_Addr_Type;
Max_Connect_No : Positive
) is
begin
null;
end Connect_Parameters_Set;
procedure Connected (Client : in out Connection) is
begin
null;
end Connected;
procedure Connected
( Listener : in out Connections_Server;
Client : in out Connection'Class
) is
begin
Client.Session := Session_Active;
end Connected;
function Create
( Factory : access Connections_Factory;
Listener : access Connections_Server'Class;
From : Sock_Addr_Type
) return Connection_Ptr is
begin
return null;
end Create;
procedure Create_Socket
( Listener : in out Connections_Server;
Socket : in out Socket_Type;
Address : Sock_Addr_Type
) is
begin
Create_Socket (Socket);
Set_Socket_Option (Socket, Socket_Level, (Reuse_Address, True));
Bind_Socket (Socket, Address);
Listen_Socket (Socket);
end Create_Socket;
function Create_Transport
( Factory : access Connections_Factory;
Listener : access Connections_Server'Class;
Client : access Connection'Class
) return Encoder_Ptr is
begin
return null;
end Create_Transport;
procedure Create_Transport (Client : in out Connection) is
begin
if Client.Transport /= null then
Raise_Exception
( Status_Error'Identity,
"Connection already has a transport layer"
);
end if;
Client.Transport :=
Create_Transport
( Client.Socket_Listener.Factory,
Client.Socket_Listener,
Client'Unchecked_Access
);
if Client.Transport = null then
Raise_Exception
( Status_Error'Identity,
"Connection transport layer is not supported"
);
end if;
Client.Session := Session_Handshaking;
if Client.Client then
Append
( Client.Socket_Listener.Postponed,
Client'Unchecked_Access,
Client.Socket_Listener.Postponed_Count
);
end if;
end Create_Transport;
procedure Data_Sent
( Listener : in out Connections_Server;
Client : Connection_Ptr
) is
begin
Client.Data_Sent := False;
Sent (Client.all);
end Data_Sent;
procedure Disconnected (Client : in out Connection) is
begin
null;
end Disconnected;
procedure Disconnected
( Listener : in out Connections_Server;
Client : in out Connection'Class
) is
begin
Client.Session := Session_Disconnected;
Remove (Listener.Postponed, Client, Listener.Postponed_Count);
end Disconnected;
procedure Downed
( Listener : in out Connections_Server;
Client : in out Connection'Class
) is
begin
Client.Session := Session_Down;
end Downed;
procedure Downed (Client : in out Connection) is
begin
null;
end Downed;
procedure Do_Connect
( Listener : in out Connections_Server'Class;
Client : in out Connection_Ptr
) is
Status : Selector_Status;
begin
Client.Connect_No := Client.Connect_No + 1;
Client.Session := Session_Connecting;
if Client.Connect_No > Client.Max_Connect_No then
Client.Try_To_Reconnect := False; -- Ensure connection killed
Save_Occurrence (Client.Last_Error, Null_Occurrence);
Stop (Listener, Client);
else
Clear (Client.all);
Connect_Socket
( Socket => Client.Socket,
Server => Client.Client_Address,
Timeout => 0.0,
Selector => Listener.Selector'Unrestricted_Access,
Status => Status
);
if Status = Completed then
Set (Listener.Read_Sockets, Client.Socket);
On_Connected (Listener, Client.all);
end if;
end if;
exception
when Connection_Error =>
Client.Try_To_Reconnect := False; -- Ensure connection killed
Save_Occurrence (Client.Last_Error, Null_Occurrence);
Stop (Listener, Client);
when Error : Socket_Error =>
if Resolve_Exception (Error) = Operation_Now_In_Progress then
Trace_Sending
( Listener.Factory.all,
Client.all,
False,
", connecting to ..."
);
else
Trace_Error
( Listener.Factory.all,
"Connect socket",
Error
);
Client.Try_To_Reconnect := False; -- Ensure object killed
Save_Occurrence (Client.Last_Error, Error);
Stop (Listener, Client);
end if;
when Error : others =>
Trace_Error
( Listener.Factory.all,
"Connect socket",
Error
);
Client.Try_To_Reconnect := False; -- Ensure connection killed
Save_Occurrence (Client.Last_Error, Error);
Stop (Listener, Client);
end Do_Connect;
procedure Elevated (Client : in out Connection) is
begin
null;
end Elevated;
procedure Fill_From_Stream
( Buffer : in out Output_Buffer;
Stream : in out Root_Stream_Type'Class;
Count : Stream_Element_Count;
Reserve : Stream_Element_Count;
Last : out Stream_Element_Offset;
Next : out Stream_Element_Offset;
Done : out Boolean
) is
begin
if Reserve >= Buffer.Written'Length then
Raise_Exception
( Data_Error'Identity,
( "Output buffer is too small for prefix and suffix ("
& Image (Reserve)
& ")"
) );
end if;
if Buffer.First_Written <= Buffer.Free_To_Write then
--
-- [ XXXXXXXXXXXXXXX ]
-- | |
-- First_Written Free_To_Write
--
declare
Tail : constant Stream_Element_Offset :=
Reserve + Buffer.Written'First - Buffer.First_Written;
begin
if Tail > 0 then
if Buffer.Free_To_Write + Tail = Buffer.Written'Last then
Done := False;
else
Next := Buffer.Written'Last - Tail;
if Count < Next - Buffer.Free_To_Write then
Next := Buffer.Free_To_Write + Count - 1;
end if;
Read
( Stream,
Buffer.Written (Buffer.Free_To_Write..Next),
Last
);
Done := Last < Next;
Next := Last + 1;
end if;
else
Next := Buffer.Written'Last;
if Count < Next - Buffer.Free_To_Write then
Next := Buffer.Free_To_Write + Count - 1;
end if;
Read
( Stream,
Buffer.Written (Buffer.Free_To_Write..Next),
Last
);
Done := Last < Next;
if Last < Buffer.Written'Last then
Next := Last + 1;
else
Next := Buffer.Written'First;
end if;
end if;
end;
else
--
-- [XXXXX XXXXXXX]
-- | |
-- Free_To_Write First_Written
--
if Buffer.Free_To_Write + Reserve >= Buffer.First_Written then
Done := False;
else
Next := Buffer.First_Written - Reserve;
if Count < Next - Buffer.Free_To_Write then
Next := Buffer.Free_To_Write + Count - 1;
end if;
Read
( Stream,
Buffer.Written (Buffer.Free_To_Write..Next),
Last
);
Done := Last < Next;
Next := Last + 1;
end if;
end if;
end Fill_From_Stream;
procedure Finalize (Listener : in out Connections_Server) is
procedure Free is
new Ada.Unchecked_Deallocation (Worker, Worker_Ptr);
begin
if Listener.Doer /= null then
Listener.Finalizing := True;
Abort_Selector (Listener.Selector);
while not Listener.Doer'Terminated loop
delay 0.001;
end loop;
Free (Listener.Doer);
end if;
Close_Selector (Listener.Selector);
end Finalize;
procedure Finalize (Client : in out Connection) is
begin
Close (Client.Socket);
Free (Client.Transport);
Object.Finalize (Object.Entity (Client));
end Finalize;
function Free (Buffer : Output_Buffer) return Stream_Element_Count is
begin
return Buffer.Written'Length - Used (Buffer) - 1;
end Free;
function From_String (Data : String) return Stream_Element_Array is
Result : Stream_Element_Array (1..Data'Length);
Pointer : Stream_Element_Offset := Result'First;
begin
for Index in Data'Range loop
Result (Pointer) := Character'Pos (Data (Index));
Pointer := Pointer + 1;
end loop;
return Result;
end From_String;
function Get_Client_Address (Client : Connection)
return Sock_Addr_Type is
begin
return Client.Client_Address;
end Get_Client_Address;
function Get_Clients_Count (Listener : Connections_Server)
return Natural is
begin
return Listener.Clients;
end Get_Clients_Count;
function Get_Client_Name
( Factory : Connections_Factory;
Client : Connection'Class
) return String is
begin
return Image (Client.Client_Address);
end Get_Client_Name;
function Get_Connections_Server (Client : Connection)
return Connections_Server_Ptr is
begin
return Client.Socket_Listener;
end Get_Connections_Server;
function Get_IO_Timeout (Factory : Connections_Factory)
return Duration is
begin
return 0.02;
end Get_IO_Timeout;
procedure Get_Occurrence
( Client : Connection;
Source : out Exception_Occurrence
) is
begin
Save_Occurrence (Source, Client.Last_Error);
end Get_Occurrence;
function Get_Overlapped_Size (Client : Connection)
return Stream_Element_Count is
begin
return Client.Overlapped_Read;
end Get_Overlapped_Size;
function Get_Polling_Timeout (Factory : Connections_Factory)
return Duration is
begin
return 0.5;
end Get_Polling_Timeout;
function Get_Server_Address
( Listener : Connections_Server
) return Sock_Addr_Type is
Address : Sock_Addr_Type;
begin
Address.Addr := Any_Inet_Addr;
Address.Port := Listener.Port;
return Address;
end Get_Server_Address;
function Get_Session_State (Client : Connection)
return Session_State is
Result : constant Session_State := Client.Session;
begin
if Result = Session_Down then
if Client.Socket = No_Socket then
return Session_Down;
else
return Session_Disconnected; -- Almost here
end if;
else
return Result;
end if;
end Get_Session_State;
function Get_Socket (Client : Connection) return Socket_Type is
begin
return Client.Socket;
end Get_Socket;
function Has_Data (Buffer : Input_Buffer) return Boolean is
begin
return
( Buffer.Free_To_Read /= Buffer.First_Read
and then
( Buffer.Expected = 0
or else
Used (Buffer) >= Buffer.Size - 1
) );
end Has_Data;
function Has_Data (Client : Connection) return Boolean is
begin
return Has_Data (Client.Read);
end Has_Data;
function Image (Code : Error_Type) return String is
Result : String := Error_Type'Image (Code);
begin
for Index in Result'First + 1..Result'Last loop
if Result (Index) = '_' then
Result (Index) := ' ';
else
Result (Index) := To_Lower (Result (Index));
end if;
end loop;
return Result;
end Image;
function Image
( Data : Stream_Element_Array;
Hexadecimal : Boolean := False
) return String is
begin
if Hexadecimal then
declare
Result : String (1..Data'Length * 3);
Pointer : Integer := 1;
begin
for Index in Data'Range loop
Put
( Destination => Result,
Pointer => Pointer,
Value => Integer (Data (Index)),
Base => 16,
Field => 2,
Fill => '0',
Justify => Right
);
Put
( Destination => Result,
Pointer => Pointer,
Value => " "
);
end loop;
return Result;
end;
else
declare
Length : Natural := 0;
begin
for Index in Data'Range loop
case Data (Index) is
when 32..36 | 38..126 =>
Length := Length + 1;
when others =>
Length := Length + 3;
end case;
end loop;
declare
Result : String (1..Length);
Pointer : Integer := 1;
begin
for Index in Data'Range loop
case Data (Index) is
when 32..36 | 38..126 =>
Put
( Destination => Result,
Pointer => Pointer,
Value => Character'Val (Data (Index))
);
when others =>
Put
( Destination => Result,
Pointer => Pointer,
Value => '%'
);
Put
( Destination => Result,
Pointer => Pointer,
Value => Integer (Data (Index)),
Base => 16,
Field => 2,
Fill => '0',
Justify => Right
);
end case;
end loop;
return Result;
end;
end;
end if;
end Image;
procedure Initialize (Listener : in out Connections_Server) is
begin
Listener.IO_Timeout := Get_IO_Timeout (Listener.Factory.all);
Listener.Polling_Timeout :=
Get_Polling_Timeout (Listener.Factory.all);
Create_Selector (Listener.Selector);
Listener.Doer := new Worker (Listener'Unchecked_Access);
end Initialize;
function Is_Connected (Client : Connection) return Boolean is
begin
return Client.Session in Session_Active..Session_Busy;
end Is_Connected;
function Is_Down (Client : Connection) return Boolean is
begin
return
( Client.Session = Session_Down
and then
Client.Socket = No_Socket
);
end Is_Down;
function Is_Elevated (Client : Connection) return Boolean is
begin
return Client.Transport /= null;
end Is_Elevated;
function Is_Incoming (Client : Connection) return Boolean is
begin
return not Client.Client;
end Is_Incoming;
function Is_Opportunistic (Client : Connection) return Boolean is
begin
return False;
end Is_Opportunistic;
function Is_TLS_Capable
( Factory : Connections_Factory
) return Boolean is
begin
return False;
end Is_TLS_Capable;
function Is_Trace_Received_On
( Factory : Connections_Factory;
Encoded : IO_Tracing_Mode
) return Boolean is
begin
return 0 /= (Factory.Trace_Flags and Receive_Masks (Encoded));
end Is_Trace_Received_On;
function Is_Trace_Sent_On
( Factory : Connections_Factory;
Encoded : IO_Tracing_Mode
) return Boolean is
begin
return 0 /= (Factory.Trace_Flags and Sent_Masks (Encoded));
end Is_Trace_Sent_On;
function Is_Unblock_Send_Queued
( Listener : Connections_Server
) return Boolean is
begin
return Listener.Unblock_Send;
end Is_Unblock_Send_Queued;
procedure Keep_On_Sending (Client : in out Connection) is
begin
Client.Dont_Block := True;
end Keep_On_Sending;
procedure On_Connected
( Listener : in out Connections_Server'Class;
Client : in out Connection'Class
) is
begin
Trace_Sending
( Listener.Factory.all,
Client,
False,
", connected"
);
Free (Client.Transport);
if not Is_Opportunistic (Client) then
Client.Transport :=
Create_Transport
( Listener.Factory,
Listener'Unchecked_Access,
Client'Unchecked_Access
);
end if;
Set (Listener.Read_Sockets, Client.Socket);
if Client.Transport = null then -- No handshaking
declare
Saved : constant Session_State := Client.Session;
begin
Client.Session := Session_Connected;
Connected (Client);
Connected (Listener, Client);
Client.Session := Session_Active;
Activated (Client);
exception
when others =>
if Client.Session in Session_Connected
.. Session_Active
then
Client.Session := Saved;
end if;
raise;
end;
else
Client.Session := Session_Handshaking;
Append
( Listener.Postponed,
Client'Unchecked_Access,
Listener.Postponed_Count
);
end if;
end On_Connected;
procedure On_Worker_Start (Listener : in out Connections_Server) is
begin
null;
end On_Worker_Start;
procedure Process
( Buffer : in out Input_Buffer;
Receiver : in out Connection'Class;
Data_Left : out Boolean
) is
Last : Stream_Element_Offset;
Pointer : Stream_Element_Offset;
begin
while Has_Data (Buffer) loop
if Buffer.Free_To_Read < Buffer.First_Read then
--
-- [XXXXXXXXXXXXXX XXXXX]
-- Free_To_Read | First_Read |
--
if Buffer.First_Read > Buffer.Read'Last then
--
-- [XXXXXXXXXXXXXX ]
-- Free_To_Read | First_Read |
--
Buffer.First_Read := Buffer.Read'First; -- Wrap
Last := Buffer.Free_To_Read - 1;
else
Last := Buffer.Read'Last;
end if;
else
--
-- [ XXXXXXXXX ]
-- First_Read | | Free_To_Read
--
Last := Buffer.Free_To_Read - 1;
end if;
Pointer := Last + 1;
Received
( Receiver,
Buffer.Read (Buffer.First_Read..Last),
Pointer
);
if Pointer < Buffer.First_Read or else Pointer > Last + 1 then
Raise_Exception
( Layout_Error'Identity,
( "Subscript error, pointer "
& Image (Pointer)
& " out of range "
& Image (Buffer.First_Read)
& ".."
& Image (Last)
& "+"
) );
elsif Pointer > Buffer.Read'Last then
if Buffer.Free_To_Read <= Buffer.Read'Last then
Buffer.First_Read := Buffer.Read'First; -- Wrap
else
Buffer.First_Read := Pointer; -- Not yet
end if;
else
Buffer.First_Read := Pointer;
end if;
if Pointer <= Last then -- Some input left unprocessed
Data_Left := True;
return;
end if;
end loop;
Data_Left := False;
end Process;
procedure Process
( Listener : in out Connections_Server;
Client : Connection_Ptr;
Data_Left : out Boolean
) is
begin
if Client.Transport = null then
Process (Client.Read, Client.all, Data_Left);
else
Process
( Client.Transport.all,
Listener,
Client.all,
Data_Left
);
end if;
end Process;
procedure Process_Packet (Client : in out Connection) is
begin
null;
end Process_Packet;
procedure Pull
( Buffer : in out Input_Buffer;
Data : in out Stream_Element_Array;
Pointer : in out Stream_Element_Offset
) is
Last : Stream_Element_Offset;
Offset : Stream_Element_Offset;
begin
while Pointer <= Data'Last and then Has_Data (Buffer) loop
if Buffer.Free_To_Read < Buffer.First_Read then
--
-- [XXXXXXXXXXXXXX XXXXX]
-- Free_To_Read | First_Read |
--
if Buffer.First_Read > Buffer.Read'Last then
--
-- [XXXXXXXXXXXXXX ]
-- Free_To_Read | First_Read |
--
Buffer.First_Read := Buffer.Read'First; -- Wrap
Last := Buffer.Free_To_Read - 1;
else
Last := Buffer.Read'Last;
end if;
else
--
-- [ XXXXXXXXX ]
-- First_Read | | Free_To_Read
--
Last := Buffer.Free_To_Read - 1;
end if;
Offset := Last - Buffer.First_Read;
if Offset > Data'Last - Pointer then
Offset := Data'Last - Pointer;
Last := Buffer.First_Read + Offset;
end if;
Data (Pointer..Pointer + Offset) :=
Buffer.Read (Buffer.First_Read..Last);
Pointer := Pointer + Offset + 1;
if Last >= Buffer.Read'Last then
if Buffer.Free_To_Read <= Buffer.Read'Last then
Buffer.First_Read := Buffer.Read'First; -- Wrap
else
Buffer.First_Read := Last + 1; -- Not yet
end if;
else
Buffer.First_Read := Last + 1;
end if;
end loop;
end Pull;
procedure Push
( Client : in out Connection;
Data : Stream_Element_Array;
Last : out Stream_Element_Offset
) is
begin
if Client.Transport = null then
Send_Socket (Client.Socket_Listener.all, Client, Data, Last);
else
Encode (Client.Transport.all, Client, Data, Last);
end if;
if Last + 1 /= Data'First then
Client.Data_Sent := True;
if ( 0
/= ( Client.Socket_Listener.Factory.Trace_Flags
and
Trace_Decoded_Sent
) )
then
Trace_Sent
( Factory => Client.Socket_Listener.Factory.all,
Client => Client,
Data => Data,
From => Data'First,
To => Last,
Encoded => False
);
end if;
end if;
end Push;
procedure Queue
( Client : in out Connection;
Data : Stream_Element_Array;
Pointer : in out Stream_Element_Offset
) is
Buffer : Output_Buffer renames Client.Written;
Free : Stream_Element_Offset;
Count : Stream_Element_Offset := Data'Last - Pointer + 1;
begin
if Buffer.First_Written = Buffer.Free_To_Write then
--
-- Moving First_Written as far back as possible to diminish
-- buffer fragmenting. We cannot move it further than the
-- number of elements we put there, because of race condition,
-- when Free_To_Write is not yet set. But when Free_To_Write
-- points into the elements written everything is OK
--
-- [ ............ ]
-- |<--Count-->|
-- | Free_To_Write = First_Written
-- new First_Written
--
Count :=
Stream_Element_Offset'Min
( Buffer.Written'Length - 1,
Count
);
Free := Stream_Element_Offset'Max