123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <netinet/in.h>
- #include <netinet/tcp.h>
- #include <string.h>
- #include <time.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/socket.h>
- #include <sys/time.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <ctype.h>
- #include <pthread.h>
- #define SIGNAL_PORT 8418
- #define COMMAND_PORT 8417
- #define SEND_ENVIORNMENT
- #define BUFSIZE 1024
- char buf[BUFSIZE];
- #define WINSIZE 400
- char window[WINSIZE];
- #define WINBUF_NUM 2400
- char* winbuf;
- char *end, *bs, *be;
- int command_desc;
- int speech_desc;
- char connected=1;
- int connect_to_host(char* host, int port);
- void read_full(int file, char* buffer, int num);
- int read_some(int file, char* buffer, int size);
- void write_buf(int file, char* buffer, int num);
- int write_amap(int file, char* buffer, int num);
- void setnonblocking(int desc);
- void finalize();
- pthread_mutex_t command_mutex;
- pthread_t stdin_thread,signal_thread;
- void* readStdin(void* ptr);
- void* readSignal(void* ptr);
- int main()
- {
- int ret;
-
- atexit(finalize);
- setlinebuf(stdin);
- setlinebuf(stdout);
- winbuf=(char*)malloc(WINSIZE*WINBUF_NUM);
- end=winbuf+WINSIZE*WINBUF_NUM;
- bs=be=winbuf;
- speech_desc=connect_to_host("localhost",SIGNAL_PORT);
- if(speech_desc<0)
- {
- perror("signal socket");
- return -1;
- }
- command_desc=connect_to_host("localhost",COMMAND_PORT);
- if(command_desc<0)
- {
- perror("command socket");
- return -1;
- }
- pthread_mutex_init(&command_mutex,NULL);
- pthread_create(&stdin_thread,NULL,readStdin,NULL);
- pthread_create(&signal_thread,NULL,readSignal,NULL);
- while(connected)
- {
- pthread_mutex_lock(&command_mutex);
- ret=read_some(command_desc,buf,BUFSIZE);
- pthread_mutex_unlock(&command_mutex);
- if(ret>0)
- {
- buf[ret]=0;
- printf("%s",buf);
- }
- }
- return 0;
- }
- void finalize()
- {
- close(command_desc);
- close(speech_desc);
- free(winbuf);
- }
- void* readStdin(void* ptr)
- {
- while(1)
- {
- fgets(buf,BUFSIZE,stdin);
- #ifdef SEND_ENVIORNMENT
- pthread_mutex_lock(&command_mutex);
- write_buf(command_desc,buf,strlen(buf));
- pthread_mutex_unlock(&command_mutex);
- #endif
- if(feof(stdin) || buf[0]=='\n')
- {
- break;
- }
- }
- while(connected)
- {
- fgets(buf,BUFSIZE,stdin);
- pthread_mutex_lock(&command_mutex);
- write_buf(command_desc,buf,strlen(buf));
- pthread_mutex_unlock(&command_mutex);
- }
- pthread_exit(NULL);
- }
- void* readSignal(void* ptr)
- {
- while(connected)
- {
- read_full(3,window,WINSIZE);
- write_buf(speech_desc,window,WINSIZE);
- }
- pthread_exit(NULL);
- }
- void read_full(int file, char* buffer, int num)
- {
- int count,pos=0;
- while(num)
- {
- count=read(file,buffer+pos,num);
- if(count==0 || (count<0 && errno!=EAGAIN))
- {
- connected=0;
- return;
- }
- num-=count;
- pos+=count;
- }
- }
- int connect_to_host(char* name, int port)
- {
- int address;
- struct hostent* host_entity;
- int res,desc;
- int opts;
- struct sockaddr_in host;
-
-
- if(!strcmp(name,"localhost"))
- address=htonl(2130706433);
- else
- {
- address=inet_addr(name);
- if(address==(in_addr_t)-1)
- {
- host_entity = gethostbyname(name);
-
- if(!host_entity)
- {
- fprintf(stderr,"EAGI proxy: Wrong address!\n");
- return -1;
- }
- address=*((int*)host_entity->h_addr);
- }
- }
- desc=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
- if(desc<0)
- {
- fprintf(stderr,"EAGI proxy: Cannot create socket!\n");
- return -1;
- }
- memset((void*)&host,0,sizeof(struct sockaddr_in));
-
- host.sin_family=AF_INET;
- host.sin_port=htons(port);
- host.sin_addr.s_addr=address;
-
- res=connect(desc,(struct sockaddr*)&host,sizeof(host));
- if(res<0)
- {
- fprintf(stderr,"EAGI proxy: Cannot connect!\n");
- return -1;
- }
-
- opts = fcntl(desc,F_GETFL);
- if (opts < 0) {
- perror("fcntl(F_GETFL)");
- exit(EXIT_FAILURE);
- }
- opts = (opts | O_NONBLOCK);
- if (fcntl(desc,F_SETFL,opts) < 0) {
- perror("fcntl(F_SETFL)");
- exit(EXIT_FAILURE);
- }
- return desc;
- }
- int read_some(int desc, char* buffer, int size)
- {
- unsigned char c;
- int res,i=0;
- for(;;)
- {
- res=read(desc,&c,1);
- if(res<1)
- {
- if(errno!=EAGAIN)
- {
- perror("Error reading");
- connected=0;
- }
- break;
- }
- if(res==0)
- {
- connected=0;
- break;
- }
-
- buffer[i]=c;
- i++;
- }
- return i;
- }
- void write_buf(int desc, char* buf, int size)
- {
- int ret;
-
- if(be!=bs)
- {
- if(be>bs)
- {
- ret=write_amap(desc,bs,be-bs);
- bs+=ret;
- }
- else
- {
- ret=write_amap(desc,bs,end-bs);
- if(ret==end-bs)
- {
- ret=write_amap(desc,winbuf,be-winbuf);
- bs=winbuf+ret;
- }
- else bs+=ret;
- }
- }
- if(be==bs)
- {
- ret=write_amap(desc,buf,size);
- buf+=ret;
- size-=ret;
- }
- if(size)
- {
- if(be>=bs)
- {
- if(size>end-be)
- {
- size-=end-be;
- memcpy(be,buf,end-be);
- be=winbuf;
- buf+=end-be;
- }
- else
- {
- memcpy(be,buf,size);
- be+=size;
- if(be>=end)
- be=winbuf;
- size=0;
- }
- }
- if(size)
- {
- if(size>=bs-be)
- {
- fprintf(stderr,"Buffer overflow!\n");
- size=bs-be-1;
- }
- if(size)
- {
- memcpy(be,buf,size);
- be+=size;
- }
- }
- }
- }
- int write_amap(int desc, char* buf, int size)
- {
- int ret;
- ret=write(desc,buf,size);
- if(ret<0)
- {
- if(errno!=EAGAIN)
- {
- perror("Error writing");
- connected=0;
- }
- return 0;
- }
- if(ret==0)
- connected=0;
- return ret;
- }
- void setnonblocking(int desc)
- {
- int opts;
-
- opts = fcntl(desc,F_GETFL);
- if(opts < 0)
- {
- perror("fcntl(F_GETFL)");
- exit(-1);
- }
- opts = (opts | O_NONBLOCK );
- if(fcntl(desc,F_SETFL,opts) < 0)
- {
- perror("fcntl(F_SETFL)");
- exit(-1);
- }
- }
|