DdeServer Class

NDde

Dynamic Data Exchange Library for .NET

DdeServer Class

This represents the server side of DDE conversations.

For a list of all members of this type, see DdeServer Members.

System.Object    DdeServer

public class DdeServer : IDisposable

Thread Safety

This type is safe for multithreaded operations.

Remarks

DDE conversations are established by specifying a service name and topic name pair. The service name is usually the name of the application acting as a DDE server. A DDE server can respond to multiple service names, but most servers usually only respond to one. The topic name is a logical context for data and is defined by the server application. A server can and usually does support many topic names.

After this object has registered its service name by calling the Register method clients can connect to it by specifying the service name the server registered and a topic name that it supports.

Event methods are invoked on the thread hosting the DdeContext. All operations must be marshaled onto the thread hosting the DdeContext associated with this object. Method calls will block until that thread becomes available. An exception will be generated if the thread does not become available in a timely manner.

Notes to Inheritors:     The event methods must be overridden in a subclass as needed.

Example

The following example demostrates how to use a DdeServer.

[C#]
            using System;
            using System.Collections;
            using System.Timers;
            using NDde.Server;

            public class Server
            {
                public static void Main()
                {
                    try
                    {
                        // Create a server that will register the service name 'myapp'.
                        using (DdeServer server = new MyServer("myapp"))
                        {
                            // Register the service name.
                            server.Register();

                            // Wait for the user to press ENTER before proceding.
                            Console.WriteLine("Press ENTER to quit...");
                            Console.ReadLine();
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                        Console.WriteLine("Press ENTER to quit...");
                        Console.ReadLine();
                    }
                }

                private sealed class MyServer : DdeServer
                {
                    private System.Timers.Timer _Timer = new System.Timers.Timer();

                    public MyServer(string service) : base(service)
                    {
                        // Create a timer that will be used to advise clients of new data.
                        _Timer.Elapsed += this.OnTimerElapsed;
                        _Timer.Interval = 1000;
                        _Timer.SynchronizingObject = this.Context;
                        _Timer.Start();
                    }

                    private void OnTimerElapsed(object sender, ElapsedEventArgs args)
                    {
                        // Advise all topic name and item name pairs.
                        Advise("*", "*");
                    }

                    protected override bool OnBeforeConnect(string topic)
                    {
                        Console.WriteLine("OnBeforeConnect:".PadRight(16)
                            + " Service='" + base.Service + "'"
                            + " Topic='" + topic + "'");

                        return true;
                    }

                    protected override void OnAfterConnect(DdeConversation conversation)
                    {
                        Console.WriteLine("OnAfterConnect:".PadRight(16)
                            + " Service='" + conversation.Service + "'"
                            + " Topic='" + conversation.Topic + "'"
                            + " Handle=" + conversation.Handle.ToString());
                    }

                    protected override void OnDisconnect(DdeConversation conversation)
                    {
                        Console.WriteLine("OnDisconnect:".PadRight(16)
                            + " Service='" + conversation.Service + "'"
                            + " Topic='" + conversation.Topic + "'"
                            + " Handle=" + conversation.Handle.ToString());
                    }

                    protected override bool OnStartAdvise(DdeConversation conversation, string item, int format)
                    {
                        Console.WriteLine("OnStartAdvise:".PadRight(16)
                            + " Service='" + conversation.Service + "'"
                            + " Topic='" + conversation.Topic + "'"
                            + " Handle=" + conversation.Handle.ToString()
                            + " Item='" + item + "'"
                            + " Format=" + format.ToString());

                        // Initiate the advisory loop only if the format is CF_TEXT.
                        return format == 1;
                    }

                    protected override void OnStopAdvise(DdeConversation conversation, string item)
                    {
                        Console.WriteLine("OnStopAdvise:".PadRight(16)
                            + " Service='" + conversation.Service + "'"
                            + " Topic='" + conversation.Topic + "'"
                            + " Handle=" + conversation.Handle.ToString()
                            + " Item='" + item + "'");
                    }

                    protected override ExecuteResult OnExecute(DdeConversation conversation, string command)
                    {
                        Console.WriteLine("OnExecute:".PadRight(16)
                            + " Service='" + conversation.Service + "'"
                            + " Topic='" + conversation.Topic + "'"
                            + " Handle=" + conversation.Handle.ToString()
                            + " Command='" + command + "'");

                        // Tell the client that the command was processed.
                        return ExecuteResult.Processed;
                    }

                    protected override PokeResult OnPoke(DdeConversation conversation, string item, byte[] data, int format)
                    {
                        Console.WriteLine("OnPoke:".PadRight(16)
                            + " Service='" + conversation.Service + "'"
                            + " Topic='" + conversation.Topic + "'"
                            + " Handle=" + conversation.Handle.ToString()
                            + " Item='" + item + "'"
                            + " Data=" + data.Length.ToString()
                            + " Format=" + format.ToString());

                        // Tell the client that the data was processed.
                        return PokeResult.Processed;
                    }

                    protected override RequestResult OnRequest(DdeConversation conversation, string item, int format)
                    {
                        Console.WriteLine("OnRequest:".PadRight(16)
                            + " Service='" + conversation.Service + "'"
                            + " Topic='" + conversation.Topic + "'"
                            + " Handle=" + conversation.Handle.ToString()
                            + " Item='" + item + "'"
                            + " Format=" + format.ToString());

                        // Return data to the client only if the format is CF_TEXT.
                        if (format == 1)
                        {
                            return new RequestResult(System.Text.Encoding.ASCII.GetBytes("Time=" + DateTime.Now.ToString() + "\0"));
                        }
                        return RequestResult.NotProcessed;
                    }

                    protected override byte[] OnAdvise(string topic, string item, int format)
                    {
                        Console.WriteLine("OnAdvise:".PadRight(16)
                            + " Service='" + this.Service + "'"
                            + " Topic='" + topic + "'"
                            + " Item='" + item + "'"
                            + " Format=" + format.ToString());

                        // Send data to the client only if the format is CF_TEXT.
                        if (format == 1)
                        {
                            return System.Text.Encoding.ASCII.GetBytes("Time=" + DateTime.Now.ToString() + "\0");
                        }
                        return null;
                    }

                } // class

            } // class
            
[Visual Basic]
            Imports NDde.Server

            Module Program

                Sub Main()

                    Try

                        ' Create a server that will register the service name 'myapp'.
                        Using server As DdeServer = New MyServer("myapp")

                            ' Register the service name.
                            server.Register()

                            ' Wait for the user to press ENTER before proceding.
                            Console.WriteLine("Press ENTER to quit...")
                            Console.ReadLine()

                        End Using

                    Catch e As Exception

                        Console.WriteLine(e)
                        Console.WriteLine("Press ENTER to quit...")
                        Console.ReadLine()

                    End Try

                End Sub

                Private Class MyServer
                    Inherits DdeServer

                    Private WithEvents theTimer As System.Timers.Timer = New System.Timers.Timer()

                    Public Sub New(ByVal service As String)
                        MyBase.New(service)
                        ' Create a timer that will be used to advise clients of new data.
                        theTimer.Interval = 1000
                        theTimer.SynchronizingObject = Me.Context
                        theTimer.Start()
                    End Sub

                    Private Sub theTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles theTimer.Elapsed
                        Me.Advise("*", "*")
                    End Sub

                    Protected Overrides Function OnBeforeConnect(ByVal topic As String) As Boolean
                        Console.WriteLine("OnBeforeConnect:".PadRight(16) _
                         + " Service='" + MyBase.Service + "'" _
                         + " Topic='" + topic + "'")

                        Return True
                    End Function

                    Protected Overrides Sub OnAfterConnect(ByVal conversation As DdeConversation)
                        Console.WriteLine("OnAfterConnect:".PadRight(16) _
                         + " Service='" + conversation.Service + "'" _
                         + " Topic='" + conversation.Topic + "'" _
                         + " Handle=" + conversation.Handle.ToString())
                    End Sub

                    Protected Overrides Sub OnDisconnect(ByVal conversation As DdeConversation)
                        Console.WriteLine("OnDisconnect:".PadRight(16) _
                         + " Service='" + conversation.Service + "'" _
                         + " Topic='" + conversation.Topic + "'" _
                         + " Handle=" + conversation.Handle.ToString())
                    End Sub

                    Protected Overrides Function OnStartAdvise(ByVal conversation As DdeConversation, ByVal item As String, ByVal format As Integer) As Boolean
                        Console.WriteLine("OnStartAdvise:".PadRight(16) _
                         + " Service='" + conversation.Service + "'" _
                         + " Topic='" + conversation.Topic + "'" _
                         + " Handle=" + conversation.Handle.ToString() _
                         + " Item='" + item + "'" _
                         + " Format=" + format.ToString())

                        ' Initiate the advisory loop only if the format is CF_TEXT.
                        Return format = 1
                    End Function

                    Protected Overrides Sub OnStopAdvise(ByVal conversation As DdeConversation, ByVal item As String)
                        Console.WriteLine("OnStopAdvise:".PadRight(16) _
                          + " Service='" + conversation.Service + "'" _
                          + " Topic='" + conversation.Topic + "'" _
                          + " Handle=" + conversation.Handle.ToString() _
                          + " Item='" + item + "'")
                    End Sub

                    Protected Overrides Function OnExecute(ByVal conversation As DdeConversation, ByVal command As String) As ExecuteResult
                        Console.WriteLine("OnExecute:".PadRight(16) _
                         + " Service='" + conversation.Service + "'" _
                         + " Topic='" + conversation.Topic + "'" _
                         + " Handle=" + conversation.Handle.ToString() _
                         + " Command='" + command + "'")

                        ' Tell the client that the command was processed.
                        Return ExecuteResult.Processed
                    End Function

                    Protected Overrides Function OnPoke(ByVal conversation As DdeConversation, ByVal item As String, ByVal data As Byte(), ByVal format As Integer) As PokeResult
                        Console.WriteLine("OnPoke:".PadRight(16) _
                         + " Service='" + conversation.Service + "'" _
                         + " Topic='" + conversation.Topic + "'" _
                         + " Handle=" + conversation.Handle.ToString() _
                         + " Item='" + item + "'" _
                         + " Data=" + data.Length.ToString() _
                         + " Format=" + format.ToString())

                        ' Tell the client that the data was processed.
                        Return PokeResult.Processed
                    End Function

                    Protected Overrides Function OnRequest(ByVal conversation As DdeConversation, ByVal item As String, ByVal format As Integer) As RequestResult
                        Console.WriteLine("OnRequest:".PadRight(16) _
                         + " Service='" + conversation.Service + "'" _
                         + " Topic='" + conversation.Topic + "'" _
                         + " Handle=" + conversation.Handle.ToString() _
                         + " Item='" + item + "'" _
                         + " Format=" + format.ToString())

                        ' Return data to the client only if the format is CF_TEXT.
                        If format = 1 Then
                            Return New RequestResult(System.Text.Encoding.ASCII.GetBytes("Time=" + DateTime.Now.ToString() + Convert.ToChar(0)))
                        End If
                        Return RequestResult.NotProcessed
                    End Function

                    Protected Overrides Function OnAdvise(ByVal topic As String, ByVal item As String, ByVal format As Integer) As Byte()
                        Console.WriteLine("OnAdvise:".PadRight(16) _
                         + " Service='" + Me.Service + "'" _
                         + " Topic='" + topic + "'" _
                         + " Item='" + item + "'" _
                         + " Format=" + format.ToString())

                        ' Send data to the client only if the format is CF_TEXT.
                        If format = 1 Then
                            Return System.Text.Encoding.ASCII.GetBytes("Time=" + DateTime.Now.ToString() + Convert.ToChar(0))
                        End If
                        Return Nothing
                    End Function

                End Class

            End Module
            

Requirements

Namespace: NDde.Server

Assembly: NDde (in NDde.dll)

See Also

DdeServer Members | NDde.Server Namespace