Ejemplo n.º 1
0
        public async Task ConnectAsync()
        {
            _udpClient = new UdpClient();

            string localIpAddress = Util.LocalHostNames().FirstOrDefault();

            await _udpClient.ConnectAsync(new UriBuilder("udp", localIpAddress, 9761).Uri, new UriBuilder("udp", _ipAddress, 9760).Uri, CancellationToken.None);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Attempts to connect to data input source.
        /// </summary>
        protected override void AttemptConnection()
        {
            // Starts historian packet parser
            m_parser.Start();

            // Starts listening for UDP data
            m_client.ConnectAsync();
        }
Ejemplo n.º 3
0
        public async Task Listen(int port = DEFAULT_PORT)
        {
            await Task.Yield();

            if (run)
            {
                try
                {
#if (!PORTABLE)
                    udp = new UdpClient(port);
#else
                    udp = new Sockets.Plugin.UdpSocketClient();
                    await udp.ConnectAsync(IPAddress.Any.ToString(), port);
#endif
                }
                catch (Exception e)
                {
                    OnErrored(e);
                    return;
                }
            }
#if (!PORTABLE)
            while (run)
            {
                UdpReceiveResult result;
                try
                {
                    result = await udp.ReceiveAsync();
                }
                catch (ObjectDisposedException e) { OnErrored(e); }
                catch (SocketException e)
                {
                    OnErrored(e);
                    continue;
                }
                HandleRequest(result);
            }
#else
            if (run)
            {
                udp.MessageReceived += (s, e) =>
                {
                    HandleRequest(e);
                };
            }
#endif
        }
Ejemplo n.º 4
0
        private static UdpClient ConnectUDPClient()
        {
            UdpClient udpClient = new UdpClient
            {
                ConnectionString = $"port={s_settings.UDPPort}; interface={s_settings.InterfaceIP}"
            };

            udpClient.ConnectionAttempt     += UdpClient_ConnectionAttempt;
            udpClient.ConnectionEstablished += UdpClient_ConnectionEstablished;
            udpClient.ConnectionException   += UdpClient_ConnectionException;
            udpClient.ConnectionTerminated  += UdpClient_ConnectionTerminated;
            udpClient.ReceiveDataException  += UdpClient_ReceiveDataException;
            udpClient.ReceiveDataComplete   += UdpClient_ReceiveDataComplete;

            udpClient.ConnectAsync();

            return(udpClient);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Subscribes (or re-subscribes) to a data publisher for a set of data points.
        /// </summary>
        /// <param name="remotelySynchronized">Boolean value that determines if subscription should be remotely synchronized - note that data publisher may not allow remote synchronization.</param>
        /// <param name="compactFormat">Boolean value that determines if the compact measurement format should be used. Set to <c>false</c> for full fidelity measurement serialization; otherwise set to <c>true</c> for bandwidth conservation.</param>
        /// <param name="connectionString">Connection string that defines required and optional parameters for the subscription.</param>
        /// <returns><c>true</c> if subscribe transmission was successful; otherwise <c>false</c>.</returns>
        public virtual bool Subscribe(bool remotelySynchronized, bool compactFormat, string connectionString)
        {
            bool success = false;

            if (!string.IsNullOrWhiteSpace(connectionString))
            {
                try
                {
                    // Parse connection string to see if it contains a data channel definition
                    Dictionary<string, string> settings = connectionString.ParseKeyValuePairs();
                    UdpClient dataChannel = null;
                    string setting;

                    // Track specified time inclusion for later deserialization
                    if (settings.TryGetValue("includeTime", out setting))
                        m_includeTime = setting.ParseBoolean();
                    else
                        m_includeTime = true;

                    settings.TryGetValue("dataChannel", out setting);

                    if (!string.IsNullOrWhiteSpace(setting))
                    {
                        dataChannel = new UdpClient(setting);

                        dataChannel.ReceiveBufferSize = ushort.MaxValue;
                        dataChannel.MaxConnectionAttempts = -1;
                        dataChannel.ConnectAsync();
                    }

                    // Assign data channel client reference and attach to needed events
                    this.DataChannel = dataChannel;

                    // Setup subscription packet
                    MemoryStream buffer = new MemoryStream();
                    DataPacketFlags flags = DataPacketFlags.NoFlags;
                    byte[] bytes;

                    if (remotelySynchronized)
                        flags |= DataPacketFlags.Synchronized;

                    if (compactFormat)
                        flags |= DataPacketFlags.Compact;

                    // Write data packet flags into buffer
                    buffer.WriteByte((byte)flags);

                    // Get encoded bytes of connection string
                    bytes = m_encoding.GetBytes(connectionString);

                    // Write encoded connection string length into buffer
                    buffer.Write(EndianOrder.BigEndian.GetBytes(bytes.Length), 0, 4);

                    // Encode connection string into buffer
                    buffer.Write(bytes, 0, bytes.Length);

                    // Cache subscribed synchronization state
                    m_synchronizedSubscription = remotelySynchronized;

                    // Send subscribe server command with associated command buffer
                    success = SendServerCommand(ServerCommand.Subscribe, buffer.ToArray());
                }
                catch (Exception ex)
                {
                    OnProcessException(new InvalidOperationException("Exception occurred while trying to make publisher subscription: " + ex.Message, ex));
                }
            }
            else
                OnProcessException(new InvalidOperationException("Cannot make publisher subscription without a connection string."));

            return success;
        }