public ParsedLineEventArgs(RawLineId id, string command, StringParameter parameter, Stream rawData)
 {
     this.Id = id;
     this.Command = command;
     this.Parameter = parameter;
     this.RawData = rawData;
 }
 public ParameterEventReceivedEventArgs(StringParameter parameter, RawLineId id)
 {
     this.Parameter = parameter;
     this.Id = id;
 }
        private void SendCommandAsyncPrivate(RawLineId id, string command, StringParameter messageParameter = null, Stream rawData = null, Action<ParsedLineEventArgs> onAnswer = null, bool disposeStream = false, long bytesToSend = -1)
        {
            var eventArgs = new ParsedLineEventArgs(id, command, messageParameter, rawData);
            if (onAnswer != null)
            {
                List<Action<ParsedLineEventArgs>> delList;
                if (!this.onAnswerDelegates.TryGetValue(id, out delList))
                {
                    delList = new List<Action<ParsedLineEventArgs>>();
                    this.onAnswerDelegates.Add(id, delList);
                }
                delList.Add(onAnswer);
            }

            command = EscapeString(command);
            var parameterString = EscapeString(messageParameter == null ? "" : messageParameter.Parameter);
            if (rawData != null && bytesToSend == -1)
            {
                bytesToSend = rawData.Length - rawData.Position;
            }

            string rawDataString = rawData == null ? string.Empty : string.Format(" RAWDATA {0}", bytesToSend);
            string commandLine = string.Format("{0} {1}{2}", command, parameterString, rawDataString);
            Logger.WriteLine("Sending: {0}, with {1}", TraceEventType.Information, commandLine, id);
            byte[] commandBytes = this.encoding.GetBytes(commandLine + "\0");
            byte[] idBytes = id.GetIdBytes();

            var completeCommandBytes = new byte[commandBytes.Length + idBytes.Length + 1];
            Array.Copy(idBytes, 0, completeCommandBytes, 0, idBytes.Length);
            completeCommandBytes[idBytes.Length] = 0x20; // Code for Space
            Array.Copy(commandBytes, 0, completeCommandBytes, idBytes.Length + 1, commandBytes.Length);

            this.EnqueuePacket(
                new SendPacket(completeCommandBytes, eventArgs, rawData, this, disposeStream, bytesToSend));
            lock (this.sendSync)
            {
                if (this.currentPacket == null)
                {
                    this.SendNextPacket();
                }
            }
        }
        public void SendCommandAsync(
            string command, 
            StringParameter parameter, 
            Stream rawData = null, 
            Action<ParsedLineEventArgs> onAnswer = null,
            bool disposeStream = false, long dataLengthToSend = -1)
        {
            RawLineId id;
            lock (this.idLock)
            {
                id = this.sendCounter;
                this.sendCounter = this.sendCounter.Inc();
            }

            this.SendCommandAsyncPrivate(id, command, parameter, rawData, onAnswer, disposeStream, dataLengthToSend);
        }
 public void SendAnswer(
     RawLineId id, StringParameter parameter, Stream rawData = null, Action<ParsedLineEventArgs> onAnswer = null, bool disposeStream = false, long dataLengthToSend = -1)
 {
     this.SendCommandAsyncPrivate(id, "_", parameter, rawData, onAnswer, disposeStream, dataLengthToSend);
 }
        public void RemoveListener(RawLineId id, Action<ParsedLineEventArgs> listener)
        {
            List<Action<ParsedLineEventArgs>> onAnswerList;

            if (this.onAnswerDelegates.TryGetValue(id, out onAnswerList))
            {
                onAnswerList.Remove(listener);
                if (onAnswerList.Count == 0)
                {
                    this.RemoveAllListener(id);
                }
            }
        }
 public bool RemoveAllListener(RawLineId id)
 {
     return this.onAnswerDelegates.Remove(id);
 }
        private LineData FinishLineData()
        {
            var id = new RawLineId(this.idByteList.ToArray());
            this.idByteList.Clear();
            this.readId = true;
            if (this.lineBuilder == null)
            {
                return new LineData(null, "", null);
            }

            this.lineBuilder.Position = 0;
            using (var reader = new StreamReader(this.lineBuilder, this.encoding))
            {
                string line = reader.ReadToEnd();
                string[] splits = line.Split(' ');

                if (splits.Length > 3 && splits[splits.Length - 2] == "RAWDATA")
                {
                    if (!long.TryParse(splits[splits.Length - 1], out this.readBytes))
                    {
                        this.readBytes = 0;
                        throw new FormatException(string.Format("Invalid bytecount {0}", splits[splits.Length - 1]));
                    }
                    Logger.WriteLine("Received Message with {0} bytes", TraceEventType.Information, this.readBytes);
                    this.tempLine = new LineData(
                        id, string.Join(" ", splits.Take(splits.Length - 2)
            #if NET2
                        .ToArray()
            #endif
                        ), new RawDataStream(1024 * 4, this.readBytes));
                    this.lineBuilder.Dispose();
                    this.lineBuilder = null;
                    if (this.readBytes < 0)
                    {
                        throw new FormatException("Invalid byte layout format (invalid readbytes value)");
                    }
                    return this.tempLine;
                }

                this.lineBuilder = null;
                return new LineData(id, line, null);
            }
        }
 public LineData(RawLineId id, string tempLine, RawDataStream rawData)
 {
     this.Id = id;
     this.Line = tempLine;
     this.RawData = rawData;
 }