当前位置:天才代写 > tutorial > C语言/C++ 教程 > Vczh Serialization Demo :在网络上通报巨大工具

Vczh Serialization Demo :在网络上通报巨大工具

2017-11-03 08:00 星期五 所属: C语言/C++ 教程 浏览:761

副标题#e#

本日将Serialization举办了重构,让其支持容器。于是利用以前的基本设施就能完成这 个Demo了。

为什么要做成这个样子呢?因为模板没有运行时匹配成果。固然仍然大量 利用模板,可是接口上就没有了。模板被用来处理惩罚其他的工作。

这个措施分为处事器 端和客户端两个部门,利用Socket毗连。处事器端监听到一个客户端之后将一棵树发送已往 之退却出。客户端吸收到这棵树之后将内容打印到屏幕上。于是我们实现一颗多态的树。树 的节点分为数字节点、字符串节点和列表节点三种,配合担任自一个基类。这样的话就可以 利用多态来结构成一棵树并举办操纵了:

1 class Node : public VL_SerializableObject
2 {
3 public:
4 typedef VL_AutoPtr<Node> Ptr;
5
6 virtual VUnicodeString ToString(VUnicodeString Prefix)=0;
7 };
8
9 class NumberNode : public Node
10 {
11 public:
12 VInt Number;
13
14 NumberNode()
15 {
16 VL_SERIALIZABLE_FIELD(NumberNode,Number);
17 }
18
19 VUnicodeString ToString(VUnicodeString Prefix)
20 {
21 return Prefix+VUnicodeString(Number);
22 }
23 };
24
25 class IdNode : public Node
26 {
27 public:
28 VUnicodeString Id;
29
30 IdNode()
31 {
32 VL_SERIALIZABLE_FIELD(IdNode,Id);
33 }
34
35 VUnicodeString ToString(VUnicodeString Prefix)
36 {
37 return Prefix+Id;
38 }
39 };
40
41 class ListNode : public Node
42 {
43 public:
44 VL_List<Ptr , false , Node*> Children;
45
46 ListNode()
47 {
48 VL_SERIALIZABLE_FIELD(ListNode,Children);
49 }
50
51 VUnicodeString ToString(VUnicodeString Prefix)
52 {
53 VUnicodeString Result;
54 Result+=Prefix+L"List {\r\n";
55 for(VInt i=0;i<Children.GetCount();i++)
56 {
57 if(Children[i])
58 {
59 Result+=Children[i]->ToString(Prefix+L" ")+L"\r\n";
60 }
61 else
62 {
63 Result+=Prefix+L" <NULL>\r\n";
64 }
65 }
66 Result+=Prefix+L"}";
67 return Result;
68 }
69 };


#p#副标题#e#

接下来初始化Serializer:

1 void InitializeSeriailzer(VL_Serializer& Serializer)
2 {
3   VL_SERIALIZABLE_CLASS(&Serializer,NumberNode);
4   VL_SERIALIZABLE_CLASS(&Serializer,IdNode);
5   VL_SERIALIZABLE_CLASS (&Serializer,ListNode);
6 }

然后实现一个处事器端的逻辑:

1 Node::Ptr BuildNode()
2 {
3 ListNode* Root=new ListNode;
4 {
5 NumberNode* Number=new NumberNode;
6 Number->Number=10;
7 Root->Children.Add(Number);
8 }
9 {
10 ListNode* List=new ListNode;
11 {
12 NumberNode* Number=new NumberNode;
13 Number->Number=20;
14 List->Children.Add(Number);
15 }
16 List->Children.Add(0);
17 {
18 IdNode* Id=new IdNode;
19 Id->Id=L"Serialization";
20 List->Children.Add(Id);
21 }
22 Root->Children.Add(List);
23 }
24 {
25 IdNode* Id=new IdNode;
26 Id->Id=L"VCZH";
27 Root->Children.Add(Id);
28 }
29 return Root;
30 }
31
32 void ServerMain()
33 {
34 GetConsole()->SetTitle(L"Vczh Library++ 2.0 Communication [Server]");
35
36 VL_Serializer Serializer;
37 InitializeSeriailzer(Serializer);
38
39 VL_SocketServer Server(vspTCP,L"8000",false);
40 if(Server.IsSuccess())
41 {
42 VL_ServerSocket* Socket=Server.WaitForConnection();
43 if(Socket)
44 {
45 GetConsole()->Write(L"监听到客户端。\r\n");
46 Node::Ptr MyNode=BuildNode();
47 WriteCommonObject(Socket->GetWriterStream(),Serializer.Serialize(MyNode.Object()));
48 delete Socket;
49 GetConsole()->Write(L"发送乐成,封锁处事端。\r\n");
50 }
51 else
52 {
53 GetConsole()->Write(L"监听端口时产生错误。\r\n");
54 }
55 }
56 else
57 {
58 GetConsole()->Write(L"建设Socket时产生错误。\r\n");
59 }
60 }

