Chinaunix首页 | 论坛 | 博客
  • 博客访问: 468102
  • 博文数量: 62
  • 博客积分: 1742
  • 博客等级: 中尉
  • 技术积分: 859
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-06 00:13
个人简介

这是一句很长很长而且又很啰嗦并且很无聊的废话...

文章分类

全部博文(62)

文章存档

2013年(1)

2012年(13)

2011年(48)

分类: LINUX

2011-12-31 20:44:28

常用代码,贴着以备查用.

Write
  1. int send_login_msg(p_Login_Msg p_lm){
  2.     int key, fifo_fd, len, pro_lock, tw_frequency = MAX_TRY_WRITE;
  3.     fd_set rfds;
  4.     struct timeval tv;
  5.     p_Fifo_Msg p_fm;
  6.     char buf[1024];
  7.     union semun sem_arg;

  8.     key = ftok(SEM, 'a');
  9.     pro_lock = semget(key, 1, (IPC_CREAT | 0666));
  10.     if(pro_lock < 0){
  11.         DBG("Error:Semget Call Failure!\n");
  12.         return 0;
  13.     }

  14.     if(access(FIFO, F_OK)){
  15.         DBG("Bengin Init Sem And Mk Fifo!\n");

  16.         sem_arg.val = 1;
  17.         if(semctl(pro_lock, 0, SETVAL, sem_arg) < 0){
  18.             DBG("Error:Init Sem Value Failure!\n");
  19.             return 0;
  20.         }
  21.         if((mkfifo(FIFO, 0777) < 0) && (errno != EEXIST)){
  22.             DBG("Error:Cannot Create FIFO File!\n");
  23.             return 0;
  24.         }
  25.     }

  26.     fifo_fd = open(FIFO, (O_RDWR | O_NONBLOCK), 0);
  27.     if(fifo_fd < 0){
  28.         DBG("Error:Fifo Open Failure!\n");
  29.         return 0;
  30.     }

  31.     p_fm = (p_Fifo_Msg)buf;
  32.     p_fm->fm_head.ft = FIFO_LOGIN;
  33.     p_fm->fm_head.msg_len = sizeof(Login_Msg);
  34.     memcpy(&p_fm->body, p_lm, sizeof(Login_Msg));

  35. try_write_again:
  36.     Pro_Lock(pro_lock, apps_thread_exit);
  37.     len = write(fifo_fd, buf, (sizeof(Fifo_Msg_Head) + p_fm->fm_head.msg_len));
  38.     Pro_UnLock(pro_lock, apps_thread_exit);
  39.     if(len < 0){
  40.         if(errno == EAGAIN){
  41.             if(tw_frequency--){
  42.                 usleep(500000);
  43.                 goto try_write_again;
  44.             }else{
  45.                 DBG("Error: Send Login Msg Fialure!\n");
  46.             }
  47.         }
  48.     }

  49. apps_thread_exit:
  50.     close(fifo_fd);

  51.     return 0;
  52. }
