Archived
3
0

corrections commentaires + memory leak

This commit is contained in:
Philippe PITTOLI 2016-12-22 21:48:35 +01:00
parent 3dd3033273
commit 9e2a27cb20
6 changed files with 72 additions and 50 deletions

View File

@ -2,7 +2,8 @@
#include <string.h> /* memset */
#include <stdio.h>
int main() {
int main()
{
int ret;
struct array_proc tab_proc;
memset(&tab_proc, 0, sizeof(struct array_proc));
@ -27,5 +28,7 @@ int main() {
}
array_proc_print(&tab_proc);
array_proc_free (&tab_proc);
return 0;
}

View File

@ -151,12 +151,22 @@ int app_write (struct service *srv, const struct msg *m)
}
/*prendre en parametre un tableau de process.
* trouver le processus/service actif et renvoyer CONNECTION/APPLICATION
* si un processus il va etre placer dans proc
* si un service il va etre placer dans service.
*/
int srv_select(struct array_proc *ap, struct service *srv, struct process **proc) {
/*
* srv_select prend en parametre
* * un tableau de process qu'on écoute
* * le service qui attend de nouvelles connexions
* * un tableau de process qui souhaitent parler
*
* la fonction trouve le processus/service actif et renvoie
* un entier correspondant à quel descripteur de fichier il faut lire
* * celui du serveur = nouvelle connexion entrante (CONNECTION)
* * celui d'un ou plusieurs processus = ils nous parlent (APPLICATION)
* * les deux à la fois (CON_APP)
*/
int srv_select (struct array_proc *ap, struct service *srv
, struct process **proc)
{
int i, j;
/* master file descriptor list */
fd_set master;
@ -182,10 +192,9 @@ int srv_select(struct array_proc *ap, struct service *srv, struct process **proc
while (1) {
readf = master;
if(select(fdmax+1, &readf, NULL, NULL, NULL) == -1)
{
perror("Server-select() error lol!");
exit(1);
if(select(fdmax+1, &readf, NULL, NULL, NULL) == -1) {
perror("select");
return -1;
}
/*run through the existing connections looking for data to be read*/
@ -193,7 +202,7 @@ int srv_select(struct array_proc *ap, struct service *srv, struct process **proc
if (FD_ISSET(i, &readf)) {
if (i == listener) {
return CONNECTION;
}else {
} else {
for(j = 0; j < ap->size; j++) {
if(i == ap->tab_proc[j]->proc_fd ) {
*proc = ap->tab_proc[j];
@ -207,9 +216,12 @@ int srv_select(struct array_proc *ap, struct service *srv, struct process **proc
}
/*calculer le max filedescriptor*/
int getMaxFd(struct array_proc *ap) {
int getMaxFd(struct array_proc *ap)
{
int i;
int max = 0;
for (i = 0; i < ap->size; i++ ) {
if (ap->tab_proc[i]->proc_fd > max) {
max = ap->tab_proc[i]->proc_fd;

View File

@ -86,9 +86,6 @@ int msg_read (int fd, struct msg *m)
}
free (buf);
// printf ("msg received: ");
// print_msg (m);
return 0;
}
@ -96,14 +93,10 @@ int msg_write (int fd, const struct msg *m)
{
assert (m != NULL);
// printf ("msg to write: ");
// print_msg (m);
char *buf = NULL;
size_t msize = 0;
msg_format_write (m, &buf, &msize);
// printf ("msg to send, real size %ld\n", msize);
int ret = usock_send (fd, buf, msize);
if (ret < 0) {
handle_err ("msg_write", "usock_send");

View File

@ -13,7 +13,7 @@
struct msg {
char type;
short valsize;
unsigned short valsize;
char *val;
};

View File

@ -33,11 +33,14 @@ void srv_process_gen (struct process *p
#endif
int add_proc(struct array_proc *aproc, struct process *p) {
int add_proc (struct array_proc *aproc, struct process *p)
{
assert(aproc != NULL);
assert(p != NULL);
aproc->size++;
aproc->tab_proc = realloc(aproc->tab_proc, sizeof(struct process) * aproc->size);
aproc->tab_proc = realloc(aproc->tab_proc
, sizeof(struct process) * aproc->size);
if (aproc->tab_proc == NULL) {
return -1;
}
@ -46,16 +49,20 @@ int add_proc(struct array_proc *aproc, struct process *p) {
return 0;
}
int del_proc(struct array_proc *aproc, struct process *p) {
int del_proc (struct array_proc *aproc, struct process *p)
{
assert(aproc != NULL);
assert(p != NULL);
int i;
for (i = 0; i < aproc->size; i++) {
if (aproc->tab_proc[i] == p) {
aproc->tab_proc[i] = aproc->tab_proc[aproc->size-1];
aproc->size--;
aproc->tab_proc = realloc(aproc->tab_proc, sizeof(struct process) * aproc->size);
aproc->tab_proc = realloc(aproc->tab_proc
, sizeof(struct process) * aproc->size);
if (aproc->tab_proc == NULL) {
return -1;
}
@ -69,10 +76,12 @@ int del_proc(struct array_proc *aproc, struct process *p) {
void process_print (struct process *p)
{
if (p != NULL)
printf ("process %d : index %d, version %d\n", p->proc_fd, p->index, p->version);
printf ("process %d : index %d, version %d\n"
, p->proc_fd, p->index, p->version);
}
void array_proc_print( struct array_proc *ap) {
void array_proc_print (struct array_proc *ap)
{
int i;
for (i = 0; i < ap->size; i++) {
printf("%d : ", i);
@ -80,4 +89,8 @@ void array_proc_print( struct array_proc *ap) {
}
}
void array_proc_free (struct array_proc *ap)
{
if (ap->tab_proc != NULL)
free (ap->tab_proc), ap->tab_proc = NULL;
}

View File

@ -18,7 +18,8 @@ int add_proc(struct array_proc *, struct process *);
int del_proc(struct array_proc *, struct process *);
void array_proc_print(struct array_proc *);
void array_proc_print (struct array_proc *);
void array_proc_free (struct array_proc *);
#if 0