📄 phone.c
字号:
writeput=writefifo; if(j<block160size) { memcpy(writeput,sndblock2+j,block160size-j); writeput+=block160size-j; } } } } else printf("Got packet with id %d, I've received up to %d\n",j,incount); } break; case CODE_TALK: if(connected) break; mesg[1+MAXNAMESIZE]=0; printf("Received TALK from %s\n",mesg+1); gottalk=1; memcpy(&buddy,from,sizeof(buddy)); if(havedsp) chime(); else { opendsp(); chime(); closedsp(); } break; case CODE_BYE: if(!connected) break; if(memcmp(&buddy.sin_addr.s_addr,&from->sin_addr.s_addr,IPADDRSIZE)) break; if(memcmp(&buddy.sin_port,&from->sin_port,IPPORTSIZE)) break; closedsp(); connected=0; memset(&buddy,0,sizeof(buddy)); printf("Disconnected, received BYE\n"); break; case CODE_NAME: printf(" %s\n",msg); break; case CODE_UNKNOWN: printf("Received a CODE_UNKNOWN from the matcher, which means your\n"); printf("email address identity is unknown to the server. Check your\n"); printf(".phonerc file.\n"); break; case CODE_EMPTY: printf("--Empty list--\n"); printf("Your contact list is empty. Add names to your contact list\n"); printf("and get them to add your name to theirs.\n"); break; case CODE_INVALID: printf("Server claims your password is invalid. Check your .phonerc\n"); printf("file and make sure you put in the pass line from the email\n"); printf("you should have received.\n"); break; case CODE_NOONE: printf("No one in your contact list is online\n"); break; }}int iswhite(int c){ return c==' ' || c=='\t' || c=='\n' || c=='\r';}void token(char *put,int len,char **take){ while(**take && iswhite(**take)) ++(*take); while(**take && !iswhite(**take) && len>1) { *put++=*(*take)++; --len; } *put=0;}void report(void){int t; if(!havedsp || !connected) { printf("Not connected to anyone\n"); return; } t=gtime2(); printf("%9ld: msec dsp open\n",t-dspopentime); printf("%9ld: msec of sound data written to dsp\n",totalwritten>>3+1); printf("%9ld: msec of sound data read from dsp\n",totalread>>3+1); printf("%9ld: outcount\n",outcount); printf("%9ld: incount\n",incount); printf("%9ld: msec latency\n",latency); printf("%9d: msec writein\n",writein>> (block160size==160 ? 3 : 4));}void dobye(){ if(!connected) return; printf("Sending BYE packet.\n"); mesg[0]=CODE_BYE; putmsg(&buddy,mesg,1); memset(&buddy,0,sizeof(buddy)); connected=0; closedsp();}void processtyped(char *msg,int size){char com[64];char data[64];char *resolved; if(--size<0) return; if(size>MAXNAMESIZE) size=MAXNAMESIZE; msg[size]=0; token(com,sizeof(com),&msg); token(data,sizeof(data),&msg); tryingtocall=0; if(!strcmp(com,"call")) { if(connected) { printf("You're already connected. Use \"bye\" to disconnect.\n"); } else { tryingtocall=1; resolved=findalias(data); if(!resolved) resolved=data; printf("Trying to call %s\n",resolved); strcpy(calling,resolved); msg[0]=CODE_CALL; computehash(msg+1,resolved); messagetoserver(msg,9); } } else if(!strcmp(com,"who")) { msg[0]=CODE_WHO; messagetoserver(msg,1); } else if(!strcmp(com,"bye")) { if(!connected) printf("You're not connected.\n"); else dobye(); } else if(!strcmp(com,"info")) { report(); } else if(!strcmp(com,"alias")) { listaliases(); } else if(!strcmp(com,"quit")) { dobye(); unregisterme(); exitflag=1; } else if(!strcmp(com,"answer")) { if(gottalk) answer(); else printf("You haven't received a TALK request.\n"); } else if(!strcmp(com,"add")) { resolved=findalias(data); if(!resolved) resolved=data; printf("Trying to add %s\n",resolved); msg[0]=CODE_ADD; computehash(msg+1,resolved); messagetoserver(msg,9); } else if(!strcmp(com,"del")) { resolved=findalias(data); if(!resolved) resolved=data; printf("Trying to remove %s\n",resolved); msg[0]=CODE_REMOVE; computehash(msg+1,resolved); messagetoserver(msg,9); } else if(!strcmp(com,"list")) { msg[0]=CODE_LIST; messagetoserver(msg,1); } else { printf("Commands:\n"); printf("add <username> = add user to your contact list\n"); printf("alias = list aliases\n"); printf("answer = answer a TALK request\n"); printf("bye = disconnect call\n"); printf("call <username> = make call\n"); printf("del <username> = remove user from your contact list\n"); printf("info = print out some statistics\n"); printf("list = printout contact list\n"); printf("quit = quit out of program\n"); printf("who = query server for who in your contact list is online\n"); printf("---\n"); }}#define THRESHOLD (8<<8)int checkloud(unsigned char *p){int i,j,max,min; i=160; max=-5000; min=5000; while(i--) { j=*p++; j|=*p++<<8; if(j>=0x8000) j-=0x10000; if(j<min) min=j; if(j>max) max=j; } if(max-min>THRESHOLD) return 1; return 0;}void cchandler(int num){ signal(SIGINT,SIG_IGN);}#define VOL 0x20chime(){unsigned char buff[1024];int i; if(!havedsp) return; if(block160size==160) { for(i=0;i<160;++i) if(i%40<20) buff[i]=0x80-VOL; else buff[i]=0x80+VOL; } else { for(i=0;i<160;++i) if(i%40<20) buff[i+i]=0,buff[i+i+1]=-VOL; else buff[i+i]=0,buff[i+i+1]=VOL; } for(i=0;i<8;++i) write(sndin,buff,block160size);}main(int argc,char *argv[]){int size,pid,result,port;long nexttime,t;fd_set inputs,outputs;struct timeval timeout;char typed[256];int samplesin;int i,j;char *s;unsigned char *p; cchandler(0); inittime(); srand(timebase); myiplen=getmyip(myip); if(myiplen<0) { printf("Failed to determine local IP address, code %d\n",myiplen); return -60; } scanrcfile(0); if(!findalias("me")) addalias("me",username); for(i=0;i<KEYSIZE;++i) if(mykey[i]) break; if(i==KEYSIZE) { printf("WARNING:You haven't specificied your password in the PHONERC file.\n"); printf("Pretty much nothing will work without that password. Visit the site\n"); printf(" http://www.linuxmotors.com/phone\n"); printf("and follow the link there to get a password assigned.\n"); } computehash(myhash,username); havedsp=0; initinputcompression(); initoutputcompression(); i=opendsp(); if(i) { printf("Failed to open dsp for test, code %d\n",i); } closedsp(); if(!openserver(SERVER,PORT)) { printf("Failed to resolve server %s\n",s); exit(2); } getsocket(0); exitflag=0; loudcount=0; samplesin=0; connected=0; gottalk=0; nexttime=gtime(); talktries=0; while(!exitflag) { if(gtime()>=nexttime) { nexttime+=UPDATETIME; registerme(); } if(talktries>0 && gtime()>=nextsendtalk) sendtalk(); FD_ZERO(&inputs); FD_ZERO(&outputs); FD_SET(udpsocket,&inputs); FD_SET(0,&inputs); if(havedsp) { FD_SET(sndin,&inputs); if(writein>sndblocksize) FD_SET(sndin,&outputs); } timeout.tv_sec=1; timeout.tv_usec=0; t=select(32,&inputs,&outputs,0,&timeout); if(!t) continue; if(FD_ISSET(0,&inputs)) // typed in something { size=read(0,typed,sizeof(typed)); if(size>0) processtyped(typed,size); } if(FD_ISSET(udpsocket,&inputs)) // received a packet { size=getmsg(0); if(size>0) {// printf ("received control input\n"); userprocessmsg(&sender,mesg,size); } } if(havedsp && FD_ISSET(sndin,&inputs)) // dsp data { size=read(sndin,sndblock1+samplesin,sndblocksize); if(connected && size>0) { totalread+=size; samplesin+=size; i=0; while(samplesin-i>=block160size) { loudcount+=checkloud(sndblock1+i); pack160to33(packed+12+packedin*33,sndblock1+i); i+=block160size; ++packedin; if(packedin==PACKETSTOPACK) { long now; now=gtime2(); packed[0]=CODE_SOUND; packed[1]=outcount>>16; packed[2]=outcount>>8; packed[3]=outcount; packed[4]=now>>24; packed[5]=now>>16; packed[6]=now>>8; packed[7]=now; packed[8]=lastdiff>>24; packed[9]=lastdiff>>16; packed[10]=lastdiff>>8; packed[11]=lastdiff; ++outcount; putmsg(&buddy,packed,12+PACKETSTOPACK*33); packedin=0; loudcount=0; } } if(i) { samplesin-=i; memcpy(sndblock1,sndblock1+i,samplesin); } } } if(havedsp && FD_ISSET(sndin,&outputs)) //&& writein>=sndblocksize) { if(writein>=sndblocksize*(MAXBLOCKS+1)) { while(writein>=sndblocksize<<1) { writein-=sndblocksize; writetake+=sndblocksize; if(writetake==writelimit) writetake=writefifo; } } write(sndin,writetake,sndblocksize); totalwritten+=sndblocksize; writein-=sndblocksize; writetake+=sndblocksize; if(writetake==writelimit) writetake=writefifo; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -