private void Reader_OnComplete(DataReciver reader, byte[] buffer, int offset, int count)
        {
            var words = Encoding.UTF8.GetString(buffer, offset, count);

            words += temp;
            temp   = string.Empty;

            _logger.LogDebug($"revice from client: {words}");

            try
            {
                int  index      = 0;
                bool needRecive = false;

                while (true)
                {
                    var firstIndex = words.IndexOf("\n");
                    if (firstIndex < 0)
                    {
                        temp += words;
                        reader.ReciveOneAsync();
                        break;
                    }

                    var sub_words = words.Substring(index, firstIndex + 1);
                    var res       = handle(sub_words, reader.Socket);

                    if (res.NeedRecive)
                    {
                        needRecive = true;
                    }

                    words = words.Replace(sub_words, string.Empty);
                    if (string.IsNullOrEmpty(words))
                    {
                        break;
                    }
                }

                if (needRecive)
                {
                    reader.ReciveOneAsync();
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.ToString());
                _logger.LogError($"handle fail msg:{words}");

                // throw;
                reader.Socket.Send(new Message <LogMassage>()
                {
                    MessageType = MessageType.Log, Content = new LogMassage(LogMsgType.Error, ex.Message)
                });
                reader.ReciveOneAsync();
            }
        }
 public void StartRecive()
 {
     reader.OnComplete += Reader_OnComplete;
     reader.OnError    += Reader_OnError;
     reader.OnReset    += Reader_OnReset;
     reader.ReciveOneAsync();
 }
Beispiel #3
0
        public void Dispatch(Socket client)
        {
            var reader = new DataReciver(client);

            reader.OnComplete += Reader_OnComplete;
            reader.OnError    += Reader_OnError;
            reader.OnReset    += Reader_OnReset;
            reader.ReciveOneAsync();
        }