private void Reset()
 {
     _buffer        = null;
     _bufferLength  = BitConverter.GetBytes(sizeof(int));
     _receivingMode = ReceivingMode.Header;
     _receivedData  = 0;
 }
        private void ReadHeader(byte[] data)
        {
            InternalRead(data, _bufferLength, _bufferLength.Length);

            var length = BitConverter.ToInt32(_bufferLength, 0);

            if (length < 0)
            {
                throw new ProtocolViolationException("length cannot be less than zero");
            }

            if (length > _maxBufferLength)
            {
                throw new ProtocolViolationException("the data length is greater than the maximum value.");
            }

            _buffer         = new byte[length];
            _readingOffset += _bufferLength.Length;
            _receivingMode  = ReceivingMode.Packet;
        }
        public void OnReceive(string data)
        {
            switch (data)
            {
            case "calendarmeetings list begin":
                _meetingsTemp.Clear();
#if DEBUG
                Debug.WriteInfo("Being Meetings List!");
#endif
                _feedbackReceivingMode = ReceivingMode.List;
                return;

            case "calendarmeetings list end":
                _meetings.Clear();
                foreach (var meeting in _meetingsTemp)
                {
                    _meetings[meeting.Key] = meeting.Value;
                }
#if DEBUG
                Debug.WriteInfo("End Meetings List!");
#endif
                // if meetings count is 0, we will not have any info so need to call events now
                if (_meetings.Count == 0)
                {
                    OnMeetingsUpdated(this, null, null);
                }

                _feedbackReceivingMode = ReceivingMode.NotSet;
                return;

            case "calendarmeetings info start":
#if DEBUG
                Debug.WriteInfo("Begin Meetings Info!");
#endif
                _feedbackReceivingMode = ReceivingMode.Info;
                return;

            case "calendarmeetings info end":
#if DEBUG
                Debug.WriteInfo("End Meetings Info!");
#endif
                _feedbackReceivingMode             = ReceivingMode.NotSet;
                _currentParsedMeeting.ReceivedInfo = true;
                _currentParsedMeeting = null;

                if (_meetings.Values.All(m => m.ReceivedInfo))
                {
#if DEBUG
                    Debug.WriteInfo("Should have received all meetings info!!");
#endif
                    OnMeetingsUpdated(this, CurrentMeeting, NextMeeting);
                }
                return;

            default:
                if (_feedbackReceivingMode == ReceivingMode.NotSet)
                {
                    return;
                }
                break;
            }

            switch (_feedbackReceivingMode)
            {
            case ReceivingMode.List:
                try
                {
#if DEBUG
                    Debug.WriteInfo("Should be meeting list item");
#endif
                    var match = Regex.Match(data, @"^(\w+)\|([^\|]+)\|([\d-:]+)\|([\d-:]+)\|([^\|]*)$");
                    if (match.Success)
                    {
                        var dateMatch = new Regex(@"^(\d{4})-(\d{2})-(\d{2}):(\d{2}):(\d{2})$");

                        var stMatch   = dateMatch.Match(match.Groups[3].Value);
                        var startTime = new DateTime(int.Parse(stMatch.Groups[1].Value),
                                                     int.Parse(stMatch.Groups[2].Value), int.Parse(stMatch.Groups[3].Value),
                                                     int.Parse(stMatch.Groups[4].Value), int.Parse(stMatch.Groups[5].Value), 0);

                        var etMatch = dateMatch.Match(match.Groups[4].Value);
                        var endTime = new DateTime(int.Parse(etMatch.Groups[1].Value),
                                                   int.Parse(etMatch.Groups[2].Value), int.Parse(etMatch.Groups[3].Value),
                                                   int.Parse(etMatch.Groups[4].Value), int.Parse(etMatch.Groups[5].Value), 0);

                        var meeting = new CalendarMeeting(_codec, match.Groups[2].Value, startTime, endTime,
                                                          match.Groups[5].Value.Trim());

                        _meetingsTemp.Add(meeting.Id, meeting);
#if DEBUG
                        Debug.WriteSuccess("Meeting", "{0} - {1}, {2}, {3}", meeting.StartTime.ToString("t"),
                                           meeting.EndTime.ToString("t"), meeting.Name, meeting.Id);
#endif
                        _codec.Send(string.Format("calendarmeetings info {0}", meeting.Id));
                    }
                }
                catch (Exception e)
                {
                    CloudLog.Exception(e);
                }
                break;

            case ReceivingMode.Info:
                try
                {
#if DEBUG
                    Debug.WriteInfo("Should be meeting list info");
#endif
                    var infoMatch = Regex.Match(data, @"^([\d-:]+)\|([\d-:]+)\|([\w]+)\|([\w]+)$");
                    if (infoMatch.Success)
                    {
                        _currentParsedMeeting.CanDial  = infoMatch.Groups[3].Value == "dialable";
                        _currentParsedMeeting.IsPublic = infoMatch.Groups[4].Value == "public";
                        return;
                    }

                    var matches = Regex.Matches(data, @"\|?([^\|]*)");

                    if (matches.Count > 0)
                    {
                        switch (matches[0].Groups[1].Value)
                        {
                        case "id":
                            var id = matches[1].Groups[1].Value;
                            if (_meetings.ContainsKey(id))
                            {
                                _currentParsedMeeting = _meetings[id];
                            }
                            break;

                        case "organizer":
                            _currentParsedMeeting.Organizer = matches[1].Groups[1].Value;
                            break;

                        case "location":
                            _currentParsedMeeting.Location = matches[1].Groups[1].Value;
                            break;

                        case "attendee":
                            _currentParsedMeeting.Attendees.Add(matches[1].Groups[1].Value);
                            break;

                        case "dialingnumber":
                            var type =
                                (DiallingNumberType)
                                Enum.Parse(typeof(DiallingNumberType), matches[1].Groups[1].Value, true);
                            var number = matches[2].Groups[1].Value;
                            _currentParsedMeeting.AddDiallingNumber(new MeetingDiallingNumber(number, type));
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    CloudLog.Exception(e);
                }
                break;
            }
        }