C # communication gateway development,

Source: Internet
Author: User

C # communication gateway development,


 

The landlord has been engaged in the development of communication gateway most of the time since he graduated from 12 years. He just learned that he would not even know what C # is, so he started to develop the gateway directly, five or six communication protocols have been established before and after. Later, I saw that the socket framework and supersocket of beifeng's God have been studied for a long time. I was thinking about building a communication gateway framework myself.

 

Framework Overview

This framework is of course Microsoft's high-performance server framework, which encapsulates some business logic and other processing.

 

Network Layer
1 /// <summary> 2 /// Service interface 3 /// </summary> 4 public interface IServer 5 {6 string Name {get ;} 7 void Start (int port); 8 void Start (EndPoint localPoint); 9 void Stop (); 10}
/// <Summary> /// request interface /// </summary> public interface IRequestInfo {/// <summary> /// unique ID /// </summary> string Key {get; set ;}}

Each Protocol has a specific format. This class is a request generated in a specific command format.

1 /// <summary> 2 // message filter 3 /// </summary> 4 /// <typeparam name = "TRequestInfo"> request interface </typeparam> 5 public interface IReceiveFilter <TRequestInfo> 6 where TRequestInfo: IRequestInfo 7 {8 /// <summary> 9 // message parsing 10 /// </summary> 11 /// <param name = "message"> </param> 12 /// <returns> </returns> 13 List <TRequestInfo> Filter (ref byte [] message ); 14}

Message filter, handling issues such as sticky packets in tcp connections

1 /// <summary> 2 // Gateway Interface 3 /// </summary> 4 /// <typeparam name = "TRequestInfo"> request data </typeparam> 5 /// <typeparam name = "TReceiveFilter"> message filter </typeparam> 6 /// <typeparam name = "TCommandFactory"> command factory </typeparam> 7 public interface IGater <TRequestInfo, TReceiveFilter, out TCommandFactory> 8 where TRequestInfo: IRequestInfo 9 where TReceiveFilter: IReceiveFilter <TRequestInfo> 10 {11 /// <summary> 12 // configure 13 /// </summary> 14 BaseConfig Config {get ;} 15 /// <summary> 16 /// command factory 17 /// </summary> 18 TCommandFactory CommandFactory {get ;} 19 /// <summary> 20 // Service Program 21 /// </summary> 22 TcpServer <TReceiveFilter, TRequestInfo> Server {get;} 23}

Gateway Interface Layer

1 /// <summary> 2 // basic Gateway 3 /// </summary> 4 /// <typeparam name = "TRequestInfo"> request data interface </typeparam> 5 // <typeparam name = "TCommand"> client command interface </typeparam> 6 // <typeparam name = "TReceiveFilter"> message filter interface </typeparam> 7/ // <typeparam name = "TCommandFactory"> Client Command Parsing factory </typeparam> 8 public abstract class BaseGate <TRequestInfo, TCommand, TReceiveFilter, TCommandFactory>: IGater <TRequestInfo, TReceiveFilter, TCommandFactory> 9 where TRequestInfo: IRequestInfo10 where TCommand: ICommand <TRequestInfo> 11 where TReceiveFilter: IReceiveFilter <TRequestInfo>, new () 12 where TCommandFactory: ICommandFactory <TRequestInfo, TCommand> 13 {14 /// <summary> 15 /// configure 16 /// </summary> 17 public BaseConfig Config {get; private set ;} 18 /// <summary> 19 /// command factory 20 /// </summary> 21 public TCommandFactory CommandFacto Ry {get; private set;} 22 // <summary> 23 // service 24 /// </summary> 25 public TcpServer <TReceiveFilter, TRequestInfo> Server {get; private set ;} 26 27 /// <summary> 28 /// constructor 29 /// </summary> 30 /// <param name = "server"> socekt service </param> 31 // <param name = "commandFactory"> command factory </param> 32 // <param name = "config"> service configuration </param> 33 /// <param name = "clientEvent"> client event processing </param> 34 protected BaseGate (TcpServ Er <TReceiveFilter, TRequestInfo> server, TCommandFactory commandFactory, BaseConfig, IClientEvent clientEvent) 35 {36 CommandFactory = commandFactory; 37 Server = server; 38 config = Config; 39 server. clientOnClosed + = clientEvent. clientOnClosed; 40 server. clientOnConnected + = clientEvent. clientOnConnected; 41 server. export enewdata + = export enewdata; 42} 43 44 // <summary> 45 // new data processing 46 /// </sum Mary> 47 // <param name = "request"> </param> 48 private void incluenewdata (TRequestInfo request) 49 {50 var item = CommandFactory. createCommand (request); 51 if (item! = Null) 52 {53 item. excuteCommand (request); 54} 55} 56 57 // <summary> 58 // initialize gateway 59 /// </summary> 60 public void Init () 61 {62 63 Server. init (Config. maxClientCounts, Config. bufferSize); 64 Server. initFilter (new TReceiveFilter (); 65} 66 // <summary> 67 // Start gateway 68 /// </summary> 69 public void Start () 70 {71 Server. start (Config. port); 72} 73}

