void Point3NormalizeValidator::validateType(SimObject *object, void *typePtr) { Point3F *v = (Point3F *) typePtr; const F32 len = v->len(); if(!mIsEqual(len, 1.0f)) { consoleError(object, "Vector length must be %g", length); *v *= length / len; } }
void IRangeValidator::validateType(SimObject *object, void *typePtr) { S32 *v = (S32 *) typePtr; if(*v < minV || *v > maxV) { consoleError(object, "Must be between %d and %d", minV, maxV); if(*v < minV) *v = minV; else if(*v > maxV) *v = maxV; } }
int clientConnect(int argc, char **argv) { hostent *ptr_client; /* Client infos */ hostent *ptr_host; /* Host infos */ char *prog; /* Program Name */ const char *hostc; /* Server address char* */ in_addr host; /* Server address */ char login[LOGIN_SIZE]; /* User name */ char pass[PASSWORD_MAX]; /* User password */ int port; /* Port of the remote machine */ bool linked = False; /* If the linbk with server is etablished */ int try = 1; /* Number of connecting try */ int try_Max = MAX_CONNECTION_TRY; /* Maximum of try to connect */ int askCo = 0; /* Result of the connection try */ STATE = False; /* Set client status to disconnected */ int errorN = 0; /* Error numlber */ struct timeval timeout; /* Timeout for read and write */ timeout.tv_sec = RS_TIMEOUT; timeout.tv_usec = 0; if (argc < 5) { if(argc>0) { prog = argv[0]; } else { prog = "client"; } sprintf(logC,"%s <server_address> <server_port> <user_name> <user_password>\n",prog); consoleUsage(logC); memset(&logC,0,strlen(logC)); exit(1); } prog = argv[0]; hostc = argv[1]; inet_aton(hostc, &host); port = atoi(argv[2]); strcpy(login,argv[3]); if (strlen(login) > (LOGIN_SIZE-1)){ // 'Cause of '\0' /* Error 13: Login size too long (17 char max) */ consoleErrorLog(13); exit(1); } strcpy(pass,argv[4]); if (strlen(pass) > (PASSWORD_MAX-1) || strlen(pass) < (PASSWORD_MIN)){ // 'Cause of '\0' /* Error 14: Password size incorrect (Between 7 and 19) */ consoleErrorLog(14); exit(1); } int argN = 5; /* while(argN<argc) { char tmp[strlen(msg)+strlen(argv[argN])+1]; strcpy(tmp,msg); strcat(tmp," "); strcat(tmp,argv[argN]); strcpy(msg,tmp); argN++; } */ char machine[NAME_SIZE+1]; gethostname(machine,NAME_SIZE); if((ptr_client = gethostbyname(machine)) == NULL) { consoleError("Can not find the machine address"); return errno; } /* character by character copy of ptr_client informations to client address */ bcopy((char*)ptr_client->h_addr, (char*)&client_address.sin_addr,ptr_client->h_length); client_address.sin_family = AF_INET; /* Use a new port number */ client_address.sin_port = htons(port); if((ptr_host = gethostbyname(hostc)) == NULL) { consoleError("Can not find the server from its address"); return errno; } /* character by character copy of ptr_host informations to local address */ bcopy((char*)ptr_host->h_addr, (char*)&server_address.sin_addr,ptr_host->h_length); server_address.sin_family = AF_INET; /* Use a new port number */ server_address.sin_port = htons(port); /* Creation of the socket */ if ((socket_descriptor = socket(AF_INET, SOCK_STREAM, 0)) <0){ consoleError("Unable to create socket connection with the server"); return errno; } if (setsockopt (socket_descriptor, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0) { consoleWarn("Set socket receive options failed\n"); } if (setsockopt (socket_descriptor, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0) { consoleWarn("Set socket send options failed\n"); } while(!STATE&&try<=try_Max) { /* Try to connect to the server with infos in server_address */ if(!linked&&(connect(socket_descriptor, (sockaddr*)(&server_address), sizeof(sockaddr_in))) < 0) { consoleError("Unable to connect to server"); errorN=errno; } else { if(!linked) { consoleLog("Connection established with the server\n"); errorN=0; } linked=True; askCo=askConnection(server_address.sin_addr,login,pass,SERVER_ADMIN_NAME); if(askCo==0&&STATE) { try=0; } else { /* Server is full, we can try until someone disconnect */ if(askCo==0) { try_Max=100; } else { // Wrong password || Login not allowed || Login already used if(askCo==3||askCo==2||askCo==1) { exit(0); } try_Max=MAX_CONNECTION_TRY; } } } if(!STATE) { sprintf(errorC,"Attempt (#%d) to connect to server failed [with error #%d]\n",try++,askCo); consoleError(errorC); memset(&errorC,0,strlen(errorC)); if(askCo==14||askCo==22) { errorN=askCo; close(socket_descriptor); return clientConnect(argc,argv); } if(try<try_Max) { consoleLog("Retry to connect in 3 seconds...\n"); sleep(3); } }
int askConnection(in_addr host, char *login, char *pass, char *dst) { packet p; /* Packet */ current_path.nb_elmt=0; /* Size of path elmt */ char msg[strlen(ASK_CONNECT)+1]; strcpy(msg,ASK_CONNECT); strcpy(user_login,login); strcat(msg,":"); consoleLog("Asking for connection in progress...\n"); int sd=send_packet_to(host,login,dst,CMD_TYPE,msg); if(!sd) { consoleLog("Request sent to server\n"); } else { consoleError("Request not sent to server"); } int rd=read_packet(socket_descriptor,&p,False); if(rd!=0) { /* Connection reset by peer */ if(rd==22||rd==14) { return rd; } } if(strcmp(p.data_type,CMD_TYPE)) { return askConnection(host,login,pass,dst); } while(!STATE) { int comd=processCOMD(p.data,server_address,login); memset(&p,0,sizeof(packet)); if(comd==0) { STATE=1; consoleLog("You are now connected to server\n"); break; } else if(comd==-1) { // Send password char pw[strlen(SEND_PASSWORD)+2+strlen(pass)]; strcpy(pw,SEND_PASSWORD); strcat(pw,":"); strcat(pw,pass); sd=send_packet_to(host,login,dst,CMD_TYPE,pw); if(!sd) { consoleLog("Request sent to server\n"); } else { consoleError("Request not sent to server"); } } else { return comd; } int rd=read_packet(socket_descriptor,&p,False); if(rd!=0) { /* Connection reset by peer */ if(rd==22||rd==14) { return rd; } } } return 0; }