Using weaving-socket teaching-boost, communicate transports like calling methods

Most communication architectures only guarantee the transmission of basic data, and in the process of writing, you have to encode or decode by yourself. Then divide the data with a big push judgment to ensure that the desired code segment is run. Well, friends who use the weaving-socket architecture, you are blessed, now you only need to define the method name and add a modification, and you can directly run it into the method you only defined by calling a method. It's simple and pleasant.

First download the project:  http://git.oschina.net/dreamsfly900/universal-Data-Communication-System-for-windows

And a fellow project

.NET Core's weaving-socket project

http://git.oschina.net/dreamsfly900/weaving-socket-core 

The first step is to write the server side:

After the new version is updated, MyInterface is  renamed WeaveBase. The name of TCPCommand has been changed, please pay attention to WeaveTCPCommand.

First, we need to create a new class library project, pay attention to the class library. Give it a name content_manage, and reference the project MyInterface.

Make your new class inherit MyInterface.TCPCommand and implement the methods that must be implemented in MyInterface.TCPCommand.

 public class content_manage : MyInterface.TCPCommand
    {
        public override void Bm_errorMessageEvent(Socket soc, _baseModel _0x01, string message)
        {
             //错误异常事件,message为错误信息,soc为产生异常的连接
        }

        public override byte Getcommand()
        {
           //此CLASS的实例,代表的指令,指令从0-254,0x9c与0xff为内部指令不能使用。
            //0x01的意思是,只要是0x01的指令,都会进入本实例进行处理
            return 0x01;
        }

        public override bool Run(string data, Socket soc)
        {
//此事件是接收事件,data 是String类型的数据,soc是发送人。
            return true;
        }

        public override void TCPCommand_EventDeleteConnSoc(Socket soc)
        {
             //此事件是当有人中断了连接,此事件会被调用
        }

        public override void TCPCommand_EventUpdataConnSoc(Socket soc)
        {
               //此事件是当有人新加入了连接,此事件会被调用
        }
    }

There is a public override bool Run(string data, Socket soc) receive event in it, and the sent data will go through this method. The topic said, if you want to be able to define the method yourself, what should you do?

For example, I want to see how many people are currently online on the client side. Then we generally define a method name: getnum.

So what to do here? see code

 [InstallFun("forever")]
        public void getnum(Socket soc, _baseModel _0x01)
        {
            int num = 9987;//假设我人数为9987
            SendRoot<int>(soc, 0x01, _0x01.Request, num, 0, _0x01.Token);
           //发送人数给客户端
   //参数1,发送给客户端对象,参数2,发送给客户端对应的方法,参数3,人数的实例,参数4,此处无作用,参数5,客户端此次token
        }

It only needs to be defined like this, [InstallFun("forever")] represents, this method is permanently valid.

Of course, there is also the corresponding [InstallFun("once")] representative, this method can only be called once and is destroyed immediately.

   public void getnum(Socket soc, _baseModel _0x01), the method name can be changed at will, and the parameters and return values ​​are fixed.

So how do we tell the client how many we have?

SendRoot<int>(soc, 0x01, _0x01.Request, num, 0, _0x01.Token); is this method.

Parameter 1, sent to the client object, parameter 2, the corresponding method sent to the client, parameter 3, the instance of the number of people, parameter 4, no effect here, parameter 5, the client token this time

This is done. Full code:

public class content_manage : MyInterface.TCPCommand
    {
        public override void Bm_errorMessageEvent(Socket soc, _baseModel _0x01, string message)
        {
             
        }
        [InstallFun("forever")]
        public void getnum(Socket soc, _baseModel _0x01)
        {
            int num = 9987;
            SendRoot<int>(soc, 0x01, _0x01.Request, num, 0, _0x01.Token);
        }
        public override byte Getcommand()
        {
            return 0x01;
        }

        public override bool Run(string data, Socket soc)
        {
            return true;
        }

        public override void TCPCommand_EventDeleteConnSoc(Socket soc)
        {
             
        }

        public override void TCPCommand_EventUpdataConnSoc(Socket soc)
        {
             
        }
    }

 

Then right-click on your project - "Properties -" Build Events - "Post-build event command line

Fill in copy $(TargetFileName) $(SolutionDir)\Zhixin build structure\bin\Debug\command

Then compile this code, and the file content_manage.dll will be generated and automatically entered

In the command directory of the communication server runner project in the  architecture , you can first generate a communication server runner project,

 Look in the Debug directory to see if there is a command directory, if not, create one manually.

Then run  the communication server runner project, 

Select TCPSOKET, fill in the port number, click Add Monitor, and then click Load Server Plugin to start running.

 

Step 2 Client:

Create a new winform project and reference the MyInterface and TCPclient projects.

Add a button to the interface:

  public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        P2Pclient p2pc = new P2Pclient(false);//初始化
        private void Form1_Load(object sender, EventArgs e)
        {
            p2pc.receiveServerEvent += P2pc_receiveServerEvent;//接收数据事件
            p2pc.timeoutevent += P2pc_timeoutevent;//超时(掉线)事件
            p2pc.start("127.0.0.1", 8989, false);//11002 是网关的端口号,刚才WEB网关占用了11001,我改成11002了
            p2pc.AddListenClass(this);//这是表示  [InstallFun("forever")]的方法,在哪个类中,全部加载出来。
           
        }
        [InstallFun("forever")]//客户端也支持像服务端那样写,刚才看懂返回的内容也是getnum,所以客户端也要把方法命名getnum
        public void getnum(System.Net.Sockets.Socket soc, _baseModel _0x01)//服务端返回内容调用的方法getnum,所以命名为getnum。
        {
            MessageBox.Show(_0x01.GetRoot<int>().ToString());//弹出返回值
          
        }
        private void P2pc_timeoutevent()
        {
            if (!p2pc.Isline)
            {
                p2pc.Restart(true);//断线重连
            }
        }

        private void P2pc_receiveServerEvent(byte command, string text)
        {
           
        }
     
        private void button1_Click(object sender, EventArgs e)
        {
            //在加个发送
            p2pc.Tokan = "随便写一个";
            p2pc.SendRoot<int>(0x01, "getnum", 0,0);//调用服务端方法getnum,是服务端的方法。
            //这样就可以了,我们试试
        }
    }

write the code

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325437664&siteId=291194637