//top main int main(int argc, char **argv){ DB(fprintf(stderr,"IN main()\n")); //argment analysis OPT serv_op; InitOpt(&serv_op); GetOpt(argc-1,argv+1,&serv_op); DB(fprintf(stderr,"n_file :%s:\n",serv_op.n_file)); DB(fprintf(stderr,"data file :%s:\n",serv_op.db_file)); DB(fprintf(stderr,"default service :%s:\n",SERVICE_NAME)); DB(fprintf(stderr,"sub port :%d:\n",serv_op.port)); //init global vars InitBuf(&RecvBuf,SHORT_BUF_SIZE); InitBuf(&CommBuf,LONG_BUF_SIZE); //init socket if(InitSocket(SERVICE_NAME,serv_op.port) == -1){ return(-1); } //init signal - bind() InitSignal(); //listen listen(Soc,SOMAXCONN); //data read read_db_n(serv_op); read_db_term(serv_op); //mainloop - data read, accept(), fork(), ChildLoop() MainLoop(serv_op); //close socket CloseSocket(); DB(fprintf(stderr,"OUT main()\n")); return(0); }
void Init(void) { var.frame=0; // kalkulasi rate frame // Init Terrain InitBuf(); // Init the Wheel Smoke InitSmoke(); // kamera var.anc=-20; var.gira=false; // True when the car is steering var.vel=0; // >0 if the speed car change var.cameratype=0; // Posisi velg dan mobil car.x=0; car.y=0.75; car.z=0; car.any=0; car.dany=0; car.velocita=10; // Inisialisasi kecepatan car.vf=10; car.dvf=0; car.xruote[0]=2.4; car.yruote[0]=0.2; car.zruote[0]=-5.2; car.xruote[1]=-2.4; car.yruote[1]=0.2; car.zruote[1]=-5.2; car.xruote[2]=2.4; car.yruote[2]=0.2; car.zruote[2]=4.3; car.xruote[3]=-2.4; car.yruote[3]=0.2; car.zruote[3]=4.3; car.anruote=0; // Sudut velg car.gira=0; // Sudut memulai mobil // Inisialisasi posisi kamera var.Iox=car.x+50*cos((car.any+(rand()%90-rand()%90))*3.141/180); var.Ioy=(float)(1+rand()%10); var.Ioz=car.z+50*sin((car.any+(rand()%90-rand()%90))*3.141/180); }
//top main int main(int argc, char **argv){ int i; struct argop options; int s = -1; struct hostent *serverhost = NULL; struct sockaddr_in server; char *carrier_buf; char *greeting_buf; char *hostname; char *path; int port = 0; int carrier_read_size = 0; int opt_stat = 0; int server_ready = -1; int greeting_retry = 0; int greeting_retry_max = 5; FILE *IN; int get_i; char get_c; int buf_len = 0; //alloc or init InitBuf(&RecvBuf,BUF_LEN); InitBuf(&SendBuf,LONG_BUF_LEN); carrier_buf = c_calloc_vec(LONG_BUF_LEN); greeting_buf = c_calloc_vec(LONG_BUF_LEN); hostname = c_calloc_vec(LONG_STR_LEN); path = c_calloc_vec(LONG_STR_LEN); init_options(&options); //argment analysis if(argc == 1){ print_help(); exit(0); }else if(argc >= 2){ opt_stat = get_options(1,argc,argv,&options); strcpy(hostname,options.host); strcpy(path,options.path); port = options.port; if((serverhost = gethostbyname(hostname)) == NULL){ fprintf(stderr,"failed : gethostbyname().\n"); exit(1); } bzero((char *)&server,sizeof(server)); server.sin_family = AF_INET; bcopy(serverhost->h_addr,(char *)&server.sin_addr,serverhost->h_length); server.sin_port = htons(port); if((s = socket(AF_INET,SOCK_STREAM,0)) < 0){ fprintf(stderr,"failed : socket().\n"); exit(1); } if(connect(s,(struct sockaddr *)&server,sizeof(server)) == -1){ fprintf(stderr,"failed : connect.\n"); exit(1); } //get greeting - check "Ready." try 5 max //SetBlock(s,0); // non-block SetBlock(s,1); // block while(greeting_retry_max - greeting_retry > 0){ RecvBuf.len = read(s,RecvBuf.buf,RecvBuf.size); if(RecvBuf.buf[RecvBuf.len - 1] == 0x04){ fprintf(stderr,"%s\n",RecvBuf.buf); TrimBuf(&RecvBuf); server_ready = strcmp(RecvBuf.buf + RecvBuf.len - strlen(READY_FLAG),READY_FLAG); if(server_ready != 0){ greeting_retry++; fprintf(stderr,"greeting error - retry.\n"); }else{ fprintf(stderr,"greeting - success!\n"); break; } }else{ fprintf(stderr,"Server error - retry.\n"); greeting_retry++; } } if(server_ready != 0){ fprintf(stderr,"Server error occurred - exit.\n"); exit(0); } //send command SetBlock(s,1); // block fprintf(stderr,"mode: %s\n",options.method); InitBuf(&SendBuf,LONG_BUF_LEN); if(strcmp(options.method,"FIN") == 0){ fprintf(stderr,"method FIN : not support.\n"); exit(0); }else if(strcmp(options.method,"SIN") == 0){ IN = stdin; while((get_i = getc(IN)) != EOF){ get_c = (char)get_i; if(buf_len + 2 > SendBuf.size){ SendBuf.buf[buf_len] = '\0'; SendBuf.len = strlen(SendBuf.buf); write(s,SendBuf.buf,SendBuf.len); } SendBuf.buf[buf_len] = get_c; buf_len++; } SendBuf.buf[buf_len] = '\0'; SendBuf.len = strlen(SendBuf.buf); AddOver(&SendBuf); write(s,SendBuf.buf,SendBuf.len); }else{ sprintf(SendBuf.buf,"Test?"); SendBuf.len = strlen(SendBuf.buf); AddOver(&SendBuf); SendBuf.len = strlen(SendBuf.buf); write(s,SendBuf.buf,SendBuf.len); } //mode set //SetBlock(s,0); // non-block SetBlock(s,1); // block while(1){ for(i=0;i<BUF_LEN;i++){ carrier_buf[i] = '\0'; } carrier_read_size = read(s,carrier_buf,BUF_LEN); if(carrier_read_size > 0){ for(i=0;i<BUF_LEN;i++){ if(carrier_buf[i] == 0x04){ goto loop_end; }else{ write(1,carrier_buf+i,1); } } }else{ goto loop_end; } } loop_end: fprintf(stderr,"break loop.\n"); close(s); exit(0); } //exit return(0); }
int softrock_open(void) { #ifdef DIRECTAUDIO int arg; int status; #endif #ifdef PORTAUDIO int rc; int status; #endif #ifdef PULSEAUDIO int error; pa_sample_spec params; pa_buffer_attr attrs; #endif #ifdef PORTAUDIO PaStreamParameters inputParameters; PaStreamParameters outputParameters; PaStreamInfo *info; int devices; int i; PaDeviceInfo* deviceInfo; if (softrock_get_verbose()) fprintf(stderr,"softrock_open: portaudio\n"); #endif #ifdef DIRECTAUDIO if (softrock_get_verbose()) fprintf(stderr,"softrock_open: %s\n",softrock_get_device()); #endif if(softrock_get_playback()) { return 0; } #ifdef PULSEAUDIO if (softrock_get_verbose()) fprintf(stderr,"Using PulseAudio\n"); params.format=PA_SAMPLE_FLOAT32LE; params.rate=softrock_get_sample_rate(); params.channels=2; attrs.maxlength=attrs.minreq=attrs.prebuf=attrs.tlength=(uint32_t)-1; attrs.fragsize=attrs.maxlength=attrs.minreq=attrs.prebuf=(uint32_t)-1; attrs.fragsize=SAMPLES_PER_BUFFER*2 * sizeof(float); attrs.tlength=SAMPLES_PER_BUFFER*2 * sizeof(float); if (softrock_get_verbose()) fprintf(stderr,"params.rate=%d\n",params.rate); stream=pa_simple_new("localhost","Softrock", PA_STREAM_RECORD, NULL, "IQ", ¶ms, NULL, &attrs, &error); if(stream==NULL) { if (softrock_get_verbose()) fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error)); exit(0); } playback_stream=pa_simple_new("localhost","Softrock", PA_STREAM_PLAYBACK, NULL, "IQ", ¶ms, NULL, &attrs, &error); if(playback_stream==NULL) { if (softrock_get_verbose()) fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error)); exit(0); } ftime(&start_time); #endif #ifdef PORTAUDIO if (softrock_get_verbose()) fprintf(stderr,"Using PortAudio\n"); rc=Pa_Initialize(); if(rc!=paNoError) { if (softrock_get_verbose()) fprintf(stderr,"Pa_Initialize failed: %s\n",Pa_GetErrorText(rc)); exit(1); } devices=Pa_GetDeviceCount(); if(devices<0) { if (softrock_get_verbose()) fprintf(stderr,"Px_GetDeviceCount failed: %s\n",Pa_GetErrorText(devices)); } else { if (softrock_get_verbose()) fprintf(stderr,"default input=%d output=%d devices=%d\n",Pa_GetDefaultInputDevice(),Pa_GetDefaultOutputDevice(),devices); for(i=0;i<devices;i++) { deviceInfo=Pa_GetDeviceInfo(i); if (softrock_get_verbose()) fprintf(stderr,"%d - %s\n",i,deviceInfo->name); if (softrock_get_verbose()) fprintf(stderr,"maxInputChannels: %d\n",deviceInfo->maxInputChannels); if (softrock_get_verbose()) fprintf(stderr,"maxOututChannels: %d\n",deviceInfo->maxOutputChannels); //if (softrock_get_verbose()) fprintf(stderr,"defaultLowInputLatency: %f\n",deviceInfo->defaultLowInputLatency); //if (softrock_get_verbose()) fprintf(stderr,"defaultLowOutputLatency: %f\n",deviceInfo->defaultLowOutputLatency); //if (softrock_get_verbose()) fprintf(stderr,"defaultHighInputLatency: %f\n",deviceInfo->defaultHighInputLatency); //if (softrock_get_verbose()) fprintf(stderr,"defaultHighOutputLatency: %f\n",deviceInfo->defaultHighOutputLatency); //if (softrock_get_verbose()) fprintf(stderr,"defaultSampleRate: %f\n",deviceInfo->defaultSampleRate); } } inputParameters.device=atoi(softrock_get_input()); inputParameters.channelCount=2; inputParameters.sampleFormat=paFloat32; inputParameters.suggestedLatency=Pa_GetDeviceInfo(inputParameters.device)->defaultLowInputLatency; inputParameters.hostApiSpecificStreamInfo=NULL; outputParameters.device=atoi(softrock_get_output()); outputParameters.channelCount=2; outputParameters.sampleFormat=paFloat32; outputParameters.suggestedLatency=Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency; outputParameters.hostApiSpecificStreamInfo=NULL; if (softrock_get_verbose()) fprintf(stderr,"input device=%d output device=%d\n",inputParameters.device,outputParameters.device); rc=Pa_OpenStream(&stream,&inputParameters,&outputParameters,(double)softrock_get_sample_rate(),(unsigned long)SAMPLES_PER_BUFFER,paNoFlag,NULL,NULL); if(rc!=paNoError) { if (softrock_get_verbose()) fprintf(stderr,"Pa_OpenStream failed: %s\n",Pa_GetErrorText(rc)); exit(1); } rc=Pa_StartStream(stream); if(rc!=paNoError) { if (softrock_get_verbose()) fprintf(stderr,"Pa_StartStream failed: %s\n",Pa_GetErrorText(rc)); exit(1); } info=Pa_GetStreamInfo(stream); if(info!=NULL) { if (softrock_get_verbose()) fprintf(stderr,"stream.sampleRate=%f\n",info->sampleRate); if (softrock_get_verbose()) fprintf(stderr,"stream.inputLatency=%f\n",info->inputLatency); if (softrock_get_verbose()) fprintf(stderr,"stream.outputLatency=%f\n",info->outputLatency); } else { if (softrock_get_verbose()) fprintf(stderr,"Pa_GetStreamInfo returned NULL\n"); } #endif #ifdef DIRECTAUDIO if (softrock_get_verbose()) fprintf(stderr,"Using direct audio\n"); /* open sound device */ fd = open(softrock_get_device(), O_RDWR); if (fd < 0) { perror("open of audio device failed"); exit(1); } /* set sampling parameters */ arg = SAMPLE_SIZE; /* sample size */ status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg); if (status == -1) perror("SOUND_PCM_WRITE_BITS ioctl failed"); if (arg != SAMPLE_SIZE) perror("unable to set write sample size"); status = ioctl(fd, SOUND_PCM_READ_BITS, &arg); if (status == -1) perror("SOUND_PCM_READ_BITS ioctl failed"); if (arg != SAMPLE_SIZE) perror("unable to set read sample size"); arg = CHANNELS; /* mono or stereo */ status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg); if (status == -1) perror("SOUND_PCM_WRITE_CHANNELS ioctl failed"); if (arg != CHANNELS) perror("unable to set number of channels"); arg = softrock_get_sample_rate(); /* sampling rate */ if (softrock_get_verbose()) fprintf(stderr,"sample_rate: %d\n",arg); status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg); if (status == -1) perror("SOUND_PCM_WRITE_WRITE ioctl failed"); arg = AFMT_S16_LE; /* signed little endian */ status = ioctl(fd, SOUND_PCM_SETFMT, &arg); if (status == -1) perror("SOUND_PCM_SETFMTS ioctl failed"); #endif InitBuf(&rx_r, "RX_R"); InitBuf(&rx_l, "RX_R"); printf("my ring buffers are set\n"); return 0; }