Read
  1. void *apps_thread(void *arg){
  2.     int key, fifo_fd, len, pro_lock;
  3.     fd_set rfds;
  4.     struct timeval tv;
  5.     p_Fifo_Msg p_fm;
  6.     char buf[1024];
  7.     union semun sem_arg;

  8.     key = ftok(SEM, 'a');
  9.     pro_lock = semget(key, 1, (IPC_CREAT | 0666));
  10.     if(pro_lock < 0){
  11.         DBG("Error:Semget Call Failure!\n");
  12.         return 0;
  13.     }

  14.     if(access(FIFO, F_OK)){
  15.         DBG("Bengin Init Sem And Mk Fifo!\n");

  16.         sem_arg.val = 1;
  17.         if(semctl(pro_lock, 0, SETVAL, sem_arg) < 0){
  18.             DBG("Error:Init Sem Value Failure!\n");
  19.             goto apps_thread_exit_sem;
  20.         }
  21.         if((mkfifo(FIFO, 0777) < 0) && (errno != EEXIST)){
  22.             DBG("Error:Cannot Create FIFO File!\n");
  23.             goto apps_thread_exit_sem;
  24.         }
  25.     }

  26.     fifo_fd = open(FIFO, (O_RDWR | O_NONBLOCK), 0);
  27.     if(fifo_fd < 0){
  28.         DBG("Error:Fifo Open Failure!\n");
  29.         goto apps_thread_exit_sem;
  30.     }

  31.     p_fm = (p_Fifo_Msg)buf;
  32.     while(1){
  33.         FD_ZERO(&rfds);
  34.         FD_SET(fifo_fd, &rfds);
  35.         tv.tv_sec = SEL_TO;
  36.         tv.tv_usec = 0;

  37.         if(select(fifo_fd + 1, &rfds, 0, 0, &tv) < 0){
  38.             DBG("Error:Select Call Failure!\n");
  39.             goto apps_thread_exit;
  40.         }

  41.         if(FD_ISSET(fifo_fd, &rfds)){
  42.             memset(buf, 0, sizeof(buf));
  43.             
  44.             Pro_Lock(pro_lock, apps_thread_exit);
  45.             len = read(fifo_fd, buf, sizeof(Fifo_Msg_Head));
  46.             if(len != sizeof(Fifo_Msg_Head)){
  47.                 DBG("Error:Cant Not Read Msg Head!\n");
  48.                 Pro_UnLock(pro_lock, apps_thread_exit);
  49.                 goto apps_thread_exit;
  50.             }

  51.             len = read(fifo_fd, (buf + sizeof(Fifo_Msg_Head)), p_fm->fm_head.msg_len);
  52.             if(len != p_fm->fm_head.msg_len){
  53.                 DBG("Error:Read Msg Body Failure!\n");
  54.                 Pro_UnLock(pro_lock, apps_thread_exit);
  55.                 goto apps_thread_exit;
  56.             }
  57.             Pro_UnLock(pro_lock, apps_thread_exit);

  58.             switch(p_fm->fm_head.ft){
  59.                 case FIFO_APPS:
  60.                     handle_apps_msg(p_fm);
  61.                     break;
  62.                 case FIFO_LOGIN:
  63.                     handle_login_msg(p_fm);
  64.                     break;
  65.                 case FIFO_GPS:
  66.                     handle_gps_msg(p_fm);
  67.                     break;
  68.                 case FIFO_CGI:
  69.                     handle_cgi_msg(p_fm);
  70.                     break;
  71.                 default:
  72.                     break;
  73.             }
  74.         }
  75.     }

  76. apps_thread_exit:
  77.     close(fifo_fd);
  78. apps_thread_exit_sem:
  79.     unlink(FIFO);
  80.     if(semctl(pro_lock, 0, IPC_RMID, sem_arg) < 0){
  81.         DBG("Error:Rm Sem Vaule Failure!\n");
  82.     }

  83.     return 0;
  84. }
  1. #define Pro_Lock(semid, err_goto) do{ \
  2.                                         struct sembuf sem_b; \
  3.                                         sem_b.sem_num = 0; \
  4.                                         sem_b.sem_op = -1; \
  5.                                         sem_b.sem_flg = SEM_UNDO; \
  6.                                         if(semop((semid), &sem_b, 1) < 0){ \
  7.                                             DBG("%s->%s->%d:Error:Sem P Ctl Fialure!\n", __FILE__, __FUNCTION__, __LINE__); \
  8.                                             goto err_goto; \
  9.                                         } \
  10.                                     }while(0) \

  11. #define Pro_UnLock(semid, err_goto) do{ \
  12.                                         struct sembuf sem_b; \
  13.                                         sem_b.sem_num = 0; \
  14.                                         sem_b.sem_op = 1; \
  15.                                         sem_b.sem_flg = SEM_UNDO; \
  16.                                         if(semop((semid), &sem_b, 1) < 0){ \
  17.                                             DBG("%s->%s->%d:Error:Sem V Ctl Fialure!\n", __FILE__, __FUNCTION__, __LINE__); \
  18.                                             goto err_goto; \
  19.                                         } \
  20.                                     }while(0) \

阅读(2462) | 评论(0) | 转发(0) |
0

上一篇:TCP/IP

下一篇:TCP的Send和Recv

给主人留下些什么吧!~~