This repository has been archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p_client.adb
116 lines (100 loc) · 3.35 KB
/
p_client.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
with GNAT.Sockets, Ada.Exceptions, Ada.Strings, Ada.Strings.Fixed, p_status, Text_IO, p_common, p_vue_graph, p_jeu, p_fenbase;
use GNAT.Sockets, Ada.Exceptions, Ada.Strings, Ada.Strings.Fixed, p_status, Text_IO, p_common, p_vue_graph, p_jeu, p_fenbase;
package body p_client is
task body T_Listen is
channel: stream_access;
begin
loop
if not est_connecte then
select
accept start(c: in stream_access) do
channel := c;
end start;
or
terminate;
end select;
else
while est_connecte loop
declare
message : string := String'input (channel);
begin
if message'length > 0 then
traiterMessage(channel, message);
end if;
end;
end loop;
end if;
end loop;
end T_Listen;
procedure initialiserSocket is
-- {} => {Un socket de connexion a été créé}
begin
--create_socket (socket);
--set_socket_option (socket, SOCKET_LEVEL, (REUSE_ADDRESS, true));
null;
end initialiserSocket;
function connexion(addr: in string; port: in integer) return boolean is
-- {} => {résultat = vrai si le client se connecte au serveur}
begin
address.addr := inet_addr(addr);
address.port := Port_Type(port);
create_socket (socket);
set_socket_option (socket, SOCKET_LEVEL, (REUSE_ADDRESS, true));
connect_socket (socket, address);
channel := stream (socket);
est_connecte := true;
listen.start(channel);
return true;
exception
when E : others =>
put_line(exception_name (E) & ": " & exception_message (E));
return false;
end connexion;
procedure deconnexion is
-- {} => {Le client se déconnecte du serveur}
begin
jeuEnCours := false;
cacherElem(fenetreDeJeu, "valider");
cacherElem(fenetreDeJeu, "abandon");
montrerElem(fenetreDeJeu, "finjeu");
authenth := false;
est_connecte := false;
close_socket (socket);
end deconnexion;
procedure traiterMessage(c: in stream_access; m: in string) is
-- {} => {Gère un message reçu par le serveur}
code : integer;
msg : string(1..15);
tailleMsg : integer;
begin
code := statutMessage(m);
case code is
when AUTHENTIFICATION_NEEDED => envoyerMessage(c, creerMessageStatut(trim(pseudoClient, BOTH), SEND_NAME));
when AUTHENTIFICATION_REUSSIE =>
authenth := true;
statutErreur := -1;
when PSEUDO_INCORRECT =>
statutErreur := PSEUDO_INCORRECT;
when SOLUTION_RESULTAT =>
decoderMessage(m, code, msg, tailleMsg);
effacerGrille(fenetreDeJeu);
actualisationEssai(fenetreDeJeu, msg(2..tailleMsg), Integer'value(msg(1..1)));
when ACTUALISATION_SCORE =>
decoderMessage(m, code, msg, tailleMsg);
changerTexte(fenetreDeJeu, "Score", trim(msg(1..tailleMsg), BOTH) &
(if Integer'value(msg(1..tailleMsg)) >= 2 then " Points" else " Point"));
when DEBUT_JEU =>
decoderMessage(m, code, msg, tailleMsg);
while not jeuOuvert loop
delay 0.1;
end loop;
chrono.start(Duration'value(msg(1..tailleMsg)));
jeuEnCours := true;
chronoJeu.start(fenetreDeJeu);
tailleSolution := 0;
when FIN_JEU =>
deconnexion;
when others => null;
end case;
end traiterMessage;
end p_client;