Click here to Skip to main content
15,886,774 members
Articles / Programming Languages / C#

Two-way Remoting with Callbacks and Events, Explained

Rate me:
Please Sign up or sign in to vote.
4.98/5 (65 votes)
19 Jul 2006CPOL9 min read 411.3K   7.5K   267  
A demonstration of how separate applications on different machines are able to effectively communicate with one another.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Messaging;
using System.Threading;

namespace RemotingServerClient
{
    public delegate void delUserInfo(string UserID);
    public delegate void delCommsInfo(CommsInfo info);

    // This class is created on the server and allows for client to register their existance and
    // a call-back that the server can use to communicate back.
    public class ServerTalk : MarshalByRefObject
    {
        private static delUserInfo _NewUser;
        private static delCommsInfo _ClientToHost;
        private static List<ClientWrap> _list = new List<ClientWrap>();

        public void RegisterHostToClient(string UserID, delCommsInfo htc)
        {
            _list.Add(new ClientWrap(UserID, htc));

            if (_NewUser != null)
                _NewUser(UserID);
        }

        /// <summary>
        /// The host should register a function pointer to which it wants a signal
        /// send when a User Registers
        /// </summary>
        public static delUserInfo NewUser
        {
            get { return _NewUser; }
            set { _NewUser = value; }
        }

        /// <summary>
        /// The host should register a function pointer to which it wants the CommsInfo object
        /// send when the client wants to communicate to the server
        /// </summary>
        public static delCommsInfo ClientToHost
        {
            get { return _ClientToHost; }
            set { _ClientToHost = value; }
        }

        // the static method that will be invoked by the server when it wants to send a message
        // to a specific user or all of them.
        public static void RaiseHostToClient(string UserID, string Message)
        {
            foreach (ClientWrap client in _list)
            {
                if ((client.UserID == UserID || UserID == "*") && client.HostToClient != null)
                    client.HostToClient(new CommsInfo(Message));
            }
        }

        // a thread-safe queue that will contain any message objects that should
        // be send to the server
        private static Queue _ClientToServer = Queue.Synchronized(new Queue());

        // this instance method allows a client to send a message to the server
        public void SendMessageToServer(CommsInfo Message)
        {
            _ClientToServer.Enqueue(Message);
        }

        public static Queue ClientToServerQueue
        {
            get { return _ClientToServer; }
        }

        // small private class to wrap the User and the callback together.
        private class ClientWrap
        {
            private string _UserID = "";
            private delCommsInfo _HostToClient = null;

            public ClientWrap(string UserID, delCommsInfo HostToClient)
            {
                _UserID = UserID;
                _HostToClient = HostToClient;
            }

            public string UserID
            {
                get { return _UserID; }
            }

            public delCommsInfo HostToClient
            {
                get { return _HostToClient; }
            }
        }
    }

    [Serializable()]
    public class CommsInfo
    {
        private string _Message = "";

        public CommsInfo(string Message)
        {
            _Message = Message;
        }

        public string Message
        {
            get { return _Message; }
            set { _Message = value; }
        }
    }

    /// <summary>
    /// This CallbackSink object will be 'anchored' on the client and is used as the target for a callback
    /// given to the server.
    /// </summary>
    public class CallbackSink : MarshalByRefObject
    {
        public event delCommsInfo OnHostToClient;

        public CallbackSink()
        { }

        [OneWay]
        public void HandleToClient(CommsInfo info)
        {
            if (OnHostToClient != null)
                OnHostToClient(info);
        }
    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
Ever since my dad bought me a Commodore 64 (some years back) I have been hooked to programming. For most of my working career I have worked intensely with C# (WinForms and WPF) but a few years back I started to investigate browser technologies and frameworks which are much more powerful than I thought at first!

I studied International Marketing but found the IT sector much more challenging. So straight from uni I took on some IT contract work in London in a buoyant market and never looked back.

If you wish to contact me then please do so on heeremans.marcel@gmail.com

Comments and Discussions