Basic Implementation of the Gateway

1 /// <summary> 2 // client connection processing interface 3 /// </summary> 4 public interface IClientEvent 5 {6 /// <summary> 7 /// new Connection 8 /// </summary> 9 /// <param name = "sender"> </param> 10 /// <param name = "e"> </ param> 11 void ClientOnConnected (object sender, clientEventArgs e ); 12 /// <summary> 13 // The connection is closed 14 /// </summary> 15 /// <param name = "sender"> </param> 16 // /<param name = "e"> </param> 17 void ClientOnClosed (object sender, clientEventArgs e); 18}

This is the connection processing of the client. It identifies a unique id based on the key in requestinfo. It can also be implemented in the gateway. I separate it because multiple servers may be deployed, multiple protocols

Business logic layer

In fact, it encapsulates the processing of commands, provides the factory interface and command interface, and generates

1 /// <summary> 2 /// command interface 3 /// </summary> 4 /// <typeparam name = "TRequestInfo"> request interface </typeparam> 5 public interface ICommand <in TRequestInfo> 6 where TRequestInfo: IRequestInfo 7 {8 /// <summary> 9 // Client ID 10 /// </summary> 11 string Key {get; set ;} 12 /// <summary> 13 /// Execute Command 14 /// </summary> 15 /// <param name = "request"> </param> 16 void ExcuteCommand (TRequestInfo request ); 17}

 

1 /// <summary> 2 // Client Command factory 3 /// </summary> 4 /// <typeparam name = "TRequestInfo"> request interface </typeparam> 5 // <typeparam name = "TCommand"> command interface </typeparam> 6 public interface ICommandFactory <in TRequestInfo, out TCommand> 7 where TRequestInfo: IRequestInfo 8 where TCommand: ICommand <TRequestInfo> 9 {10 /// <summary> 11 /// construct command 12 /// </summary> 13 /// <param name = "request"> </param> 14 // <returns> </returns> 15 TCommand CreateCommand (TRequestInfo request ); 16}
 

As long as you inherit the above interface, you can basically implement it.

 

 

This is. Let's take a look. By the way, let's give some comments.

Http://pan.baidu.com/s/1dDIOkyH

 

 


Symbol in C Language <Yes

Left shift operator (<)

Removes all the binary bits of an operation object from the left and adds 0 to the right ).

For example, a = a <2 shifts the binary bits of a two places to the left and complements 0 to the right,

Move 1 to the left and then a = a * 2;

If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.
Shift right operator (>)

Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.

The operand shifts one digit to the right, which is equivalent to dividing the number by 2.

For example, a = a> 2 shifts the binary bit of a two places to the right,

0 or 1 to see whether the number is positive or negative.

Symbol in C Language <Yes

Left shift operator (<)

Removes all the binary bits of an operation object from the left and adds 0 to the right ).

For example, a = a <2 shifts the binary bits of a two places to the left and complements 0 to the right,

Move 1 to the left and then a = a * 2;

If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.
Shift right operator (>)

Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.

The operand shifts one digit to the right, which is equivalent to dividing the number by 2.

For example, a = a> 2 shifts the binary bit of a two places to the right,

0 or 1 to see whether the number is positive or negative.

Related Article

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.