/* la sortie... */ int read_server_for_list (char *ligne, int deb, int max) { int ret; char *buf; if (renvoie_courte_liste==1) { renvoie_courte_liste=0; strcpy(ligne,".\r\n"); return 3; } else if (renvoie_courte_liste==2) { if (ligne!=tcp_line_read) strcpy(ligne,tcp_line_read); renvoie_courte_liste=1; return strlen(tcp_line_read); } if (filter_cmd_list[0]=='\0') return read_server(ligne,deb,max); while (1) { ret=read_server(ligne,deb,max); if (ligne[1]=='\r') return ret; buf=strchr(ligne,' '); if (buf) *buf='\0'; if (pattern_match(ligne,filter_cmd_list)) { if (buf) *buf=' '; return ret; } } }
static void app(const char *address, const char *name) { SOCKET sock = init_connection(address); char buffer[BUF_SIZE]; fd_set rdfs; /* le client envoie son nom */ write_server(sock, name); while(1) { FD_ZERO(&rdfs); /* ajouter un STDIN_FILENO */ FD_SET(STDIN_FILENO, &rdfs); /* ajouter le socket */ FD_SET(sock, &rdfs); if(select(sock + 1, &rdfs, NULL, NULL, NULL) == -1) { perror("select()"); exit(errno); } /* quelque chose qui vient du clavier */ if(FD_ISSET(STDIN_FILENO, &rdfs)) { fgets(buffer, BUF_SIZE - 1, stdin); { char *p = NULL; p = strstr(buffer, "\n"); if(p != NULL) { *p = 0; } else { /* fclean */ buffer[BUF_SIZE - 1] = 0; } } write_server(sock, buffer); } else if(FD_ISSET(sock, &rdfs)) { int n = read_server(sock, buffer); /* Si le serveur est down */ if(n == 0) { /* On arrête */ break; } puts(buffer); } } end_connection(sock); }
static void app(const char *address, const char *name) { SOCKET sock = init_connection(address); char buffer[BUF_SIZE]; fd_set rdfs; /* send our name */ write_server(sock, name); while(1) { FD_ZERO(&rdfs); /* add STDIN_FILENO */ FD_SET(STDIN_FILENO, &rdfs); /* add the socket */ FD_SET(sock, &rdfs); if(select(sock + 1, &rdfs, NULL, NULL, NULL) == -1) { perror("select()"); exit(errno); } /* something from standard input : i.e keyboard */ if(FD_ISSET(STDIN_FILENO, &rdfs)) { fgets(buffer, BUF_SIZE - 1, stdin); { char *p = NULL; p = strstr(buffer, "\n"); if(p != NULL) { *p = 0; } else { /* fclean */ buffer[BUF_SIZE - 1] = 0; } } write_server(sock, buffer); } else if(FD_ISSET(sock, &rdfs)) { int n = read_server(sock, buffer); /* server down */ if(n == 0) { printf("Server disconnected !\n"); break; } puts(buffer); } } end_connection(sock); }
/* Cette fonction fait un read_server de retour et se reconnecte au cas ou */ int read_server_with_reconnect (char *ligne, int deb, int max) { int lus, code; if (renvoie_direct) { renvoie_direct=0; if (ligne==tcp_line_read) return strlen(tcp_line_read); /* Aie, ici c'est un bug. On élimine l'idée du renvoie_direct */ /* Mais logiquement, il ne peut y avoir ce bug */ } lus=read_server(ligne, deb, max); if (lus<3) { if (debug) fprintf(stderr, "Echec en lecture du serveur\n"); /* dans le cas ou c'est -1, on a une fin de connection */ if (lus==-1) { if (debug) fprintf(stderr,"Un timeout ?\n"); if (reconnect_after_timeout(1)<0) return -1; return read_server_with_reconnect(ligne,deb,max); } return -1; } code=atoi(ligne); /* On va essayer de gerer le cas du timeout */ if ((code==503) && (_nntp_try_parse_timeout(ligne))) { if (debug) fprintf(stderr,"Un timeout... %s",ligne); if (reconnect_after_timeout(1)<0) return -1; /* envoie aussi toutes les bonnes commandes au serveur */ return read_server_with_reconnect(ligne,deb,max); } return lus; }
static void client(int sock, const char *name) { char buff[BUF_SIZE + 1]; fd_set rdfs; write_server(sock, name); while (42) { do_select(sock, &rdfs); if (FD_ISSET(STDIN_FILENO, &rdfs)) { read_msg(buff); write_server(sock, buff); } else if (FD_ISSET(sock, &rdfs)) { if (read_server(sock, buff) == 0) { printf("Server disconnected !\n"); break ; } ft_putendl(buff); if (ft_strcmp(buff, "Pseudo already used") == 0) write_server(sock, pseudo()); } } close(sock); }
void voir(int fd, t_data *data) { char *msg; data = data; write(fd, "voir\n", 5); msg = read_server(fd); printf("Server answer : %s\n", msg); }
void gauche(int fd, t_data *data) { char *msg; data = data; write(fd, "gauche\n", 7); msg = read_server(fd); printf("Server answer : %s\n", msg); }
/* renvoie -1 en cas d'erreur. */ int discard_server() { int ret; /* cas d'un renvoie_courte_liste */ if (renvoie_courte_liste) { renvoie_courte_liste=0; return 0; } do { ret=read_server(tcp_line_read, 1, MAX_READ_SIZE-1); if (ret<1) return -1; if ((tcp_line_read[0]=='.') && (tcp_line_read[1]!='.')) return 0; while (tcp_line_read[ret-1]!='\n') { ret=read_server(tcp_line_read,1,MAX_READ_SIZE); if (ret<1) return -1; } } while (1); }
void invent(int fd, t_data *data) { char *msg; data = data; write(fd, "inventaire\n", 11); msg = read_server(fd); printf("Server answer : %s\n", msg); parsing_invent(&(msg[1]), data); }
void serverlist_frame() { int i; for (i = 0; i < server_count; i++) { msg_t *msg = sock_recv(&serverlist[i].sock); if (msg) { serverlist[i].ping_end = millis(); skip_data(msg, strlen("info\n")); read_server(serverlist + i, read_string(msg)); if (partial_match(filter, serverlist[i].name) || partial_match(filter, serverlist[i].map) || partial_match(filter, serverlist[i].mod) || partial_match(filter, serverlist[i].gametype)) ui_output(output_client, "^5%i ^7(%i) %s %s ^5[^7%s^5] [^7%s:%s^5]\n", i, serverlist[i].ping_end - serverlist[i].ping_start, serverlist[i].players, serverlist[i].name, serverlist[i].map, serverlist[i].mod, serverlist[i].gametype); serverlist[i].received = qtrue; } if (serverlist[i].ping_retries > 0 && !serverlist[i].received && millis() >= serverlist[i].ping_start + PING_TIMEOUT) ping_server(serverlist + i); } master_t *master; for (master = masters; master->address; master++) { msg_t *msg = sock_recv(&master->sock); if (!msg) continue; char address_string[32]; qbyte address[4]; unsigned short port; skip_data(msg, strlen("getserversResponse")); while (msg->readcount + 7 <= msg->cursize) { char prefix = read_char(msg); port = 0; if (prefix == '\\') { read_data(msg, address, 4); port = ShortSwap(read_short(msg)); sprintf(address_string, "%u.%u.%u.%u", address[0], address[1], address[2], address[3]); } if (port != 0) { server_t *server = find_server(address_string, port); if (server != NULL) continue; server = serverlist + server_count++; sock_init(&server->sock); strcpy(server->address, address_string); server->port = port; server->received = qfalse; server->ping_retries = MAX_PING_RETRIES + 1; ping_server(server); } } } }
/* Cette fonction se charge d'envoyer QUIT au serveur, avant de quitter */ void quit_server () { int res; res=write_command(CMD_QUIT,0,NULL); if (res>0) { res=read_server(tcp_line_read, 3, MAX_READ_SIZE-1); if (res<0) return; } close(tcp_fd); /* Bon, cette procedure est non fiable. */ }
/* On utilise read_server_with_reconnect */ int return_code () { int lus, code; char *buf; lus=(init_connection ? read_server(tcp_line_read,3,MAX_READ_SIZE-1) : read_server_with_reconnect(tcp_line_read, 3, MAX_READ_SIZE-1)); if (lus<3) return -1; code=(int)strtol(tcp_line_read,&buf,10); /* Ici, on gere le cas d'une ligne trop longue */ while (tcp_line_read[lus-1]!='\n') { if (debug) fprintf(stderr, "ligne tres longue ???\n"); lus=read_server(tcp_line_read, 1, MAX_READ_SIZE-1); if (lus<1) { if (debug) fprintf(stderr, "Echec en lecture du serveur\n"); return -1; } if (debug) fprintf(stderr, "%s", tcp_line_read); } if (group_instead_of_list) { group_instead_of_list=0; if (code<400) { long min, max; /* Attention, on a change de groupe, il faut revenir au groupe normal... */ strtol(buf,&buf,10); min=strtol(buf,&buf,10); max=strtol(buf,&buf,10); if (Newsgroup_courant) { va_dans_groupe(); return_code(); /* ICI et pas ailleurs, sinon on casse tcp_line_read */ } snprintf(tcp_line_read,2047,"%s %ld %ld y\r\n",filter_cmd_list,max,min); renvoie_courte_liste=2; } else renvoie_courte_liste=1; return 215; } return code; }
void app(const char *address, const char *name) { int sock; char buffer[BUF_SIZE]; fd_set rdfs; strcpy(buffer, "hello"); sock = init_connection(address); write_server(sock, name); while (1) { init_socket(sock, &rdfs); if(select(sock + 1, &rdfs, NULL, NULL, NULL) == -1) { perror("select()"); exit(errno); } if(FD_ISSET(STDIN_FILENO, &rdfs)) { if (read_keyboard(buffer) < 0) continue; transmit_cmd(buffer,sock); } else if(FD_ISSET(sock, &rdfs)) { int n = read_server(sock, &buffer); printf("receive : %s|\n", buffer); if (fgets(buffer, BUF_SIZE - 1, stdin) == NULL) { perror("fgets()"); continue; } printf("receive:%s\n",buffer); /* server down */ if(n == 0) { printf("Server disconnected !\n"); break; } puts(buffer); } } end_connection(sock); }
int read_server (char *ligne, int deb, int max) { static char raw_buf[MAX_BUF_SIZE], *ptr; static int stockes; int rendus, ret_raw_read, ret; char *ptr2, *ligne_ptr=ligne; if (deb==-1) { stockes=0; return 0; /* retour sans importance */ } if (stockes<=0) { /* nouvelle lecture */ ret_raw_read=raw_read_server(raw_buf, (deb==0 ? 1 : deb) ); if (ret_raw_read<0) /* BUG */ return ret_raw_read; ptr=raw_buf; stockes=ret_raw_read; raw_buf[stockes]='\0'; } ptr2=strchr (ptr, (int)'\n'); if (ptr2==NULL) { if ((rendus=strlen(ptr))<max) { stockes-=rendus; /* devrait etre zero, a moins de lire '\0' */ strcpy(ligne, ptr); ptr+=rendus; ligne_ptr+=rendus; if (stockes>0) { ligne_ptr++; /* on a effectivement lu '\0' ????? */ rendus++; stockes--; ptr++; if (rendus==max) { /* Ouinnnn */ ligne[max]='\0'; return max; } } ret=read_server(ligne_ptr, 0, max-rendus); if (ret<0) return ret; rendus+=ret; ligne[rendus]='\0'; return rendus; } else { rendus=max; strncpy(ligne, ptr, max); ptr+=rendus; stockes-=rendus; ligne[max]='\0'; return max; } } else { ptr2++; if (ptr2-ptr>max) { /* Que des emmerdes ! */ rendus=max; strncpy(ligne, ptr, max); ptr+=rendus; stockes-=rendus; ligne[max]='\0'; return max; } else { /* le cas qui devrait marcher le plus souvent... */ rendus=ptr2-ptr; strncpy(ligne, ptr, rendus); ptr=ptr2; stockes-=rendus; /* stockes devrait etre toujours >= 0 */ ligne[rendus]='\0'; return rendus; } } /* Unreachable */ }
int main(int argc , char ** argv) { if(argc < 2) { printf("Usage : %s [address] [pseudo]\n", argv[0]); return 1; } char buffer[BUF_SIZE_SOCKET]; fd_set rdfs; //SOCKET define sur intd SOCKET sock = init_connection(argv[1]); /* send our name*/ write_server(sock, argv[2]); while (1) { FD_ZERO(&rdfs); // Vide l'ensemble /*add STDION_FILENO */ //descripteur 0 FD_SET(STDIN_FILENO, &rdfs); /* add socket*/ FD_SET(sock, &rdfs); if (select(sock + 1, &rdfs, NULL, NULL, NULL) == -1) { perror("error select"); exit(errno); } if (FD_ISSET(STDIN_FILENO, &rdfs)) { fgets(buffer, BUF_SIZE_SOCKET - 1, stdin); { char *p = NULL; p = strstr(buffer, "\n"); if (p != NULL) { *p = 0; } else { /* fclean */ buffer[BUF_SIZE_SOCKET -1] = 0; } } write_server(sock, buffer); } else if(FD_ISSET(sock, &rdfs)) { int n = read_server(sock, buffer); /*server down*/ if (n == 0) { printf("Server disconnected !\n"); break; } puts(buffer); write_server(sock, "ok"); } } end_connection(sock); return (0); }