Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Two-way Remoting with Callbacks and Events, Explained

, 19 Jul 2006 CPOL
A demonstration of how separate applications on different machines are able to effectively communicate with one another.
twowayremotingclient_src.zip
RemotingClient
RemotingClient
bin
Properties
Settings.settings
ServerClient.dll
twowayremotingserver_src.zip
RemotingServer
RemotingServer
bin
Properties
Settings.settings
ServerClient.dll
ServerClient
bin
Properties
twowayremoting_src.zip
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)

Share

About the Author

marcel heeremans
Software Developer (Senior)
United Kingdom United Kingdom
In the early days I started developing using MS Access version 1 through all versions of VB followed now by .NET. Things are getting better!
 
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@googlemail.com

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 19 Jul 2006
Article Copyright 2006 by marcel heeremans
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid