2013年9月24日星期二

wp8 use TCP -based communications Beetle.NetPackage Order

 

in the new version of Beetle.NetPackage Protobuf and Controller provides support , so in WP8 use Beetle.NetPackage for TCP-based data exchange is an very simple thing. following components WP8 through the simple TCP-based communication of orders online tracking .

 

protocol definition

 

To simplify the interactive data processing Protobuf used here to describe the data by interacting Protobuf formulate a series of request and response objects instead of the usual cumbersome data under the TCP stream processing. below to describe Order By Protobuf communication protocol .

 
        
   
  1 [ProtoContract] 
2 public class GetCustomer
3 {
4 [ProtoMember(1)]
5 public string Name { get; set; }
6 }
7
8 [ProtoContract]
9 public class GetCustomerResponse
10 {
11
12 [ProtoMember(1)]
13 public IList<Customer> Items
14 {
15 get;
16 set;
17 }
18
19 }
20
21 [ProtoContract]
22 public class Customer
23 {
24 [ProtoMember(1)]
25 public string ID
26 {
27 get;
28 set;
29 }
30 [ProtoMember(2)]
31 public string Name
32 {
33 get;
34 set;
35 }
36 public override string ToString()
37 {
38 return Name;
39 }
40 }
41
42 [ProtoContract]
43 public class GetEmployee
44 {
45 [ProtoMember(1)]
46 public string Name { get; set; }
47 }
48
49 [ProtoContract]
50 public class GetEmployeeResponse
51 {
52 [ProtoMember(1)]
53 public IList<Employee> Items
54 {
55 get;
56 set;
57 }
58 }
59
60 [ProtoContract]
61 public class Employee
62 {
63
64 [ProtoMember(1)]
65 public string ID
66 {
67 get;
68 set;
69 }
70 [ProtoMember(2)]
71 public string Name
72 {
73 get;
74 set;
75 }
76 public override string ToString()
77 {
78 return Name;
79 }
80
81 }
82
83 [ProtoContract]
84 public class OrderSearch
85 {
86 [ProtoMember(1)]
87 public string Employee { get; set; }
88 [ProtoMember(2)]
89 public int PageIndex { get; set; }
90 [ProtoMember(3)]
91 public string Customer { get; set; }
92 [ProtoMember(4)]
93 public string FromDate { get; set; }
94 [ProtoMember(5)]
95 public string ToDate { get; set; }
96 }
97
98 [ProtoContract]
99 public class OrderSearchResponse
100 {
101 [ProtoMember(1)]
102 public IList<Order> Items
103 {
104 get;
105 set;
106 }
107 [ProtoMember(2)]
108 public int PageIndex
109 {
110 get;
111 set;
112 }
113 [ProtoMember(3)]
114 public int Pages
115 {
116 get;
117 set;
118 }
119 }
120
121 [ProtoContract]
122 public class Order
123 {
124 [ProtoMember(1)]
125 public string OrderID { get; set; }
126 [ProtoMember(2)]
127 public string Employee { get; set; }
128 [ProtoMember(3)]
129 public string Customer { get; set; }
130 [ProtoMember(4)]
131 public string OrderDate { get; set; }
132 [ProtoMember(5)]
133 public string RequiredDate { get; set; }
134 [ProtoMember(6)]
135 public string ShippedDate { get; set; }
136 [ProtoMember(7)]
137 public string ShipName { get; set; }
138 [ProtoMember(8)]
139 public string ShipAddress { get; set; }
140 [ProtoMember(9)]
141 public string ShipCity { get; set; }
142 [ProtoMember(10)]
143 public string ShipRegion { get; set; }
144 }
145
146 [ProtoContract]
147 public class GetOrderDetail
148 {
149 [ProtoMember(1)]
150 public string OrderID { get; set; }
151 }
152
153 [ProtoContract]
154 public class GetOrderDetailResponse
155 {
156 [ProtoMember(1)]
157 public IList<OrderDetail> Items
158 {
159 get;
160 set;
161 }
162 }
163
164 [ProtoContract]
165 public class OrderDetail
166 {
167 [ProtoMember(1)]
168 public string OrderID { get; set; }
169 [ProtoMember(2)]
170 public string Product { get; set; }
171 [ProtoMember(3)]
172 public double UnitPrice { get; set; }
173 [ProtoMember(4)]
174 public int Quantity { get; set; }
175 [ProtoMember(5)]
176 public float Discount { get; set; }
177 }
  
   View Code  
 

