ACG狂人

其实我更爱姐汁...

终于完成了自己的模板设计,初步实现了filter_streambuf,cge项目启动......

实现的目的是为了在一些特定情况下不去使用boost的filter_streambuf,不使用boost::iostreams的理由如下:
1、基于运行时配置的过滤器,效率稍低
2、对于网络通讯而言,boost的filter_streambuf乃至整个iostreams库都显得较为臃肿。
所以,我自己编写了一套filter_streambuf,继承了std::streambuf,并配合自己重新设计的archive和batch_data进行网络通讯,无论是效率还是易用性上都超出使用boost的iostreams。而boost的那套东西经过我的反复使用后,觉得更适合用在文件读写和数据持久化上。
如果要说哪里不如boost的filter_stream,也就是boost的filter_streambuf可以动态配置filter,而我使用的是模板技术将filter的关系在编译期就关联了起来,所以只能是静态配置filter。下面是具体使用时的完整例子代码:
 1 #include <ccs/util/ios/ifilter_streambuf.hpp>
 2 #include <ccs/util/ios/ofilter_streambuf.hpp>
 3 #include <ccs/util/ios/memory_terminal.hpp>
 4 
 5 using namespace ccs;
 6 using namespace util;
 7 
 8 // 输出过滤
 9 struct my_ofilter
10 {
11     typedef ios::ofilter_tag tag_type;
12 
13     template<typename OutT>
14     std::streamsize write(const char* p, std::streamsize n, OutT& _out)
15     {
16         std::streamsize i = 0;
17         for (; i < n; ++i)
18         {
19             char c = p[i];
20             if (_out.write(&++c, 1!= 1)
21                 break;
22         }
23         return i;
24     }
25 };
26 
27 // 输入过滤
28 struct my_ifilter
29 {
30     typedef ios::ifilter_tag tag_type;
31 
32     template<typename InT>
33     std::streamsize read(char* p, std::streamsize n, InT& _in)
34     {
35         std::streamsize i = 0;
36         for (; i < n; ++i)
37         {
38             char c;
39             if (_in.read(&c, 1!= 1)
40                 break;
41             p[i] = --c;
42         }
43         return i;
44     }
45 };
46 
47 // 输出内存设备
48 struct memory_odevice
49 {
50     typedef ios::dest_tag tag_type;
51 
52     std::streamsize write(const char* p, std::streamsize n, ios::memory_oterminal& _out)
53     {
54         return _out.write(p, n);
55     }
56 };
57 
58 // 输入内存设备
59 struct memory_idevice
60 {
61     typedef ios::source_tag tag_type;
62 
63     std::streamsize read(char* p, std::streamsize n, ios::memory_iterminal& _in)
64     {
65         return _in.read(p, n);
66     }
67 };
68 
69 
70 int main(int _Argc, char** _Args)
71 {
72     char buf[256];
73     ios::memory_oterminal memout(buf, 256);
74     ios::memory_iterminal memin(buf, 256);
75     ios::ifilter_streambuf<ios::memory_iterminal, mpl::list2<my_ifilter, memory_idevice> > insbuf(&memin);
76     ios::ofilter_streambuf<ios::memory_oterminal, mpl::list2<my_ofilter, memory_odevice> > outsbuf(&memout);
77     std::istream is(&insbuf);
78     std::ostream os(&outsbuf);
79 
80     int num = 188;
81     os.write((char*)&num, sizeof(int));
82     os.flush();
83     is.read((char*)&num, sizeof(int));
84 
85     std::cout << num << std::endl;
86     system("pause");
87 }

代码中的意思就是将写入的数据逐字节的加1,并保存在内存缓冲里,然后又从内存缓冲中读出,逐字节减1,并输出到控制台,一套经过过滤的读写流便完成了。由于使用了模板元的list作为链接,在release模式下所有的过滤器操作都是内联的,这虽然也是我预想的效果,但看完汇编码之后,着实让我高兴了一晚上,这种成就感真的是programer最大的乐趣。

需要说明的是:代码中的mpl::list2是自己实现的模板元链表...过段时间考虑研究一下boost的并替换过来,因为那个list后面的2让我觉得很不够智能...当然,如果boost的list实现过于复杂,或是不能让我的代码完全内联化的话,肯定不会考虑使用。

完成这个之后,我便准备着手构建cge项目,所谓的cge,就是cloud game engine的缩写...顾名思义就是使用了云技术的游戏引擎,我想在业余时间尝试一些颠覆传统cs架构的在线游戏引擎架构设计,具体难点估计会有2个:
1、运用gpgpu group的并行运算技术,考虑使用目前市场占用率最大的nvidia tesla服务器配合cuda,在服务器用physX实现一定的物理模拟。
2、在即时性较强的在线游戏中,ping值一直是最大的挑战,所以有选择性的使用云计算技术,这是架构设计上的挑战。
关于cge的设计思考和规划,会另外开贴具体阐述,并记录开发进度和情况。

posted on 2010-10-12 19:37 酿妹汁 阅读(2891) 评论(4)  编辑 收藏 引用 所属分类: C++

评论

# re: 终于完成了自己的模板设计,初步实现了filter_streambuf,cge项目启动...... 2010-10-14 10:22 陈梓瀚(vczh)

cge囧……  回复  更多评论   

# re: 终于完成了自己的模板设计,初步实现了filter_streambuf,cge项目启动......[未登录] 2010-10-14 11:46 jans2002

赞一个,简洁高效。  回复  更多评论   

# re: 终于完成了自己的模板设计,初步实现了filter_streambuf,cge项目启动...... 2010-10-16 19:09 纳尼!!!

囧...囧啥= =  回复  更多评论   

# re: 终于完成了自己的模板设计,初步实现了filter_streambuf,cge项目启动......[未登录] 2010-12-25 23:36 expter

不知道你读写是用memcpy还是自己写的数据流的方式。,  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理