#p#副标题#e#

#p#分页标题#e#

下面是客户端的逻辑:

1 void ClientMain()
2 {
3 GetConsole()->SetTitle(L"Vczh Library++ 2.0 Communication [Client]");
4
5 VL_Serializer Serializer;
6 InitializeSeriailzer(Serializer);
7
8 VL_ClientSocket Socket(vspTCP,L"127.0.0.1",L"8000");
9 if(Socket.Connect())
10 {
11 GetConsole()->Write(L"毗连随处事端。\r\n");
12 Node::Ptr MyNode=Serializer.Deserialize(ReadCommonObject(Socket.GetReaderStream()));
13 GetConsole()->Write(MyNode->ToString(L""));
14 }
15 else
16 {
17 GetConsole()->Write(L"毗连处事端时产生错误。\r\n");
18 }
19 }

最后用Main函数来结构一个Event。程 序将被启动两次,先启动的成为处事器,后启动的成为客户端,利便将两部门的代码都放在 同一个文件内里:

1 void vlmain()
2 {
3   GetConsole()- >SetTitle(L"Vczh Library++ 2.0 Communication");
4   GetConsole()->SetTestMemoryLeaks(true);
5   GetConsole()- >SetPauseOnExit(true);
6
7   VL_SynEvent Event;
8   switch (Event.Create(false,false,L"VCZH"))
9   {
10   case VL_SynObject::arSucceed:
11     ServerMain();
12     break;
13   case VL_SynObject::arAlreadyExists:
14     ClientMain();
15     break;
16   case VL_SynObject::arFail:
17     GetConsole()->Write(L"结构Event时产生错误。\r\n");
18     break;
19   }
20 }

让我们看看运行功效:

Vczh Serialization Demo :在网络上传递庞大东西

#p#副标题#e#

下面是完 整的代码:

