C # communication Gateway Development

Source: Internet
Author: User

C # communication Gateway Development
Framework overview this framework is of course Microsoft's high-performance server framework, which encapsulates some business logic and other processing. Copy code 1 at the network layer /// <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} copy the code and copy the code /// <summary> // request interface /// </summary> public interface IRequestInfo {// <summary> // unique ID/ // </summary> string Key {get; set ;}} copy the code. Each Protocol has a specific format, this class is the request generated according to the specific command format to copy code 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} filter for copying code messages, copy code 1 /// <summary> 2 // Gateway Interface 3 /// </summary> 4 /// <typ Eparam 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;} 1 5 /// <summary> 16 /// command factory 17 /// </summary> 18 TCommandFactory CommandFactory {get ;} 19 /// <summary> 20 // Service Program 21 /// </summary> 22 TcpServer <TReceiveFilter, TRequestInfo> Server {get ;} 23} copy code Gateway Interface Layer copy code 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: rule where TCommand: ICommand <TRequestInfo> 11 where rule: Rule <TRequestInfo>, new () 12 where TCommandFactory: ICommandF Acloud <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 CommandFactory {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 (TcpServer <TReceiveFilter, TRequestInfo> server, TCommandFactory commandFactory, BaseConfig config, IClientEvent clientEvent) 35 {36 CommandFactory = commandFactory; 37 Server = server; 38 Config = config; 39 server. clientOnClosed + = clientEvent. clientOnClosed; 40 server. clientOnConnected + = clientEvent. clientOnConnected; 41 server. required enewdata + = required enewdata; 42} 43 44 // <summary> 45 // new data processing 46 // </summary> 47 // <param name = "request"> </param> 48 private void initialize enewdata (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} copy the basic implementation of code gateway copy code 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} copy the Code. This is the connection processing of the client. It identifies the unique id based on the key in requestinfo. It can also be implemented in the gateway. I extracted it separately, if multiple servers are deployed, the business logic layer of multiple protocols encapsulates command processing and provides the factory interface and command interface, generate a copy code 1 // <summary> 2 // command interface 3 /// </summary> 4 /// <typeparam name = "TRequestInfo"> request based on the 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}

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.