An event-driven communication c# program

来源:互联网 发布:老年人意外伤害数据 编辑:程序博客网 时间:2024/06/07 03:04

process communication can be analog to the theory of mobile network - there is a network that every subscriber can utilize to send and receive information.

of cause in program we should have resembles to network and user.

i have thought a lot on such kind of program implementation and structure, finally i found event driven or message driven is the best shape. to understand this concept one have to image there is a unique Centerlized static thing in your code - a postman. he is responsible for carrying everything for you and from you. there is no better choice. other implementation require tight coupling more or less, if every one talk to each other directly in code there should be at least n*(n-1) channel for this purpose. a centerlized - postman style - structure has a side good effect that it can written as message driven it mean the procedure can sleep until the message for him coming in. a static class called "Postman.cs" is defined in this quick demo written off duty tonight. this is also a must since every user want to see him clearly. this is a extreme situation where static class is the solution. i also wrote some Tcp class simulating Tcp communication mechanism, another Center.cs for simulating management entity since management part always have to look every thing. during implementation i found Broadcasting capability is the essence of communication protocol. in a microscopic view every communication path means a series of broadcasting in and only in useful scenario. since people have to register a mountain of handler function to the center. yes filter can be used to differentiate the sender object - but ovbiously i have to omit it for your dig. this is a working sample compile with vs2012 and run results as my expectation. it will be used in my machine talking project. which is on going. yeh.

here are output:

线程 'vshost.NotifyLoad' (0x2598) 已退出,返回值为 0 (0x0)。
线程 '<无名称>' (0x14c8) 已退出,返回值为 0 (0x0)。
线程 '<无名称>' (0x2570) 已退出,返回值为 0 (0x0)。
线程 'vshost.LoadReference' (0x2090) 已退出,返回值为 0 (0x0)。
“postman.vshost.exe”(托管(v4.0.30319)): 已加载“d:\mydocuments\visual studio 2012\Projects\postman\postman\bin\Debug\postman.exe”,符号已加载。
“postman.vshost.exe”(托管(v4.0.30319)): 已加载“C:\windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll”
OnSend->Civil Send out data
OnSend->System.EventArgs100
OnRecv->System.EventArgs123
OnRecv->System.EventArgs456
OnRecv->System.EventArgs789
OnRecv->System.EventArgs100
OnRecv->...
OnRecv->System.EventArgs100
OnSend->System.EventArgs123
OnSend->System.EventArgs456
OnSend->System.EventArgs789
OnSend->System.EventArgs100
OnRecv->...
OnRecv->System.EventArgs123
OnSend->System.EventArgs123
OnSend->System.EventArgs456
OnSend->System.EventArgs789
OnSend->System.EventArgs100
线程 '<线程结束>' (0x6c0) 已退出,返回值为 0 (0x0)。
OnRecv->...
OnRecv->System.EventArgs456
OnSend->System.EventArgs123
OnSend->System.EventArgs456
OnSend->System.EventArgs789
OnSend->System.EventArgs100
OnRecv->...
OnRecv->System.EventArgs789
OnSend->System.EventArgs123
OnSend->System.EventArgs456
OnSend->System.EventArgs789
OnSend->System.EventArgs100
OnSend->Tcp send->58
OnSend->System.EventArgs123
OnRecv->System.EventArgs123
OnRecv->System.EventArgs456
OnRecv->System.EventArgs789
OnRecv->System.EventArgs100
OnSend->Tcp send->59
OnSend->System.EventArgs456
OnRecv->System.EventArgs123
OnRecv->System.EventArgs456
OnRecv->System.EventArgs789
OnRecv->System.EventArgs100
OnSend->Tcp send->60
OnSend->System.EventArgs789
OnRecv->System.EventArgs123
OnRecv->System.EventArgs456
OnRecv->System.EventArgs789
OnRecv->System.EventArgs100
单步执行: 正在逐过程执行不含符号“Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly”的方法
线程 'vshost.RunParkingWindow' (0x14a0) 已退出,返回值为 0 (0x0)。
单步执行: 正在逐过程执行不含符号“System.Threading.ExecutionContext.RunInternal”的方法
线程 '<无名称>' (0x26e0) 已退出,返回值为 0 (0x0)。
程序“[9692] postman.vshost.exe: 托管(v4.0.30319)”已退出,返回值为 0 (0x0)。
程序“[9692] postman.vshost.exe: 程序跟踪”已退出,返回值为 0 (0x0)。


here are code plain text:

---------------------------------------

Postman.cs


namespace postman
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    static class Postman
    {
        public delegate void Handler(object sender, EventArgs args);
        public static void UpdatePostHandlers(object sender, string message) {
            if (null != PostHandlers) {
                PostHandlers(sender, new EventArgs());
            }
        }

        public static void UpdateSinkHandlers(object sender, string message) {
            if (null != SinkHandlers) {
                SinkHandlers(sender, new EventArgs());
            }
        }

        public static void RegPostHandler(Handler h)
        {
            PostHandlers += h;
        }
        public static void RegSinkHandler(Handler h)
        {
            SinkHandlers += h;
        }
        public static void DeRegPostHandler(Handler h)
        {
            PostHandlers -= h;
        }
        public static void DeRegSinkHandler(Handler h)
        {
            SinkHandlers -= h;
        }

        private static event Handler PostHandlers = null;
        private static event Handler SinkHandlers = null;
    }
}


---------------------------------------

Tcp.cs


namespace postman
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Diagnostics;

    class Tcp
    {
        public int P { get; private set; }
        public Tcp(int port) {
            this.P = port;
            Postman.RegPostHandler(OnSend);
            Postman.RegSinkHandler(OnRecv);
        }

        public void OnSend(object sender, EventArgs args) { Debug.WriteLine("OnSend->" + args + this.P); }
        public void OnRecv(object sender, EventArgs args) { Debug.WriteLine("OnRecv->" + args + this.P); }

        public void Send(string msg) { Debug.WriteLine("OnSend->" + msg); this.OnSend(this, new EventArgs()); Postman.UpdateSinkHandlers(this, msg); }
        public void Recv(string msg) { Debug.WriteLine("OnRecv->..."); this.OnRecv(this, new EventArgs()); Postman.UpdatePostHandlers(this, msg); }

        ~Tcp()
        {
            Postman.DeRegPostHandler(OnSend);
            Postman.DeRegSinkHandler(OnRecv);
        }

    }
}


---------------------------------------

Center.cs


namespace postman
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Diagnostics;  
   
    // can be known as a subnet:)
    class Center
    {
                public int Q { get; private set; }
                public Center(int port)
                {
            this.Q = port;
            Postman.RegPostHandler(OnSend);
            Postman.RegSinkHandler(OnRecv);
        }

        public void OnSend(object sender, EventArgs args) { Debug.WriteLine("OnSend->" + args + this.Q); }
        public void OnRecv(object sender, EventArgs args) { Debug.WriteLine("OnRecv->" + args + this.Q); }

        public void Send(string msg) { Debug.WriteLine("OnSend->" + msg); this.OnSend(this, new EventArgs()); Postman.UpdateSinkHandlers(this, msg); }
        public void Recv(string msg) { Debug.WriteLine("OnRecv->..."); this.OnRecv(this, new EventArgs()); Postman.UpdatePostHandlers(this, msg); }
        ~Center()
        {
            Postman.DeRegPostHandler(OnSend);
            Postman.DeRegSinkHandler(OnRecv);
        }

    }
}
---------------------------------------

end


0 0
原创粉丝点击