Browse Source

support control_file

wangyu- 8 years ago
parent
commit
740e10bd04
2 changed files with 31 additions and 1 deletions
  1. 1 0
      common.h
  2. 30 1
      main.cpp

+ 1 - 0
common.h

@@ -21,6 +21,7 @@
 #include <sys/wait.h>
 #include <sys/socket.h>    //for socket ofcourse
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <stdlib.h> //for exit(0);
 #include <errno.h> //For errno - the error number
 #include <netinet/tcp.h>   //Provides declarations for tcp header

+ 30 - 1
main.cpp

@@ -7,7 +7,7 @@
 #include "encrypt.h"
 
 int mtu_warn=1375;//if a packet larger than mtu warn is receviced,there will be a warning
-
+char fifo_file[1000]="./test.fifo";
 
 int server_on_raw_recv_pre_ready(conn_info_t &conn_info,char * ip_port,u32_t tmp_oppsite_const_id);
 int server_on_raw_recv_ready(conn_info_t &conn_info,char * ip_port,char type,char *data,int data_len);
@@ -1002,6 +1002,12 @@ int client_event_loop()
 	packet_info_t &send_info=conn_info.raw_info.send_info;
 	packet_info_t &recv_info=conn_info.raw_info.recv_info;
 
+	assert(mkfifo (fifo_file, 0666)==0);
+	int fifo_fd=open (fifo_file, O_RDWR);
+	assert(fifo_fd>0);
+	setnonblocking(fifo_fd);
+
+
 
 	if(lower_level)
 	{
@@ -1136,6 +1142,15 @@ int client_event_loop()
 		myexit(-1);
 	}
 
+	ev.events = EPOLLIN;
+	ev.data.u64 = fifo_fd;
+
+	ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, fifo_fd, &ev);
+	if (ret!= 0) {
+		mylog(log_fatal,"add fifo_fd error\n");
+		myexit(-1);
+	}
+
 	////add_timer for fake_tcp_keep_connection_client
 
 	//sleep(10);
@@ -1156,6 +1171,9 @@ int client_event_loop()
 			if(errno==EINTR  )
 			{
 				mylog(log_info,"epoll interrupted by signal\n");
+				if(fifo_fd>0)
+					unlink(fifo_file);
+				//close(fifo_fd);
 				myexit(0);
 			}
 			else
@@ -1180,6 +1198,17 @@ int client_event_loop()
 				mylog(log_trace,"epoll_trigger_counter:  %d \n",epoll_trigger_counter);
 				epoll_trigger_counter=0;
 			}
+			else if (events[idx].data.u64 == (u64_t)fifo_fd)
+			{
+				int len=read (fifo_fd, buf, sizeof (buf));
+				assert(len>=0);
+				buf[len]=0;
+				mylog(log_info,"got data from fifo,len=%d,s=%s\n",len,buf);
+
+				conn_info.state.client_current_state=client_idle;
+				conn_info.my_id=get_true_random_number_nz();
+
+			}
 			else if (events[idx].data.u64 == (u64_t)udp_fd)
 			{