Write your own Socket framework (III), socket framework

Source: Internet
Author: User

Write your own Socket framework (III), socket framework

After the communication is completed, the application layer receives the byte [] thrown by the Socket. In this case, this data is unfriendly for developers who actually write logic, we need to unify the rules of a package at the application layer (the application layer protocol). After processing, it is passed to the actual logic layer for processing.

The following is a common Command mode. After receiving the passed package, execute the corresponding Command (logic) according to the Command ).

We assume that our package (all of the following packages refer to the application layer package, rather than the Socket layer package) is divided into Command Header/data.

public class InterUnit    {        public string Command;        public JToken Body;    }

Because the Command mode is used, we define an interface ICommand

    public interface ICommand    {        InterUnit Execute(InterUnit unit);    }

The Command of the Command corresponds to the actual logic. Ioc is commonly used, but you can also perform hard encoding. I use the Attribute method to match it.

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]    public class CommandAttribute : Attribute    {        public CommandAttribute(string command)        {            this.Command = command;        }        public string Command;    }

After matching, you need to find the corresponding Class based on Command to execute the logic at the place where the package is received.

Public class CommandFactory {private Dictionary <string, ICommand> _ commandMap; public CommandFactory () {if (_ commandMap = null) {_ commandMap = new Dictionary <string, ICommand> () ;}//< summary> /// puts the instance labeled CommandAttribute into the dictionary through reflection. /// Dynamic injection is not performed until the call is required. /// </Summary> /// <param name = "assembly"> </param> public void Init (params string [] assembly) {if (assembly! = Null) {foreach (string s in assembly) {var ass = Assembly. Load (s); if (ass! = Null) {var types = ass. getTypes (); foreach (var type in types) {CommandAttribute attr = type. getCustomAttribute (typeof (CommandAttribute), false) as CommandAttribute; if (attr! = Null) {if (attr. command = null | attr. command. length = 0) {_ commandMap [type. name] = Activator. createInstance (type) as ICommand;} else {_ commandMap [attr. command] = Activator. createInstance (type) as ICommand ;}}}}} public void ExecuteCommand (SocketSession session, InterUnit unit) {if (_ commandMap. containsKey (unit. command) {ICommand command = _ commandMap [unit. command]; var rtv = com Mand. Execute (unit); if (rtv! = Null) {session. Send (BsonHelper. ToBson <InterUnit> (unit ));}}}}View Code

Here I use the Bson format for data transmission.

One thing to note is that when sending, we didn't actually define the format of the Socket package, because the Protocol already handles this problem, it will automatically add a header to the data you sent.

Public interface IProtocol {byte [] OnDataReceivedCallBack (byte [] data, ref int offset); byte [] OnDataSendBefore (byte [] data);} public class DefaultProtocol: IProtocol {public byte [] OnDataReceivedCallBack (byte [] data, ref int offset) {int length = BitConverter. toInt32 (data, offset); int package_head = 4; int package_length = length + package_head; byte [] buffer = null; if (length> 0) {if (offset + package_length <= data. length) {buffer = new byte [length]; Array. copy (data, offset + package_head, buffer, 0, length); offset + = package_length ;}} else {offset =-1 ;}return buffer ;} public byte [] OnDataSendBefore (byte [] data) {int length = data. length; var head = BitConverter. getBytes (length); byte [] buffer = new byte [data. length + head. length]; Buffer. blockCopy (head, 0, buffer, 0, head. length); Buffer. blockCopy (data, 0, buffer, head. length, data. length); return buffer ;}}View Code

 


Use socket to write a connection-oriented network programming framework for me

This ..... Source code?
 
Which of the following is the JAVA framework?

There are more than three Java frameworks. Many companies have their own frameworks, small companies all use SSH (struts2 + spring + hibernate3), the three most popular frameworks on the Internet, and wish the landlord an early academic success! ~~~

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.