43 #include <netinet/in.h>
44 #include <netinet/tcp.h>
47 #include <sys/types.h>
49 #include <sys/socket.h>
51 #include <arpa/inet.h>
59 #define SIGNAL_PORT 8418
60 #define COMMAND_PORT 8417
61 #define SEND_ENVIORNMENT
71 #define WINBUF_NUM 2400
84 int connect_to_host(
char* host,
int port);
86 void read_full(
int file,
char* buffer,
int num);
87 int read_some(
int file,
char* buffer,
int size);
89 void write_buf(
int file,
char* buffer,
int num);
90 int write_amap(
int file,
char* buffer,
int num);
92 void setnonblocking(
int desc);
96 pthread_mutex_t command_mutex;
97 pthread_t stdin_thread,signal_thread;
98 void* readStdin(
void* ptr);
99 void* readSignal(
void* ptr);
116 winbuf=(
char*)malloc(WINSIZE*WINBUF_NUM);
117 end=winbuf+WINSIZE*WINBUF_NUM;
120 speech_desc=connect_to_host(
"localhost",SIGNAL_PORT);
123 perror(
"signal socket");
128 command_desc=connect_to_host(
"localhost",COMMAND_PORT);
131 perror(
"command socket");
135 pthread_mutex_init(&command_mutex,NULL);
136 pthread_create(&stdin_thread,NULL,readStdin,NULL);
137 pthread_create(&signal_thread,NULL,readSignal,NULL);
141 pthread_mutex_lock(&command_mutex);
142 ret=read_some(command_desc,buf,BUFSIZE);
143 pthread_mutex_unlock(&command_mutex);
161 void* readStdin(
void* ptr)
165 fgets(buf,BUFSIZE,stdin);
166 #ifdef SEND_ENVIORNMENT
167 pthread_mutex_lock(&command_mutex);
168 write_buf(command_desc,buf,strlen(buf));
169 pthread_mutex_unlock(&command_mutex);
171 if(feof(stdin) || buf[0]==
'\n')
179 fgets(buf,BUFSIZE,stdin);
180 pthread_mutex_lock(&command_mutex);
181 write_buf(command_desc,buf,strlen(buf));
182 pthread_mutex_unlock(&command_mutex);
188 void* readSignal(
void* ptr)
192 read_full(3,window,WINSIZE);
193 write_buf(speech_desc,window,WINSIZE);
200 void read_full(
int file,
char* buffer,
int num)
206 count=read(file,buffer+pos,num);
207 if(count==0 || (count<0 && errno!=EAGAIN))
217 int connect_to_host(
char* name,
int port)
220 struct hostent* host_entity;
223 struct sockaddr_in host;
227 if(!strcmp(name,
"localhost"))
228 address=htonl(2130706433);
231 address=inet_addr(name);
232 if(address==(in_addr_t)-1)
234 host_entity = gethostbyname(name);
238 fprintf(stderr,
"EAGI proxy: Wrong address!\n");
241 address=*((
int*)host_entity->h_addr);
245 desc=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
248 fprintf(stderr,
"EAGI proxy: Cannot create socket!\n");
252 memset((
void*)&host,0,
sizeof(
struct sockaddr_in));
254 host.sin_family=AF_INET;
255 host.sin_port=htons(port);
258 res=connect(desc,(
struct sockaddr*)&host,
sizeof(host));
261 fprintf(stderr,
"EAGI proxy: Cannot connect!\n");
266 opts = fcntl(desc,F_GETFL);
268 perror(
"fcntl(F_GETFL)");
271 opts = (opts | O_NONBLOCK);
272 if (fcntl(desc,F_SETFL,opts) < 0) {
273 perror(
"fcntl(F_SETFL)");
281 int read_some(
int desc,
char* buffer,
int size)
293 perror(
"Error reading");
312 void write_buf(
int desc,
char* buf,
int size)
322 ret=write_amap(desc,bs,be-bs);
327 ret=write_amap(desc,bs,end-bs);
330 ret=write_amap(desc,winbuf,be-winbuf);
339 ret=write_amap(desc,buf,size);
351 memcpy(be,buf,end-be);
369 fprintf(stderr,
"Buffer overflow!\n");
382 int write_amap(
int desc,
char* buf,
int size)
385 ret=write(desc,buf,size);
390 perror(
"Error writing");
402 void setnonblocking(
int desc)
406 opts = fcntl(desc,F_GETFL);
409 perror(
"fcntl(F_GETFL)");
413 opts = (opts | O_NONBLOCK );
414 if(fcntl(desc,F_SETFL,opts) < 0)
416 perror(
"fcntl(F_SETFL)");
Time-related functions and macros.
static struct ast_sockaddr address
Address for UDPTL.
int main(int argc, char *argv[])