Loki du placard | Voici un petit exemple avec un selector. C'est vraiment très limité, ce n'est pas ce qu'il y a de plus propre, mais ça illustre bien le fonctionnement.
Code :
- with GNAT.Sockets; use GNAT.Sockets;
- with Text_Io; use Text_Io;
- procedure Network is
- Liste : array(1..10) of Socket_Type;
- Bliste : array(1..10) of Boolean;
- Reads,Write : Socket_Set_Type;
- Selector : Selector_Type;
- Srv_Adr,Adr : Sock_Addr_Type;
- Srv_Socket : Socket_Type;
- Status : Selector_Status;
- package Intio is new Text_Io.Integer_Io(Integer);
- Ptr : Integer := 0;
- C : Character;
- begin
- -- Initialisation de GNAT.Sockets
- Initialize;
- -- Creation de la socket serveur
- Srv_Adr.Addr := Any_Inet_Addr;
- Srv_Adr.Port := 3128;
- Create_Socket(Srv_Socket);
- Bind_Socket(Srv_Socket,Srv_Adr);
- Listen_Socket(Srv_Socket);
- -- Creation
- Create_Selector(Selector);
- Set(Write,Srv_Socket);
- for I in 1..10 loop
- Bliste(I) := False;
- end loop;
- loop
- --Creation des sets
- Empty(Reads);
- Set(Reads,Srv_Socket);
- for I in 1..10 loop
- if Bliste(I) then
- Set(Reads,Liste(I));
- end if;
- end loop;
- Check_Selector(Selector,Reads,Write,Status);
- case Status is
- when Completed =>
- if Is_Set(Reads,Srv_Socket) then
- Ptr := Ptr + 1;
- Put_Line("Connexion : "&Integer'Image(Ptr));
- Accept_Socket(Srv_Socket,Liste(Ptr),Adr);
- Bliste(Ptr) := True;
- else
- for I in 1..10 loop
- if Bliste(I) then
- if Is_Set(Reads,Liste(I)) then
- Character'Read(Stream(Liste(I)),C);
- Put_line("socket "&Integer'Image(I)&Character'Image(C));
- end if;
- end if;
- end loop;
- end if;
- when Expired =>
- Put_Line("expired" );
- when Aborted =>
- Put_Line("aborted" );
- end case;
- end loop;
- -- Destruction du selector
- Close_Selector(Selector);
- -- Finalisation pour le package GNAT.Sockets
- Finalize;
- exception
- when SOCKET_ERROR =>
- Put_Line("SOCKET_ERROR" );
- end Network;
|
|