Chapter 1 of dycom White Paper (technical direction)

Source: Internet
Author: User
Tags tojson

Dycom is used to develop the communication functions of network applications and supports cross-platform communication technology. Supported clients can learn more from dycom Official Website: http://dy2com.com...

Preparations before use:

Let's take a look at the usage of dycom. First, dycom provides a secondary programming interface through DLL. First, we can download the corresponding DLL component from the dycom official website.

Dycom is designed based on the server <---> client applications. That is, there must be a server running a server application developed by dycom, which can be accessed by any computer on the Internet. On the other hand, we need to develop a client application. This client application is also developed through the client components provided by dycom. Therefore, this network communication application solution is: server <---> client communication mode.

Go to the dycom official website. We can download dycom from the main menu of the website. We can see all the dycom servers and client components, and provide category indexes on the right of the webpage, we can quickly find the dycom components of each technical platform we want to download through classification:

 

A) project templates can help you quickly create a dycom basic service. They can be downloaded to the related project template files in the "server template" in the "column list" on the right. To download the downloaded Project template, you only need to put it in the directory of your Visual Studio Project template (for example, c: \ Users \ JAC \ Documents \ Visual Studio 2010 \ Templates \ projecttemplates ), after restarting Visual Studio, in the new project, we can see that a considerable number of projects in dycom are created. For example:

In this way, we can create a dycom basic service project just like creating a common project. The new project has a service prototype and has a code description:

Running this project directly in Visual Studio is a basic server-side program. It provides a basic function that receives a string and broadcasts it to all connected clients immediately. To create a basic server program, you don't need to write a single line of code. You just need to click it to create it.

 

B) network communication protocol for dycom. In order to make the service suitable for the development of IM, game, video, music and other application fields, dycom transmits all binary stream data in the core method, dycom also provides protocol writers and readers for ease of use. In addition to the support for basic common types such as string, int32, double, datetime, and Boolean. JSON data encoding and decoder are added. This gives us the freedom to customize communication protocols. It also provides quick and feasible solutions for developing game plug-ins or private server applications.

 

C) dycom protocol writer (dywriter) and reader (dyreader ). Non-delicious non-developed online games and communication applications require a high-speed protocol that consumes less network resources to improve the user experience of applications, the most direct idea is to use binary stream data for network transmission. The Protocol writer and reader of dycom are generated for this function. They can help you convert your common variables into binary streams, and restore them to your original common variable types after receiving them at the other end of the network. In addition, the order of each variable can be defined at will. As long as they comply with one rule, the reader must write the Protocol in the same order as the Protocol writer when reading the Protocol variables. Here are two examples to better illustrate the problem. You can use these two simple codes to better understand the Protocol writer and reader of dycom.

Example 1: encode and restore a string

VaR MSG = dywriter. getdybytes ("Hello dycom", encoding. utf8 );

The above Code aims to use the dycom protocol writer to encode a string into a binary stream in utf8 encoding format and pay the value to the MSG variable. We only need to call the dycom. Send method to send MSG. Then we can use the following dycom protocol reader to restore the "Hello dycom" string:

// Instantiate the dycom message decoder
Dyreader READ = new dyreader ("received binary stream ");

// Variable
String message;
// Read a string value to the Message variable and use utf8 to decode it
If (read. readstring (out message, encoding. utf8 ))
{

// Process the read message variable

}

In the above example, we can see that it is so easy to use dycom to send a string. The problem arises again. If we need to send multiple variable values of different types in the same message at the same time, what should we do?

Example 2: encode and restore a message with two variable parameters. The first one is to send an int value of 1, and the second one is to send a string "Hello dycom"

Dywriter. Merge (dywriter. getdybytes (1), dywriter. getdybytes ("Hello dycom", encoding. utf8 )));

The above sentence code shows that the Merge function is used to merge Multiple Streams generated by the dycom writer. In its parameters, we can use the bean number to separate various writers and write operations. Note that when we read this stream, the read order must be the same as that in writing. That is to say, the first read must be an int value, the second is the string value.

// Instantiate the dycom message decoder
Dyreader READ = new dyreader ("received binary stream ");

// Variable

Int intvalue;
String stringvalue;
// Read a string value to the Message variable and use utf8 to decode it
If (read. readint32 (Out intvalue) & read. readstring (Out stringvalue, encoding. utf8 ))
{

// Process the read intvalue and stringvalue Variables

}

When reading and restoring data, You need to register two places. First, the reading order must be the same as the writing order, second, we can use & in the IF statement to read all the variables of the entire message. The operation code is more elegant.

The above two examples are used. I believe you know the usage of the dycom protocol writer and reader. If you have more questions, please go to dycom official Knowledge Forum: http://bbs.dy2com.com for more discussions.

 

D) How to Use the JSON sequencer in dycom. First, we should first understand that JSON itself is a technology that allows the conversion between strong types and strings freely. The first thing we can determine is the JSON string. We can transmit a serialized JSON string using the method in Example 1. Next, I will give an example to illustrate how to use the JSON serializer in dycom:

Example 3: Use the JSON serializer of dycom to serialize and deserialize a class.

1. First define a class:

Public class info
{
Public int intdata {Get; set ;}
Public String stringdata {Get; set ;}
}

2. instantiate a JSON serializer and name it mapper:

Jsonmapper mapper = new jsonmapper ();

3. serialize an info class into a JSON string to a jsonstring variable:

Info infoinstance = new info ();
Infoinstance. intdata = 1;
Infoinstance. stringdata = "Hello dycom ";

String jsonstring = Mapper. tojson (infoinstance );

The above code mainly uses the Mapper. tojson function to serialize the info instance type infoinstance into a JSON string and pay the value to jsonstring. Since jsonstring is a string, we use the preceding example 1 or example 2 to transmit the jsonstring over the network.

4. After we reach the jsonstring side of the network, how can we deserialize the string to infoinstance? The Code is as follows:

Info infoinstance = Mapper. toobject <info> (jsonstring );

It is so simple that we have completed operations related to the use of JSON in dycom. All of this is within the dycom component, and no other components need to be referenced!

 

In the next chapter, I will introduce how to use dycom's server and client and related functions.

More dycom cases:

Open Source Project: dycom-based multiplayer online games (Silverlight client) Address: http://flyer.codeplex.com

Silverlight real-time head Sharing Project: http://funsl.com/content/silverlight4-%E4%B8%80%E5%AF%B9%E5%A4%9A%E8%A7%86%E9%A2%91%E5% AE %9E%E6%97%B6%E5%85%B1%E4%BA% AB

Simple online game synchronization example: http://funsl.com/content/silverlight%E5%A4%9A%E4%BA%BA%E5%9C%A8%E7%BA%BF%E6%B8%B8%E6%88%8F%E7%A4%BA%E4%BE%8B

Update case please go to dycom Official Website: http://www.dy2com.com/a/DYcomanli/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.