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
|
//créer un bloc à insérer dans frame
void creer_bloc(struct data* d, char type, char* v, int taille)
{
d->vals = malloc(taille);
d->type = type;
if(type != STRING)
{
d->length = taille;
d->length_string = 0;
memcpy(d->vals, v, taille);
}
else
{
d->length = 0;
d->length_string = taille;
strcpy(d->vals, v);
}
}
//créer une trame à partir d'un bloc
void creer_trame(struct frame* f, char h, char id, char n, struct data* d)
{
f->header = h;
f->id = id;
f->n = n;
if(d == NULL)
f->bloc = NULL;
else
{
f->bloc = malloc(sizeof(struct data));
memcpy(f->bloc, d, sizeof(*d));
}
}
//ajouter un bloc dans une trame existante
void ajouter_bloc_dans_trame(struct frame* f, struct data* d, int decalage)
{
if(d->type == STRING)
{
f->bloc[decalage].vals = malloc(sizeof(d->length_string));
memcpy(f->bloc[decalage].vals, d->vals, d->length_string);
}
else
{
f->bloc[decalage].vals = d->vals;
}
f->bloc[decalage].type = d->type;
f->bloc[decalage].length = d->length;
f->bloc[decalage].length_string = d->length_string;
}
//FONCTION DE TEST : envoi chaque trame décrivant chaque client à tous les clients à chaque fois
void test(struct Client** c)
{
int i, j;
char* buffer;
struct data* d = malloc(sizeof(struct data));
struct frame* t = malloc(sizeof(struct frame));
for(i=0 ; j<NOMBRE_CLIENT ; i++)
{
for(j=0 ; j<NOMBRE_CLIENT ; j++)
{
//ajouter un bloc contenant un UINT16
buffer = malloc(2);
EntierToBinaire(c[j]->id, buffer, UINT16);
creer_bloc(d, UINT16, buffer, 2); //2 : 2 octets pour un UINT16
creer_trame(t, 0xFF, 0x40, 9, d); //il y aura 9 "bloc" (struct data) dans la trame
//ajouter un bloc contenant un STRING
creer_bloc(d, STRING, c[j]->name, strlen(c[j]->name));
ajouter_bloc_dans_trame(t, d, 1);
//ajouter un block contenant un UINT8
buffer = malloc(1);
EntierToBinaire(c[j]->tab[0], buffer, UINT8);
creer_bloc(d, UINT8, buffer, 1);
ajouter_bloc_dans_trame(t, d, 2);
//ajouter un block contenant un UINT8
buffer = malloc(1);
EntierToBinaire(c[j]->tab[1], buffer, UINT8);
creer_bloc(d, UINT8, buffer, 1);
ajouter_bloc_dans_trame(t, d, 3);
//ajouter un block contenant un UINT8
buffer = malloc(1);
EntierToBinaire(c[j]->tab[2], buffer, UINT8);
creer_bloc(d, UINT8, buffer, 1);
ajouter_bloc_dans_trame(t, d, 4);
//ajouter un bloc contenant un UINT16
buffer = malloc(2);
EntierToBinaire(c[j]->a, buffer, UINT16);
creer_bloc(d, UINT16, buffer, 2); //2 : 2 octets pour un UINT16
ajouter_bloc_dans_trame(t, d, 5);
//ajouter un bloc contenant un UINT16
buffer = malloc(2);
EntierToBinaire(c[j]->b, buffer, UINT16);
creer_bloc(d, UINT16, buffer, 2); //2 : 2 octets pour un UINT16
ajouter_bloc_dans_trame(t, d, 6);
//ajouter un block contenant un UINT8
buffer = malloc(1);
EntierToBinaire(c[j]->c, buffer, UINT8);
creer_bloc(d, UINT8, buffer, 1);
ajouter_bloc_dans_trame(t, d, 7);
//ajouter un block contenant un UINT8
buffer = malloc(1);
EntierToBinaire(c[j]->d, buffer, UINT8);
creer_bloc(d, UINT8, buffer, 1);
ajouter_bloc_dans_trame(t, d, 8);
envoyer_trame(c[i]->mysocket, t);
}
}
} |
Partager