tcpReceiver.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include"tcpReceiver.h"
  2. #include"usermanager.h"
  3. #include"fileManager.h"
  4. #include"definations.h"
  5. #include"msgManager.h"
  6. #include"chaos.h"
  7. #include"include/ipmsg.h"
  8. #include"fixedmath.h"
  9. #include"packMaker.h"
  10. #include<pthread.h>
  11. #include<netinet/in.h>
  12. #include<sys/socket.h>
  13. #include<arpa/inet.h>
  14. #include<errno.h>
  15. #include<cstdio>
  16. #include<fcntl.h>
  17. #include<unistd.h>
  18. #include<cstdlib>
  19. #include<ctime>
  20. using namespace std;
  21. static Chaos* chaos=0;
  22. static UserManager* users=0;
  23. static MsgManager* msgs=0;
  24. static FileManager* filels=0;
  25. static char ver[40]={0};
  26. static char raw_pkgnum[30];
  27. static unsigned pkgnum;
  28. static char sysusrname[100]={0};
  29. static char computername[100]={0};
  30. static char raw_command[30];
  31. static unsigned command;
  32. static int optstart;
  33. static bool needreply=false;
  34. struct threadarg_t{
  35. int sockfd;
  36. unsigned clientIp;
  37. };
  38. void* thd_tcpReceiver(void*){
  39. //init..
  40. chaos=Chaos::getInstance();
  41. if(unlikely(chaos==NULL)){
  42. printf("\e[31mFatel error:\e[0mChaos not exist.\n");
  43. dpf("%s,%d,%d,%d\n",__FILE__,__LINE__,chaos,errno);
  44. exit(1);
  45. }
  46. users=UserManager::getInstance();
  47. if(unlikely(users==NULL)){
  48. printf("\e[31mFatel error:\e[0mUserManager not exist.\n");
  49. dpf("%s,%d,%d,%d\n",__FILE__,__LINE__,users,errno);
  50. exit(1);
  51. }
  52. msgs=MsgManager::getInstance();
  53. if(unlikely(msgs==NULL)){
  54. printf("\e[31mFatel error:\e[0mMsgManager not exist.\n");
  55. dpf("%s,%d,%p,%d\n",__FILE__,__LINE__,msgs,errno);
  56. }
  57. filels=FileManager::getInstance();
  58. if(unlikely(filels==NULL)){
  59. printf("\e[31mFatel error:\e[0mFileManager not exist.\n");
  60. dpf("%s,%d,%p,%d\n",__FILE__,__LINE__,filels,errno);
  61. }
  62. struct sockaddr_in s_addr;
  63. struct sockaddr_in c_addr;
  64. int sock;
  65. socklen_t addr_len;
  66. int len;
  67. char buff[BUFFLEN];
  68. sock = socket(AF_INET, SOCK_STREAM,0);
  69. int flags = fcntl(sock, F_GETFL, 0);
  70. fcntl(sock, F_SETFL, flags | O_NONBLOCK);
  71. if(unlikely(sock==-1)){
  72. printf("\e[31mFatel error:\e[0mTCP init error.\n");
  73. dpf("%s,%d,%d,%d\n",__FILE__,__LINE__,sock,errno);
  74. exit(1);
  75. }
  76. memset(&s_addr,0,sizeof(struct sockaddr_in));
  77. s_addr.sin_family = AF_INET;
  78. s_addr.sin_port = htons(PORT);
  79. s_addr.sin_addr.s_addr=INADDR_ANY;
  80. int funcAns = bind(sock,(struct sockaddr*)&s_addr,sizeof(s_addr));
  81. if(unlikely(funcAns==-1)){
  82. printf("\e[31mFatel error:\e[0mTCP init error.\n");
  83. dpf("%s,%d,%d,%d\n",__FILE__,__LINE__,funcAns,errno);
  84. exit(1);
  85. }
  86. int listen_sock=listen(sock,10);
  87. if(unlikely(listen_sock==-1)){
  88. printf("\e[31mFatel error:\e[0mTCP init error.\n");
  89. dpf("%s,%d,%d,%d\n",__FILE__,__LINE__,listen_sock,errno);
  90. }
  91. addr_len=sizeof(c_addr);
  92. chaos->tcpListen=true;
  93. pthread_t tcpinstance;
  94. int tcpthdfd;
  95. while(chaos->programOK){
  96. int accept_fd=accept(sock,(struct sockaddr*)&c_addr,&addr_len);
  97. if(accept_fd==-1){
  98. sleep(1);
  99. continue;
  100. }
  101. threadarg_t *arg=(threadarg_t *)malloc(sizeof(threadarg_t));
  102. arg->clientIp=c_addr.sin_addr.s_addr;
  103. arg->sockfd=accept_fd;
  104. tcpthdfd=pthread_create(&tcpinstance,NULL,tcp_inst,(void *)arg);
  105. pthread_detach(tcpinstance);
  106. }
  107. close(sock);
  108. return 0;
  109. }
  110. void *tcp_inst(void* arg){
  111. int sockfd=((threadarg_t*)arg)->sockfd;
  112. int clientIp=((threadarg_t*)arg)->clientIp;
  113. free(arg);
  114. char ver[40]={0};
  115. char raw_pkgnum[30];
  116. unsigned pkgnum;
  117. char sysusrname[100]={0};
  118. char computername[100]={0};
  119. char raw_command[30];
  120. unsigned command;
  121. int optstart;
  122. char recvbuff[BUFFLEN];
  123. int recvlen=recv(sockfd,recvbuff,sizeof(recvbuff),0);
  124. if(unlikely(recvlen<0)){
  125. dpf("tcp recv error,%d\n",errno);
  126. close(sockfd);
  127. return 0;
  128. }
  129. recvbuff[recvlen]=0;
  130. ver[0]=raw_pkgnum[0]=sysusrname[0]=computername[0]=raw_command[0]=0;
  131. sscanf(recvbuff,"%[0-9a-zA-Z.]:%[0-9]:%[^:]:%[^:]:%[0-9]:%n",
  132. ver,raw_pkgnum,sysusrname,computername,raw_command,&optstart);
  133. if((ver[0]==0)||(raw_pkgnum[0]==0)||(sysusrname[0]==0)||
  134. (computername[0]==0)||(raw_command[0]==0)){
  135. dpf("\e[33mtcppack error:not a pack\e[0m\n");
  136. }
  137. command=u32atoi(raw_command);
  138. if((command&IPMSG_GETFILEDATA)==IPMSG_GETFILEDATA){
  139. unsigned fileno;
  140. int optsplit=0;
  141. sscanf(recvbuff+optstart+optsplit,"%x%n",&fileno,&optsplit);
  142. optsplit++;
  143. sscanf(recvbuff+optstart+optsplit,"%x",&fileno);
  144. if((command==IPMSG_GETFILEDATA)){
  145. sendfile_t tmpsendfile;
  146. int findans=filels->Findsend(fileno,clientIp,tmpsendfile);
  147. if(findans==FUNCSUCCEED){
  148. char *filebuff=(char *)malloc(tmpsendfile.filesize);
  149. //FILE* fp=fopen(tmpsendfile.filelocate.c_str(),"rb");
  150. int fd = open(tmpsendfile.filelocate.c_str(), O_RDONLY);
  151. //if(fp!=NULL){
  152. if (fd != -1){
  153. //fread(filebuff,tmpsendfile.filesize,1,fp);
  154. int len;
  155. while ((len = read(fd, filebuff, tmpsendfile.filesize)) > 0){
  156. //send(sockfd,filebuff,sizeof(tmpsendfile.filesize),0);
  157. write(sockfd, filebuff, len);
  158. }
  159. close(fd);
  160. }else{
  161. dpf("\e[33mSendfile failed,file %s open failed:%d.\e[0m\n",tmpsendfile.filelocate.c_str(),errno);
  162. }
  163. free(filebuff);
  164. filels->Delsend(fileno,clientIp);
  165. }else{
  166. dpf("\e[33mSendfile failed,file %ld for %lx not exist.\e[0m\n",fileno,clientIp);
  167. }
  168. }else if((command&IPMSG_FILE_DIR)==IPMSG_FILE_DIR){
  169. sendfile_t tmpsendfile;
  170. int findans=filels->Findsend(fileno,clientIp,tmpsendfile);
  171. if(findans==FUNCSUCCEED){
  172. char sendbuff[BUFFLEN];
  173. int bufheadlen;
  174. char array_bufheadlen[10];
  175. sprintf(sendbuff,"0000:%s:%u:%d:14=%lx:16=%lx%n",tmpsendfile.filename,tmpsendfile.filesize,tmpsendfile.filetype,time(NULL),time(NULL),&bufheadlen);
  176. sprintf(array_bufheadlen,"%04x",bufheadlen);
  177. memcpy(sendbuff,array_bufheadlen,4);
  178. send(sockfd,sendbuff,sizeof(sendbuff),0);
  179. filels->Delsend(fileno,clientIp);
  180. }else{
  181. dpf("\e[33mSendfile failed,file %ld for %lx not exist.\e[0m\n",fileno,clientIp);
  182. }
  183. }else{
  184. dpf("\e[33munknown recv command,%d\e[0m\n",command);
  185. }
  186. }else{
  187. dpf("\e[33munknown tcp command,%d\e[0m\n",command);
  188. }
  189. close(sockfd);
  190. return 0;
  191. }