1 #include "..\..\..\Library\Platform\VL_Console.h"
2 #include "..\..\..\Library\Data\VL_CommonData.h"
3 #include "..\..\..\Library\Data\VL_Serialization.h"
4 #include "..\..\..\Library\Data\VL_Communication.h"
5 #include "..\..\..\Library\Data\VL_Stream.h"
6 #include "..\..\..\Library\Data\VL_System.h"
7
8 using namespace vl;
9 using namespace vl::platform;
10 using namespace vl::stream;
11 using namespace vl::system;
12 using namespace vl::system::synchronization;
13 using namespace vl::serialization;
14 using namespace vl::commondata;
15 using namespace vl::communication;
16
17 class Node : public VL_SerializableObject
18 {
19 public:
20 typedef VL_AutoPtr<Node> Ptr;
21
22 virtual VUnicodeString ToString(VUnicodeString Prefix)=0;
23 };
24
25 class NumberNode : public Node
26 {
27 public:
28 VInt Number;
29
30 NumberNode()
31 {
32 VL_SERIALIZABLE_FIELD(NumberNode,Number);
33 }
34
35 VUnicodeString ToString(VUnicodeString Prefix)
36 {
37 return Prefix+VUnicodeString(Number);
38 }
39 };
40
41 class IdNode : public Node
42 {
43 public:
44 VUnicodeString Id;
45
46 IdNode()
47 {
48 VL_SERIALIZABLE_FIELD(IdNode,Id);
49 }
50
51 VUnicodeString ToString(VUnicodeString Prefix)
52 {
53 return Prefix+Id;
54 }
55 };
56
57 class ListNode : public Node
58 {
59 public:
60 VL_List<Ptr , false , Node*> Children;
61
62 ListNode()
63 {
64 VL_SERIALIZABLE_FIELD(ListNode,Children);
65 }
66
67 VUnicodeString ToString(VUnicodeString Prefix)
68 {
69 VUnicodeString Result;
70 Result+=Prefix+L"List {\r\n";
71 for(VInt i=0;i<Children.GetCount();i++)
72 {
73 if(Children[i])
74 {
75 Result+=Children[i]->ToString(Prefix+L" ")+L"\r\n";
76 }
77 else
78 {
79 Result+=Prefix+L" <NULL>\r\n";
80 }
81 }
82 Result+=Prefix+L"}";
83 return Result;
84 }
85 };
86
87 void InitializeSeriailzer(VL_Serializer& Serializer)
88 {
89 VL_SERIALIZABLE_CLASS(&Serializer,NumberNode);
90 VL_SERIALIZABLE_CLASS(&Serializer,IdNode);
91 VL_SERIALIZABLE_CLASS(&Serializer,ListNode);
92 }
93
94 Node::Ptr BuildNode()
95 {
96 ListNode* Root=new ListNode;
97 {
98 NumberNode* Number=new NumberNode;
99 Number->Number=10;
100 Root->Children.Add(Number);
101 }
102 {
103 ListNode* List=new ListNode;
104 {
105 NumberNode* Number=new NumberNode;
106 Number->Number=20;
107 List->Children.Add(Number);
108 }
109 List->Children.Add(0);
110 {
111 IdNode* Id=new IdNode;
112 Id->Id=L"Serialization";
113 List->Children.Add(Id);
114 }
115 Root->Children.Add(List);
116 }
117 {
118 IdNode* Id=new IdNode;
119 Id->Id=L"VCZH";
120 Root->Children.Add(Id);
121 }
122 return Root;
123 }
124
125 void ServerMain()
126 {
127 GetConsole()->SetTitle(L"Vczh Library++ 2.0 Communication [Server]");
128
129 VL_Serializer Serializer;
130 InitializeSeriailzer(Serializer);
131
132 VL_SocketServer Server(vspTCP,L"8000",false);
133 if(Server.IsSuccess())
134 {
135 VL_ServerSocket* Socket=Server.WaitForConnection();
136 if(Socket)
137 {
138 GetConsole()->Write(L"监听到客户端。\r\n");
139 Node::Ptr MyNode=BuildNode();
140 WriteCommonObject(Socket->GetWriterStream(),Serializer.Serialize(MyNode.Object()));
141 delete Socket;
142 GetConsole()->Write(L"发送乐成,封锁处事端。\r\n");
143 }
144 else
145 {
146 GetConsole()->Write(L"监听端口时产生错误。\r\n");
147 }
148 }
149 else
150 {
151 GetConsole()->Write(L"建设Socket时产生错误。\r\n");
152 }
153 }
154
155 void ClientMain()
156 {
157 GetConsole()->SetTitle(L"Vczh Library++ 2.0 Communication [Client]");
158
159 VL_Serializer Serializer;
160 InitializeSeriailzer(Serializer);
161
162 VL_ClientSocket Socket(vspTCP,L"127.0.0.1",L"8000");
163 if(Socket.Connect())
164 {
165 GetConsole()->Write(L"毗连随处事端。\r\n");
166 Node::Ptr MyNode=Serializer.Deserialize(ReadCommonObject(Socket.GetReaderStream()));
167 GetConsole()->Write(MyNode->ToString(L""));
168 }
169 else
170 {
171 GetConsole()->Write(L"毗连处事端时产生错误。\r\n");
172 }
173 }
174
175 void vlmain()
176 {
177 GetConsole()->SetTitle(L"Vczh Library++ 2.0 Communication");
178 GetConsole()->SetTestMemoryLeaks(true);
179 GetConsole()->SetPauseOnExit(true);
180
181 VL_SynEvent Event;
182 switch(Event.Create(false,false,L"VCZH"))
183 {
184 case VL_SynObject::arSucceed:
185 ServerMain();
186 break;
187 case VL_SynObject::arAlreadyExists:
188 ClientMain();
189 break;
190 case VL_SynObject::arFail:
191 GetConsole()->Write(L"结构Event时产生错误。\r\n");
192 break;
193 }
194 }

 

    关键字:

天才代写-代写联系方式