在NET Remoting中傳遞事件

客戶端

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Net;
using System.Runtime.Serialization.Formatters;
using FaxCommon;

namespace Client
{
    public partial class ClientForm : Form
    {
        private IFaxBusiness m_fax =null;
        private EventWrapper m_Wrapper = null;
        private IBroadCast m_Watch = null;

        private delegate void SetTextCallBack(string Text);

        private string GetIPAddress()
        {
            IPHostEntry ipHE = Dns.GetHostEntry(Dns.GetHostName());
            return ipHE.AddressList[0].ToString();
        }

        public ClientForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
            BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
            serverProvider.TypeFilterLevel = TypeFilterLevel.Full;
            IDictionary props = new Hashtable();
            props["port"] = 0;
            HttpChannel channel = new HttpChannel(props, clientProvider, serverProvider);

            ChannelServices.RegisterChannel(channel, false);

            m_Watch = (IBroadCast)Activator.GetObject(typeof(IBroadCast), "http://192.168.168.9:1234/BroadCastMessage.soap");
            m_Wrapper = new EventWrapper();
            m_Wrapper.LocalBroadCastEvent += new BroadCastEventHandler(BroadCastingMessage);
            m_Watch.BroadCastEvent += new BroadCastEventHandler(m_Wrapper.BroadCasting);

            m_fax = (IFaxBusiness)System.Activator.GetObject(
                typeof(IFaxBusiness),
                "http://km-it-p09:1234/FaxBusiness.soap");
        }

        public void BroadCastingMessage(string message)
        {
            if (this.textBox1.InvokeRequired)
            {
                SetTextCallBack d = new SetTextCallBack(BroadCastingMessage);
                this.Invoke(d, new object[] { message });
            }
            else
            {
                textBox1.Text += "I got it:" + message;
                textBox1.Text += System.Environment.NewLine;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox1.Text))
            {
                string faxstr = "來自" + GetIPAddress() + "客戶端的傳真" + System.Environment.NewLine;
                faxstr += textBox1.Text;
                m_fax.SendFax(faxstr);
            }
            else
            {
                MessageBox.Show("請輸入傳輸內容!");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            m_Watch.BroadCastEvent  -= new BroadCastEventHandler(m_Wrapper.BroadCasting);
            MessageBox.Show("取消訂閱廣播成功!");
        }

        private void ClientForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            m_Watch.BroadCastEvent -= new BroadCastEventHandler(m_Wrapper.BroadCasting);
        }
    }
}

 

 //連接庫

using System;
using System.Runtime.Remoting.Messaging;
using System.Windows.Forms;
using FaxCommon;

namespace FaxProcess
{
    public delegate void FaxEventHandler(string Fax);

    public class FaxBusiness : MarshalByRefObject , IFaxBusiness 
    {
        public static event FaxEventHandler FaxSendedEvent = null;

        public void SendFax(string Fax)
        {
            if (FaxSendedEvent != null)
            {
                FaxSendedEvent(Fax);
            }
        }

        public override object InitializeLifetimeService()
        {
            return null;
        }
    }

    public class BroadCast : MarshalByRefObject, IBroadCast
    {
        public event BroadCastEventHandler BroadCastEvent = null;

        //[OneWay]
        public void BroadCastingInfo(string info)
        {
            if (BroadCastEvent != null)
            {
                BroadCastEventHandler tempEvent = null;
                int index = 1; //記錄事件訂閱者委托的索引

                foreach (Delegate del in BroadCastEvent.GetInvocationList())
                {
                    try
                    {
                        tempEvent = (BroadCastEventHandler)del;
                        tempEvent(info);
                    }
                    catch
                    {
                        MessageBox.Show("事件訂閱者" + index.ToString() + "發生錯誤,系統將取消事件訂閱");
                        BroadCastEvent -= tempEvent;
                    }

                    index++;
                }
            }
            else
            {
                MessageBox.Show("事件未被訂閱或發生錯誤!");
            }
        }

        public override object InitializeLifetimeService()
        {
            return null;
        }
    }
}
//連接庫

 

using System;
using System.Collections.Generic;
using System.Text;

namespace FaxCommon
{
    public class EventWrapper : MarshalByRefObject
    {
        public event BroadCastEventHandler LocalBroadCastEvent;

        public void BroadCasting(string message)
        {
            LocalBroadCastEvent(message);
        }

        public override object InitializeLifetimeService()
        {
            return null;
        }

    }
}
using System;

namespace FaxCommon
{
    public delegate void BroadCastEventHandler(string info);

    public interface IFaxBusiness
    {
        void SendFax(string Fax);
    }

    public interface IBroadCast
    {
        event BroadCastEventHandler BroadCastEvent;
        void BroadCastingInfo(string Info);
    }
}

//服務端

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Serialization.Formatters;
using FaxProcess;


namespace Server
{
    public partial class ServerForm : Form
    {
        //遠程對象變量
        private  static BroadCast  Obj = null;

        delegate void SetTextCallback(string text);

        //winForm控制項線程安全訪問方式
        private void SetText(string text)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.textBox1.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.textBox1.Text = text;
                textBox1.Text += System.Environment.NewLine;
            }
        }


        public ServerForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
            BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
            serverProvider.TypeFilterLevel = TypeFilterLevel.Full;

            IDictionary props = new Hashtable();
            props["port"] = 1234;
            HttpChannel channel = new HttpChannel(props, clientProvider, serverProvider);
            ChannelServices.RegisterChannel(channel,false );
            Obj = new BroadCast();
            ObjRef objRef = RemotingServices.Marshal(Obj, "BroadCastMessage.soap");

            RemotingConfiguration.RegisterWellKnownServiceType(
                typeof(FaxBusiness),
                "FaxBusiness.soap",
                WellKnownObjectMode.Singleton);

            FaxBusiness.FaxSendedEvent += new FaxEventHandler(OnFaxSended);

        }

        public void OnFaxSended(string Fax)
        {
            this.SetText(Fax);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Obj.BroadCastingInfo(textBox1.Text);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值