As used here protobuf-net, so to combine through classes and properties , in fact, easy to standardize treatment is recommended to use other platforms proto file descriptions to generate the corresponding class , so you can easily generate C + +, JAVA and other interactive objects on different platforms .

 

define TCP communication objects

 

through Beetle.NetPackage on protobuf support in WP8 established under the corresponding TCP communication is a very simple thing .

 
  
1 Beetle.NetPackage.ProtoPakcage.Register(typeof(MainPage).Assembly); 
2 if (mClient == null)
3 {
4 mClient = new Beetle.NetPackage.NetClient("192.168.0.104", 9088, new Beetle.NetPackage.ProtoPakcage(), this);
5 mClient.LittleEndian = false;
6 }
7 mClient.Connect();
 
 

use protobuf ProtoPakcage.Register passed before the assembly following the corresponding registration protobuf message into components , and then create the appropriate NetClient data communication can be carried out in here in order to interact and android compatible to the client LittleEndian property is set to false in the data processing procedure that uses high- word order as some data types are handled .

 

message routing distribution

 

in the new version of Beetle.NetPackage provides simple message routing function , so when writing messages no longer need to receive treatment to determine if different messages through to the calling method. using message routing does not need developers to define complex message rules , only need to define the parameters of the corresponding message types can make components to help complete this thing .

 
  
 1 public void OnSearchOrderResponse(Beetle.NetPackage.NetClient client, OrderSearchResponse e) 
2 {
3 if (e.Items == null)
4 lstOrders.ItemsSource = null;
5 else
6 lstOrders.ItemsSource = e.Items.ToList();
7 mPageIndex = e.PageIndex;
8 mPages = e.Pages;
9 }
10
11 public void OnGetEmployee(Beetle.NetPackage.NetClient client, GetEmployeeResponse e)
12 {
13 mEmployees = e.Items;
14 lstEmployee.ItemsSource = e.Items.ToList();
15 }
16 public void OnGetCustomer(Beetle.NetPackage.NetClient client, GetCustomerResponse e)
17 {
18 mCustomers = e.Items;
19 lstCustomer.ItemsSource = e.Items.ToList();
20
21 }
22 public void OnGetOrderDetail(Beetle.NetPackage.NetClient client, GetOrderDetailResponse e)
23 {
24 DialogOrderDetail detail = new DialogOrderDetail();
25 CustomMessageBox mOrderDetailDialog = new CustomMessageBox
26 {
27 Content = detail,
28 Title = "OrderDetail",
29 RightButtonContent = "OK"
30 };
31 detail.ListSelector.ItemsSource = e.Items.ToList();
32 mOrderDetailDialog.Show();
33 }
 
 

above is to develop the processing of different messages , receive messages when triggered in these processes simply call Controller.Invoke method to complete .

 
  
1 public void ClientReceive(Beetle.NetPackage.NetClient client, object message) 
2 {
3 this.Dispatcher.BeginInvoke(() =>
4 {
5 Beetle.NetPackage.Controller.Invoke(this, mClient, message);
6 });
7 }
 
 

method call in the message received Controller.Invoke component will automatically match the appropriate message processing method and call , because it is the component in the development of automatic matching process also requires methods to follow a rule that is in an object corresponding message handling method must be unique .

 

operating results

 

 

Summary

 

through Beetle.NetPackage requires only a small amount of code to be able to complete a TCP-based interactive processing object data , and the developer is totally do not care about the details of the underlying protocol processing , Beetle.NetPackage wp8 not only provide support , but also provides support for flash and android .

 

download sample

 

open source components official website : https://beetlenp.codeplex.com/

没有评论:

发表评论