RSS
热门关键字:  QQ密码破解  最新的盗QQ的软件  江淮影视  网页密码破解  最后诊断
当前位置 :| 主页>应用程序>c++>

封装两个类readpipe,writepipe,项目需要进程间使用命名管道fifo来交换数据<二>

来源: 作者: 时间:2008-09-28 Tag: 点击:
writepipe头文件:
  1. //    
  2. // File:   writepipe.h   
  3. // Author: root   
  4. //   
  5. // Created on February 19, 2008, 8:51 AM   
  6. //   
  7.   
  8. #ifndef _WRITEPIPE_H   
  9. #define  _WRITEPIPE_H   
  10.   
  11. #include <unistd.h>   
  12. #include <stdio.h>   
  13. #include <string.h>   
  14. #include <stdlib.h>   
  15. #include <sys/types.h>   
  16. #include <sys/stat.h>   
  17. #include <errno.h>   
  18. #include <fcntl.h>   
  19. #include <string>   
  20.   
  21. using namespace std;   
  22.   
  23. class writepipe{   
  24. private:   
  25.     string _wpname;   
  26.     int _e;   
  27.     int _wpfd;   
  28.     bool _block;   
  29.        
  30. public:   
  31.     int gete(){   
  32.         return _e;   
  33.     }   
  34.     int wpwrite(char *buffer,int size);   
  35.     int wpwrite(const string& buffer);   
  36.     writepipe(const string& name,bool isblock);   
  37.     ~writepipe();   
  38. };   
  39.   
  40.   
  41. #endif  /* _WRITEPIPE_H */   


writepipe实现文件:
  1. #include "writepipe.h"   
  2.   
  3. writepipe::writepipe(const string& name,bool isblock){   
  4.     //   
  5.     _wpname = name;   
  6.     _block = isblock;   
  7.     unlink(_wpname.c_str());   
  8.     int _rt = mkfifo(_wpname.c_str(),O_CREAT|O_EXCL);   
  9.     if(_rt < 0 && errno == EEXIST){   
  10.         //   
  11.         _e = errno;   
  12.         unlink(_wpname.c_str());   
  13.         _rt = mkfifo(_wpname.c_str(),O_CREAT|O_EXCL);   
  14.         //goto check;   
  15.     }   
  16.        
  17.         if(_block == false){   
  18.             _wpfd = open(_wpname.c_str(),O_WRONLY|O_NONBLOCK,0);   
  19.             /*  
  20.             if(_wpfd != 0){  
  21.                 _e = errno;  
  22.                 close(_wpfd);  
  23.                 _wpfd = -1;  
  24.             }  
  25.              */  
  26.         }else{   
  27.             _wpfd = open(_wpname.c_str(),O_WRONLY,0);   
  28.             /*  
  29.             if(_wpfd != 0){  
  30.                 _e = errno;  
  31.                 close(_wpfd);  
  32.                 _wpfd = -1;  
  33.             }  
  34.              */  
  35.         }   
  36. }   
  37.   
  38. int writepipe::wpwrite(char *buffer,int size){   
  39.     //   
  40.     if(_wpfd == -1){   
  41.         return -1;   
  42.     }else{   
  43.         int _rn = 0;   
  44.         _rn=write(_wpfd,buffer,size);   
  45.         //printf("buffer is %s \n",buffer);   
  46.   if(_rn==-1)   
  47.   {   
  48.             _e = errno;   
  49.   }   
  50.         return _rn;   
  51.     }   
  52. }   
  53. int writepipe::wpwrite(const string& buffer){   
  54.     //   
  55.     if(_wpfd == -1){   
  56.         return -1;   
  57.     }else{   
  58.         int _rn = 0;   
  59.         int sz = buffer.length();   
  60.         char *tmp = new char[sz + 1];   
  61.         bzero(tmp,sz +1);   
  62.         memcpy(tmp,buffer.c_str(),sz);   
  63.         _rn=write(_wpfd,tmp,sz);   
  64.         delete tmp;   
  65.         tmp = NULL;   
  66.   if(_rn==-1)   
  67.   {   
  68.             _e = errno;   
  69.   }   
  70.         return _rn;   
  71.     }   
  72. }   
  73.   
  74. writepipe::~writepipe(){   
  75.     //   
  76.     close(_wpfd);   
  77.     _wpfd = -1;   
  78. }   


测试代码:
  1. //    
  2. // File:   maintest.cc   
  3. // Author: root   
  4. //   
  5. // Created on February 18, 2008, 5:37 PM   
  6. //   
  7.   
  8. #include <stdlib.h>   
  9. #include <stdio.h>   
  10. #include <string>   
  11. #include <iostream>   
  12. #include <unistd.h>   
  13.   
  14. //#include "readpipe.h"   
  15. #include "writepipe.h"   
  16.   
  17. using namespace std;   
  18.   
  19.   
  20. int main(int argc, char** argv) {       
  21.        
  22.     writepipe *_p = new writepipe("/tmp/TESTRP",true);   
  23.     int i = 0;   
  24.     while(i < 10){   
  25.         char *b = "r u ok?";   
  26.         _p->wpwrite(b,strlen(b));   
  27.         i++;   
  28.         printf("i is %d", i);   
  29.         sleep(1);   
  30.     }   
  31.         delete _p;   
  32.        
  33.     return (EXIT_SUCCESS);   
  34. }   


栏目列表