Represents a UDP-based communication server.

Use UdpServer when the primary purpose is to transmit data.

The UdpServer.Server socket can be bound to a specified interface on a machine with multiple interfaces by specifying the interface in the ServerBase.ConfigurationString (Example: "Port=8888; Clients=localhost:8989; Interface=127.0.0.1")

The UdpServer.Server socket can be used just for transmitting data without being bound to a local interface by specifying -1 for the port number in the ServerBase.ConfigurationString (Example: "Port=-1; Clients=localhost:8989")

Inheritance: ServerBase
示例#1
0
文件: ServerBase.cs 项目: avs009/gsf
        /// <summary>
        /// Create a communications server
        /// </summary>
        /// <remarks>
        /// Note that typical configuration string should be prefixed with a "protocol=tcp" or a "protocol=udp"
        /// </remarks>
        /// <param name="configurationString">The configuration string for the server.</param>
        /// <returns>A communications server.</returns>
        public static IServer Create(string configurationString)
        {
            Dictionary<string, string> configurationSettings = configurationString.ParseKeyValuePairs();
            IServer server;
            string protocol;

            if (configurationSettings.TryGetValue("protocol", out protocol))
            {
                configurationSettings.Remove("protocol");
                StringBuilder settings = new StringBuilder();

                foreach (string key in configurationSettings.Keys)
                {
                    settings.Append(key);
                    settings.Append("=");
                    settings.Append(configurationSettings[key]);
                    settings.Append(";");
                }

                // Create a server instance for the specified protocol.
                switch (protocol.Trim().ToLower())
                {
                    case "tls":
                        server = new TlsServer(settings.ToString());
                        break;
                    case "tcp":
                        server = new TcpServer(settings.ToString());
                        break;
                    case "udp":
                        server = new UdpServer(settings.ToString());
                        break;
                    case "zeromq":
                        server = new ZeroMQServer(settings.ToString());
                        break;
                    default:
                        throw new ArgumentException("Transport protocol \'" + protocol + "\' is not valid");
                }

                // Apply server settings from the connection string to the client.
                foreach (KeyValuePair<string, string> setting in configurationSettings)
                {
                    PropertyInfo property = server.GetType().GetProperty(setting.Key);
                    if (property != null)
                        property.SetValue(server, Convert.ChangeType(setting.Value, property.PropertyType), null);
                }
            }
            else
            {
                throw new ArgumentException("Transport protocol must be specified");
            }

            return server;
        }
示例#2
0
        private void m_reconnectTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                m_parent.OnStatusMessage("Attempting to restart data channel...");
                DataChannel = null;

                UdpServer dataChannel = new UdpServer(m_configurationString);
                dataChannel.Start();

                this.DataChannel = dataChannel;
                m_parent.OnStatusMessage("Data channel successfully restarted.");
            }
            catch (Exception ex)
            {
                m_parent.OnStatusMessage("Failed to restart data channel due to exception: {0}", ex.Message);
                m_reconnectTimer.Start();
            }
        }
        private void m_reconnectTimer_Elapsed(object sender, EventArgs<DateTime> e)
        {
            try
            {
                m_parent.OnStatusMessage(MessageLevel.Info, "Attempting to restart data channel...");
                DataChannel = null;

                UdpServer dataChannel = new UdpServer(m_configurationString);
                dataChannel.Start();

                DataChannel = dataChannel;
                m_parent.OnStatusMessage(MessageLevel.Info, "Data channel successfully restarted.");
            }
            catch (Exception ex)
            {
                m_parent.OnStatusMessage(MessageLevel.Warning, $"Failed to restart data channel due to exception: {ex.Message}");
                m_reconnectTimer.Start();
            }
        }