Exemplo n.º 1
0
        /// <summary>
        /// Sends the given data to the EVL device.
        /// </summary>
        /// <param name="data">Data to send to EVL</param>
        /// <returns></returns>
        public async Task SendAsync(string data)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(data + "\r\n");
            await writeStream.WriteAsync(buffer, 0, buffer.Length);

            lastSentCommand = Tpi.GetCommandPart(data);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Processes a login command with the given data.
        /// </summary>
        /// <param name="data">Data sent with login command.</param>
        private async Task ProcessLoginAsync(string data)
        {
            var loginType = (Command.LoginType) int.Parse(data);

            if (loginType == Command.LoginType.IncorrectPassword)
            {
                // Login failed - throw exception
                throw new Exception("Invalid password.");
            }
            else if (loginType == Command.LoginType.TimeOut)
            {
                // Login timed out - throw exception
                throw new Exception("Login to EVL timed out.");
            }
            else if (loginType == Command.LoginType.PasswordRequest)
            {
                // Login request - send credentials
                string command = Command.NetworkLogin + Password;
                await SendAsync(command + Tpi.CalculateChecksum(command));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Reads data from the EVL connection and processes commands until connection is terminated.
        /// </summary>
        /// <param name="stream">Stream from EVL connection</param>
        private async Task ReadAsync(NetworkStream stream)
        {
            using (stream)
            {
                string separator  = string.Join("", Separators);
                byte[] buffer     = new byte[1024];
                string incomplete = "";
                bool   read       = true;

                while (read)
                {
                    int result = await stream.ReadAsync(buffer, 0, 1024);

                    if (result == 0)
                    {
                        break;
                    }

                    string incoming = Encoding.UTF8.GetString(buffer, 0, result);

                    string[] packets;
                    if (incoming.EndsWith(separator))
                    {
                        // Incoming contains complete command(s)

                        // Append new data to stored incomplete command
                        incomplete += incoming;
                        packets     = incomplete.Split(Separators, StringSplitOptions.RemoveEmptyEntries);

                        // Clear incomplete buffer
                        incomplete = string.Empty;
                    }
                    else
                    {
                        // Incoming contains partial command
                        incomplete += incoming;

                        // Separate into individual packets
                        string[] partial = incomplete.Split(Separators, StringSplitOptions.RemoveEmptyEntries);

                        // Store complete packets in packets array
                        packets = new string[partial.Length - 1];
                        Array.Copy(partial, packets, partial.Length - 1);

                        // Store incomplete command to use when rest of data arrives
                        incomplete = partial[partial.Length - 1];
                    }

                    // Process each complete command received
                    foreach (string packet in packets)
                    {
                        bool valid = Tpi.VerifyChecksum(packet);
                        if (!valid)
                        {
                            // TODO: Expand on this.
                            Console.WriteLine("Incorrect checksum!");
                            break;
                        }

                        string command = Tpi.GetCommandPart(packet);
                        string data    = Tpi.GetDataPart(packet);

                        try
                        {
                            await ProcessAsync(command, data);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Error: " + e.Message);
                            read = false;
                            break;
                        }
                    }
                }
            }

            // Disconnected, clean up connection and other stuff.
            Console.WriteLine("Disconnected from EVL!");
            Disconnect();
        }