Esempio n. 1
0
        public static AmiMessage FromBytes(Byte[] bytes)
        {
            var result = new AmiMessage();

            for (var nrLine = 1;; nrLine++)
            {
                var crlfPos = bytes.Find(AmiMessage.TerminatorBytes, 0, bytes.Length);

                if (crlfPos == -1)
                {
                    throw new ArgumentException($"unexpected end of message after {nrLine} line(s)", nameof(bytes));
                }

                var line = Encoding.UTF8.GetString(bytes.Slice(0, crlfPos));
                bytes = bytes.Slice(crlfPos + AmiMessage.TerminatorBytes.Length);

                if (line.Equals(String.Empty))
                {
                    break;                     // empty line terminates
                }

                var kvp = line.Split(new[] { ':' }, 2);

                if (kvp.Length != 2)
                {
                    throw new ArgumentException($"malformed field on line {nrLine}", nameof(bytes));
                }

                result.Add(kvp[0], kvp[1]);
            }

            return(result);
        }
Esempio n. 2
0
        public static AmiMessage FromBytes(Byte[] bytes)
        {
            var result = new AmiMessage();

            var stream = new MemoryStream(bytes);

            var reader = new StreamReader(stream, new UTF8Encoding(false));

            for (var nrLine = 1; ; nrLine++)
            {
                var line = reader.ReadLine();

                if (line == null)
                {
                    throw new ArgumentException("unterminated message", nameof(bytes));
                }

                if (line.Equals(String.Empty))
                {
                    break; // empty line terminates
                }

                var kvp = line.Split(new[] { ':' }, 2);

                if (kvp.Length != 2)
                {
                    throw new ArgumentException($"malformed field on line {nrLine}", nameof(bytes));
                }

                result.Add(kvp[0], kvp[1]);
            }

            return(result);
        }
Esempio n. 3
0
        private void WorkerMain()
        {
            try
            {
                this.processing = true;

                var reader = new StreamReader(this.stream, new UTF8Encoding(false));

                if (this.processing && this.stream != null && this.stream.CanRead)
                {
                    var line = reader.ReadLine();

                    this.DataReceived?.Invoke(this, new DataEventArgs(line + "\r\n"));

                    if (!line.StartsWith("Asterisk Call Manager", StringComparison.OrdinalIgnoreCase))
                    {
                        throw new Exception("this does not appear to be an Asterisk server");
                    }
                }

                var lines = new List <String>();

                while (this.processing && this.stream != null && this.stream.CanRead)
                {
                    lines.Add(reader.ReadLine());

                    if (lines.Last() != String.Empty)
                    {
                        continue;
                    }

                    this.DataReceived?.Invoke(this, new DataEventArgs(String.Join("\r\n", lines) + "\r\n"));

                    var message = new AmiMessage();

                    foreach (var line in lines.Where(line => line != String.Empty))
                    {
                        var kv = line.Split(new[] { ':' }, 2);

                        Debug.Assert(kv.Length == 2);

                        message.Add(kv[0], kv[1]);
                    }

                    if (message["Response"] != null && this.inFlight.TryGetValue(message["ActionID"], out var tcs))
                    {
                        Debug.Assert(tcs.TrySetResult(message));
                    }
                    else
                    {
                        this.Dispatch(message);
                    }

                    lines.Clear();
                }
            }
            catch (ThreadAbortException)
            {
                Thread.ResetAbort();
            }
            catch (Exception ex)
            {
                this.Dispatch(ex);
            }
        }