home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / antipolix-2.0 / messages.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-22  |  5.4 KB  |  269 lines

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/time.h>
  4. #include <string.h>
  5. #include <malloc.h>
  6. #include <memory.h>
  7.  
  8. #include "messages.h"
  9. #include "creation.h"
  10.  
  11.  
  12. /* ---------------------------- fonctions de base --------------------------- */
  13.  
  14. int Read(int sockfd, char *buf, int taille)
  15. {
  16.   static int tot, nb, cpt;
  17.  
  18.   tot=0;
  19.   cpt=0;
  20.   while (tot<taille && cpt<MAX_READ_ATTEMPTS)
  21.     {
  22.       nb=read(sockfd, buf+tot, taille-tot);
  23.       if (!nb)
  24.     cpt++;
  25.       else
  26.     tot+=nb;
  27.     }
  28.   if (cpt<MAX_READ_ATTEMPTS)
  29.     return tot;
  30.   else
  31.     return -1;
  32. }
  33.  
  34.  
  35. void GetInt(char *buf, int *i)
  36. {
  37.   bcopy(buf, (char *)i, szi);
  38. }
  39.  
  40.  
  41. void PutInt(char *buf, int i)
  42. {
  43.   bcopy((char *)&i, buf, szi);
  44. }
  45.  
  46.  
  47. int MyRead(int sockfd, char *buf, int l, int delai)
  48. {
  49.   if (WaitForThisSocket(sockfd, delai))
  50.     return (Read(sockfd, buf, l));
  51.   else
  52.     return -1;
  53. }
  54.  
  55. int CliRead(int sockfd, char *buf, int l, int delai, char *Message)
  56. {
  57.   while(!WaitForThisSocket(sockfd, delai))
  58.     fprintf(stdout, Message);
  59.   
  60.   return (Read(sockfd, buf, l));
  61. }
  62.  
  63. /* -------------------------------- ENTETES ------------------------------------ */
  64.  
  65.  
  66. void SendEntete(int sockfd, int Entete)
  67. {
  68.   char buf[ENTETE_BUF];
  69.  
  70.   PutInt(buf, Entete);
  71.   write(sockfd, buf, ENTETE_BUF);
  72. }
  73.  
  74.  
  75. int GetEntete(int sockfd)
  76. {
  77.   char buf[ENTETE_BUF];
  78.   int Entete;
  79.  
  80.   if (Read(sockfd, buf, ENTETE_BUF)>0)
  81.     {
  82.       GetInt(buf, &Entete);
  83.       return Entete;
  84.     }
  85.   else
  86.     return DATAERROR;
  87. }
  88.  
  89.  
  90. /* --------------------------------- UNIVERS --------------------------------- */
  91.  
  92.  
  93. void SendUniverse(int sockfd, Univers *U)
  94. {
  95.   int i, j, k;
  96.   char Entier[ENTIER_BUF], Case[CASE_BUF];
  97.  
  98.   SendEntete(sockfd, UNIVERS);
  99.  
  100.   PutInt(Entier, U->NbPlateaux);
  101.   write(sockfd, Entier, ENTIER_BUF);
  102.  
  103.   for (i=0; i<U->NbPlateaux; i++)                /* i */
  104.     {
  105.       PutInt(Entier, U->P[i].Taille);
  106.       write(sockfd, Entier, ENTIER_BUF);
  107.  
  108.       PutInt(Entier, U->P[i].Caract);
  109.       write(sockfd, Entier, ENTIER_BUF);
  110.  
  111.       for (j=0; j<U->P[i].Taille; j++)          /* j */
  112.     for (k=0; k<U->P[i].Taille; k++)         /* k */
  113.       {
  114.         PutInt(Case, U->P[i].Case[j][k].Couleur);
  115.         PutInt(Case+ENTIER_BUF, U->P[i].Case[j][k].Type);
  116.         PutInt(Case+2*ENTIER_BUF, U->P[i].Case[j][k].Caract);
  117.         write(sockfd, Case, CASE_BUF);
  118.       }
  119.     }
  120. }
  121.  
  122.  
  123. void GetUniverse(int sockfd, Univers *U)
  124. {
  125.   int i, j, k, Valeur;
  126.   char Entier[ENTIER_BUF], Case[CASE_BUF];
  127.  
  128.  
  129.   Read(sockfd, Entier, ENTIER_BUF);
  130.   GetInt(Entier, &Valeur);
  131.  
  132.   U->NbPlateaux=Valeur;
  133.   for (i=0; i<U->NbPlateaux; i++)
  134.     {
  135.       Read(sockfd, Entier, ENTIER_BUF);
  136.       GetInt(Entier, &(U->P[i].Taille));
  137.  
  138.       Read(sockfd, Entier, ENTIER_BUF);
  139.       GetInt(Entier, &(U->P[i].Caract));
  140.  
  141.       for (j=0; j<U->P[i].Taille; j++)
  142.     for (k=0; k<U->P[i].Taille; k++)
  143.       {
  144.         Read(sockfd, Case, CASE_BUF);
  145.         GetInt(Case, &(U->P[i].Case[j][k].Couleur));
  146.         GetInt(Case+ENTIER_BUF, &(U->P[i].Case[j][k].Type));
  147.         GetInt(Case+2*ENTIER_BUF, &(U->P[i].Case[j][k].Caract));
  148.       }
  149.     }
  150. }
  151.  
  152.  
  153. /* --------------------------- MOVEMENTS -------------------------------- */
  154.  
  155. void SendMovement(int sockfd, OneMovement *Moves)
  156. {
  157.   int i, j;
  158.   char buf[ENTIER_BUF];
  159.  
  160.   SendEntete(sockfd, MOVEMENT);
  161.   
  162.   for (i=0; i<NB_MOVES; i++)
  163.     {
  164.       PutInt(buf, Moves[i].nbcases);
  165.       write(sockfd, buf, ENTIER_BUF);
  166.       
  167.       PutInt(buf, Moves[i].type);
  168.       write(sockfd, buf, ENTIER_BUF);
  169.  
  170.       for (j=0; j<Moves[i].nbcases; j++)
  171.     {
  172.       PutInt(buf, Moves[i].cases[j].x);
  173.       write(sockfd, buf, ENTIER_BUF);
  174.       PutInt(buf, Moves[i].cases[j].y);
  175.       write(sockfd, buf, ENTIER_BUF);
  176.       PutInt(buf, Moves[i].cases[j].p);
  177.       write(sockfd, buf, ENTIER_BUF);
  178.     }
  179.     }
  180. }
  181.  
  182.  
  183. void GetMovement(int sockfd, OneMovement *Moves)
  184. {
  185.   int i, j;
  186.   char buf[ENTIER_BUF];
  187.  
  188.   for (i=0; i<NB_MOVES; i++)
  189.     {
  190.       Read(sockfd, buf, ENTIER_BUF);
  191.       GetInt(buf, &(Moves[i].nbcases));
  192.       
  193.       Read(sockfd, buf, ENTIER_BUF);
  194.       GetInt(buf, &(Moves[i].type));
  195.  
  196.       if (Moves[i].nbcases)
  197.     Moves[i].cases=malloc(Moves[i].nbcases * sizeof(Case));
  198.  
  199.       for (j=0; j<Moves[i].nbcases; j++)
  200.     {
  201.       Read(sockfd, buf, ENTIER_BUF);
  202.       GetInt(buf, &(Moves[i].cases[j].x));
  203.       Read(sockfd, buf, ENTIER_BUF);
  204.       GetInt(buf, &(Moves[i].cases[j].y));
  205.       Read(sockfd, buf, ENTIER_BUF);
  206.       GetInt(buf, &(Moves[i].cases[j].p));
  207.     }
  208.     }
  209. }
  210.  
  211.  
  212.  
  213.  
  214. /* --------------------------- MESSAGES --------------------------------- */
  215.  
  216. void SendMessage(int sockfd, char *Message, int Who)
  217. /* Who == -1 => message du serveur */
  218. {
  219.   char buf[MESSAGE_BUF], WHO[ENTIER_BUF];
  220.  
  221.   SendEntete(sockfd, MESSAGE);
  222.  
  223.   PutInt(WHO, Who);
  224.   write(sockfd, WHO, ENTIER_BUF);
  225.  
  226.   memcpy(buf, Message, MESSAGE_BUF);
  227.   buf[MESSAGE_BUF-1]='\0';
  228.   write(sockfd, buf, MESSAGE_BUF);
  229. }
  230.  
  231.  
  232.  
  233. void GetMessage(int sockfd, int *Who, char *Message)
  234. {
  235.   char buf[MESSAGE_BUF], WHO[ENTIER_BUF];
  236.  
  237.   Read(sockfd, WHO, ENTIER_BUF);
  238.   GetInt(WHO, Who);
  239.  
  240.   Read(sockfd, buf, MESSAGE_BUF);
  241.   memcpy(Message, buf, MESSAGE_BUF);
  242. }
  243.  
  244.  
  245. /* --------------------------- AUTRES --------------------------------------- */
  246.  
  247.  
  248. void StopError(int Type)
  249. {
  250.   fprintf(stderr, "** transmission error (#%d)\n", Type);
  251.   exit(0);
  252. }
  253.  
  254.  
  255. int WaitForThisSocket(int sockfd, int secondes)
  256. /* Renvoie 1 si donnees arrivees a temps, 0 sinon */
  257. {
  258.   fd_set readfds, writefds;
  259.   struct timeval timeout;
  260.   
  261.   FD_ZERO(&writefds);
  262.   FD_ZERO(&readfds);
  263.   FD_SET(sockfd, &readfds);
  264.   timeout.tv_sec=secondes;
  265.   timeout.tv_usec=0;
  266.   select(getdtablesize(), &readfds, NULL, NULL, &timeout);
  267.   return FD_ISSET(sockfd, &readfds);
  268. }
  269.